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