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);
30 usage
: git difftool
[-t
|--tool
=<tool
>] [--tool
-help
]
33 [--prompt
] [-y
|--no-prompt
]
42 # Git->repository->wc_path() does not honor changes to the working
43 # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
45 return Git
::command_oneline
('rev-parse', '--show-toplevel');
50 my $cmd = 'TOOL_MODE=diff';
51 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
52 $cmd .= ' && show_tool_help';
54 # See the comment at the bottom of file_diff() for the reason behind
55 # using system() followed by exit() instead of exec().
56 my $rc = system('sh', '-c', $cmd);
57 exit($rc | ($rc >> 8));
62 my ($tmpdir, $status) = @_;
65 if ($status and $errno) {
66 my ($package, $file, $line) = caller();
67 warn "$file line $line: $errno\n";
69 exit($status | ($status >> 8));
74 my ($repo, $workdir, $file, $sha1) = @_;
75 my $null_sha1 = '0' x
40;
77 if (! -e
"$workdir/$file") {
78 # If the file doesn't exist in the working tree, we cannot
80 return (0, $null_sha1);
83 my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
84 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
85 return ($use, $wt_sha1);
90 my ($repo_path, $index, $worktree) = @_;
91 $ENV{GIT_INDEX_FILE
} = $index;
92 $ENV{GIT_WORK_TREE
} = $worktree;
93 my $must_unset_git_dir = 0;
94 if (not defined($ENV{GIT_DIR
})) {
95 $must_unset_git_dir = 1;
96 $ENV{GIT_DIR
} = $repo_path;
99 my @refreshargs = qw
/update-index --really-refresh -q --unmerged/;
100 my @gitargs = qw
/diff-files --name-only -z/;
102 Git
::command_oneline
(@refreshargs);
103 } catch Git
::Error
::Command with
{};
105 my $line = Git
::command_oneline
(@gitargs);
108 @files = split('\0', $line);
113 delete($ENV{GIT_INDEX_FILE
});
114 delete($ENV{GIT_WORK_TREE
});
115 delete($ENV{GIT_DIR
}) if ($must_unset_git_dir);
117 return map { $_ => 1 } @files;
122 my ($repo, $workdir, $symlinks) = @_;
124 # Run the diff; exit immediately if no diff found
125 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
126 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
127 # by Git->repository->command*.
128 my $repo_path = $repo->repo_path();
129 my %repo_args = (Repository
=> $repo_path, WorkingCopy
=> $workdir);
130 my $diffrepo = Git
->repository(%repo_args);
132 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
133 my $diffrtn = $diffrepo->command_oneline(@gitargs);
134 exit(0) unless defined($diffrtn);
136 # Build index info for left and right sides of the diff
137 my $submodule_mode = '160000';
138 my $symlink_mode = '120000';
139 my $null_mode = '0' x
6;
140 my $null_sha1 = '0' x
40;
146 my @working_tree = ();
147 my @rawdiff = split('\0', $diffrtn);
150 while ($i < $#rawdiff) {
151 if ($rawdiff[$i] =~ /^::/) {
153 Combined diff formats
('-c' and '--cc') are
not supported
in
154 directory diff mode
('-d' and '--dir-diff').
159 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
160 split(' ', substr($rawdiff[$i], 1));
161 my $src_path = $rawdiff[$i + 1];
164 if ($status =~ /^[CR]/) {
165 $dst_path = $rawdiff[$i + 2];
168 $dst_path = $src_path;
172 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
173 $submodule{$src_path}{left
} = $lsha1;
174 if ($lsha1 ne $rsha1) {
175 $submodule{$dst_path}{right
} = $rsha1;
177 $submodule{$dst_path}{right
} = "$rsha1-dirty";
182 if ($lmode eq $symlink_mode) {
183 $symlink{$src_path}{left
} =
184 $diffrepo->command_oneline('show', "$lsha1");
187 if ($rmode eq $symlink_mode) {
188 $symlink{$dst_path}{right
} =
189 $diffrepo->command_oneline('show', "$rsha1");
192 if ($lmode ne $null_mode and $status !~ /^C/) {
193 $lindex .= "$lmode $lsha1\t$src_path\0";
196 if ($rmode ne $null_mode) {
197 my ($use, $wt_sha1) = use_wt_file
($repo, $workdir,
200 push @working_tree, $dst_path;
201 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
203 $rindex .= "$rmode $rsha1\t$dst_path\0";
208 # Setup temp directories
209 my $tmpdir = tempdir
('git-difftool.XXXXX', CLEANUP
=> 0, TMPDIR
=> 1);
210 my $ldir = "$tmpdir/left";
211 my $rdir = "$tmpdir/right";
212 mkpath
($ldir) or exit_cleanup
($tmpdir, 1);
213 mkpath
($rdir) or exit_cleanup
($tmpdir, 1);
215 # If $GIT_DIR is not set prior to calling 'git update-index' and
216 # 'git checkout-index', then those commands will fail if difftool
217 # is called from a directory other than the repo root.
218 my $must_unset_git_dir = 0;
219 if (not defined($ENV{GIT_DIR
})) {
220 $must_unset_git_dir = 1;
221 $ENV{GIT_DIR
} = $repo_path;
224 # Populate the left and right directories based on each index file
226 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
228 $repo->command_input_pipe(qw(update-index -z --index-info));
229 print($inpipe $lindex);
230 $repo->command_close_pipe($inpipe, $ctx);
232 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
233 exit_cleanup
($tmpdir, $rc) if $rc != 0;
235 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
237 $repo->command_input_pipe(qw(update-index -z --index-info));
238 print($inpipe $rindex);
239 $repo->command_close_pipe($inpipe, $ctx);
241 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
242 exit_cleanup
($tmpdir, $rc) if $rc != 0;
244 $ENV{GIT_INDEX_FILE
} = "$tmpdir/wtindex";
246 $repo->command_input_pipe(qw(update-index --info-only -z --index-info));
247 print($inpipe $wtindex);
248 $repo->command_close_pipe($inpipe, $ctx);
250 # If $GIT_DIR was explicitly set just for the update/checkout
251 # commands, then it should be unset before continuing.
252 delete($ENV{GIT_DIR
}) if ($must_unset_git_dir);
253 delete($ENV{GIT_INDEX_FILE
});
255 # Changes in the working tree need special treatment since they are
256 # not part of the index. Remove any trailing slash from $workdir
257 # before starting to avoid double slashes in symlink targets.
259 for my $file (@working_tree) {
260 my $dir = dirname
($file);
261 unless (-d
"$rdir/$dir") {
262 mkpath
("$rdir/$dir") or
263 exit_cleanup
($tmpdir, 1);
266 symlink("$workdir/$file", "$rdir/$file") or
267 exit_cleanup
($tmpdir, 1);
269 copy
("$workdir/$file", "$rdir/$file") or
270 exit_cleanup
($tmpdir, 1);
272 my $mode = stat("$workdir/$file")->mode;
273 chmod($mode, "$rdir/$file") or
274 exit_cleanup
($tmpdir, 1);
278 # Changes to submodules require special treatment. This loop writes a
279 # temporary file to both the left and right directories to show the
280 # change in the recorded SHA1 for the submodule.
281 for my $path (keys %submodule) {
283 if (defined($submodule{$path}{left
})) {
284 $ok = write_to_file
("$ldir/$path",
285 "Subproject commit $submodule{$path}{left}");
287 if (defined($submodule{$path}{right
})) {
288 $ok = write_to_file
("$rdir/$path",
289 "Subproject commit $submodule{$path}{right}");
291 exit_cleanup
($tmpdir, 1) if not $ok;
294 # Symbolic links require special treatment. The standard "git diff"
295 # shows only the link itself, not the contents of the link target.
296 # This loop replicates that behavior.
297 for my $path (keys %symlink) {
299 if (defined($symlink{$path}{left
})) {
300 $ok = write_to_file
("$ldir/$path",
301 $symlink{$path}{left
});
303 if (defined($symlink{$path}{right
})) {
304 $ok = write_to_file
("$rdir/$path",
305 $symlink{$path}{right
});
307 exit_cleanup
($tmpdir, 1) if not $ok;
310 return ($ldir, $rdir, $tmpdir, @working_tree);
318 # Make sure the path to the file exists
319 my $dir = dirname
($path);
321 mkpath
("$dir") or return 0;
324 # If the file already exists in that location, delete it. This
325 # is required in the case of symbolic links.
328 open(my $fh, '>', $path) or return 0;
337 # parse command-line options. all unrecognized options and arguments
338 # are passed through to the 'git diff' command.
340 difftool_cmd
=> undef,
346 symlinks
=> $^O
ne 'cygwin' &&
347 $^O
ne 'MSWin32' && $^O
ne 'msys',
349 trust_exit_code
=> undef,
351 GetOptions
('g|gui!' => \
$opts{gui
},
352 'd|dir-diff' => \
$opts{dirdiff
},
354 'prompt!' => \
$opts{prompt
},
355 'y' => sub { $opts{prompt
} = 0; },
356 'symlinks' => \
$opts{symlinks
},
357 'no-symlinks' => sub { $opts{symlinks
} = 0; },
358 't|tool:s' => \
$opts{difftool_cmd
},
359 'tool-help' => \
$opts{tool_help
},
360 'trust-exit-code' => \
$opts{trust_exit_code
},
361 'no-trust-exit-code' => sub { $opts{trust_exit_code
} = 0; },
362 'x|extcmd:s' => \
$opts{extcmd
});
364 if (defined($opts{help
})) {
367 if (defined($opts{tool_help
})) {
370 if (defined($opts{difftool_cmd
})) {
371 if (length($opts{difftool_cmd
}) > 0) {
372 $ENV{GIT_DIFF_TOOL
} = $opts{difftool_cmd
};
374 print "No <tool> given for --tool=<tool>\n";
378 if (defined($opts{extcmd
})) {
379 if (length($opts{extcmd
}) > 0) {
380 $ENV{GIT_DIFFTOOL_EXTCMD
} = $opts{extcmd
};
382 print "No <cmd> given for --extcmd=<cmd>\n";
387 my $guitool = Git
::config
('diff.guitool');
388 if (defined($guitool) && length($guitool) > 0) {
389 $ENV{GIT_DIFF_TOOL
} = $guitool;
393 if (!defined $opts{trust_exit_code
}) {
394 $opts{trust_exit_code
} = Git
::config_bool
('difftool.trustExitCode');
396 if ($opts{trust_exit_code
}) {
397 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'true';
399 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE
} = 'false';
402 # In directory diff mode, 'git-difftool--helper' is called once
403 # to compare the a/b directories. In file diff mode, 'git diff'
404 # will invoke a separate instance of 'git-difftool--helper' for
405 # each file that changed.
406 if (defined($opts{dirdiff
})) {
407 dir_diff
($opts{extcmd
}, $opts{symlinks
});
409 file_diff
($opts{prompt
});
415 my ($extcmd, $symlinks) = @_;
418 my $repo = Git
->repository();
419 my $workdir = find_worktree
();
420 my ($a, $b, $tmpdir, @worktree) =
421 setup_dir_diff
($repo, $workdir, $symlinks);
423 if (defined($extcmd)) {
424 $rc = system($extcmd, $a, $b);
426 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
427 $rc = system('git', 'difftool--helper', $a, $b);
429 # If the diff including working copy files and those
430 # files were modified during the diff, then the changes
431 # should be copied back to the working tree.
432 # Do not copy back files when symlinks are used and the
433 # external tool did not replace the original link with a file.
435 # These hashes are loaded lazily since they aren't needed
436 # in the common case of --symlinks and the difftool updating
437 # files through the symlink.
440 my $indices_loaded = 0;
442 for my $file (@worktree) {
443 next if $symlinks && -l
"$b/$file";
444 next if ! -f
"$b/$file";
446 if (!$indices_loaded) {
447 %wt_modified = changed_files
($repo->repo_path(),
448 "$tmpdir/wtindex", "$workdir");
449 %tmp_modified = changed_files
($repo->repo_path(),
450 "$tmpdir/wtindex", "$b");
454 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
455 my $errmsg = "warning: Both files modified: ";
456 $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
457 $errmsg .= "warning: Working tree file has been left.\n";
458 $errmsg .= "warning:\n";
461 } elsif (exists $tmp_modified{$file}) {
462 my $mode = stat("$b/$file")->mode;
463 copy
("$b/$file", "$workdir/$file") or
464 exit_cleanup
($tmpdir, 1);
466 chmod($mode, "$workdir/$file") or
467 exit_cleanup
($tmpdir, 1);
471 warn "warning: Temporary files exist in '$tmpdir'.\n";
472 warn "warning: You may want to cleanup or recover these.\n";
475 exit_cleanup
($tmpdir, $rc);
483 if (defined($prompt)) {
485 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
487 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
491 $ENV{GIT_PAGER
} = '';
492 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
494 # ActiveState Perl for Win32 does not implement POSIX semantics of
495 # exec* system call. It just spawns the given executable and finishes
496 # the starting program, exiting with code 0.
497 # system will at least catch the errors returned by git diff,
498 # allowing the caller of git difftool better handling of failures.
499 my $rc = system('git', 'diff', @ARGV);
500 exit($rc | ($rc >> 8));