]> git.ipfire.org Git - thirdparty/git.git/blame - git-difftool.perl
difftool: fix argument handling in subdirs
[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;
67aa147a 16use Error qw(:try);
7c7584b9 17use File::Basename qw(dirname);
7e0abcec 18use File::Copy;
7c7584b9 19use File::Find;
7e0abcec 20use File::stat;
ceb1497a 21use File::Path qw(mkpath rmtree);
7e0abcec 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{
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'
44 # config variable.
94eaa806 45 return Git::command_oneline('rev-parse', '--show-toplevel');
7e0abcec
TH
46}
47
bf73fc21
TH
48sub print_tool_help
49{
abaf175c
JK
50 # See the comment at the bottom of file_diff() for the reason behind
51 # using system() followed by exit() instead of exec().
4fb4b02d 52 my $rc = system(qw(git mergetool --tool-help=diff));
abaf175c 53 exit($rc | ($rc >> 8));
bf73fc21
TH
54}
55
ceb1497a
DA
56sub exit_cleanup
57{
58 my ($tmpdir, $status) = @_;
59 my $errno = $!;
60 rmtree($tmpdir);
61 if ($status and $errno) {
62 my ($package, $file, $line) = caller();
63 warn "$file line $line: $errno\n";
64 }
65 exit($status | ($status >> 8));
66}
67
02c56314
JK
68sub use_wt_file
69{
32eaf1de 70 my ($repo, $workdir, $file, $sha1) = @_;
02c56314
JK
71 my $null_sha1 = '0' x 40;
72
cfe2d4be 73 if (-l "$workdir/$file" || ! -e _) {
1f197a1d
JK
74 return (0, $null_sha1);
75 }
76
02c56314 77 my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
67aa147a
JK
78 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
79 return ($use, $wt_sha1);
80}
81
82sub changed_files
83{
84 my ($repo_path, $index, $worktree) = @_;
85 $ENV{GIT_INDEX_FILE} = $index;
86 $ENV{GIT_WORK_TREE} = $worktree;
87 my $must_unset_git_dir = 0;
88 if (not defined($ENV{GIT_DIR})) {
89 $must_unset_git_dir = 1;
90 $ENV{GIT_DIR} = $repo_path;
91 }
92
93 my @refreshargs = qw/update-index --really-refresh -q --unmerged/;
94 my @gitargs = qw/diff-files --name-only -z/;
95 try {
96 Git::command_oneline(@refreshargs);
97 } catch Git::Error::Command with {};
98
99 my $line = Git::command_oneline(@gitargs);
100 my @files;
101 if (defined $line) {
102 @files = split('\0', $line);
103 } else {
104 @files = ();
105 }
106
107 delete($ENV{GIT_INDEX_FILE});
108 delete($ENV{GIT_WORK_TREE});
109 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
110
111 return map { $_ => 1 } @files;
02c56314
JK
112}
113
7e0abcec
TH
114sub setup_dir_diff
115{
1f229345 116 my ($repo, $workdir, $symlinks) = @_;
75cd7583 117
75cd7583 118 my $repo_path = $repo->repo_path();
a4cd5be3 119 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
9ec26e79 120 my $diffrtn = $repo->command_oneline(@gitargs);
ed36e5bd 121 exit(0) unless defined($diffrtn);
7e0abcec 122
7e0abcec
TH
123 # Build index info for left and right sides of the diff
124 my $submodule_mode = '160000';
125 my $symlink_mode = '120000';
126 my $null_mode = '0' x 6;
127 my $null_sha1 = '0' x 40;
128 my $lindex = '';
129 my $rindex = '';
67aa147a 130 my $wtindex = '';
7e0abcec
TH
131 my %submodule;
132 my %symlink;
75cd7583 133 my @working_tree = ();
366f9cea 134 my %working_tree_dups = ();
7e0abcec
TH
135 my @rawdiff = split('\0', $diffrtn);
136
137 my $i = 0;
138 while ($i < $#rawdiff) {
139 if ($rawdiff[$i] =~ /^::/) {
a4cd5be3
DA
140 warn << 'EOF';
141Combined diff formats ('-c' and '--cc') are not supported in
142directory diff mode ('-d' and '--dir-diff').
143EOF
7e0abcec
TH
144 exit(1);
145 }
146
a4cd5be3
DA
147 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
148 split(' ', substr($rawdiff[$i], 1));
7e0abcec
TH
149 my $src_path = $rawdiff[$i + 1];
150 my $dst_path;
151
152 if ($status =~ /^[CR]/) {
153 $dst_path = $rawdiff[$i + 2];
154 $i += 3;
155 } else {
156 $dst_path = $src_path;
157 $i += 2;
158 }
159
a4cd5be3 160 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
7e0abcec
TH
161 $submodule{$src_path}{left} = $lsha1;
162 if ($lsha1 ne $rsha1) {
163 $submodule{$dst_path}{right} = $rsha1;
164 } else {
165 $submodule{$dst_path}{right} = "$rsha1-dirty";
166 }
167 next;
168 }
169
170 if ($lmode eq $symlink_mode) {
a4cd5be3 171 $symlink{$src_path}{left} =
9ec26e79 172 $repo->command_oneline('show', "$lsha1");
7e0abcec
TH
173 }
174
175 if ($rmode eq $symlink_mode) {
a4cd5be3 176 $symlink{$dst_path}{right} =
9ec26e79 177 $repo->command_oneline('show', "$rsha1");
7e0abcec
TH
178 }
179
a4cd5be3 180 if ($lmode ne $null_mode and $status !~ /^C/) {
7e0abcec
TH
181 $lindex .= "$lmode $lsha1\t$src_path\0";
182 }
183
184 if ($rmode ne $null_mode) {
366f9cea
DA
185 # Avoid duplicate working_tree entries
186 if ($working_tree_dups{$dst_path}++) {
187 next;
188 }
67aa147a 189 my ($use, $wt_sha1) = use_wt_file($repo, $workdir,
32eaf1de 190 $dst_path, $rsha1);
67aa147a
JK
191 if ($use) {
192 push @working_tree, $dst_path;
193 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
02c56314
JK
194 } else {
195 $rindex .= "$rmode $rsha1\t$dst_path\0";
7e0abcec
TH
196 }
197 }
198 }
199
ceb1497a
DA
200 # Setup temp directories
201 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
202 my $ldir = "$tmpdir/left";
203 my $rdir = "$tmpdir/right";
204 mkpath($ldir) or exit_cleanup($tmpdir, 1);
205 mkpath($rdir) or exit_cleanup($tmpdir, 1);
206
7e0abcec
TH
207 # If $GIT_DIR is not set prior to calling 'git update-index' and
208 # 'git checkout-index', then those commands will fail if difftool
209 # is called from a directory other than the repo root.
210 my $must_unset_git_dir = 0;
211 if (not defined($ENV{GIT_DIR})) {
212 $must_unset_git_dir = 1;
213 $ENV{GIT_DIR} = $repo_path;
214 }
215
216 # Populate the left and right directories based on each index file
217 my ($inpipe, $ctx);
218 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
a4cd5be3
DA
219 ($inpipe, $ctx) =
220 $repo->command_input_pipe(qw(update-index -z --index-info));
7e0abcec
TH
221 print($inpipe $lindex);
222 $repo->command_close_pipe($inpipe, $ctx);
ceb1497a 223
75cd7583 224 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
ceb1497a 225 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec
TH
226
227 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
a4cd5be3
DA
228 ($inpipe, $ctx) =
229 $repo->command_input_pipe(qw(update-index -z --index-info));
7e0abcec
TH
230 print($inpipe $rindex);
231 $repo->command_close_pipe($inpipe, $ctx);
ceb1497a 232
7e0abcec 233 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
ceb1497a 234 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec 235
67aa147a
JK
236 $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
237 ($inpipe, $ctx) =
238 $repo->command_input_pipe(qw(update-index --info-only -z --index-info));
239 print($inpipe $wtindex);
240 $repo->command_close_pipe($inpipe, $ctx);
241
7e0abcec
TH
242 # If $GIT_DIR was explicitly set just for the update/checkout
243 # commands, then it should be unset before continuing.
244 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
245 delete($ENV{GIT_INDEX_FILE});
246
247 # Changes in the working tree need special treatment since they are
e0976dcf
JK
248 # not part of the index. Remove any trailing slash from $workdir
249 # before starting to avoid double slashes in symlink targets.
250 $workdir =~ s|/$||;
7e0abcec
TH
251 for my $file (@working_tree) {
252 my $dir = dirname($file);
253 unless (-d "$rdir/$dir") {
ceb1497a
DA
254 mkpath("$rdir/$dir") or
255 exit_cleanup($tmpdir, 1);
7e0abcec 256 }
1f229345 257 if ($symlinks) {
ceb1497a
DA
258 symlink("$workdir/$file", "$rdir/$file") or
259 exit_cleanup($tmpdir, 1);
1f229345 260 } else {
ceb1497a
DA
261 copy("$workdir/$file", "$rdir/$file") or
262 exit_cleanup($tmpdir, 1);
263
1f229345 264 my $mode = stat("$workdir/$file")->mode;
ceb1497a
DA
265 chmod($mode, "$rdir/$file") or
266 exit_cleanup($tmpdir, 1);
1f229345 267 }
7e0abcec
TH
268 }
269
270 # Changes to submodules require special treatment. This loop writes a
271 # temporary file to both the left and right directories to show the
272 # change in the recorded SHA1 for the submodule.
273 for my $path (keys %submodule) {
951b551d 274 my $ok = 0;
7e0abcec 275 if (defined($submodule{$path}{left})) {
ceb1497a
DA
276 $ok = write_to_file("$ldir/$path",
277 "Subproject commit $submodule{$path}{left}");
7e0abcec
TH
278 }
279 if (defined($submodule{$path}{right})) {
ceb1497a
DA
280 $ok = write_to_file("$rdir/$path",
281 "Subproject commit $submodule{$path}{right}");
7e0abcec 282 }
ceb1497a 283 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
284 }
285
286 # Symbolic links require special treatment. The standard "git diff"
287 # shows only the link itself, not the contents of the link target.
288 # This loop replicates that behavior.
289 for my $path (keys %symlink) {
951b551d 290 my $ok = 0;
7e0abcec 291 if (defined($symlink{$path}{left})) {
ceb1497a
DA
292 $ok = write_to_file("$ldir/$path",
293 $symlink{$path}{left});
7e0abcec
TH
294 }
295 if (defined($symlink{$path}{right})) {
ceb1497a
DA
296 $ok = write_to_file("$rdir/$path",
297 $symlink{$path}{right});
7e0abcec 298 }
ceb1497a 299 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
300 }
301
ceb1497a 302 return ($ldir, $rdir, $tmpdir, @working_tree);
7e0abcec
TH
303}
304
305sub write_to_file
306{
307 my $path = shift;
308 my $value = shift;
309
310 # Make sure the path to the file exists
311 my $dir = dirname($path);
312 unless (-d "$dir") {
ceb1497a 313 mkpath("$dir") or return 0;
7e0abcec
TH
314 }
315
316 # If the file already exists in that location, delete it. This
317 # is required in the case of symbolic links.
ceb1497a 318 unlink($path);
7e0abcec 319
ceb1497a 320 open(my $fh, '>', $path) or return 0;
7e0abcec
TH
321 print($fh $value);
322 close($fh);
ceb1497a
DA
323
324 return 1;
7e0abcec
TH
325}
326
75cd7583
DA
327sub main
328{
329 # parse command-line options. all unrecognized options and arguments
330 # are passed through to the 'git diff' command.
c9bdd505
DA
331 my %opts = (
332 difftool_cmd => undef,
333 dirdiff => undef,
334 extcmd => undef,
335 gui => undef,
336 help => undef,
337 prompt => undef,
190f5475
DA
338 symlinks => $^O ne 'cygwin' &&
339 $^O ne 'MSWin32' && $^O ne 'msys',
c9bdd505 340 tool_help => undef,
2b52123f 341 trust_exit_code => undef,
c9bdd505
DA
342 );
343 GetOptions('g|gui!' => \$opts{gui},
344 'd|dir-diff' => \$opts{dirdiff},
345 'h' => \$opts{help},
346 'prompt!' => \$opts{prompt},
347 'y' => sub { $opts{prompt} = 0; },
1f229345
DA
348 'symlinks' => \$opts{symlinks},
349 'no-symlinks' => sub { $opts{symlinks} = 0; },
c9bdd505
DA
350 't|tool:s' => \$opts{difftool_cmd},
351 'tool-help' => \$opts{tool_help},
2b52123f
DA
352 'trust-exit-code' => \$opts{trust_exit_code},
353 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; },
c9bdd505
DA
354 'x|extcmd:s' => \$opts{extcmd});
355
356 if (defined($opts{help})) {
75cd7583 357 usage(0);
5c38ea31 358 }
c9bdd505 359 if (defined($opts{tool_help})) {
75cd7583 360 print_tool_help();
3f94ff75 361 }
c9bdd505
DA
362 if (defined($opts{difftool_cmd})) {
363 if (length($opts{difftool_cmd}) > 0) {
364 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
75cd7583
DA
365 } else {
366 print "No <tool> given for --tool=<tool>\n";
367 usage(1);
368 }
369 }
c9bdd505
DA
370 if (defined($opts{extcmd})) {
371 if (length($opts{extcmd}) > 0) {
372 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
75cd7583
DA
373 } else {
374 print "No <cmd> given for --extcmd=<cmd>\n";
375 usage(1);
376 }
377 }
c9bdd505
DA
378 if ($opts{gui}) {
379 my $guitool = Git::config('diff.guitool');
af14b5cf 380 if (defined($guitool) && length($guitool) > 0) {
75cd7583
DA
381 $ENV{GIT_DIFF_TOOL} = $guitool;
382 }
383 }
384
2b52123f
DA
385 if (!defined $opts{trust_exit_code}) {
386 $opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode');
387 }
388 if ($opts{trust_exit_code}) {
389 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true';
390 } else {
391 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false';
392 }
393
75cd7583
DA
394 # In directory diff mode, 'git-difftool--helper' is called once
395 # to compare the a/b directories. In file diff mode, 'git diff'
396 # will invoke a separate instance of 'git-difftool--helper' for
397 # each file that changed.
c9bdd505 398 if (defined($opts{dirdiff})) {
1f229345 399 dir_diff($opts{extcmd}, $opts{symlinks});
75cd7583 400 } else {
c9bdd505 401 file_diff($opts{prompt});
3f94ff75
TH
402 }
403}
7e0abcec 404
75cd7583
DA
405sub dir_diff
406{
1f229345 407 my ($extcmd, $symlinks) = @_;
75cd7583 408 my $rc;
ceb1497a 409 my $error = 0;
75cd7583 410 my $repo = Git->repository();
94eaa806 411 my $workdir = find_worktree();
ceb1497a
DA
412 my ($a, $b, $tmpdir, @worktree) =
413 setup_dir_diff($repo, $workdir, $symlinks);
414
7e0abcec
TH
415 if (defined($extcmd)) {
416 $rc = system($extcmd, $a, $b);
3f94ff75 417 } else {
7e0abcec
TH
418 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
419 $rc = system('git', 'difftool--helper', $a, $b);
d531174f 420 }
7e0abcec
TH
421 # If the diff including working copy files and those
422 # files were modified during the diff, then the changes
1f229345
DA
423 # should be copied back to the working tree.
424 # Do not copy back files when symlinks are used and the
425 # external tool did not replace the original link with a file.
67aa147a
JK
426 #
427 # These hashes are loaded lazily since they aren't needed
428 # in the common case of --symlinks and the difftool updating
429 # files through the symlink.
430 my %wt_modified;
431 my %tmp_modified;
432 my $indices_loaded = 0;
433
1f229345
DA
434 for my $file (@worktree) {
435 next if $symlinks && -l "$b/$file";
283abb2c
DA
436 next if ! -f "$b/$file";
437
67aa147a
JK
438 if (!$indices_loaded) {
439 %wt_modified = changed_files($repo->repo_path(),
440 "$tmpdir/wtindex", "$workdir");
441 %tmp_modified = changed_files($repo->repo_path(),
442 "$tmpdir/wtindex", "$b");
443 $indices_loaded = 1;
444 }
445
446 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
447 my $errmsg = "warning: Both files modified: ";
448 $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
449 $errmsg .= "warning: Working tree file has been left.\n";
450 $errmsg .= "warning:\n";
283abb2c 451 warn $errmsg;
ceb1497a 452 $error = 1;
67aa147a 453 } elsif (exists $tmp_modified{$file}) {
1f229345 454 my $mode = stat("$b/$file")->mode;
ceb1497a
DA
455 copy("$b/$file", "$workdir/$file") or
456 exit_cleanup($tmpdir, 1);
457
458 chmod($mode, "$workdir/$file") or
459 exit_cleanup($tmpdir, 1);
05df5326 460 }
7e0abcec 461 }
ceb1497a
DA
462 if ($error) {
463 warn "warning: Temporary files exist in '$tmpdir'.\n";
464 warn "warning: You may want to cleanup or recover these.\n";
465 exit(1);
466 } else {
467 exit_cleanup($tmpdir, $rc);
468 }
75cd7583
DA
469}
470
471sub file_diff
472{
473 my ($prompt) = @_;
474
7e0abcec
TH
475 if (defined($prompt)) {
476 if ($prompt) {
477 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
478 } else {
479 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
480 }
481 }
482
483 $ENV{GIT_PAGER} = '';
484 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
485
486 # ActiveState Perl for Win32 does not implement POSIX semantics of
487 # exec* system call. It just spawns the given executable and finishes
488 # the starting program, exiting with code 0.
489 # system will at least catch the errors returned by git diff,
490 # allowing the caller of git difftool better handling of failures.
491 my $rc = system('git', 'diff', @ARGV);
492 exit($rc | ($rc >> 8));
493}
75cd7583
DA
494
495main();