2 # Copyright (c) 2009, 2010 David Aguilar
3 # Copyright (c) 2012 Tim Henigan
5 # This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
6 # git-difftool--helper script.
8 # This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
9 # The GIT_DIFF* variables are exported for use by git-difftool--helper.
11 # Any arguments that are unknown to this script are forwarded to 'git diff'.
17 use File
::Basename
qw(dirname);
21 use File
::Path
qw(mkpath rmtree);
22 use File
::Temp
qw(tempdir);
23 use Getopt
::Long
qw(:config pass_through);
31 usage
: git difftool
[-t
|--tool
=<tool
>] [--tool
-help
]
34 [--prompt
] [-y
|--no-prompt
]
43 # See the comment at the bottom of file_diff() for the reason behind
44 # using system() followed by exit() instead of exec().
45 my $rc = system(qw(git mergetool --tool-help=diff));
46 exit($rc | ($rc >> 8));
51 my ($tmpdir, $status) = @_;
54 if ($status and $errno) {
55 my ($package, $file, $line) = caller();
56 warn "$file line $line: $errno\n";
58 exit($status | ($status >> 8));
63 my ($file, $sha1) = @_;
64 my $null_sha1 = '0' x
40;
66 if (-l
$file || ! -e _
) {
67 return (0, $null_sha1);
70 my $wt_sha1 = Git
::command_oneline
('hash-object', $file);
71 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
72 return ($use, $wt_sha1);
77 my ($repo_path, $index, $worktree) = @_;
78 $ENV{GIT_INDEX_FILE
} = $index;
80 my @gitargs = ('--git-dir', $repo_path, '--work-tree', $worktree);
82 @gitargs, 'update-index',
83 '--really-refresh', '-q', '--unmerged');
85 Git
::command_oneline
(@refreshargs);
86 } catch Git
::Error
::Command with
{};
88 my @diffargs = (@gitargs, 'diff-files', '--name-only', '-z');
89 my $line = Git
::command_oneline
(@diffargs);
92 @files = split('\0', $line);
97 delete($ENV{GIT_INDEX_FILE
});
99 return map { $_ => 1 } @files;
104 my ($worktree, $symlinks) = @_;
105 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
106 my $diffrtn = Git
::command_oneline
(@gitargs);
107 exit(0) unless defined($diffrtn);
109 # Go to the root of the worktree now that we've captured the list of
110 # changed files. The paths returned by diff --raw are relative to the
111 # top-level of the repository, but we defer changing directories so
112 # that @ARGV can perform pathspec limiting in the current directory.
115 # Build index info for left and right sides of the diff
116 my $submodule_mode = '160000';
117 my $symlink_mode = '120000';
118 my $null_mode = '0' x
6;
119 my $null_sha1 = '0' x
40;
126 my %working_tree_dups = ();
127 my @rawdiff = split('\0', $diffrtn);
130 while ($i < $#rawdiff) {
131 if ($rawdiff[$i] =~ /^::/) {
133 Combined diff formats ('-c' and '--cc') are not supported in
134 directory diff mode ('-d' and '--dir-diff').
139 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
140 split(' ', substr($rawdiff[$i], 1));
141 my $src_path = $rawdiff[$i + 1];
144 if ($status =~ /^[CR]/) {
145 $dst_path = $rawdiff[$i + 2];
148 $dst_path = $src_path;
152 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
153 $submodule{$src_path}{left
} = $lsha1;
154 if ($lsha1 ne $rsha1) {
155 $submodule{$dst_path}{right
} = $rsha1;
157 $submodule{$dst_path}{right
} = "$rsha1-dirty";
162 if ($lmode eq $symlink_mode) {
163 $symlink{$src_path}{left
} =
164 Git
::command_oneline
('show', $lsha1);
167 if ($rmode eq $symlink_mode) {
168 $symlink{$dst_path}{right
} =
169 Git
::command_oneline
('show', $rsha1);
172 if ($lmode ne $null_mode and $status !~ /^C/) {
173 $lindex .= "$lmode $lsha1\t$src_path\0";
176 if ($rmode ne $null_mode) {
177 # Avoid duplicate entries
178 if ($working_tree_dups{$dst_path}++) {
181 my ($use, $wt_sha1) =
182 use_wt_file
($dst_path, $rsha1);
184 push @files, $dst_path;
185 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
187 $rindex .= "$rmode $rsha1\t$dst_path\0";
192 # Go to the root of the worktree so that the left index files
193 # are properly setup -- the index is toplevel-relative.
196 # Setup temp directories
197 my $tmpdir = tempdir
('git-difftool.XXXXX', CLEANUP
=> 0, TMPDIR
=> 1);
198 my $ldir = "$tmpdir/left";
199 my $rdir = "$tmpdir/right";
200 mkpath
($ldir) or exit_cleanup
($tmpdir, 1);
201 mkpath
($rdir) or exit_cleanup
($tmpdir, 1);
203 # Populate the left and right directories based on each index file
205 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
207 Git
::command_input_pipe
('update-index', '-z', '--index-info');
208 print($inpipe $lindex);
209 Git
::command_close_pipe
($inpipe, $ctx);
211 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
212 exit_cleanup
($tmpdir, $rc) if $rc != 0;
214 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
216 Git
::command_input_pipe
('update-index', '-z', '--index-info');
217 print($inpipe $rindex);
218 Git
::command_close_pipe
($inpipe, $ctx);
220 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
221 exit_cleanup
($tmpdir, $rc) if $rc != 0;
223 $ENV{GIT_INDEX_FILE
} = "$tmpdir/wtindex";
225 Git
::command_input_pipe
('update-index', '--info-only', '-z', '--index-info');
226 print($inpipe $wtindex);
227 Git
::command_close_pipe
($inpipe, $ctx);
229 # If $GIT_DIR was explicitly set just for the update/checkout
230 # commands, then it should be unset before continuing.
231 delete($ENV{GIT_INDEX_FILE
});
233 # Changes in the working tree need special treatment since they are
234 # not part of the index.
235 for my $file (@files) {
236 my $dir = dirname
($file);
237 unless (-d
"$rdir/$dir") {
238 mkpath
("$rdir/$dir") or
239 exit_cleanup
($tmpdir, 1);
242 symlink("$worktree/$file", "$rdir/$file") or
243 exit_cleanup
($tmpdir, 1);
245 copy
($file, "$rdir/$file") or
246 exit_cleanup
($tmpdir, 1);
248 my $mode = stat($file)->mode;
249 chmod($mode, "$rdir/$file") or
250 exit_cleanup
($tmpdir, 1);
254 # Changes to submodules require special treatment. This loop writes a
255 # temporary file to both the left and right directories to show the
256 # change in the recorded SHA1 for the submodule.
257 for my $path (keys %submodule) {
259 if (defined($submodule{$path}{left
})) {
260 $ok = write_to_file
("$ldir/$path",
261 "Subproject commit $submodule{$path}{left}");
263 if (defined($submodule{$path}{right
})) {
264 $ok = write_to_file
("$rdir/$path",
265 "Subproject commit $submodule{$path}{right}");
267 exit_cleanup
($tmpdir, 1) if not $ok;
270 # Symbolic links require special treatment. The standard "git diff"
271 # shows only the link itself, not the contents of the link target.
272 # This loop replicates that behavior.
273 for my $path (keys %symlink) {
275 if (defined($symlink{$path}{left
})) {
276 $ok = write_to_file
("$ldir/$path",
277 $symlink{$path}{left
});
279 if (defined($symlink{$path}{right
})) {
280 $ok = write_to_file
("$rdir/$path",
281 $symlink{$path}{right
});
283 exit_cleanup
($tmpdir, 1) if not $ok;
286 return ($ldir, $rdir, $tmpdir, @files);
294 # Make sure the path to the file exists
295 my $dir = dirname
($path);
297 mkpath
("$dir") or return 0;
300 # If the file already exists in that location, delete it. This
301 # is required in the case of symbolic links.
304 open(my $fh, '>', $path) or return 0;
313 # parse command-line options. all unrecognized options and arguments
314 # are passed through to the 'git diff' command.
316 difftool_cmd
=> undef,
322 symlinks
=> $^O
ne 'cygwin' &&
323 $^O
ne 'MSWin32' && $^O
ne 'msys',
325 trust_exit_code
=> undef,
327 GetOptions
('g|gui!' => \
$opts{gui
},
328 'd|dir-diff' => \
$opts{dirdiff
},
330 'prompt!' => \
$opts{prompt
},
331 'y' => sub { $opts{prompt
} = 0; },
332 'symlinks' => \
$opts{symlinks
},
333 'no-symlinks' => sub { $opts{symlinks
} = 0; },
334 't|tool:s' => \
$opts{difftool_cmd
},
335 'tool-help' => \
$opts{tool_help
},
336 'trust-exit-code' => \
$opts{trust_exit_code
},
337 'no-trust-exit-code' => sub { $opts{trust_exit_code
} = 0; },
338 'x|extcmd:s' => \
$opts{extcmd
});
340 if (defined($opts{help
})) {
343 if (defined($opts{tool_help
})) {
346 if (defined($opts{difftool_cmd
})) {
347 if (length($opts{difftool_cmd
}) > 0) {
348 $ENV{GIT_DIFF_TOOL
} = $opts{difftool_cmd
};
350 print __
("No <tool> given for --tool=<tool>\n");
354 if (defined($opts{extcmd
})) {
355 if (length($opts{extcmd
}) > 0) {
356 $ENV{GIT_DIFFTOOL_EXTCMD
} = $opts{extcmd
};
358 print __
("No <cmd> given for --extcmd=<cmd>\n");
363 my $guitool = Git
::config
('diff.guitool');
364 if (defined($guitool) && length($guitool) > 0) {
365 $ENV{GIT_DIFF_TOOL
} = $guitool;
369 if (!defined $opts{trust_exit_code
}) {
370 $opts{trust_exit_code
} = Git
::config_bool
('difftool.trustExitCode');
372 if ($opts{trust_exit_code
}) {
373 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'true';
375 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'false';
378 # In directory diff mode, 'git-difftool--helper' is called once
379 # to compare the a/b directories. In file diff mode, 'git diff'
380 # will invoke a separate instance of 'git-difftool--helper' for
381 # each file that changed.
382 if (defined($opts{dirdiff
})) {
383 dir_diff
($opts{extcmd
}, $opts{symlinks
});
385 file_diff
($opts{prompt
});
391 my ($extcmd, $symlinks) = @_;
394 my $repo = Git
->repository();
395 my $repo_path = $repo->repo_path();
396 my $worktree = $repo->wc_path();
397 $worktree =~ s
|/$||; # Avoid double slashes in symlink targets
398 my ($a, $b, $tmpdir, @files) = setup_dir_diff
($worktree, $symlinks);
400 if (defined($extcmd)) {
401 $rc = system($extcmd, $a, $b);
403 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
404 $rc = system('git', 'difftool--helper', $a, $b);
406 # If the diff including working copy files and those
407 # files were modified during the diff, then the changes
408 # should be copied back to the working tree.
409 # Do not copy back files when symlinks are used and the
410 # external tool did not replace the original link with a file.
412 # These hashes are loaded lazily since they aren't needed
413 # in the common case of --symlinks and the difftool updating
414 # files through the symlink.
417 my $indices_loaded = 0;
419 for my $file (@files) {
420 next if $symlinks && -l
"$b/$file";
421 next if ! -f
"$b/$file";
423 if (!$indices_loaded) {
424 %wt_modified = changed_files
(
425 $repo_path, "$tmpdir/wtindex", $worktree);
426 %tmp_modified = changed_files
(
427 $repo_path, "$tmpdir/wtindex", $b);
431 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
433 "warning: Both files modified:\n" .
434 "'%s/%s' and '%s/%s'.\n" .
435 "warning: Working tree file has been left.\n" .
436 "warning:\n"), $worktree, $file, $b, $file);
438 } elsif (exists $tmp_modified{$file}) {
439 my $mode = stat("$b/$file")->mode;
440 copy
("$b/$file", $file) or
441 exit_cleanup
($tmpdir, 1);
443 chmod($mode, $file) or
444 exit_cleanup
($tmpdir, 1);
449 "warning: Temporary files exist in '%s'.\n" .
450 "warning: You may want to cleanup or recover these.\n"), $tmpdir);
453 exit_cleanup
($tmpdir, $rc);
461 if (defined($prompt)) {
463 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
465 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
469 $ENV{GIT_PAGER
} = '';
470 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
472 # ActiveState Perl for Win32 does not implement POSIX semantics of
473 # exec* system call. It just spawns the given executable and finishes
474 # the starting program, exiting with code 0.
475 # system will at least catch the errors returned by git diff,
476 # allowing the caller of git difftool better handling of failures.
477 my $rc = system('git', 'diff', @ARGV);
478 exit($rc | ($rc >> 8));