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 ($workdir, $file, $sha1) = @_;
64 my $null_sha1 = '0' x
40;
66 if (-l
"$workdir/$file" || ! -e _
) {
67 return (0, $null_sha1);
70 my $wt_sha1 = Git
::command_oneline
('hash-object', "$workdir/$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 ($workdir, $symlinks) = @_;
105 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
106 my $diffrtn = Git
::command_oneline
(@gitargs);
107 exit(0) unless defined($diffrtn);
109 # Build index info for left and right sides of the diff
110 my $submodule_mode = '160000';
111 my $symlink_mode = '120000';
112 my $null_mode = '0' x
6;
113 my $null_sha1 = '0' x
40;
119 my @working_tree = ();
120 my %working_tree_dups = ();
121 my @rawdiff = split('\0', $diffrtn);
124 while ($i < $#rawdiff) {
125 if ($rawdiff[$i] =~ /^::/) {
127 Combined diff formats ('-c' and '--cc') are not supported in
128 directory diff mode ('-d' and '--dir-diff').
133 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
134 split(' ', substr($rawdiff[$i], 1));
135 my $src_path = $rawdiff[$i + 1];
138 if ($status =~ /^[CR]/) {
139 $dst_path = $rawdiff[$i + 2];
142 $dst_path = $src_path;
146 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
147 $submodule{$src_path}{left
} = $lsha1;
148 if ($lsha1 ne $rsha1) {
149 $submodule{$dst_path}{right
} = $rsha1;
151 $submodule{$dst_path}{right
} = "$rsha1-dirty";
156 if ($lmode eq $symlink_mode) {
157 $symlink{$src_path}{left
} =
158 Git
::command_oneline
('show', $lsha1);
161 if ($rmode eq $symlink_mode) {
162 $symlink{$dst_path}{right
} =
163 Git
::command_oneline
('show', $rsha1);
166 if ($lmode ne $null_mode and $status !~ /^C/) {
167 $lindex .= "$lmode $lsha1\t$src_path\0";
170 if ($rmode ne $null_mode) {
171 # Avoid duplicate working_tree entries
172 if ($working_tree_dups{$dst_path}++) {
175 my ($use, $wt_sha1) =
176 use_wt_file
($workdir, $dst_path, $rsha1);
178 push @working_tree, $dst_path;
179 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
181 $rindex .= "$rmode $rsha1\t$dst_path\0";
186 # Setup temp directories
187 my $tmpdir = tempdir
('git-difftool.XXXXX', CLEANUP
=> 0, TMPDIR
=> 1);
188 my $ldir = "$tmpdir/left";
189 my $rdir = "$tmpdir/right";
190 mkpath
($ldir) or exit_cleanup
($tmpdir, 1);
191 mkpath
($rdir) or exit_cleanup
($tmpdir, 1);
193 # Populate the left and right directories based on each index file
195 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
197 Git
::command_input_pipe
('update-index', '-z', '--index-info');
198 print($inpipe $lindex);
199 Git
::command_close_pipe
($inpipe, $ctx);
201 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
202 exit_cleanup
($tmpdir, $rc) if $rc != 0;
204 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
206 Git
::command_input_pipe
('update-index', '-z', '--index-info');
207 print($inpipe $rindex);
208 Git
::command_close_pipe
($inpipe, $ctx);
210 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
211 exit_cleanup
($tmpdir, $rc) if $rc != 0;
213 $ENV{GIT_INDEX_FILE
} = "$tmpdir/wtindex";
215 Git
::command_input_pipe
('update-index', '--info-only', '-z', '--index-info');
216 print($inpipe $wtindex);
217 Git
::command_close_pipe
($inpipe, $ctx);
219 # If $GIT_DIR was explicitly set just for the update/checkout
220 # commands, then it should be unset before continuing.
221 delete($ENV{GIT_INDEX_FILE
});
223 # Changes in the working tree need special treatment since they are
224 # not part of the index. Remove any trailing slash from $workdir
225 # before starting to avoid double slashes in symlink targets.
227 for my $file (@working_tree) {
228 my $dir = dirname
($file);
229 unless (-d
"$rdir/$dir") {
230 mkpath
("$rdir/$dir") or
231 exit_cleanup
($tmpdir, 1);
234 symlink("$workdir/$file", "$rdir/$file") or
235 exit_cleanup
($tmpdir, 1);
237 copy
("$workdir/$file", "$rdir/$file") or
238 exit_cleanup
($tmpdir, 1);
240 my $mode = stat("$workdir/$file")->mode;
241 chmod($mode, "$rdir/$file") or
242 exit_cleanup
($tmpdir, 1);
246 # Changes to submodules require special treatment. This loop writes a
247 # temporary file to both the left and right directories to show the
248 # change in the recorded SHA1 for the submodule.
249 for my $path (keys %submodule) {
251 if (defined($submodule{$path}{left
})) {
252 $ok = write_to_file
("$ldir/$path",
253 "Subproject commit $submodule{$path}{left}");
255 if (defined($submodule{$path}{right
})) {
256 $ok = write_to_file
("$rdir/$path",
257 "Subproject commit $submodule{$path}{right}");
259 exit_cleanup
($tmpdir, 1) if not $ok;
262 # Symbolic links require special treatment. The standard "git diff"
263 # shows only the link itself, not the contents of the link target.
264 # This loop replicates that behavior.
265 for my $path (keys %symlink) {
267 if (defined($symlink{$path}{left
})) {
268 $ok = write_to_file
("$ldir/$path",
269 $symlink{$path}{left
});
271 if (defined($symlink{$path}{right
})) {
272 $ok = write_to_file
("$rdir/$path",
273 $symlink{$path}{right
});
275 exit_cleanup
($tmpdir, 1) if not $ok;
278 return ($ldir, $rdir, $tmpdir, @working_tree);
286 # Make sure the path to the file exists
287 my $dir = dirname
($path);
289 mkpath
("$dir") or return 0;
292 # If the file already exists in that location, delete it. This
293 # is required in the case of symbolic links.
296 open(my $fh, '>', $path) or return 0;
305 # parse command-line options. all unrecognized options and arguments
306 # are passed through to the 'git diff' command.
308 difftool_cmd
=> undef,
314 symlinks
=> $^O
ne 'cygwin' &&
315 $^O
ne 'MSWin32' && $^O
ne 'msys',
317 trust_exit_code
=> undef,
319 GetOptions
('g|gui!' => \
$opts{gui
},
320 'd|dir-diff' => \
$opts{dirdiff
},
322 'prompt!' => \
$opts{prompt
},
323 'y' => sub { $opts{prompt
} = 0; },
324 'symlinks' => \
$opts{symlinks
},
325 'no-symlinks' => sub { $opts{symlinks
} = 0; },
326 't|tool:s' => \
$opts{difftool_cmd
},
327 'tool-help' => \
$opts{tool_help
},
328 'trust-exit-code' => \
$opts{trust_exit_code
},
329 'no-trust-exit-code' => sub { $opts{trust_exit_code
} = 0; },
330 'x|extcmd:s' => \
$opts{extcmd
});
332 if (defined($opts{help
})) {
335 if (defined($opts{tool_help
})) {
338 if (defined($opts{difftool_cmd
})) {
339 if (length($opts{difftool_cmd
}) > 0) {
340 $ENV{GIT_DIFF_TOOL
} = $opts{difftool_cmd
};
342 print __
("No <tool> given for --tool=<tool>\n");
346 if (defined($opts{extcmd
})) {
347 if (length($opts{extcmd
}) > 0) {
348 $ENV{GIT_DIFFTOOL_EXTCMD
} = $opts{extcmd
};
350 print __
("No <cmd> given for --extcmd=<cmd>\n");
355 my $guitool = Git
::config
('diff.guitool');
356 if (defined($guitool) && length($guitool) > 0) {
357 $ENV{GIT_DIFF_TOOL
} = $guitool;
361 if (!defined $opts{trust_exit_code
}) {
362 $opts{trust_exit_code
} = Git
::config_bool
('difftool.trustExitCode');
364 if ($opts{trust_exit_code
}) {
365 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'true';
367 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'false';
370 # In directory diff mode, 'git-difftool--helper' is called once
371 # to compare the a/b directories. In file diff mode, 'git diff'
372 # will invoke a separate instance of 'git-difftool--helper' for
373 # each file that changed.
374 if (defined($opts{dirdiff
})) {
375 dir_diff
($opts{extcmd
}, $opts{symlinks
});
377 file_diff
($opts{prompt
});
383 my ($extcmd, $symlinks) = @_;
386 my $repo = Git
->repository();
387 my $repo_path = $repo->repo_path();
388 my $workdir = $repo->wc_path();
389 my ($a, $b, $tmpdir, @worktree) = setup_dir_diff
($workdir, $symlinks);
391 if (defined($extcmd)) {
392 $rc = system($extcmd, $a, $b);
394 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
395 $rc = system('git', 'difftool--helper', $a, $b);
397 # If the diff including working copy files and those
398 # files were modified during the diff, then the changes
399 # should be copied back to the working tree.
400 # Do not copy back files when symlinks are used and the
401 # external tool did not replace the original link with a file.
403 # These hashes are loaded lazily since they aren't needed
404 # in the common case of --symlinks and the difftool updating
405 # files through the symlink.
408 my $indices_loaded = 0;
410 for my $file (@worktree) {
411 next if $symlinks && -l
"$b/$file";
412 next if ! -f
"$b/$file";
414 if (!$indices_loaded) {
415 %wt_modified = changed_files
(
416 $repo_path, "$tmpdir/wtindex", $workdir);
417 %tmp_modified = changed_files
(
418 $repo_path, "$tmpdir/wtindex", $b);
422 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
424 "warning: Both files modified:\n" .
425 "'%s/%s' and '%s/%s'.\n" .
426 "warning: Working tree file has been left.\n" .
427 "warning:\n"), $workdir, $file, $b, $file);
429 } elsif (exists $tmp_modified{$file}) {
430 my $mode = stat("$b/$file")->mode;
431 copy
("$b/$file", "$workdir/$file") or
432 exit_cleanup
($tmpdir, 1);
434 chmod($mode, "$workdir/$file") or
435 exit_cleanup
($tmpdir, 1);
440 "warning: Temporary files exist in '%s'.\n" .
441 "warning: You may want to cleanup or recover these.\n"), $tmpdir);
444 exit_cleanup
($tmpdir, $rc);
452 if (defined($prompt)) {
454 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
456 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
460 $ENV{GIT_PAGER
} = '';
461 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
463 # ActiveState Perl for Win32 does not implement POSIX semantics of
464 # exec* system call. It just spawns the given executable and finishes
465 # the starting program, exiting with code 0.
466 # system will at least catch the errors returned by git diff,
467 # allowing the caller of git difftool better handling of failures.
468 my $rc = system('git', 'diff', @ARGV);
469 exit($rc | ($rc >> 8));