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