2 # Copyright (c) 2009, 2010 David Aguilar
3 # Copyright (c) 2012 Tim Henigan
5 # This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
6 # git-difftool--helper script.
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.
11 # Any arguments that are unknown to this script are forwarded to 'git diff'.
16 use File
::Basename
qw(dirname);
21 use File
::Path
qw(mkpath rmtree);
22 use File
::Temp
qw(tempdir);
23 use Getopt
::Long
qw(:config pass_through);
30 usage
: git difftool
[-t
|--tool
=<tool
>] [--tool
-help
]
33 [--prompt
] [-y
|--no-prompt
]
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'
48 my $env_worktree = $ENV{GIT_WORK_TREE
};
49 my $core_worktree = Git
::config
('core.worktree');
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;
56 $worktree = $repo->wc_path();
64 my $cmd = 'TOOL_MODE=diff';
65 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
66 $cmd .= ' && show_tool_help';
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));
76 my ($tmpdir, $status) = @_;
79 if ($status and $errno) {
80 my ($package, $file, $line) = caller();
81 warn "$file line $line: $errno\n";
83 exit($status | ($status >> 8));
88 my ($repo, $workdir, $file, $sha1, $symlinks) = @_;
89 my $null_sha1 = '0' x
40;
91 if ($sha1 eq $null_sha1) {
93 } elsif (not $symlinks) {
97 my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
98 return $sha1 eq $wt_sha1;
103 my ($repo, $workdir, $symlinks) = @_;
105 # Run the diff; exit immediately if no diff found
106 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
107 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
108 # by Git->repository->command*.
109 my $repo_path = $repo->repo_path();
110 my %repo_args = (Repository
=> $repo_path, WorkingCopy
=> $workdir);
111 my $diffrepo = Git
->repository(%repo_args);
113 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
114 my $diffrtn = $diffrepo->command_oneline(@gitargs);
115 exit(0) unless defined($diffrtn);
117 # Build index info for left and right sides of the diff
118 my $submodule_mode = '160000';
119 my $symlink_mode = '120000';
120 my $null_mode = '0' x
6;
121 my $null_sha1 = '0' x
40;
126 my @working_tree = ();
127 my @rawdiff = split('\0', $diffrtn);
130 while ($i < $#rawdiff) {
131 if ($rawdiff[$i] =~ /^::/) {
133 Combined diff formats
('-c' and '--cc') are
not supported
in
134 directory diff mode
('-d' and '--dir-diff').
139 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
140 split(' ', substr($rawdiff[$i], 1));
141 my $src_path = $rawdiff[$i + 1];
144 if ($status =~ /^[CR]/) {
145 $dst_path = $rawdiff[$i + 2];
148 $dst_path = $src_path;
152 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
153 $submodule{$src_path}{left
} = $lsha1;
154 if ($lsha1 ne $rsha1) {
155 $submodule{$dst_path}{right
} = $rsha1;
157 $submodule{$dst_path}{right
} = "$rsha1-dirty";
162 if ($lmode eq $symlink_mode) {
163 $symlink{$src_path}{left
} =
164 $diffrepo->command_oneline('show', "$lsha1");
167 if ($rmode eq $symlink_mode) {
168 $symlink{$dst_path}{right
} =
169 $diffrepo->command_oneline('show', "$rsha1");
172 if ($lmode ne $null_mode and $status !~ /^C/) {
173 $lindex .= "$lmode $lsha1\t$src_path\0";
176 if ($rmode ne $null_mode) {
177 if (use_wt_file
($repo, $workdir, $dst_path, $rsha1, $symlinks)) {
178 push(@working_tree, $dst_path);
180 $rindex .= "$rmode $rsha1\t$dst_path\0";
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);
192 # If $GIT_DIR is not set prior to calling 'git update-index' and
193 # 'git checkout-index', then those commands will fail if difftool
194 # is called from a directory other than the repo root.
195 my $must_unset_git_dir = 0;
196 if (not defined($ENV{GIT_DIR
})) {
197 $must_unset_git_dir = 1;
198 $ENV{GIT_DIR
} = $repo_path;
201 # Populate the left and right directories based on each index file
203 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
205 $repo->command_input_pipe(qw(update-index -z --index-info));
206 print($inpipe $lindex);
207 $repo->command_close_pipe($inpipe, $ctx);
209 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
210 exit_cleanup
($tmpdir, $rc) if $rc != 0;
212 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
214 $repo->command_input_pipe(qw(update-index -z --index-info));
215 print($inpipe $rindex);
216 $repo->command_close_pipe($inpipe, $ctx);
218 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
219 exit_cleanup
($tmpdir, $rc) if $rc != 0;
221 # If $GIT_DIR was explicitly set just for the update/checkout
222 # commands, then it should be unset before continuing.
223 delete($ENV{GIT_DIR
}) if ($must_unset_git_dir);
224 delete($ENV{GIT_INDEX_FILE
});
226 # Changes in the working tree need special treatment since they are
227 # not part of the index. Remove any trailing slash from $workdir
228 # before starting to avoid double slashes in symlink targets.
230 for my $file (@working_tree) {
231 my $dir = dirname
($file);
232 unless (-d
"$rdir/$dir") {
233 mkpath
("$rdir/$dir") or
234 exit_cleanup
($tmpdir, 1);
237 symlink("$workdir/$file", "$rdir/$file") or
238 exit_cleanup
($tmpdir, 1);
240 copy
("$workdir/$file", "$rdir/$file") or
241 exit_cleanup
($tmpdir, 1);
243 my $mode = stat("$workdir/$file")->mode;
244 chmod($mode, "$rdir/$file") or
245 exit_cleanup
($tmpdir, 1);
249 # Changes to submodules require special treatment. This loop writes a
250 # temporary file to both the left and right directories to show the
251 # change in the recorded SHA1 for the submodule.
252 for my $path (keys %submodule) {
254 if (defined($submodule{$path}{left
})) {
255 $ok = write_to_file
("$ldir/$path",
256 "Subproject commit $submodule{$path}{left}");
258 if (defined($submodule{$path}{right
})) {
259 $ok = write_to_file
("$rdir/$path",
260 "Subproject commit $submodule{$path}{right}");
262 exit_cleanup
($tmpdir, 1) if not $ok;
265 # Symbolic links require special treatment. The standard "git diff"
266 # shows only the link itself, not the contents of the link target.
267 # This loop replicates that behavior.
268 for my $path (keys %symlink) {
270 if (defined($symlink{$path}{left
})) {
271 $ok = write_to_file
("$ldir/$path",
272 $symlink{$path}{left
});
274 if (defined($symlink{$path}{right
})) {
275 $ok = write_to_file
("$rdir/$path",
276 $symlink{$path}{right
});
278 exit_cleanup
($tmpdir, 1) if not $ok;
281 return ($ldir, $rdir, $tmpdir, @working_tree);
289 # Make sure the path to the file exists
290 my $dir = dirname
($path);
292 mkpath
("$dir") or return 0;
295 # If the file already exists in that location, delete it. This
296 # is required in the case of symbolic links.
299 open(my $fh, '>', $path) or return 0;
308 # parse command-line options. all unrecognized options and arguments
309 # are passed through to the 'git diff' command.
311 difftool_cmd
=> undef,
317 symlinks
=> $^O
ne 'cygwin' &&
318 $^O
ne 'MSWin32' && $^O
ne 'msys',
321 GetOptions
('g|gui!' => \
$opts{gui
},
322 'd|dir-diff' => \
$opts{dirdiff
},
324 'prompt!' => \
$opts{prompt
},
325 'y' => sub { $opts{prompt
} = 0; },
326 'symlinks' => \
$opts{symlinks
},
327 'no-symlinks' => sub { $opts{symlinks
} = 0; },
328 't|tool:s' => \
$opts{difftool_cmd
},
329 'tool-help' => \
$opts{tool_help
},
330 'x|extcmd:s' => \
$opts{extcmd
});
332 if (defined($opts{help
})) {
335 if (defined($opts{tool_help
})) {
338 if (defined($opts{difftool_cmd
})) {
339 if (length($opts{difftool_cmd
}) > 0) {
340 $ENV{GIT_DIFF_TOOL
} = $opts{difftool_cmd
};
342 print "No <tool> given for --tool=<tool>\n";
346 if (defined($opts{extcmd
})) {
347 if (length($opts{extcmd
}) > 0) {
348 $ENV{GIT_DIFFTOOL_EXTCMD
} = $opts{extcmd
};
350 print "No <cmd> given for --extcmd=<cmd>\n";
355 my $guitool = Git
::config
('diff.guitool');
356 if (length($guitool) > 0) {
357 $ENV{GIT_DIFF_TOOL
} = $guitool;
361 # In directory diff mode, 'git-difftool--helper' is called once
362 # to compare the a/b directories. In file diff mode, 'git diff'
363 # will invoke a separate instance of 'git-difftool--helper' for
364 # each file that changed.
365 if (defined($opts{dirdiff
})) {
366 dir_diff
($opts{extcmd
}, $opts{symlinks
});
368 file_diff
($opts{prompt
});
374 my ($extcmd, $symlinks) = @_;
377 my $repo = Git
->repository();
378 my $workdir = find_worktree
($repo);
379 my ($a, $b, $tmpdir, @worktree) =
380 setup_dir_diff
($repo, $workdir, $symlinks);
382 if (defined($extcmd)) {
383 $rc = system($extcmd, $a, $b);
385 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
386 $rc = system('git', 'difftool--helper', $a, $b);
388 # If the diff including working copy files and those
389 # files were modified during the diff, then the changes
390 # should be copied back to the working tree.
391 # Do not copy back files when symlinks are used and the
392 # external tool did not replace the original link with a file.
393 for my $file (@worktree) {
394 next if $symlinks && -l
"$b/$file";
395 next if ! -f
"$b/$file";
397 my $diff = compare
("$b/$file", "$workdir/$file");
400 } elsif ($diff == -1) {
401 my $errmsg = "warning: Could not compare ";
402 $errmsg += "'$b/$file' with '$workdir/$file'\n";
405 } elsif ($diff == 1) {
406 my $mode = stat("$b/$file")->mode;
407 copy
("$b/$file", "$workdir/$file") or
408 exit_cleanup
($tmpdir, 1);
410 chmod($mode, "$workdir/$file") or
411 exit_cleanup
($tmpdir, 1);
415 warn "warning: Temporary files exist in '$tmpdir'.\n";
416 warn "warning: You may want to cleanup or recover these.\n";
419 exit_cleanup
($tmpdir, $rc);
427 if (defined($prompt)) {
429 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
431 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
435 $ENV{GIT_PAGER
} = '';
436 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
438 # ActiveState Perl for Win32 does not implement POSIX semantics of
439 # exec* system call. It just spawns the given executable and finishes
440 # the starting program, exiting with code 0.
441 # system will at least catch the errors returned by git diff,
442 # allowing the caller of git difftool better handling of failures.
443 my $rc = system('git', 'diff', @ARGV);
444 exit($rc | ($rc >> 8));