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