blob: a5790d03a075884ffe93dbff782b28ec87263051 [file] [log] [blame]
Tim Henigan7e0abce2012-04-23 14:23:41 -04001#!/usr/bin/perl
David Aguilarf47f1e22010-01-15 14:03:43 -08002# Copyright (c) 2009, 2010 David Aguilar
Tim Henigan7e0abce2012-04-23 14:23:41 -04003# Copyright (c) 2012 Tim Henigan
David Aguilar5c38ea32009-01-16 00:00:02 -08004#
5# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
David Aguilara9043922009-04-07 01:21:22 -07006# git-difftool--helper script.
7#
8# This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
Tim Henigan7e0abce2012-04-23 14:23:41 -04009# The GIT_DIFF* variables are exported for use by git-difftool--helper.
David Aguilara9043922009-04-07 01:21:22 -070010#
David Aguilar5c38ea32009-01-16 00:00:02 -080011# Any arguments that are unknown to this script are forwarded to 'git diff'.
12
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +000013use 5.008;
David Aguilar5c38ea32009-01-16 00:00:02 -080014use strict;
15use warnings;
John Keeping67aa1472013-03-29 22:07:39 +000016use Error qw(:try);
David Aguilar7c7584b2012-07-24 20:14:22 -070017use File::Basename qw(dirname);
Tim Henigan7e0abce2012-04-23 14:23:41 -040018use File::Copy;
David Aguilar7c7584b2012-07-24 20:14:22 -070019use File::Find;
Tim Henigan7e0abce2012-04-23 14:23:41 -040020use File::stat;
David Aguilarceb14972012-07-26 12:36:19 -070021use File::Path qw(mkpath rmtree);
Tim Henigan7e0abce2012-04-23 14:23:41 -040022use File::Temp qw(tempdir);
Tim Henigan3f94ff72012-03-22 15:52:16 -040023use Getopt::Long qw(:config pass_through);
24use Git;
David Aguilar5c38ea32009-01-16 00:00:02 -080025
26sub usage
27{
Tim Henigan28360762012-03-22 15:52:18 -040028 my $exitcode = shift;
David Aguilar5c38ea32009-01-16 00:00:02 -080029 print << 'USAGE';
Tim Heniganbf73fc22012-03-29 09:39:18 -040030usage: git difftool [-t|--tool=<tool>] [--tool-help]
Tim Henigan85089602012-03-22 15:52:17 -040031 [-x|--extcmd=<cmd>]
32 [-g|--gui] [--no-gui]
Tim Henigan3f94ff72012-03-22 15:52:16 -040033 [--prompt] [-y|--no-prompt]
Tim Henigan7e0abce2012-04-23 14:23:41 -040034 [-d|--dir-diff]
David Aguilarf47f1e22010-01-15 14:03:43 -080035 ['git diff' options]
David Aguilar5c38ea32009-01-16 00:00:02 -080036USAGE
Tim Henigan28360762012-03-22 15:52:18 -040037 exit($exitcode);
David Aguilar5c38ea32009-01-16 00:00:02 -080038}
39
Tim Heniganbf73fc22012-03-29 09:39:18 -040040sub print_tool_help
41{
John Keepingabaf1752013-01-25 01:43:51 -080042 # See the comment at the bottom of file_diff() for the reason behind
43 # using system() followed by exit() instead of exec().
Charles Bailey4fb4b022014-10-11 01:39:38 -070044 my $rc = system(qw(git mergetool --tool-help=diff));
John Keepingabaf1752013-01-25 01:43:51 -080045 exit($rc | ($rc >> 8));
Tim Heniganbf73fc22012-03-29 09:39:18 -040046}
47
David Aguilarceb14972012-07-26 12:36:19 -070048sub 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
John Keeping02c56312013-03-14 20:19:41 +000060sub use_wt_file
61{
David Aguilar32b8c582016-07-18 20:57:56 -070062 my ($workdir, $file, $sha1) = @_;
John Keeping02c56312013-03-14 20:19:41 +000063 my $null_sha1 = '0' x 40;
64
David Aguilarcfe2d4b2015-10-29 11:19:01 -070065 if (-l "$workdir/$file" || ! -e _) {
John Keeping1f197a12013-05-17 19:26:08 +010066 return (0, $null_sha1);
67 }
68
David Aguilar32b8c582016-07-18 20:57:56 -070069 my $wt_sha1 = Git::command_oneline('hash-object', "$workdir/$file");
John Keeping67aa1472013-03-29 22:07:39 +000070 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;
John Keeping67aa1472013-03-29 22:07:39 +000078
David Aguilar98f917e2016-07-18 20:57:55 -070079 my @gitargs = ('--git-dir', $repo_path, '--work-tree', $worktree);
80 my @refreshargs = (
81 @gitargs, 'update-index',
82 '--really-refresh', '-q', '--unmerged');
John Keeping67aa1472013-03-29 22:07:39 +000083 try {
84 Git::command_oneline(@refreshargs);
85 } catch Git::Error::Command with {};
86
David Aguilar98f917e2016-07-18 20:57:55 -070087 my @diffargs = (@gitargs, 'diff-files', '--name-only', '-z');
88 my $line = Git::command_oneline(@diffargs);
John Keeping67aa1472013-03-29 22:07:39 +000089 my @files;
90 if (defined $line) {
91 @files = split('\0', $line);
92 } else {
93 @files = ();
94 }
95
96 delete($ENV{GIT_INDEX_FILE});
John Keeping67aa1472013-03-29 22:07:39 +000097
98 return map { $_ => 1 } @files;
John Keeping02c56312013-03-14 20:19:41 +000099}
100
Tim Henigan7e0abce2012-04-23 14:23:41 -0400101sub setup_dir_diff
102{
David Aguilar32b8c582016-07-18 20:57:56 -0700103 my ($workdir, $symlinks) = @_;
David Aguilara4cd5be2012-07-25 23:07:57 -0700104 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
David Aguilar32b8c582016-07-18 20:57:56 -0700105 my $diffrtn = Git::command_oneline(@gitargs);
Ross Lagerwalled36e5b2012-08-21 12:21:40 +0200106 exit(0) unless defined($diffrtn);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400107
Tim Henigan7e0abce2012-04-23 14:23:41 -0400108 # Build index info for left and right sides of the diff
109 my $submodule_mode = '160000';
110 my $symlink_mode = '120000';
111 my $null_mode = '0' x 6;
112 my $null_sha1 = '0' x 40;
113 my $lindex = '';
114 my $rindex = '';
John Keeping67aa1472013-03-29 22:07:39 +0000115 my $wtindex = '';
Tim Henigan7e0abce2012-04-23 14:23:41 -0400116 my %submodule;
117 my %symlink;
David Aguilar75cd7582012-07-22 20:57:08 -0700118 my @working_tree = ();
David Aguilar366f9ce2016-05-16 11:05:37 -0700119 my %working_tree_dups = ();
Tim Henigan7e0abce2012-04-23 14:23:41 -0400120 my @rawdiff = split('\0', $diffrtn);
121
122 my $i = 0;
123 while ($i < $#rawdiff) {
124 if ($rawdiff[$i] =~ /^::/) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700125 warn << 'EOF';
126Combined diff formats ('-c' and '--cc') are not supported in
127directory diff mode ('-d' and '--dir-diff').
128EOF
Tim Henigan7e0abce2012-04-23 14:23:41 -0400129 exit(1);
130 }
131
David Aguilara4cd5be2012-07-25 23:07:57 -0700132 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
133 split(' ', substr($rawdiff[$i], 1));
Tim Henigan7e0abce2012-04-23 14:23:41 -0400134 my $src_path = $rawdiff[$i + 1];
135 my $dst_path;
136
137 if ($status =~ /^[CR]/) {
138 $dst_path = $rawdiff[$i + 2];
139 $i += 3;
140 } else {
141 $dst_path = $src_path;
142 $i += 2;
143 }
144
David Aguilara4cd5be2012-07-25 23:07:57 -0700145 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400146 $submodule{$src_path}{left} = $lsha1;
147 if ($lsha1 ne $rsha1) {
148 $submodule{$dst_path}{right} = $rsha1;
149 } else {
150 $submodule{$dst_path}{right} = "$rsha1-dirty";
151 }
152 next;
153 }
154
155 if ($lmode eq $symlink_mode) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700156 $symlink{$src_path}{left} =
David Aguilar32b8c582016-07-18 20:57:56 -0700157 Git::command_oneline('show', $lsha1);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400158 }
159
160 if ($rmode eq $symlink_mode) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700161 $symlink{$dst_path}{right} =
David Aguilar32b8c582016-07-18 20:57:56 -0700162 Git::command_oneline('show', $rsha1);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400163 }
164
David Aguilara4cd5be2012-07-25 23:07:57 -0700165 if ($lmode ne $null_mode and $status !~ /^C/) {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400166 $lindex .= "$lmode $lsha1\t$src_path\0";
167 }
168
169 if ($rmode ne $null_mode) {
David Aguilar366f9ce2016-05-16 11:05:37 -0700170 # Avoid duplicate working_tree entries
171 if ($working_tree_dups{$dst_path}++) {
172 next;
173 }
David Aguilar32b8c582016-07-18 20:57:56 -0700174 my ($use, $wt_sha1) =
175 use_wt_file($workdir, $dst_path, $rsha1);
John Keeping67aa1472013-03-29 22:07:39 +0000176 if ($use) {
177 push @working_tree, $dst_path;
178 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
John Keeping02c56312013-03-14 20:19:41 +0000179 } else {
180 $rindex .= "$rmode $rsha1\t$dst_path\0";
Tim Henigan7e0abce2012-04-23 14:23:41 -0400181 }
182 }
183 }
184
David Aguilarceb14972012-07-26 12:36:19 -0700185 # Setup temp directories
186 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
187 my $ldir = "$tmpdir/left";
188 my $rdir = "$tmpdir/right";
189 mkpath($ldir) or exit_cleanup($tmpdir, 1);
190 mkpath($rdir) or exit_cleanup($tmpdir, 1);
191
Tim Henigan7e0abce2012-04-23 14:23:41 -0400192 # Populate the left and right directories based on each index file
193 my ($inpipe, $ctx);
194 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
David Aguilara4cd5be2012-07-25 23:07:57 -0700195 ($inpipe, $ctx) =
David Aguilar32b8c582016-07-18 20:57:56 -0700196 Git::command_input_pipe('update-index', '-z', '--index-info');
Tim Henigan7e0abce2012-04-23 14:23:41 -0400197 print($inpipe $lindex);
David Aguilar32b8c582016-07-18 20:57:56 -0700198 Git::command_close_pipe($inpipe, $ctx);
David Aguilarceb14972012-07-26 12:36:19 -0700199
David Aguilar75cd7582012-07-22 20:57:08 -0700200 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
David Aguilarceb14972012-07-26 12:36:19 -0700201 exit_cleanup($tmpdir, $rc) if $rc != 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400202
203 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
David Aguilara4cd5be2012-07-25 23:07:57 -0700204 ($inpipe, $ctx) =
David Aguilar32b8c582016-07-18 20:57:56 -0700205 Git::command_input_pipe('update-index', '-z', '--index-info');
Tim Henigan7e0abce2012-04-23 14:23:41 -0400206 print($inpipe $rindex);
David Aguilar32b8c582016-07-18 20:57:56 -0700207 Git::command_close_pipe($inpipe, $ctx);
David Aguilarceb14972012-07-26 12:36:19 -0700208
Tim Henigan7e0abce2012-04-23 14:23:41 -0400209 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
David Aguilarceb14972012-07-26 12:36:19 -0700210 exit_cleanup($tmpdir, $rc) if $rc != 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400211
John Keeping67aa1472013-03-29 22:07:39 +0000212 $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
213 ($inpipe, $ctx) =
David Aguilar32b8c582016-07-18 20:57:56 -0700214 Git::command_input_pipe('update-index', '--info-only', '-z', '--index-info');
John Keeping67aa1472013-03-29 22:07:39 +0000215 print($inpipe $wtindex);
David Aguilar32b8c582016-07-18 20:57:56 -0700216 Git::command_close_pipe($inpipe, $ctx);
John Keeping67aa1472013-03-29 22:07:39 +0000217
Tim Henigan7e0abce2012-04-23 14:23:41 -0400218 # If $GIT_DIR was explicitly set just for the update/checkout
219 # commands, then it should be unset before continuing.
Tim Henigan7e0abce2012-04-23 14:23:41 -0400220 delete($ENV{GIT_INDEX_FILE});
221
222 # Changes in the working tree need special treatment since they are
John Keepinge0976dc2013-03-14 20:19:40 +0000223 # not part of the index. Remove any trailing slash from $workdir
224 # before starting to avoid double slashes in symlink targets.
225 $workdir =~ s|/$||;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400226 for my $file (@working_tree) {
227 my $dir = dirname($file);
228 unless (-d "$rdir/$dir") {
David Aguilarceb14972012-07-26 12:36:19 -0700229 mkpath("$rdir/$dir") or
230 exit_cleanup($tmpdir, 1);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400231 }
David Aguilar1f229342012-07-22 23:05:30 -0700232 if ($symlinks) {
David Aguilarceb14972012-07-26 12:36:19 -0700233 symlink("$workdir/$file", "$rdir/$file") or
234 exit_cleanup($tmpdir, 1);
David Aguilar1f229342012-07-22 23:05:30 -0700235 } else {
David Aguilarceb14972012-07-26 12:36:19 -0700236 copy("$workdir/$file", "$rdir/$file") or
237 exit_cleanup($tmpdir, 1);
238
David Aguilar1f229342012-07-22 23:05:30 -0700239 my $mode = stat("$workdir/$file")->mode;
David Aguilarceb14972012-07-26 12:36:19 -0700240 chmod($mode, "$rdir/$file") or
241 exit_cleanup($tmpdir, 1);
David Aguilar1f229342012-07-22 23:05:30 -0700242 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400243 }
244
245 # Changes to submodules require special treatment. This loop writes a
246 # temporary file to both the left and right directories to show the
247 # change in the recorded SHA1 for the submodule.
248 for my $path (keys %submodule) {
David Aguilar951b5512016-05-16 11:05:36 -0700249 my $ok = 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400250 if (defined($submodule{$path}{left})) {
David Aguilarceb14972012-07-26 12:36:19 -0700251 $ok = write_to_file("$ldir/$path",
252 "Subproject commit $submodule{$path}{left}");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400253 }
254 if (defined($submodule{$path}{right})) {
David Aguilarceb14972012-07-26 12:36:19 -0700255 $ok = write_to_file("$rdir/$path",
256 "Subproject commit $submodule{$path}{right}");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400257 }
David Aguilarceb14972012-07-26 12:36:19 -0700258 exit_cleanup($tmpdir, 1) if not $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400259 }
260
261 # Symbolic links require special treatment. The standard "git diff"
262 # shows only the link itself, not the contents of the link target.
263 # This loop replicates that behavior.
264 for my $path (keys %symlink) {
David Aguilar951b5512016-05-16 11:05:36 -0700265 my $ok = 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400266 if (defined($symlink{$path}{left})) {
David Aguilarceb14972012-07-26 12:36:19 -0700267 $ok = write_to_file("$ldir/$path",
268 $symlink{$path}{left});
Tim Henigan7e0abce2012-04-23 14:23:41 -0400269 }
270 if (defined($symlink{$path}{right})) {
David Aguilarceb14972012-07-26 12:36:19 -0700271 $ok = write_to_file("$rdir/$path",
272 $symlink{$path}{right});
Tim Henigan7e0abce2012-04-23 14:23:41 -0400273 }
David Aguilarceb14972012-07-26 12:36:19 -0700274 exit_cleanup($tmpdir, 1) if not $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400275 }
276
David Aguilarceb14972012-07-26 12:36:19 -0700277 return ($ldir, $rdir, $tmpdir, @working_tree);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400278}
279
280sub write_to_file
281{
282 my $path = shift;
283 my $value = shift;
284
285 # Make sure the path to the file exists
286 my $dir = dirname($path);
287 unless (-d "$dir") {
David Aguilarceb14972012-07-26 12:36:19 -0700288 mkpath("$dir") or return 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400289 }
290
291 # If the file already exists in that location, delete it. This
292 # is required in the case of symbolic links.
David Aguilarceb14972012-07-26 12:36:19 -0700293 unlink($path);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400294
David Aguilarceb14972012-07-26 12:36:19 -0700295 open(my $fh, '>', $path) or return 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400296 print($fh $value);
297 close($fh);
David Aguilarceb14972012-07-26 12:36:19 -0700298
299 return 1;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400300}
301
David Aguilar75cd7582012-07-22 20:57:08 -0700302sub main
303{
304 # parse command-line options. all unrecognized options and arguments
305 # are passed through to the 'git diff' command.
David Aguilarc9bdd502012-07-22 20:57:09 -0700306 my %opts = (
307 difftool_cmd => undef,
308 dirdiff => undef,
309 extcmd => undef,
310 gui => undef,
311 help => undef,
312 prompt => undef,
David Aguilar190f5472012-07-24 20:14:24 -0700313 symlinks => $^O ne 'cygwin' &&
314 $^O ne 'MSWin32' && $^O ne 'msys',
David Aguilarc9bdd502012-07-22 20:57:09 -0700315 tool_help => undef,
David Aguilar2b521232014-10-26 18:15:42 -0700316 trust_exit_code => undef,
David Aguilarc9bdd502012-07-22 20:57:09 -0700317 );
318 GetOptions('g|gui!' => \$opts{gui},
319 'd|dir-diff' => \$opts{dirdiff},
320 'h' => \$opts{help},
321 'prompt!' => \$opts{prompt},
322 'y' => sub { $opts{prompt} = 0; },
David Aguilar1f229342012-07-22 23:05:30 -0700323 'symlinks' => \$opts{symlinks},
324 'no-symlinks' => sub { $opts{symlinks} = 0; },
David Aguilarc9bdd502012-07-22 20:57:09 -0700325 't|tool:s' => \$opts{difftool_cmd},
326 'tool-help' => \$opts{tool_help},
David Aguilar2b521232014-10-26 18:15:42 -0700327 'trust-exit-code' => \$opts{trust_exit_code},
328 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; },
David Aguilarc9bdd502012-07-22 20:57:09 -0700329 'x|extcmd:s' => \$opts{extcmd});
Tim Henigan3f94ff72012-03-22 15:52:16 -0400330
David Aguilarc9bdd502012-07-22 20:57:09 -0700331 if (defined($opts{help})) {
David Aguilar75cd7582012-07-22 20:57:08 -0700332 usage(0);
David Aguilar5c38ea32009-01-16 00:00:02 -0800333 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700334 if (defined($opts{tool_help})) {
David Aguilar75cd7582012-07-22 20:57:08 -0700335 print_tool_help();
Tim Henigan3f94ff72012-03-22 15:52:16 -0400336 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700337 if (defined($opts{difftool_cmd})) {
338 if (length($opts{difftool_cmd}) > 0) {
339 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
David Aguilar75cd7582012-07-22 20:57:08 -0700340 } else {
341 print "No <tool> given for --tool=<tool>\n";
342 usage(1);
343 }
344 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700345 if (defined($opts{extcmd})) {
346 if (length($opts{extcmd}) > 0) {
347 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
David Aguilar75cd7582012-07-22 20:57:08 -0700348 } else {
349 print "No <cmd> given for --extcmd=<cmd>\n";
350 usage(1);
351 }
352 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700353 if ($opts{gui}) {
354 my $guitool = Git::config('diff.guitool');
David Aguilaraf14b5c2013-02-15 21:47:43 -0800355 if (defined($guitool) && length($guitool) > 0) {
David Aguilar75cd7582012-07-22 20:57:08 -0700356 $ENV{GIT_DIFF_TOOL} = $guitool;
357 }
358 }
359
David Aguilar2b521232014-10-26 18:15:42 -0700360 if (!defined $opts{trust_exit_code}) {
361 $opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode');
362 }
363 if ($opts{trust_exit_code}) {
364 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true';
365 } else {
366 $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false';
367 }
368
David Aguilar75cd7582012-07-22 20:57:08 -0700369 # In directory diff mode, 'git-difftool--helper' is called once
370 # to compare the a/b directories. In file diff mode, 'git diff'
371 # will invoke a separate instance of 'git-difftool--helper' for
372 # each file that changed.
David Aguilarc9bdd502012-07-22 20:57:09 -0700373 if (defined($opts{dirdiff})) {
David Aguilar1f229342012-07-22 23:05:30 -0700374 dir_diff($opts{extcmd}, $opts{symlinks});
David Aguilar75cd7582012-07-22 20:57:08 -0700375 } else {
David Aguilarc9bdd502012-07-22 20:57:09 -0700376 file_diff($opts{prompt});
Tim Henigan3f94ff72012-03-22 15:52:16 -0400377 }
378}
Tim Henigan7e0abce2012-04-23 14:23:41 -0400379
David Aguilar75cd7582012-07-22 20:57:08 -0700380sub dir_diff
381{
David Aguilar1f229342012-07-22 23:05:30 -0700382 my ($extcmd, $symlinks) = @_;
David Aguilar75cd7582012-07-22 20:57:08 -0700383 my $rc;
David Aguilarceb14972012-07-26 12:36:19 -0700384 my $error = 0;
David Aguilar75cd7582012-07-22 20:57:08 -0700385 my $repo = Git->repository();
David Aguilar32b8c582016-07-18 20:57:56 -0700386 my $repo_path = $repo->repo_path();
387 my $workdir = $repo->wc_path();
388 my ($a, $b, $tmpdir, @worktree) = setup_dir_diff($workdir, $symlinks);
David Aguilarceb14972012-07-26 12:36:19 -0700389
Tim Henigan7e0abce2012-04-23 14:23:41 -0400390 if (defined($extcmd)) {
391 $rc = system($extcmd, $a, $b);
Tim Henigan3f94ff72012-03-22 15:52:16 -0400392 } else {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400393 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
394 $rc = system('git', 'difftool--helper', $a, $b);
Ramsay Jonesd5311742010-12-14 18:27:48 +0000395 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400396 # If the diff including working copy files and those
397 # files were modified during the diff, then the changes
David Aguilar1f229342012-07-22 23:05:30 -0700398 # should be copied back to the working tree.
399 # Do not copy back files when symlinks are used and the
400 # external tool did not replace the original link with a file.
John Keeping67aa1472013-03-29 22:07:39 +0000401 #
402 # These hashes are loaded lazily since they aren't needed
403 # in the common case of --symlinks and the difftool updating
404 # files through the symlink.
405 my %wt_modified;
406 my %tmp_modified;
407 my $indices_loaded = 0;
408
David Aguilar1f229342012-07-22 23:05:30 -0700409 for my $file (@worktree) {
410 next if $symlinks && -l "$b/$file";
David Aguilar283abb22012-07-24 20:14:23 -0700411 next if ! -f "$b/$file";
412
John Keeping67aa1472013-03-29 22:07:39 +0000413 if (!$indices_loaded) {
David Aguilar32b8c582016-07-18 20:57:56 -0700414 %wt_modified = changed_files(
415 $repo_path, "$tmpdir/wtindex", $workdir);
416 %tmp_modified = changed_files(
417 $repo_path, "$tmpdir/wtindex", $b);
John Keeping67aa1472013-03-29 22:07:39 +0000418 $indices_loaded = 1;
419 }
420
421 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
422 my $errmsg = "warning: Both files modified: ";
423 $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
424 $errmsg .= "warning: Working tree file has been left.\n";
425 $errmsg .= "warning:\n";
David Aguilar283abb22012-07-24 20:14:23 -0700426 warn $errmsg;
David Aguilarceb14972012-07-26 12:36:19 -0700427 $error = 1;
John Keeping67aa1472013-03-29 22:07:39 +0000428 } elsif (exists $tmp_modified{$file}) {
David Aguilar1f229342012-07-22 23:05:30 -0700429 my $mode = stat("$b/$file")->mode;
David Aguilarceb14972012-07-26 12:36:19 -0700430 copy("$b/$file", "$workdir/$file") or
431 exit_cleanup($tmpdir, 1);
432
433 chmod($mode, "$workdir/$file") or
434 exit_cleanup($tmpdir, 1);
Tim Henigan05df5322012-07-19 01:27:09 -0700435 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400436 }
David Aguilarceb14972012-07-26 12:36:19 -0700437 if ($error) {
438 warn "warning: Temporary files exist in '$tmpdir'.\n";
439 warn "warning: You may want to cleanup or recover these.\n";
440 exit(1);
441 } else {
442 exit_cleanup($tmpdir, $rc);
443 }
David Aguilar75cd7582012-07-22 20:57:08 -0700444}
445
446sub file_diff
447{
448 my ($prompt) = @_;
449
Tim Henigan7e0abce2012-04-23 14:23:41 -0400450 if (defined($prompt)) {
451 if ($prompt) {
452 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
453 } else {
454 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
455 }
456 }
457
458 $ENV{GIT_PAGER} = '';
459 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
460
461 # ActiveState Perl for Win32 does not implement POSIX semantics of
462 # exec* system call. It just spawns the given executable and finishes
463 # the starting program, exiting with code 0.
464 # system will at least catch the errors returned by git diff,
465 # allowing the caller of git difftool better handling of failures.
466 my $rc = system('git', 'diff', @ARGV);
467 exit($rc | ($rc >> 8));
David Aguilar5c38ea32009-01-16 00:00:02 -0800468}
David Aguilar75cd7582012-07-22 20:57:08 -0700469
470main();