1 | #!/usr/bin/env perl |
---|
2 | #------------------------------------------------------------------------------- |
---|
3 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
4 | # |
---|
5 | # This file is part of FCM, tools for managing and building source code. |
---|
6 | # |
---|
7 | # FCM is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # FCM is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | #------------------------------------------------------------------------------- |
---|
20 | |
---|
21 | use strict; |
---|
22 | use warnings; |
---|
23 | |
---|
24 | my %IMPL = ('fcm-dummy-diff' => \&_fcm_dummy_diff, |
---|
25 | 'xxdiff' => \&_xxdiff, |
---|
26 | 'meld' => \&_meld, |
---|
27 | 'kdiff3' => \&_kdiff3); |
---|
28 | |
---|
29 | my %UNRESOLVED = ( |
---|
30 | 'nodecision' => "You have made no decision.\n", |
---|
31 | 'merged' => "You have not resolved all the conflicts.\n", |
---|
32 | ); |
---|
33 | |
---|
34 | if (!caller()) { |
---|
35 | # 0 - no diff |
---|
36 | # 1 - diff |
---|
37 | # other return code - fatal |
---|
38 | exit main(@ARGV); |
---|
39 | } |
---|
40 | |
---|
41 | sub main { |
---|
42 | my $command = 'xxdiff'; |
---|
43 | if (exists($ENV{FCM_GRAPHIC_MERGE}) && $ENV{FCM_GRAPHIC_MERGE}) { |
---|
44 | $command = $ENV{FCM_GRAPHIC_MERGE}; |
---|
45 | } |
---|
46 | if (!exists($IMPL{$command})) { |
---|
47 | die("$command: merge tool not yet supported.\n"); |
---|
48 | } |
---|
49 | $IMPL{$command}->(@_); |
---|
50 | } |
---|
51 | |
---|
52 | sub _fcm_dummy_diff { |
---|
53 | my ($base, $mine, $older, $yours) = @_; |
---|
54 | my @command = (qw{diff3}, $mine, $older, $yours); |
---|
55 | print(join(" ", @command) . "\n"); |
---|
56 | my @out_lines = qx{@command}; |
---|
57 | for my $line (@out_lines) { |
---|
58 | print($line); |
---|
59 | } |
---|
60 | return 0; |
---|
61 | } |
---|
62 | |
---|
63 | sub _xxdiff { |
---|
64 | my ($base, $mine, $older, $yours) = @_; |
---|
65 | my @command = (qw{xxdiff -m -M}, $base, qw{-O -X}, $mine, $older, $yours); |
---|
66 | my @out_lines = qx{@command}; |
---|
67 | my $rc = $?; |
---|
68 | if (!@out_lines) { |
---|
69 | return 2; |
---|
70 | } |
---|
71 | my ($decision) = map {chomp($_); lc($_);} @out_lines; |
---|
72 | if ($rc && exists($UNRESOLVED{$decision})) { |
---|
73 | print($UNRESOLVED{$decision}); |
---|
74 | return 1; |
---|
75 | } |
---|
76 | printf("You %s all the changes.\n", $decision); |
---|
77 | return 0; |
---|
78 | } |
---|
79 | |
---|
80 | sub _kdiff3 { |
---|
81 | my ($base, $mine, $older, $yours) = @_; |
---|
82 | my @command = (qw{kdiff3 -o}, $base, $mine, $older, $yours); |
---|
83 | my @out_lines = qx{@command}; |
---|
84 | my $rc = $?; |
---|
85 | # kdiff3 produces a file called $base.orig, so we delete that |
---|
86 | unlink $base . ".orig"; |
---|
87 | # kdiff3 doesn't produce any output, so we just assume a non-zero |
---|
88 | # exit code means unresolved merges |
---|
89 | if ($rc) { |
---|
90 | print($UNRESOLVED{'merged'}); |
---|
91 | return 1; |
---|
92 | } |
---|
93 | printf("You merged all the changes.\n"); |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | sub _meld { |
---|
98 | my ($base, $mine, $older, $yours) = @_; |
---|
99 | # Not using --auto-merge option as it alters the middle pane before user |
---|
100 | # can review changes. |
---|
101 | my @command = (qw{meld -a -o}, $base, $mine, $older, $yours); |
---|
102 | my @out_lines = qx{@command}; |
---|
103 | my $rc = $?; |
---|
104 | # meld doesn't produce any output, so we just assume a non-zero |
---|
105 | # exit code means unresolved merges |
---|
106 | if ($rc) { |
---|
107 | print($UNRESOLVED{'merged'}); |
---|
108 | return 1; |
---|
109 | } |
---|
110 | printf("You merged all the changes.\n"); |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | __END__ |
---|
115 | |
---|
116 | =head1 NAME |
---|
117 | |
---|
118 | fcm_graphic_merge |
---|
119 | |
---|
120 | =head1 SYNOPSIS |
---|
121 | |
---|
122 | fcm_graphic_merge BASE MINE OLDER YOURS |
---|
123 | |
---|
124 | =head1 DESCRIPTION |
---|
125 | |
---|
126 | Invokes L<xxdiff|xxdiff> (or the command specified in the FCM_GRAPHIC_MERGE |
---|
127 | environment variable) to compare the CURRENT, ANCESTOR and NEW files, where possible. |
---|
128 | |
---|
129 | Wrap L<xxdiff|xxdiff>. Invoke L<xxdiff|xxdiff> as: |
---|
130 | |
---|
131 | xxdiff -m -M BASE -O -X MINE OLDER YOURS |
---|
132 | |
---|
133 | Print friendlier decision messages. |
---|
134 | |
---|
135 | Return 0 if no diff remains, 1 if any diff remains, and 2 for fatal errors. |
---|
136 | |
---|
137 | =head1 ARGUMENTS |
---|
138 | |
---|
139 | BASE is the file you want to save the merge result into. |
---|
140 | |
---|
141 | MINE is the original file. |
---|
142 | |
---|
143 | YOURS is the file you want MINE to merge with. |
---|
144 | |
---|
145 | OLDER is the common ancestor of MINE and YOURS. |
---|
146 | |
---|
147 | =head1 COPYRIGHT |
---|
148 | |
---|
149 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
150 | |
---|
151 | =cut |
---|