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