]> git.ipfire.org Git - thirdparty/git.git/blame - git-difftool.perl
sequencer (rebase -i): respect strategy/strategy_opts settings
[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;
a948ebac 25use Git::I18N;
5c38ea31
DA
26
27sub usage
28{
28360769 29 my $exitcode = shift;
5c38ea31 30 print << 'USAGE';
bf73fc21 31usage: git difftool [-t|--tool=<tool>] [--tool-help]
85089604
TH
32 [-x|--extcmd=<cmd>]
33 [-g|--gui] [--no-gui]
3f94ff75 34 [--prompt] [-y|--no-prompt]
7e0abcec 35 [-d|--dir-diff]
f47f1e2c 36 ['git diff' options]
5c38ea31 37USAGE
28360769 38 exit($exitcode);
5c38ea31
DA
39}
40
bf73fc21
TH
41sub print_tool_help
42{
abaf175c
JK
43 # See the comment at the bottom of file_diff() for the reason behind
44 # using system() followed by exit() instead of exec().
4fb4b02d 45 my $rc = system(qw(git mergetool --tool-help=diff));
abaf175c 46 exit($rc | ($rc >> 8));
bf73fc21
TH
47}
48
ceb1497a
DA
49sub exit_cleanup
50{
51 my ($tmpdir, $status) = @_;
52 my $errno = $!;
53 rmtree($tmpdir);
54 if ($status and $errno) {
55 my ($package, $file, $line) = caller();
56 warn "$file line $line: $errno\n";
57 }
58 exit($status | ($status >> 8));
59}
60
02c56314
JK
61sub use_wt_file
62{
f242a03d 63 my ($file, $sha1) = @_;
02c56314
JK
64 my $null_sha1 = '0' x 40;
65
f242a03d 66 if (-l $file || ! -e _) {
1f197a1d
JK
67 return (0, $null_sha1);
68 }
69
f242a03d 70 my $wt_sha1 = Git::command_oneline('hash-object', $file);
67aa147a
JK
71 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
72 return ($use, $wt_sha1);
73}
74
75sub changed_files
76{
77 my ($repo_path, $index, $worktree) = @_;
78 $ENV{GIT_INDEX_FILE} = $index;
67aa147a 79
98f917ed
DA
80 my @gitargs = ('--git-dir', $repo_path, '--work-tree', $worktree);
81 my @refreshargs = (
82 @gitargs, 'update-index',
83 '--really-refresh', '-q', '--unmerged');
67aa147a
JK
84 try {
85 Git::command_oneline(@refreshargs);
86 } catch Git::Error::Command with {};
87
98f917ed
DA
88 my @diffargs = (@gitargs, 'diff-files', '--name-only', '-z');
89 my $line = Git::command_oneline(@diffargs);
67aa147a
JK
90 my @files;
91 if (defined $line) {
92 @files = split('\0', $line);
93 } else {
94 @files = ();
95 }
96
97 delete($ENV{GIT_INDEX_FILE});
67aa147a
JK
98
99 return map { $_ => 1 } @files;
02c56314
JK
100}
101
7e0abcec
TH
102sub setup_dir_diff
103{
ce692697 104 my ($worktree, $symlinks) = @_;
a4cd5be3 105 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
32b8c581 106 my $diffrtn = Git::command_oneline(@gitargs);
ed36e5bd 107 exit(0) unless defined($diffrtn);
7e0abcec 108
f242a03d
DA
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.
ce692697 113 chdir($worktree);
f242a03d 114
7e0abcec
TH
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;
120 my $lindex = '';
121 my $rindex = '';
67aa147a 122 my $wtindex = '';
7e0abcec
TH
123 my %submodule;
124 my %symlink;
ce692697 125 my @files = ();
366f9cea 126 my %working_tree_dups = ();
7e0abcec
TH
127 my @rawdiff = split('\0', $diffrtn);
128
129 my $i = 0;
130 while ($i < $#rawdiff) {
131 if ($rawdiff[$i] =~ /^::/) {
a948ebac 132 warn __ <<'EOF';
a4cd5be3
DA
133Combined diff formats ('-c' and '--cc') are not supported in
134directory diff mode ('-d' and '--dir-diff').
135EOF
7e0abcec
TH
136 exit(1);
137 }
138
a4cd5be3
DA
139 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
140 split(' ', substr($rawdiff[$i], 1));
7e0abcec
TH
141 my $src_path = $rawdiff[$i + 1];
142 my $dst_path;
143
144 if ($status =~ /^[CR]/) {
145 $dst_path = $rawdiff[$i + 2];
146 $i += 3;
147 } else {
148 $dst_path = $src_path;
149 $i += 2;
150 }
151
a4cd5be3 152 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
7e0abcec
TH
153 $submodule{$src_path}{left} = $lsha1;
154 if ($lsha1 ne $rsha1) {
155 $submodule{$dst_path}{right} = $rsha1;
156 } else {
157 $submodule{$dst_path}{right} = "$rsha1-dirty";
158 }
159 next;
160 }
161
162 if ($lmode eq $symlink_mode) {
a4cd5be3 163 $symlink{$src_path}{left} =
32b8c581 164 Git::command_oneline('show', $lsha1);
7e0abcec
TH
165 }
166
167 if ($rmode eq $symlink_mode) {
a4cd5be3 168 $symlink{$dst_path}{right} =
32b8c581 169 Git::command_oneline('show', $rsha1);
7e0abcec
TH
170 }
171
a4cd5be3 172 if ($lmode ne $null_mode and $status !~ /^C/) {
7e0abcec
TH
173 $lindex .= "$lmode $lsha1\t$src_path\0";
174 }
175
176 if ($rmode ne $null_mode) {
ce692697 177 # Avoid duplicate entries
366f9cea
DA
178 if ($working_tree_dups{$dst_path}++) {
179 next;
180 }
32b8c581 181 my ($use, $wt_sha1) =
f242a03d 182 use_wt_file($dst_path, $rsha1);
67aa147a 183 if ($use) {
ce692697 184 push @files, $dst_path;
67aa147a 185 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
02c56314
JK
186 } else {
187 $rindex .= "$rmode $rsha1\t$dst_path\0";
7e0abcec
TH
188 }
189 }
190 }
191
853e10c1
DA
192 # Go to the root of the worktree so that the left index files
193 # are properly setup -- the index is toplevel-relative.
5a5d3f1f 194 chdir($worktree);
853e10c1 195
ceb1497a
DA
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);
202
7e0abcec
TH
203 # Populate the left and right directories based on each index file
204 my ($inpipe, $ctx);
205 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
a4cd5be3 206 ($inpipe, $ctx) =
32b8c581 207 Git::command_input_pipe('update-index', '-z', '--index-info');
7e0abcec 208 print($inpipe $lindex);
32b8c581 209 Git::command_close_pipe($inpipe, $ctx);
ceb1497a 210
75cd7583 211 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
ceb1497a 212 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec
TH
213
214 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
a4cd5be3 215 ($inpipe, $ctx) =
32b8c581 216 Git::command_input_pipe('update-index', '-z', '--index-info');
7e0abcec 217 print($inpipe $rindex);
32b8c581 218 Git::command_close_pipe($inpipe, $ctx);
ceb1497a 219
7e0abcec 220 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
ceb1497a 221 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec 222
67aa147a
JK
223 $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
224 ($inpipe, $ctx) =
32b8c581 225 Git::command_input_pipe('update-index', '--info-only', '-z', '--index-info');
67aa147a 226 print($inpipe $wtindex);
32b8c581 227 Git::command_close_pipe($inpipe, $ctx);
67aa147a 228
7e0abcec
TH
229 # If $GIT_DIR was explicitly set just for the update/checkout
230 # commands, then it should be unset before continuing.
7e0abcec
TH
231 delete($ENV{GIT_INDEX_FILE});
232
233 # Changes in the working tree need special treatment since they are
e6e3e2a6 234 # not part of the index.
ce692697 235 for my $file (@files) {
7e0abcec
TH
236 my $dir = dirname($file);
237 unless (-d "$rdir/$dir") {
ceb1497a
DA
238 mkpath("$rdir/$dir") or
239 exit_cleanup($tmpdir, 1);
7e0abcec 240 }
1f229345 241 if ($symlinks) {
ce692697 242 symlink("$worktree/$file", "$rdir/$file") or
ceb1497a 243 exit_cleanup($tmpdir, 1);
1f229345 244 } else {
f242a03d 245 copy($file, "$rdir/$file") or
ceb1497a
DA
246 exit_cleanup($tmpdir, 1);
247
f242a03d 248 my $mode = stat($file)->mode;
ceb1497a
DA
249 chmod($mode, "$rdir/$file") or
250 exit_cleanup($tmpdir, 1);
1f229345 251 }
7e0abcec
TH
252 }
253
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) {
951b551d 258 my $ok = 0;
7e0abcec 259 if (defined($submodule{$path}{left})) {
ceb1497a
DA
260 $ok = write_to_file("$ldir/$path",
261 "Subproject commit $submodule{$path}{left}");
7e0abcec
TH
262 }
263 if (defined($submodule{$path}{right})) {
ceb1497a
DA
264 $ok = write_to_file("$rdir/$path",
265 "Subproject commit $submodule{$path}{right}");
7e0abcec 266 }
ceb1497a 267 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
268 }
269
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) {
951b551d 274 my $ok = 0;
7e0abcec 275 if (defined($symlink{$path}{left})) {
ceb1497a
DA
276 $ok = write_to_file("$ldir/$path",
277 $symlink{$path}{left});
7e0abcec
TH
278 }
279 if (defined($symlink{$path}{right})) {
ceb1497a
DA
280 $ok = write_to_file("$rdir/$path",
281 $symlink{$path}{right});
7e0abcec 282 }
ceb1497a 283 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
284 }
285
ce692697 286 return ($ldir, $rdir, $tmpdir, @files);
7e0abcec
TH
287}
288
289sub write_to_file
290{
291 my $path = shift;
292 my $value = shift;
293
294 # Make sure the path to the file exists
295 my $dir = dirname($path);
296 unless (-d "$dir") {
ceb1497a 297 mkpath("$dir") or return 0;
7e0abcec
TH
298 }
299
300 # If the file already exists in that location, delete it. This
301 # is required in the case of symbolic links.
ceb1497a 302 unlink($path);
7e0abcec 303
ceb1497a 304 open(my $fh, '>', $path) or return 0;
7e0abcec
TH
305 print($fh $value);
306 close($fh);
ceb1497a
DA
307
308 return 1;
7e0abcec
TH
309}
310
75cd7583
DA
311sub main
312{
313 # parse command-line options. all unrecognized options and arguments
314 # are passed through to the 'git diff' command.
c9bdd505
DA
315 my %opts = (
316 difftool_cmd => undef,
317 dirdiff => undef,
318 extcmd => undef,
319 gui => undef,
320 help => undef,
321 prompt => undef,
190f5475
DA
322 symlinks => $^O ne 'cygwin' &&
323 $^O ne 'MSWin32' && $^O ne 'msys',
c9bdd505 324 tool_help => undef,
2b52123f 325 trust_exit_code => undef,
c9bdd505
DA
326 );
327 GetOptions('g|gui!' => \$opts{gui},
328 'd|dir-diff' => \$opts{dirdiff},
329 'h' => \$opts{help},
330 'prompt!' => \$opts{prompt},
331 'y' => sub { $opts{prompt} = 0; },
1f229345
DA
332 'symlinks' => \$opts{symlinks},
333 'no-symlinks' => sub { $opts{symlinks} = 0; },
c9bdd505
DA
334 't|tool:s' => \$opts{difftool_cmd},
335 'tool-help' => \$opts{tool_help},
2b52123f
DA
336 'trust-exit-code' => \$opts{trust_exit_code},
337 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; },
c9bdd505
DA
338 'x|extcmd:s' => \$opts{extcmd});
339
340 if (defined($opts{help})) {
75cd7583 341 usage(0);
5c38ea31 342 }
c9bdd505 343 if (defined($opts{tool_help})) {
75cd7583 344 print_tool_help();
3f94ff75 345 }
c9bdd505
DA
346 if (defined($opts{difftool_cmd})) {
347 if (length($opts{difftool_cmd}) > 0) {
348 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
75cd7583 349 } else {
a948ebac 350 print __("No <tool> given for --tool=<tool>\n");
75cd7583
DA
351 usage(1);
352 }
353 }
c9bdd505
DA
354 if (defined($opts{extcmd})) {
355 if (length($opts{extcmd}) > 0) {
356 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
75cd7583 357 } else {
a948ebac 358 print __("No <cmd> given for --extcmd=<cmd>\n");
75cd7583
DA
359 usage(1);
360 }
361 }
c9bdd505
DA
362 if ($opts{gui}) {
363 my $guitool = Git::config('diff.guitool');
af14b5cf 364 if (defined($guitool) && length($guitool) > 0) {
75cd7583
DA
365 $ENV{GIT_DIFF_TOOL} = $guitool;
366 }
367 }
368
2b52123f
DA
369 if (!defined $opts{trust_exit_code}) {
370 $opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode');
371 }
372 if ($opts{trust_exit_code}) {
373 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true';
374 } else {
375 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false';
376 }
377
75cd7583
DA
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.
c9bdd505 382 if (defined($opts{dirdiff})) {
1f229345 383 dir_diff($opts{extcmd}, $opts{symlinks});
75cd7583 384 } else {
c9bdd505 385 file_diff($opts{prompt});
3f94ff75
TH
386 }
387}
7e0abcec 388
75cd7583
DA
389sub dir_diff
390{
1f229345 391 my ($extcmd, $symlinks) = @_;
75cd7583 392 my $rc;
ceb1497a 393 my $error = 0;
75cd7583 394 my $repo = Git->repository();
32b8c581 395 my $repo_path = $repo->repo_path();
ce692697
DA
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);
ceb1497a 399
7e0abcec
TH
400 if (defined($extcmd)) {
401 $rc = system($extcmd, $a, $b);
3f94ff75 402 } else {
7e0abcec
TH
403 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
404 $rc = system('git', 'difftool--helper', $a, $b);
d531174f 405 }
7e0abcec
TH
406 # If the diff including working copy files and those
407 # files were modified during the diff, then the changes
1f229345
DA
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.
67aa147a
JK
411 #
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.
415 my %wt_modified;
416 my %tmp_modified;
417 my $indices_loaded = 0;
418
ce692697 419 for my $file (@files) {
1f229345 420 next if $symlinks && -l "$b/$file";
283abb2c
DA
421 next if ! -f "$b/$file";
422
67aa147a 423 if (!$indices_loaded) {
32b8c581 424 %wt_modified = changed_files(
ce692697 425 $repo_path, "$tmpdir/wtindex", $worktree);
32b8c581
DA
426 %tmp_modified = changed_files(
427 $repo_path, "$tmpdir/wtindex", $b);
67aa147a
JK
428 $indices_loaded = 1;
429 }
430
431 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
a948ebac
VA
432 warn sprintf(__(
433 "warning: Both files modified:\n" .
434 "'%s/%s' and '%s/%s'.\n" .
435 "warning: Working tree file has been left.\n" .
5a5d3f1f 436 "warning:\n"), $worktree, $file, $b, $file);
ceb1497a 437 $error = 1;
67aa147a 438 } elsif (exists $tmp_modified{$file}) {
1f229345 439 my $mode = stat("$b/$file")->mode;
f242a03d 440 copy("$b/$file", $file) or
ceb1497a
DA
441 exit_cleanup($tmpdir, 1);
442
f242a03d 443 chmod($mode, $file) or
ceb1497a 444 exit_cleanup($tmpdir, 1);
05df5326 445 }
7e0abcec 446 }
ceb1497a 447 if ($error) {
a948ebac
VA
448 warn sprintf(__(
449 "warning: Temporary files exist in '%s'.\n" .
450 "warning: You may want to cleanup or recover these.\n"), $tmpdir);
ceb1497a
DA
451 exit(1);
452 } else {
453 exit_cleanup($tmpdir, $rc);
454 }
75cd7583
DA
455}
456
457sub file_diff
458{
459 my ($prompt) = @_;
460
7e0abcec
TH
461 if (defined($prompt)) {
462 if ($prompt) {
463 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
464 } else {
465 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
466 }
467 }
468
469 $ENV{GIT_PAGER} = '';
470 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
471
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));
479}
75cd7583
DA
480
481main();