]>
Commit | Line | Data |
---|---|---|
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 | 13 | use 5.008; |
5c38ea31 DA |
14 | use strict; |
15 | use warnings; | |
67aa147a | 16 | use Error qw(:try); |
7c7584b9 | 17 | use File::Basename qw(dirname); |
7e0abcec | 18 | use File::Copy; |
7c7584b9 | 19 | use File::Find; |
7e0abcec | 20 | use File::stat; |
ceb1497a | 21 | use File::Path qw(mkpath rmtree); |
7e0abcec | 22 | use File::Temp qw(tempdir); |
3f94ff75 TH |
23 | use Getopt::Long qw(:config pass_through); |
24 | use Git; | |
5c38ea31 DA |
25 | |
26 | sub usage | |
27 | { | |
28360769 | 28 | my $exitcode = shift; |
5c38ea31 | 29 | print << 'USAGE'; |
bf73fc21 | 30 | usage: 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 | 36 | USAGE |
28360769 | 37 | exit($exitcode); |
5c38ea31 DA |
38 | } |
39 | ||
bf73fc21 TH |
40 | sub 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 |
48 | sub 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 |
60 | sub use_wt_file |
61 | { | |
32b8c581 | 62 | my ($workdir, $file, $sha1) = @_; |
02c56314 JK |
63 | my $null_sha1 = '0' x 40; |
64 | ||
cfe2d4be | 65 | if (-l "$workdir/$file" || ! -e _) { |
1f197a1d JK |
66 | return (0, $null_sha1); |
67 | } | |
68 | ||
32b8c581 | 69 | my $wt_sha1 = Git::command_oneline('hash-object', "$workdir/$file"); |
67aa147a JK |
70 | my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1); |
71 | return ($use, $wt_sha1); | |
72 | } | |
73 | ||
74 | sub 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 |
101 | sub setup_dir_diff |
102 | { | |
32b8c581 | 103 | my ($workdir, $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 | |
7e0abcec TH |
108 | # 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 = ''; | |
67aa147a | 115 | my $wtindex = ''; |
7e0abcec TH |
116 | my %submodule; |
117 | my %symlink; | |
75cd7583 | 118 | my @working_tree = (); |
366f9cea | 119 | my %working_tree_dups = (); |
7e0abcec TH |
120 | my @rawdiff = split('\0', $diffrtn); |
121 | ||
122 | my $i = 0; | |
123 | while ($i < $#rawdiff) { | |
124 | if ($rawdiff[$i] =~ /^::/) { | |
a4cd5be3 DA |
125 | warn << 'EOF'; |
126 | Combined diff formats ('-c' and '--cc') are not supported in | |
127 | directory diff mode ('-d' and '--dir-diff'). | |
128 | EOF | |
7e0abcec TH |
129 | exit(1); |
130 | } | |
131 | ||
a4cd5be3 DA |
132 | my ($lmode, $rmode, $lsha1, $rsha1, $status) = |
133 | split(' ', substr($rawdiff[$i], 1)); | |
7e0abcec TH |
134 | 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 | ||
a4cd5be3 | 145 | if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) { |
7e0abcec TH |
146 | $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) { | |
a4cd5be3 | 156 | $symlink{$src_path}{left} = |
32b8c581 | 157 | Git::command_oneline('show', $lsha1); |
7e0abcec TH |
158 | } |
159 | ||
160 | if ($rmode eq $symlink_mode) { | |
a4cd5be3 | 161 | $symlink{$dst_path}{right} = |
32b8c581 | 162 | Git::command_oneline('show', $rsha1); |
7e0abcec TH |
163 | } |
164 | ||
a4cd5be3 | 165 | if ($lmode ne $null_mode and $status !~ /^C/) { |
7e0abcec TH |
166 | $lindex .= "$lmode $lsha1\t$src_path\0"; |
167 | } | |
168 | ||
169 | if ($rmode ne $null_mode) { | |
366f9cea DA |
170 | # Avoid duplicate working_tree entries |
171 | if ($working_tree_dups{$dst_path}++) { | |
172 | next; | |
173 | } | |
32b8c581 DA |
174 | my ($use, $wt_sha1) = |
175 | use_wt_file($workdir, $dst_path, $rsha1); | |
67aa147a JK |
176 | if ($use) { |
177 | push @working_tree, $dst_path; | |
178 | $wtindex .= "$rmode $wt_sha1\t$dst_path\0"; | |
02c56314 JK |
179 | } else { |
180 | $rindex .= "$rmode $rsha1\t$dst_path\0"; | |
7e0abcec TH |
181 | } |
182 | } | |
183 | } | |
184 | ||
ceb1497a DA |
185 | # 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 | ||
7e0abcec TH |
192 | # Populate the left and right directories based on each index file |
193 | my ($inpipe, $ctx); | |
194 | $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex"; | |
a4cd5be3 | 195 | ($inpipe, $ctx) = |
32b8c581 | 196 | Git::command_input_pipe('update-index', '-z', '--index-info'); |
7e0abcec | 197 | print($inpipe $lindex); |
32b8c581 | 198 | Git::command_close_pipe($inpipe, $ctx); |
ceb1497a | 199 | |
75cd7583 | 200 | my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/"); |
ceb1497a | 201 | exit_cleanup($tmpdir, $rc) if $rc != 0; |
7e0abcec TH |
202 | |
203 | $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex"; | |
a4cd5be3 | 204 | ($inpipe, $ctx) = |
32b8c581 | 205 | Git::command_input_pipe('update-index', '-z', '--index-info'); |
7e0abcec | 206 | print($inpipe $rindex); |
32b8c581 | 207 | Git::command_close_pipe($inpipe, $ctx); |
ceb1497a | 208 | |
7e0abcec | 209 | $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/"); |
ceb1497a | 210 | exit_cleanup($tmpdir, $rc) if $rc != 0; |
7e0abcec | 211 | |
67aa147a JK |
212 | $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex"; |
213 | ($inpipe, $ctx) = | |
32b8c581 | 214 | Git::command_input_pipe('update-index', '--info-only', '-z', '--index-info'); |
67aa147a | 215 | print($inpipe $wtindex); |
32b8c581 | 216 | Git::command_close_pipe($inpipe, $ctx); |
67aa147a | 217 | |
7e0abcec TH |
218 | # If $GIT_DIR was explicitly set just for the update/checkout |
219 | # commands, then it should be unset before continuing. | |
7e0abcec TH |
220 | delete($ENV{GIT_INDEX_FILE}); |
221 | ||
222 | # Changes in the working tree need special treatment since they are | |
e0976dcf JK |
223 | # not part of the index. Remove any trailing slash from $workdir |
224 | # before starting to avoid double slashes in symlink targets. | |
225 | $workdir =~ s|/$||; | |
7e0abcec TH |
226 | for my $file (@working_tree) { |
227 | my $dir = dirname($file); | |
228 | unless (-d "$rdir/$dir") { | |
ceb1497a DA |
229 | mkpath("$rdir/$dir") or |
230 | exit_cleanup($tmpdir, 1); | |
7e0abcec | 231 | } |
1f229345 | 232 | if ($symlinks) { |
ceb1497a DA |
233 | symlink("$workdir/$file", "$rdir/$file") or |
234 | exit_cleanup($tmpdir, 1); | |
1f229345 | 235 | } else { |
ceb1497a DA |
236 | copy("$workdir/$file", "$rdir/$file") or |
237 | exit_cleanup($tmpdir, 1); | |
238 | ||
1f229345 | 239 | my $mode = stat("$workdir/$file")->mode; |
ceb1497a DA |
240 | chmod($mode, "$rdir/$file") or |
241 | exit_cleanup($tmpdir, 1); | |
1f229345 | 242 | } |
7e0abcec TH |
243 | } |
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) { | |
951b551d | 249 | my $ok = 0; |
7e0abcec | 250 | if (defined($submodule{$path}{left})) { |
ceb1497a DA |
251 | $ok = write_to_file("$ldir/$path", |
252 | "Subproject commit $submodule{$path}{left}"); | |
7e0abcec TH |
253 | } |
254 | if (defined($submodule{$path}{right})) { | |
ceb1497a DA |
255 | $ok = write_to_file("$rdir/$path", |
256 | "Subproject commit $submodule{$path}{right}"); | |
7e0abcec | 257 | } |
ceb1497a | 258 | exit_cleanup($tmpdir, 1) if not $ok; |
7e0abcec TH |
259 | } |
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) { | |
951b551d | 265 | my $ok = 0; |
7e0abcec | 266 | if (defined($symlink{$path}{left})) { |
ceb1497a DA |
267 | $ok = write_to_file("$ldir/$path", |
268 | $symlink{$path}{left}); | |
7e0abcec TH |
269 | } |
270 | if (defined($symlink{$path}{right})) { | |
ceb1497a DA |
271 | $ok = write_to_file("$rdir/$path", |
272 | $symlink{$path}{right}); | |
7e0abcec | 273 | } |
ceb1497a | 274 | exit_cleanup($tmpdir, 1) if not $ok; |
7e0abcec TH |
275 | } |
276 | ||
ceb1497a | 277 | return ($ldir, $rdir, $tmpdir, @working_tree); |
7e0abcec TH |
278 | } |
279 | ||
280 | sub 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") { | |
ceb1497a | 288 | mkpath("$dir") or return 0; |
7e0abcec TH |
289 | } |
290 | ||
291 | # If the file already exists in that location, delete it. This | |
292 | # is required in the case of symbolic links. | |
ceb1497a | 293 | unlink($path); |
7e0abcec | 294 | |
ceb1497a | 295 | open(my $fh, '>', $path) or return 0; |
7e0abcec TH |
296 | print($fh $value); |
297 | close($fh); | |
ceb1497a DA |
298 | |
299 | return 1; | |
7e0abcec TH |
300 | } |
301 | ||
75cd7583 DA |
302 | sub main |
303 | { | |
304 | # parse command-line options. all unrecognized options and arguments | |
305 | # are passed through to the 'git diff' command. | |
c9bdd505 DA |
306 | my %opts = ( |
307 | difftool_cmd => undef, | |
308 | dirdiff => undef, | |
309 | extcmd => undef, | |
310 | gui => undef, | |
311 | help => undef, | |
312 | prompt => undef, | |
190f5475 DA |
313 | symlinks => $^O ne 'cygwin' && |
314 | $^O ne 'MSWin32' && $^O ne 'msys', | |
c9bdd505 | 315 | tool_help => undef, |
2b52123f | 316 | trust_exit_code => undef, |
c9bdd505 DA |
317 | ); |
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; }, | |
1f229345 DA |
323 | 'symlinks' => \$opts{symlinks}, |
324 | 'no-symlinks' => sub { $opts{symlinks} = 0; }, | |
c9bdd505 DA |
325 | 't|tool:s' => \$opts{difftool_cmd}, |
326 | 'tool-help' => \$opts{tool_help}, | |
2b52123f DA |
327 | 'trust-exit-code' => \$opts{trust_exit_code}, |
328 | 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; }, | |
c9bdd505 DA |
329 | 'x|extcmd:s' => \$opts{extcmd}); |
330 | ||
331 | if (defined($opts{help})) { | |
75cd7583 | 332 | usage(0); |
5c38ea31 | 333 | } |
c9bdd505 | 334 | if (defined($opts{tool_help})) { |
75cd7583 | 335 | print_tool_help(); |
3f94ff75 | 336 | } |
c9bdd505 DA |
337 | if (defined($opts{difftool_cmd})) { |
338 | if (length($opts{difftool_cmd}) > 0) { | |
339 | $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd}; | |
75cd7583 DA |
340 | } else { |
341 | print "No <tool> given for --tool=<tool>\n"; | |
342 | usage(1); | |
343 | } | |
344 | } | |
c9bdd505 DA |
345 | if (defined($opts{extcmd})) { |
346 | if (length($opts{extcmd}) > 0) { | |
347 | $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd}; | |
75cd7583 DA |
348 | } else { |
349 | print "No <cmd> given for --extcmd=<cmd>\n"; | |
350 | usage(1); | |
351 | } | |
352 | } | |
c9bdd505 DA |
353 | if ($opts{gui}) { |
354 | my $guitool = Git::config('diff.guitool'); | |
af14b5cf | 355 | if (defined($guitool) && length($guitool) > 0) { |
75cd7583 DA |
356 | $ENV{GIT_DIFF_TOOL} = $guitool; |
357 | } | |
358 | } | |
359 | ||
2b52123f DA |
360 | 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 | ||
75cd7583 DA |
369 | # 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. | |
c9bdd505 | 373 | if (defined($opts{dirdiff})) { |
1f229345 | 374 | dir_diff($opts{extcmd}, $opts{symlinks}); |
75cd7583 | 375 | } else { |
c9bdd505 | 376 | file_diff($opts{prompt}); |
3f94ff75 TH |
377 | } |
378 | } | |
7e0abcec | 379 | |
75cd7583 DA |
380 | sub dir_diff |
381 | { | |
1f229345 | 382 | my ($extcmd, $symlinks) = @_; |
75cd7583 | 383 | my $rc; |
ceb1497a | 384 | my $error = 0; |
75cd7583 | 385 | my $repo = Git->repository(); |
32b8c581 DA |
386 | my $repo_path = $repo->repo_path(); |
387 | my $workdir = $repo->wc_path(); | |
388 | my ($a, $b, $tmpdir, @worktree) = setup_dir_diff($workdir, $symlinks); | |
ceb1497a | 389 | |
7e0abcec TH |
390 | if (defined($extcmd)) { |
391 | $rc = system($extcmd, $a, $b); | |
3f94ff75 | 392 | } else { |
7e0abcec TH |
393 | $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true'; |
394 | $rc = system('git', 'difftool--helper', $a, $b); | |
d531174f | 395 | } |
7e0abcec TH |
396 | # If the diff including working copy files and those |
397 | # files were modified during the diff, then the changes | |
1f229345 DA |
398 | # 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. | |
67aa147a JK |
401 | # |
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 | ||
1f229345 DA |
409 | for my $file (@worktree) { |
410 | next if $symlinks && -l "$b/$file"; | |
283abb2c DA |
411 | next if ! -f "$b/$file"; |
412 | ||
67aa147a | 413 | if (!$indices_loaded) { |
32b8c581 DA |
414 | %wt_modified = changed_files( |
415 | $repo_path, "$tmpdir/wtindex", $workdir); | |
416 | %tmp_modified = changed_files( | |
417 | $repo_path, "$tmpdir/wtindex", $b); | |
67aa147a JK |
418 | $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"; | |
283abb2c | 426 | warn $errmsg; |
ceb1497a | 427 | $error = 1; |
67aa147a | 428 | } elsif (exists $tmp_modified{$file}) { |
1f229345 | 429 | my $mode = stat("$b/$file")->mode; |
ceb1497a DA |
430 | copy("$b/$file", "$workdir/$file") or |
431 | exit_cleanup($tmpdir, 1); | |
432 | ||
433 | chmod($mode, "$workdir/$file") or | |
434 | exit_cleanup($tmpdir, 1); | |
05df5326 | 435 | } |
7e0abcec | 436 | } |
ceb1497a DA |
437 | 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 | } | |
75cd7583 DA |
444 | } |
445 | ||
446 | sub file_diff | |
447 | { | |
448 | my ($prompt) = @_; | |
449 | ||
7e0abcec TH |
450 | 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)); | |
468 | } | |
75cd7583 DA |
469 | |
470 | main(); |