]> git.ipfire.org Git - thirdparty/git.git/blame - git-difftool.perl
difftool: Check all return codes from compare()
[thirdparty/git.git] / git-difftool.perl
CommitLineData
7e0abcec 1#!/usr/bin/perl
f47f1e2c 2# Copyright (c) 2009, 2010 David Aguilar
7e0abcec 3# Copyright (c) 2012 Tim Henigan
5c38ea31
DA
4#
5# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
a904392e
DA
6# git-difftool--helper script.
7#
8# This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
7e0abcec 9# The GIT_DIFF* variables are exported for use by git-difftool--helper.
a904392e 10#
5c38ea31
DA
11# Any arguments that are unknown to this script are forwarded to 'git diff'.
12
d48b2841 13use 5.008;
5c38ea31
DA
14use strict;
15use warnings;
7c7584b9 16use File::Basename qw(dirname);
7e0abcec 17use File::Copy;
05df5326 18use File::Compare;
7c7584b9 19use File::Find;
7e0abcec
TH
20use File::stat;
21use File::Path qw(mkpath);
22use File::Temp qw(tempdir);
3f94ff75
TH
23use Getopt::Long qw(:config pass_through);
24use Git;
5c38ea31
DA
25
26sub usage
27{
28360769 28 my $exitcode = shift;
5c38ea31 29 print << 'USAGE';
bf73fc21 30usage: git difftool [-t|--tool=<tool>] [--tool-help]
85089604
TH
31 [-x|--extcmd=<cmd>]
32 [-g|--gui] [--no-gui]
3f94ff75 33 [--prompt] [-y|--no-prompt]
7e0abcec 34 [-d|--dir-diff]
f47f1e2c 35 ['git diff' options]
5c38ea31 36USAGE
28360769 37 exit($exitcode);
5c38ea31
DA
38}
39
7e0abcec
TH
40sub find_worktree
41{
75cd7583
DA
42 my ($repo) = @_;
43
7e0abcec
TH
44 # Git->repository->wc_path() does not honor changes to the working
45 # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
46 # config variable.
47 my $worktree;
48 my $env_worktree = $ENV{GIT_WORK_TREE};
49 my $core_worktree = Git::config('core.worktree');
50
51 if (defined($env_worktree) and (length($env_worktree) > 0)) {
52 $worktree = $env_worktree;
53 } elsif (defined($core_worktree) and (length($core_worktree) > 0)) {
54 $worktree = $core_worktree;
55 } else {
56 $worktree = $repo->wc_path();
57 }
58
59 return $worktree;
60}
61
7c7584b9
DA
62sub filter_tool_scripts
63{
64 my ($tools) = @_;
65 if (-d $_) {
66 if ($_ ne ".") {
67 # Ignore files in subdirectories
68 $File::Find::prune = 1;
69 }
70 } else {
71 if ((-f $_) && ($_ ne "defaults")) {
72 push(@$tools, $_);
73 }
74 }
75}
76
bf73fc21
TH
77sub print_tool_help
78{
7c7584b9 79 my ($cmd, @found, @notfound, @tools);
bf73fc21
TH
80 my $gitpath = Git::exec_path();
81
7c7584b9 82 find(sub { filter_tool_scripts(\@tools) }, "$gitpath/mergetools");
bf73fc21
TH
83
84 foreach my $tool (@tools) {
85 $cmd = "TOOL_MODE=diff";
86 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
87 $cmd .= " && get_merge_tool_path $tool >/dev/null 2>&1";
88 $cmd .= " && can_diff >/dev/null 2>&1";
89 if (system('sh', '-c', $cmd) == 0) {
90 push(@found, $tool);
91 } else {
92 push(@notfound, $tool);
93 }
94 }
95
96 print "'git difftool --tool=<tool>' may be set to one of the following:\n";
7c7584b9 97 print "\t$_\n" for (sort(@found));
bf73fc21
TH
98
99 print "\nThe following tools are valid, but not currently available:\n";
7c7584b9 100 print "\t$_\n" for (sort(@notfound));
bf73fc21
TH
101
102 print "\nNOTE: Some of the tools listed above only work in a windowed\n";
103 print "environment. If run in a terminal-only session, they will fail.\n";
104
105 exit(0);
106}
107
7e0abcec
TH
108sub setup_dir_diff
109{
1f229345 110 my ($repo, $workdir, $symlinks) = @_;
75cd7583 111
7e0abcec
TH
112 # Run the diff; exit immediately if no diff found
113 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
114 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
115 # by Git->repository->command*.
75cd7583 116 my $repo_path = $repo->repo_path();
7e0abcec
TH
117 my $diffrepo = Git->repository(Repository => $repo_path, WorkingCopy => $workdir);
118 my $diffrtn = $diffrepo->command_oneline('diff', '--raw', '--no-abbrev', '-z', @ARGV);
119 exit(0) if (length($diffrtn) == 0);
120
121 # Setup temp directories
b965e8f4 122 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 1, TMPDIR => 1);
7e0abcec
TH
123 my $ldir = "$tmpdir/left";
124 my $rdir = "$tmpdir/right";
125 mkpath($ldir) or die $!;
126 mkpath($rdir) or die $!;
127
128 # Build index info for left and right sides of the diff
129 my $submodule_mode = '160000';
130 my $symlink_mode = '120000';
131 my $null_mode = '0' x 6;
132 my $null_sha1 = '0' x 40;
133 my $lindex = '';
134 my $rindex = '';
135 my %submodule;
136 my %symlink;
75cd7583 137 my @working_tree = ();
7e0abcec
TH
138 my @rawdiff = split('\0', $diffrtn);
139
140 my $i = 0;
141 while ($i < $#rawdiff) {
142 if ($rawdiff[$i] =~ /^::/) {
143 print "Combined diff formats ('-c' and '--cc') are not supported in directory diff mode.\n";
144 exit(1);
145 }
146
147 my ($lmode, $rmode, $lsha1, $rsha1, $status) = split(' ', substr($rawdiff[$i], 1));
148 my $src_path = $rawdiff[$i + 1];
149 my $dst_path;
150
151 if ($status =~ /^[CR]/) {
152 $dst_path = $rawdiff[$i + 2];
153 $i += 3;
154 } else {
155 $dst_path = $src_path;
156 $i += 2;
157 }
158
159 if (($lmode eq $submodule_mode) or ($rmode eq $submodule_mode)) {
160 $submodule{$src_path}{left} = $lsha1;
161 if ($lsha1 ne $rsha1) {
162 $submodule{$dst_path}{right} = $rsha1;
163 } else {
164 $submodule{$dst_path}{right} = "$rsha1-dirty";
165 }
166 next;
167 }
168
169 if ($lmode eq $symlink_mode) {
170 $symlink{$src_path}{left} = $diffrepo->command_oneline('show', "$lsha1");
171 }
172
173 if ($rmode eq $symlink_mode) {
174 $symlink{$dst_path}{right} = $diffrepo->command_oneline('show', "$rsha1");
175 }
176
177 if (($lmode ne $null_mode) and ($status !~ /^C/)) {
178 $lindex .= "$lmode $lsha1\t$src_path\0";
179 }
180
181 if ($rmode ne $null_mode) {
182 if ($rsha1 ne $null_sha1) {
183 $rindex .= "$rmode $rsha1\t$dst_path\0";
184 } else {
185 push(@working_tree, $dst_path);
186 }
187 }
188 }
189
190 # If $GIT_DIR is not set prior to calling 'git update-index' and
191 # 'git checkout-index', then those commands will fail if difftool
192 # is called from a directory other than the repo root.
193 my $must_unset_git_dir = 0;
194 if (not defined($ENV{GIT_DIR})) {
195 $must_unset_git_dir = 1;
196 $ENV{GIT_DIR} = $repo_path;
197 }
198
199 # Populate the left and right directories based on each index file
200 my ($inpipe, $ctx);
201 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
202 ($inpipe, $ctx) = $repo->command_input_pipe(qw/update-index -z --index-info/);
203 print($inpipe $lindex);
204 $repo->command_close_pipe($inpipe, $ctx);
75cd7583 205 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
7e0abcec
TH
206 exit($rc | ($rc >> 8)) if ($rc != 0);
207
208 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
209 ($inpipe, $ctx) = $repo->command_input_pipe(qw/update-index -z --index-info/);
210 print($inpipe $rindex);
211 $repo->command_close_pipe($inpipe, $ctx);
212 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
213 exit($rc | ($rc >> 8)) if ($rc != 0);
214
215 # If $GIT_DIR was explicitly set just for the update/checkout
216 # commands, then it should be unset before continuing.
217 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
218 delete($ENV{GIT_INDEX_FILE});
219
220 # Changes in the working tree need special treatment since they are
221 # not part of the index
222 for my $file (@working_tree) {
223 my $dir = dirname($file);
224 unless (-d "$rdir/$dir") {
225 mkpath("$rdir/$dir") or die $!;
226 }
1f229345
DA
227 if ($symlinks) {
228 symlink("$workdir/$file", "$rdir/$file") or die $!;
229 } else {
230 copy("$workdir/$file", "$rdir/$file") or die $!;
231 my $mode = stat("$workdir/$file")->mode;
232 chmod($mode, "$rdir/$file") or die $!;
233 }
7e0abcec
TH
234 }
235
236 # Changes to submodules require special treatment. This loop writes a
237 # temporary file to both the left and right directories to show the
238 # change in the recorded SHA1 for the submodule.
239 for my $path (keys %submodule) {
240 if (defined($submodule{$path}{left})) {
241 write_to_file("$ldir/$path", "Subproject commit $submodule{$path}{left}");
242 }
243 if (defined($submodule{$path}{right})) {
244 write_to_file("$rdir/$path", "Subproject commit $submodule{$path}{right}");
245 }
246 }
247
248 # Symbolic links require special treatment. The standard "git diff"
249 # shows only the link itself, not the contents of the link target.
250 # This loop replicates that behavior.
251 for my $path (keys %symlink) {
252 if (defined($symlink{$path}{left})) {
253 write_to_file("$ldir/$path", $symlink{$path}{left});
254 }
255 if (defined($symlink{$path}{right})) {
256 write_to_file("$rdir/$path", $symlink{$path}{right});
257 }
258 }
259
75cd7583 260 return ($ldir, $rdir, @working_tree);
7e0abcec
TH
261}
262
263sub write_to_file
264{
265 my $path = shift;
266 my $value = shift;
267
268 # Make sure the path to the file exists
269 my $dir = dirname($path);
270 unless (-d "$dir") {
271 mkpath("$dir") or die $!;
272 }
273
274 # If the file already exists in that location, delete it. This
275 # is required in the case of symbolic links.
276 unlink("$path");
277
278 open(my $fh, '>', "$path") or die $!;
279 print($fh $value);
280 close($fh);
281}
282
75cd7583
DA
283sub main
284{
285 # parse command-line options. all unrecognized options and arguments
286 # are passed through to the 'git diff' command.
c9bdd505
DA
287 my %opts = (
288 difftool_cmd => undef,
289 dirdiff => undef,
290 extcmd => undef,
291 gui => undef,
292 help => undef,
293 prompt => undef,
1f229345 294 symlinks => $^O ne 'MSWin32' && $^O ne 'msys',
c9bdd505
DA
295 tool_help => undef,
296 );
297 GetOptions('g|gui!' => \$opts{gui},
298 'd|dir-diff' => \$opts{dirdiff},
299 'h' => \$opts{help},
300 'prompt!' => \$opts{prompt},
301 'y' => sub { $opts{prompt} = 0; },
1f229345
DA
302 'symlinks' => \$opts{symlinks},
303 'no-symlinks' => sub { $opts{symlinks} = 0; },
c9bdd505
DA
304 't|tool:s' => \$opts{difftool_cmd},
305 'tool-help' => \$opts{tool_help},
306 'x|extcmd:s' => \$opts{extcmd});
307
308 if (defined($opts{help})) {
75cd7583 309 usage(0);
5c38ea31 310 }
c9bdd505 311 if (defined($opts{tool_help})) {
75cd7583 312 print_tool_help();
3f94ff75 313 }
c9bdd505
DA
314 if (defined($opts{difftool_cmd})) {
315 if (length($opts{difftool_cmd}) > 0) {
316 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
75cd7583
DA
317 } else {
318 print "No <tool> given for --tool=<tool>\n";
319 usage(1);
320 }
321 }
c9bdd505
DA
322 if (defined($opts{extcmd})) {
323 if (length($opts{extcmd}) > 0) {
324 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
75cd7583
DA
325 } else {
326 print "No <cmd> given for --extcmd=<cmd>\n";
327 usage(1);
328 }
329 }
c9bdd505
DA
330 if ($opts{gui}) {
331 my $guitool = Git::config('diff.guitool');
75cd7583
DA
332 if (length($guitool) > 0) {
333 $ENV{GIT_DIFF_TOOL} = $guitool;
334 }
335 }
336
337 # In directory diff mode, 'git-difftool--helper' is called once
338 # to compare the a/b directories. In file diff mode, 'git diff'
339 # will invoke a separate instance of 'git-difftool--helper' for
340 # each file that changed.
c9bdd505 341 if (defined($opts{dirdiff})) {
1f229345 342 dir_diff($opts{extcmd}, $opts{symlinks});
75cd7583 343 } else {
c9bdd505 344 file_diff($opts{prompt});
3f94ff75
TH
345 }
346}
7e0abcec 347
75cd7583
DA
348sub dir_diff
349{
1f229345 350 my ($extcmd, $symlinks) = @_;
75cd7583
DA
351
352 my $rc;
353 my $repo = Git->repository();
354
355 my $workdir = find_worktree($repo);
1f229345 356 my ($a, $b, @worktree) = setup_dir_diff($repo, $workdir, $symlinks);
7e0abcec
TH
357 if (defined($extcmd)) {
358 $rc = system($extcmd, $a, $b);
3f94ff75 359 } else {
7e0abcec
TH
360 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
361 $rc = system('git', 'difftool--helper', $a, $b);
d531174f 362 }
5c38ea31 363
7e0abcec
TH
364 exit($rc | ($rc >> 8)) if ($rc != 0);
365
366 # If the diff including working copy files and those
367 # files were modified during the diff, then the changes
1f229345
DA
368 # should be copied back to the working tree.
369 # Do not copy back files when symlinks are used and the
370 # external tool did not replace the original link with a file.
371 for my $file (@worktree) {
372 next if $symlinks && -l "$b/$file";
283abb2c
DA
373 next if ! -f "$b/$file";
374
375 my $diff = compare("$b/$file", "$workdir/$file");
376 if ($diff == 0) {
377 next;
378 } elsif ($diff == -1) {
379 my $errmsg = "warning: Could not compare ";
380 $errmsg += "'$b/$file' with '$workdir/$file'\n";
381 warn $errmsg;
382 } elsif ($diff == 1) {
05df5326 383 copy("$b/$file", "$workdir/$file") or die $!;
1f229345
DA
384 my $mode = stat("$b/$file")->mode;
385 chmod($mode, "$workdir/$file") or die $!;
05df5326 386 }
7e0abcec 387 }
1f229345 388 exit(0);
75cd7583
DA
389}
390
391sub file_diff
392{
393 my ($prompt) = @_;
394
7e0abcec
TH
395 if (defined($prompt)) {
396 if ($prompt) {
397 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
398 } else {
399 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
400 }
401 }
402
403 $ENV{GIT_PAGER} = '';
404 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
405
406 # ActiveState Perl for Win32 does not implement POSIX semantics of
407 # exec* system call. It just spawns the given executable and finishes
408 # the starting program, exiting with code 0.
409 # system will at least catch the errors returned by git diff,
410 # allowing the caller of git difftool better handling of failures.
411 my $rc = system('git', 'diff', @ARGV);
412 exit($rc | ($rc >> 8));
413}
75cd7583
DA
414
415main();