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(basename dirname);
20 use File
::Path
qw(mkpath);
21 use File
::Temp
qw(tempdir);
22 use Getopt
::Long
qw(:config pass_through);
27 my $repo = Git
->repository();
28 my $repo_path = $repo->repo_path();
34 usage
: git difftool
[-t
|--tool
=<tool
>] [--tool
-help
]
37 [--prompt
] [-y
|--no-prompt
]
46 # Git->repository->wc_path() does not honor changes to the working
47 # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
50 my $env_worktree = $ENV{GIT_WORK_TREE
};
51 my $core_worktree = Git
::config
('core.worktree');
53 if (defined($env_worktree) and (length($env_worktree) > 0)) {
54 $worktree = $env_worktree;
55 } elsif (defined($core_worktree) and (length($core_worktree) > 0)) {
56 $worktree = $core_worktree;
58 $worktree = $repo->wc_path();
64 my $workdir = find_worktree
();
68 my ($cmd, @found, @notfound);
69 my $gitpath = Git
::exec_path
();
71 my @files = map { basename
($_) } glob("$gitpath/mergetools/*");
72 my @tools = sort(grep { !m{^defaults$} } @files);
74 foreach my $tool (@tools) {
75 $cmd = "TOOL_MODE=diff";
76 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
77 $cmd .= " && get_merge_tool_path $tool >/dev/null 2>&1";
78 $cmd .= " && can_diff >/dev/null 2>&1";
79 if (system('sh', '-c', $cmd) == 0) {
82 push(@notfound, $tool);
86 print "'git difftool --tool=<tool>' may be set to one of the following:\n";
87 print "\t$_\n" for (@found);
89 print "\nThe following tools are valid, but not currently available:\n";
90 print "\t$_\n" for (@notfound);
92 print "\nNOTE: Some of the tools listed above only work in a windowed\n";
93 print "environment. If run in a terminal-only session, they will fail.\n";
100 # Run the diff; exit immediately if no diff found
101 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
102 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
103 # by Git->repository->command*.
104 my $diffrepo = Git
->repository(Repository
=> $repo_path, WorkingCopy
=> $workdir);
105 my $diffrtn = $diffrepo->command_oneline('diff', '--raw', '--no-abbrev', '-z', @ARGV);
106 exit(0) if (length($diffrtn) == 0);
108 # Setup temp directories
109 my $tmpdir = tempdir
('git-diffall.XXXXX', CLEANUP
=> 1, TMPDIR
=> 1);
110 my $ldir = "$tmpdir/left";
111 my $rdir = "$tmpdir/right";
112 mkpath
($ldir) or die $!;
113 mkpath
($rdir) or die $!;
115 # Build index info for left and right sides of the diff
116 my $submodule_mode = '160000';
117 my $symlink_mode = '120000';
118 my $null_mode = '0' x
6;
119 my $null_sha1 = '0' x
40;
124 my @rawdiff = split('\0', $diffrtn);
127 while ($i < $#rawdiff) {
128 if ($rawdiff[$i] =~ /^::/) {
129 print "Combined diff formats ('-c' and '--cc') are not supported in directory diff mode.\n";
133 my ($lmode, $rmode, $lsha1, $rsha1, $status) = split(' ', substr($rawdiff[$i], 1));
134 my $src_path = $rawdiff[$i + 1];
137 if ($status =~ /^[CR]/) {
138 $dst_path = $rawdiff[$i + 2];
141 $dst_path = $src_path;
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;
150 $submodule{$dst_path}{right
} = "$rsha1-dirty";
155 if ($lmode eq $symlink_mode) {
156 $symlink{$src_path}{left
} = $diffrepo->command_oneline('show', "$lsha1");
159 if ($rmode eq $symlink_mode) {
160 $symlink{$dst_path}{right
} = $diffrepo->command_oneline('show', "$rsha1");
163 if (($lmode ne $null_mode) and ($status !~ /^C/)) {
164 $lindex .= "$lmode $lsha1\t$src_path\0";
167 if ($rmode ne $null_mode) {
168 if ($rsha1 ne $null_sha1) {
169 $rindex .= "$rmode $rsha1\t$dst_path\0";
171 push(@working_tree, $dst_path);
176 # If $GIT_DIR is not set prior to calling 'git update-index' and
177 # 'git checkout-index', then those commands will fail if difftool
178 # is called from a directory other than the repo root.
179 my $must_unset_git_dir = 0;
180 if (not defined($ENV{GIT_DIR
})) {
181 $must_unset_git_dir = 1;
182 $ENV{GIT_DIR
} = $repo_path;
185 # Populate the left and right directories based on each index file
187 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
188 ($inpipe, $ctx) = $repo->command_input_pipe(qw
/update-index -z --index-info/);
189 print($inpipe $lindex);
190 $repo->command_close_pipe($inpipe, $ctx);
191 $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
192 exit($rc | ($rc >> 8)) if ($rc != 0);
194 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
195 ($inpipe, $ctx) = $repo->command_input_pipe(qw
/update-index -z --index-info/);
196 print($inpipe $rindex);
197 $repo->command_close_pipe($inpipe, $ctx);
198 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
199 exit($rc | ($rc >> 8)) if ($rc != 0);
201 # If $GIT_DIR was explicitly set just for the update/checkout
202 # commands, then it should be unset before continuing.
203 delete($ENV{GIT_DIR
}) if ($must_unset_git_dir);
204 delete($ENV{GIT_INDEX_FILE
});
206 # Changes in the working tree need special treatment since they are
207 # not part of the index
208 for my $file (@working_tree) {
209 my $dir = dirname
($file);
210 unless (-d
"$rdir/$dir") {
211 mkpath
("$rdir/$dir") or die $!;
213 copy
("$workdir/$file", "$rdir/$file") or die $!;
214 chmod(stat("$workdir/$file")->mode, "$rdir/$file") or die $!;
217 # Changes to submodules require special treatment. This loop writes a
218 # temporary file to both the left and right directories to show the
219 # change in the recorded SHA1 for the submodule.
220 for my $path (keys %submodule) {
221 if (defined($submodule{$path}{left
})) {
222 write_to_file
("$ldir/$path", "Subproject commit $submodule{$path}{left}");
224 if (defined($submodule{$path}{right
})) {
225 write_to_file
("$rdir/$path", "Subproject commit $submodule{$path}{right}");
229 # Symbolic links require special treatment. The standard "git diff"
230 # shows only the link itself, not the contents of the link target.
231 # This loop replicates that behavior.
232 for my $path (keys %symlink) {
233 if (defined($symlink{$path}{left
})) {
234 write_to_file
("$ldir/$path", $symlink{$path}{left
});
236 if (defined($symlink{$path}{right
})) {
237 write_to_file
("$rdir/$path", $symlink{$path}{right
});
241 return ($ldir, $rdir);
249 # Make sure the path to the file exists
250 my $dir = dirname
($path);
252 mkpath
("$dir") or die $!;
255 # If the file already exists in that location, delete it. This
256 # is required in the case of symbolic links.
259 open(my $fh, '>', "$path") or die $!;
264 # parse command-line options. all unrecognized options and arguments
265 # are passed through to the 'git diff' command.
266 my ($difftool_cmd, $dirdiff, $extcmd, $gui, $help, $prompt, $tool_help);
267 GetOptions
('g|gui!' => \
$gui,
268 'd|dir-diff' => \
$dirdiff,
270 'prompt!' => \
$prompt,
271 'y' => sub { $prompt = 0; },
272 't|tool:s' => \
$difftool_cmd,
273 'tool-help' => \
$tool_help,
274 'x|extcmd:s' => \
$extcmd);
276 if (defined($help)) {
279 if (defined($tool_help)) {
282 if (defined($difftool_cmd)) {
283 if (length($difftool_cmd) > 0) {
284 $ENV{GIT_DIFF_TOOL
} = $difftool_cmd;
286 print "No <tool> given for --tool=<tool>\n";
290 if (defined($extcmd)) {
291 if (length($extcmd) > 0) {
292 $ENV{GIT_DIFFTOOL_EXTCMD
} = $extcmd;
294 print "No <cmd> given for --extcmd=<cmd>\n";
300 $guitool = Git
::config
('diff.guitool');
301 if (length($guitool) > 0) {
302 $ENV{GIT_DIFF_TOOL
} = $guitool;
306 # In directory diff mode, 'git-difftool--helper' is called once
307 # to compare the a/b directories. In file diff mode, 'git diff'
308 # will invoke a separate instance of 'git-difftool--helper' for
309 # each file that changed.
310 if (defined($dirdiff)) {
311 my ($a, $b) = setup_dir_diff
();
312 if (defined($extcmd)) {
313 $rc = system($extcmd, $a, $b);
315 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
316 $rc = system('git', 'difftool--helper', $a, $b);
319 exit($rc | ($rc >> 8)) if ($rc != 0);
321 # If the diff including working copy files and those
322 # files were modified during the diff, then the changes
323 # should be copied back to the working tree
324 for my $file (@working_tree) {
325 if (-e
"$b/$file" && compare
("$b/$file", "$workdir/$file")) {
326 copy
("$b/$file", "$workdir/$file") or die $!;
327 chmod(stat("$b/$file")->mode, "$workdir/$file") or die $!;
331 if (defined($prompt)) {
333 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
335 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
339 $ENV{GIT_PAGER
} = '';
340 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
342 # ActiveState Perl for Win32 does not implement POSIX semantics of
343 # exec* system call. It just spawns the given executable and finishes
344 # the starting program, exiting with code 0.
345 # system will at least catch the errors returned by git diff,
346 # allowing the caller of git difftool better handling of failures.
347 my $rc = system('git', 'diff', @ARGV);
348 exit($rc | ($rc >> 8));