blob: 18ca61e8d0493bde9c21ed337043bc72fa5c73a7 [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 Henigan7e0abce2012-04-23 14:23:41 -040040sub 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.
David Aguilar94eaa802014-02-23 19:12:35 -080045 return Git::command_oneline('rev-parse', '--show-toplevel');
Tim Henigan7e0abce2012-04-23 14:23:41 -040046}
47
Tim Heniganbf73fc22012-03-29 09:39:18 -040048sub print_tool_help
49{
John Keepingabaf1752013-01-25 01:43:51 -080050 my $cmd = 'TOOL_MODE=diff';
51 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
52 $cmd .= ' && show_tool_help';
Tim Heniganbf73fc22012-03-29 09:39:18 -040053
John Keepingabaf1752013-01-25 01:43:51 -080054 # See the comment at the bottom of file_diff() for the reason behind
55 # using system() followed by exit() instead of exec().
56 my $rc = system('sh', '-c', $cmd);
57 exit($rc | ($rc >> 8));
Tim Heniganbf73fc22012-03-29 09:39:18 -040058}
59
David Aguilarceb14972012-07-26 12:36:19 -070060sub exit_cleanup
61{
62 my ($tmpdir, $status) = @_;
63 my $errno = $!;
64 rmtree($tmpdir);
65 if ($status and $errno) {
66 my ($package, $file, $line) = caller();
67 warn "$file line $line: $errno\n";
68 }
69 exit($status | ($status >> 8));
70}
71
John Keeping02c56312013-03-14 20:19:41 +000072sub use_wt_file
73{
Kenichi Saita32eaf1d2013-05-30 01:01:23 +090074 my ($repo, $workdir, $file, $sha1) = @_;
John Keeping02c56312013-03-14 20:19:41 +000075 my $null_sha1 = '0' x 40;
76
John Keeping1f197a12013-05-17 19:26:08 +010077 if (! -e "$workdir/$file") {
78 # If the file doesn't exist in the working tree, we cannot
79 # use it.
80 return (0, $null_sha1);
81 }
82
John Keeping02c56312013-03-14 20:19:41 +000083 my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
John Keeping67aa1472013-03-29 22:07:39 +000084 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
85 return ($use, $wt_sha1);
86}
87
88sub changed_files
89{
90 my ($repo_path, $index, $worktree) = @_;
91 $ENV{GIT_INDEX_FILE} = $index;
92 $ENV{GIT_WORK_TREE} = $worktree;
93 my $must_unset_git_dir = 0;
94 if (not defined($ENV{GIT_DIR})) {
95 $must_unset_git_dir = 1;
96 $ENV{GIT_DIR} = $repo_path;
97 }
98
99 my @refreshargs = qw/update-index --really-refresh -q --unmerged/;
100 my @gitargs = qw/diff-files --name-only -z/;
101 try {
102 Git::command_oneline(@refreshargs);
103 } catch Git::Error::Command with {};
104
105 my $line = Git::command_oneline(@gitargs);
106 my @files;
107 if (defined $line) {
108 @files = split('\0', $line);
109 } else {
110 @files = ();
111 }
112
113 delete($ENV{GIT_INDEX_FILE});
114 delete($ENV{GIT_WORK_TREE});
115 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
116
117 return map { $_ => 1 } @files;
John Keeping02c56312013-03-14 20:19:41 +0000118}
119
Tim Henigan7e0abce2012-04-23 14:23:41 -0400120sub setup_dir_diff
121{
David Aguilar1f229342012-07-22 23:05:30 -0700122 my ($repo, $workdir, $symlinks) = @_;
David Aguilar75cd7582012-07-22 20:57:08 -0700123
Tim Henigan7e0abce2012-04-23 14:23:41 -0400124 # Run the diff; exit immediately if no diff found
125 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
126 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
127 # by Git->repository->command*.
David Aguilar75cd7582012-07-22 20:57:08 -0700128 my $repo_path = $repo->repo_path();
David Aguilara4cd5be2012-07-25 23:07:57 -0700129 my %repo_args = (Repository => $repo_path, WorkingCopy => $workdir);
130 my $diffrepo = Git->repository(%repo_args);
131
132 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
133 my $diffrtn = $diffrepo->command_oneline(@gitargs);
Ross Lagerwalled36e5b2012-08-21 12:21:40 +0200134 exit(0) unless defined($diffrtn);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400135
Tim Henigan7e0abce2012-04-23 14:23:41 -0400136 # Build index info for left and right sides of the diff
137 my $submodule_mode = '160000';
138 my $symlink_mode = '120000';
139 my $null_mode = '0' x 6;
140 my $null_sha1 = '0' x 40;
141 my $lindex = '';
142 my $rindex = '';
John Keeping67aa1472013-03-29 22:07:39 +0000143 my $wtindex = '';
Tim Henigan7e0abce2012-04-23 14:23:41 -0400144 my %submodule;
145 my %symlink;
David Aguilar75cd7582012-07-22 20:57:08 -0700146 my @working_tree = ();
Tim Henigan7e0abce2012-04-23 14:23:41 -0400147 my @rawdiff = split('\0', $diffrtn);
148
149 my $i = 0;
150 while ($i < $#rawdiff) {
151 if ($rawdiff[$i] =~ /^::/) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700152 warn << 'EOF';
153Combined diff formats ('-c' and '--cc') are not supported in
154directory diff mode ('-d' and '--dir-diff').
155EOF
Tim Henigan7e0abce2012-04-23 14:23:41 -0400156 exit(1);
157 }
158
David Aguilara4cd5be2012-07-25 23:07:57 -0700159 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
160 split(' ', substr($rawdiff[$i], 1));
Tim Henigan7e0abce2012-04-23 14:23:41 -0400161 my $src_path = $rawdiff[$i + 1];
162 my $dst_path;
163
164 if ($status =~ /^[CR]/) {
165 $dst_path = $rawdiff[$i + 2];
166 $i += 3;
167 } else {
168 $dst_path = $src_path;
169 $i += 2;
170 }
171
David Aguilara4cd5be2012-07-25 23:07:57 -0700172 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400173 $submodule{$src_path}{left} = $lsha1;
174 if ($lsha1 ne $rsha1) {
175 $submodule{$dst_path}{right} = $rsha1;
176 } else {
177 $submodule{$dst_path}{right} = "$rsha1-dirty";
178 }
179 next;
180 }
181
182 if ($lmode eq $symlink_mode) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700183 $symlink{$src_path}{left} =
184 $diffrepo->command_oneline('show', "$lsha1");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400185 }
186
187 if ($rmode eq $symlink_mode) {
David Aguilara4cd5be2012-07-25 23:07:57 -0700188 $symlink{$dst_path}{right} =
189 $diffrepo->command_oneline('show', "$rsha1");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400190 }
191
David Aguilara4cd5be2012-07-25 23:07:57 -0700192 if ($lmode ne $null_mode and $status !~ /^C/) {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400193 $lindex .= "$lmode $lsha1\t$src_path\0";
194 }
195
196 if ($rmode ne $null_mode) {
John Keeping67aa1472013-03-29 22:07:39 +0000197 my ($use, $wt_sha1) = use_wt_file($repo, $workdir,
Kenichi Saita32eaf1d2013-05-30 01:01:23 +0900198 $dst_path, $rsha1);
John Keeping67aa1472013-03-29 22:07:39 +0000199 if ($use) {
200 push @working_tree, $dst_path;
201 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
John Keeping02c56312013-03-14 20:19:41 +0000202 } else {
203 $rindex .= "$rmode $rsha1\t$dst_path\0";
Tim Henigan7e0abce2012-04-23 14:23:41 -0400204 }
205 }
206 }
207
David Aguilarceb14972012-07-26 12:36:19 -0700208 # Setup temp directories
209 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
210 my $ldir = "$tmpdir/left";
211 my $rdir = "$tmpdir/right";
212 mkpath($ldir) or exit_cleanup($tmpdir, 1);
213 mkpath($rdir) or exit_cleanup($tmpdir, 1);
214
Tim Henigan7e0abce2012-04-23 14:23:41 -0400215 # If $GIT_DIR is not set prior to calling 'git update-index' and
216 # 'git checkout-index', then those commands will fail if difftool
217 # is called from a directory other than the repo root.
218 my $must_unset_git_dir = 0;
219 if (not defined($ENV{GIT_DIR})) {
220 $must_unset_git_dir = 1;
221 $ENV{GIT_DIR} = $repo_path;
222 }
223
224 # Populate the left and right directories based on each index file
225 my ($inpipe, $ctx);
226 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
David Aguilara4cd5be2012-07-25 23:07:57 -0700227 ($inpipe, $ctx) =
228 $repo->command_input_pipe(qw(update-index -z --index-info));
Tim Henigan7e0abce2012-04-23 14:23:41 -0400229 print($inpipe $lindex);
230 $repo->command_close_pipe($inpipe, $ctx);
David Aguilarceb14972012-07-26 12:36:19 -0700231
David Aguilar75cd7582012-07-22 20:57:08 -0700232 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
David Aguilarceb14972012-07-26 12:36:19 -0700233 exit_cleanup($tmpdir, $rc) if $rc != 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400234
235 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
David Aguilara4cd5be2012-07-25 23:07:57 -0700236 ($inpipe, $ctx) =
237 $repo->command_input_pipe(qw(update-index -z --index-info));
Tim Henigan7e0abce2012-04-23 14:23:41 -0400238 print($inpipe $rindex);
239 $repo->command_close_pipe($inpipe, $ctx);
David Aguilarceb14972012-07-26 12:36:19 -0700240
Tim Henigan7e0abce2012-04-23 14:23:41 -0400241 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
David Aguilarceb14972012-07-26 12:36:19 -0700242 exit_cleanup($tmpdir, $rc) if $rc != 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400243
John Keeping67aa1472013-03-29 22:07:39 +0000244 $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
245 ($inpipe, $ctx) =
246 $repo->command_input_pipe(qw(update-index --info-only -z --index-info));
247 print($inpipe $wtindex);
248 $repo->command_close_pipe($inpipe, $ctx);
249
Tim Henigan7e0abce2012-04-23 14:23:41 -0400250 # If $GIT_DIR was explicitly set just for the update/checkout
251 # commands, then it should be unset before continuing.
252 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
253 delete($ENV{GIT_INDEX_FILE});
254
255 # Changes in the working tree need special treatment since they are
John Keepinge0976dc2013-03-14 20:19:40 +0000256 # not part of the index. Remove any trailing slash from $workdir
257 # before starting to avoid double slashes in symlink targets.
258 $workdir =~ s|/$||;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400259 for my $file (@working_tree) {
260 my $dir = dirname($file);
261 unless (-d "$rdir/$dir") {
David Aguilarceb14972012-07-26 12:36:19 -0700262 mkpath("$rdir/$dir") or
263 exit_cleanup($tmpdir, 1);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400264 }
David Aguilar1f229342012-07-22 23:05:30 -0700265 if ($symlinks) {
David Aguilarceb14972012-07-26 12:36:19 -0700266 symlink("$workdir/$file", "$rdir/$file") or
267 exit_cleanup($tmpdir, 1);
David Aguilar1f229342012-07-22 23:05:30 -0700268 } else {
David Aguilarceb14972012-07-26 12:36:19 -0700269 copy("$workdir/$file", "$rdir/$file") or
270 exit_cleanup($tmpdir, 1);
271
David Aguilar1f229342012-07-22 23:05:30 -0700272 my $mode = stat("$workdir/$file")->mode;
David Aguilarceb14972012-07-26 12:36:19 -0700273 chmod($mode, "$rdir/$file") or
274 exit_cleanup($tmpdir, 1);
David Aguilar1f229342012-07-22 23:05:30 -0700275 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400276 }
277
278 # Changes to submodules require special treatment. This loop writes a
279 # temporary file to both the left and right directories to show the
280 # change in the recorded SHA1 for the submodule.
281 for my $path (keys %submodule) {
David Aguilarceb14972012-07-26 12:36:19 -0700282 my $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400283 if (defined($submodule{$path}{left})) {
David Aguilarceb14972012-07-26 12:36:19 -0700284 $ok = write_to_file("$ldir/$path",
285 "Subproject commit $submodule{$path}{left}");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400286 }
287 if (defined($submodule{$path}{right})) {
David Aguilarceb14972012-07-26 12:36:19 -0700288 $ok = write_to_file("$rdir/$path",
289 "Subproject commit $submodule{$path}{right}");
Tim Henigan7e0abce2012-04-23 14:23:41 -0400290 }
David Aguilarceb14972012-07-26 12:36:19 -0700291 exit_cleanup($tmpdir, 1) if not $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400292 }
293
294 # Symbolic links require special treatment. The standard "git diff"
295 # shows only the link itself, not the contents of the link target.
296 # This loop replicates that behavior.
297 for my $path (keys %symlink) {
David Aguilarceb14972012-07-26 12:36:19 -0700298 my $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400299 if (defined($symlink{$path}{left})) {
David Aguilarceb14972012-07-26 12:36:19 -0700300 $ok = write_to_file("$ldir/$path",
301 $symlink{$path}{left});
Tim Henigan7e0abce2012-04-23 14:23:41 -0400302 }
303 if (defined($symlink{$path}{right})) {
David Aguilarceb14972012-07-26 12:36:19 -0700304 $ok = write_to_file("$rdir/$path",
305 $symlink{$path}{right});
Tim Henigan7e0abce2012-04-23 14:23:41 -0400306 }
David Aguilarceb14972012-07-26 12:36:19 -0700307 exit_cleanup($tmpdir, 1) if not $ok;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400308 }
309
David Aguilarceb14972012-07-26 12:36:19 -0700310 return ($ldir, $rdir, $tmpdir, @working_tree);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400311}
312
313sub write_to_file
314{
315 my $path = shift;
316 my $value = shift;
317
318 # Make sure the path to the file exists
319 my $dir = dirname($path);
320 unless (-d "$dir") {
David Aguilarceb14972012-07-26 12:36:19 -0700321 mkpath("$dir") or return 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400322 }
323
324 # If the file already exists in that location, delete it. This
325 # is required in the case of symbolic links.
David Aguilarceb14972012-07-26 12:36:19 -0700326 unlink($path);
Tim Henigan7e0abce2012-04-23 14:23:41 -0400327
David Aguilarceb14972012-07-26 12:36:19 -0700328 open(my $fh, '>', $path) or return 0;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400329 print($fh $value);
330 close($fh);
David Aguilarceb14972012-07-26 12:36:19 -0700331
332 return 1;
Tim Henigan7e0abce2012-04-23 14:23:41 -0400333}
334
David Aguilar75cd7582012-07-22 20:57:08 -0700335sub main
336{
337 # parse command-line options. all unrecognized options and arguments
338 # are passed through to the 'git diff' command.
David Aguilarc9bdd502012-07-22 20:57:09 -0700339 my %opts = (
340 difftool_cmd => undef,
341 dirdiff => undef,
342 extcmd => undef,
343 gui => undef,
344 help => undef,
345 prompt => undef,
David Aguilar190f5472012-07-24 20:14:24 -0700346 symlinks => $^O ne 'cygwin' &&
347 $^O ne 'MSWin32' && $^O ne 'msys',
David Aguilarc9bdd502012-07-22 20:57:09 -0700348 tool_help => undef,
349 );
350 GetOptions('g|gui!' => \$opts{gui},
351 'd|dir-diff' => \$opts{dirdiff},
352 'h' => \$opts{help},
353 'prompt!' => \$opts{prompt},
354 'y' => sub { $opts{prompt} = 0; },
David Aguilar1f229342012-07-22 23:05:30 -0700355 'symlinks' => \$opts{symlinks},
356 'no-symlinks' => sub { $opts{symlinks} = 0; },
David Aguilarc9bdd502012-07-22 20:57:09 -0700357 't|tool:s' => \$opts{difftool_cmd},
358 'tool-help' => \$opts{tool_help},
359 'x|extcmd:s' => \$opts{extcmd});
Tim Henigan3f94ff72012-03-22 15:52:16 -0400360
David Aguilarc9bdd502012-07-22 20:57:09 -0700361 if (defined($opts{help})) {
David Aguilar75cd7582012-07-22 20:57:08 -0700362 usage(0);
David Aguilar5c38ea32009-01-16 00:00:02 -0800363 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700364 if (defined($opts{tool_help})) {
David Aguilar75cd7582012-07-22 20:57:08 -0700365 print_tool_help();
Tim Henigan3f94ff72012-03-22 15:52:16 -0400366 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700367 if (defined($opts{difftool_cmd})) {
368 if (length($opts{difftool_cmd}) > 0) {
369 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
David Aguilar75cd7582012-07-22 20:57:08 -0700370 } else {
371 print "No <tool> given for --tool=<tool>\n";
372 usage(1);
373 }
374 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700375 if (defined($opts{extcmd})) {
376 if (length($opts{extcmd}) > 0) {
377 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
David Aguilar75cd7582012-07-22 20:57:08 -0700378 } else {
379 print "No <cmd> given for --extcmd=<cmd>\n";
380 usage(1);
381 }
382 }
David Aguilarc9bdd502012-07-22 20:57:09 -0700383 if ($opts{gui}) {
384 my $guitool = Git::config('diff.guitool');
David Aguilaraf14b5c2013-02-15 21:47:43 -0800385 if (defined($guitool) && length($guitool) > 0) {
David Aguilar75cd7582012-07-22 20:57:08 -0700386 $ENV{GIT_DIFF_TOOL} = $guitool;
387 }
388 }
389
390 # In directory diff mode, 'git-difftool--helper' is called once
391 # to compare the a/b directories. In file diff mode, 'git diff'
392 # will invoke a separate instance of 'git-difftool--helper' for
393 # each file that changed.
David Aguilarc9bdd502012-07-22 20:57:09 -0700394 if (defined($opts{dirdiff})) {
David Aguilar1f229342012-07-22 23:05:30 -0700395 dir_diff($opts{extcmd}, $opts{symlinks});
David Aguilar75cd7582012-07-22 20:57:08 -0700396 } else {
David Aguilarc9bdd502012-07-22 20:57:09 -0700397 file_diff($opts{prompt});
Tim Henigan3f94ff72012-03-22 15:52:16 -0400398 }
399}
Tim Henigan7e0abce2012-04-23 14:23:41 -0400400
David Aguilar75cd7582012-07-22 20:57:08 -0700401sub dir_diff
402{
David Aguilar1f229342012-07-22 23:05:30 -0700403 my ($extcmd, $symlinks) = @_;
David Aguilar75cd7582012-07-22 20:57:08 -0700404 my $rc;
David Aguilarceb14972012-07-26 12:36:19 -0700405 my $error = 0;
David Aguilar75cd7582012-07-22 20:57:08 -0700406 my $repo = Git->repository();
David Aguilar94eaa802014-02-23 19:12:35 -0800407 my $workdir = find_worktree();
David Aguilarceb14972012-07-26 12:36:19 -0700408 my ($a, $b, $tmpdir, @worktree) =
409 setup_dir_diff($repo, $workdir, $symlinks);
410
Tim Henigan7e0abce2012-04-23 14:23:41 -0400411 if (defined($extcmd)) {
412 $rc = system($extcmd, $a, $b);
Tim Henigan3f94ff72012-03-22 15:52:16 -0400413 } else {
Tim Henigan7e0abce2012-04-23 14:23:41 -0400414 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
415 $rc = system('git', 'difftool--helper', $a, $b);
Ramsay Jonesd5311742010-12-14 18:27:48 +0000416 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400417 # If the diff including working copy files and those
418 # files were modified during the diff, then the changes
David Aguilar1f229342012-07-22 23:05:30 -0700419 # should be copied back to the working tree.
420 # Do not copy back files when symlinks are used and the
421 # external tool did not replace the original link with a file.
John Keeping67aa1472013-03-29 22:07:39 +0000422 #
423 # These hashes are loaded lazily since they aren't needed
424 # in the common case of --symlinks and the difftool updating
425 # files through the symlink.
426 my %wt_modified;
427 my %tmp_modified;
428 my $indices_loaded = 0;
429
David Aguilar1f229342012-07-22 23:05:30 -0700430 for my $file (@worktree) {
431 next if $symlinks && -l "$b/$file";
David Aguilar283abb22012-07-24 20:14:23 -0700432 next if ! -f "$b/$file";
433
John Keeping67aa1472013-03-29 22:07:39 +0000434 if (!$indices_loaded) {
435 %wt_modified = changed_files($repo->repo_path(),
436 "$tmpdir/wtindex", "$workdir");
437 %tmp_modified = changed_files($repo->repo_path(),
438 "$tmpdir/wtindex", "$b");
439 $indices_loaded = 1;
440 }
441
442 if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
443 my $errmsg = "warning: Both files modified: ";
444 $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
445 $errmsg .= "warning: Working tree file has been left.\n";
446 $errmsg .= "warning:\n";
David Aguilar283abb22012-07-24 20:14:23 -0700447 warn $errmsg;
David Aguilarceb14972012-07-26 12:36:19 -0700448 $error = 1;
John Keeping67aa1472013-03-29 22:07:39 +0000449 } elsif (exists $tmp_modified{$file}) {
David Aguilar1f229342012-07-22 23:05:30 -0700450 my $mode = stat("$b/$file")->mode;
David Aguilarceb14972012-07-26 12:36:19 -0700451 copy("$b/$file", "$workdir/$file") or
452 exit_cleanup($tmpdir, 1);
453
454 chmod($mode, "$workdir/$file") or
455 exit_cleanup($tmpdir, 1);
Tim Henigan05df5322012-07-19 01:27:09 -0700456 }
Tim Henigan7e0abce2012-04-23 14:23:41 -0400457 }
David Aguilarceb14972012-07-26 12:36:19 -0700458 if ($error) {
459 warn "warning: Temporary files exist in '$tmpdir'.\n";
460 warn "warning: You may want to cleanup or recover these.\n";
461 exit(1);
462 } else {
463 exit_cleanup($tmpdir, $rc);
464 }
David Aguilar75cd7582012-07-22 20:57:08 -0700465}
466
467sub file_diff
468{
469 my ($prompt) = @_;
470
Tim Henigan7e0abce2012-04-23 14:23:41 -0400471 if (defined($prompt)) {
472 if ($prompt) {
473 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
474 } else {
475 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
476 }
477 }
478
479 $ENV{GIT_PAGER} = '';
480 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
481
482 # ActiveState Perl for Win32 does not implement POSIX semantics of
483 # exec* system call. It just spawns the given executable and finishes
484 # the starting program, exiting with code 0.
485 # system will at least catch the errors returned by git diff,
486 # allowing the caller of git difftool better handling of failures.
487 my $rc = system('git', 'diff', @ARGV);
488 exit($rc | ($rc >> 8));
David Aguilar5c38ea32009-01-16 00:00:02 -0800489}
David Aguilar75cd7582012-07-22 20:57:08 -0700490
491main();