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, $symlinks) = @_;
90 # Run the diff; exit immediately if no diff found
91 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
92 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
93 # by Git->repository->command*.
94 my $repo_path = $repo->repo_path();
95 my %repo_args = (Repository
=> $repo_path, WorkingCopy
=> $workdir);
96 my $diffrepo = Git
->repository(%repo_args);
98 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
99 my $diffrtn = $diffrepo->command_oneline(@gitargs);
100 exit(0) unless defined($diffrtn);
102 # Build index info for left and right sides of the diff
103 my $submodule_mode = '160000';
104 my $symlink_mode = '120000';
105 my $null_mode = '0' x
6;
106 my $null_sha1 = '0' x
40;
111 my @working_tree = ();
112 my @rawdiff = split('\0', $diffrtn);
115 while ($i < $#rawdiff) {
116 if ($rawdiff[$i] =~ /^::/) {
118 Combined diff formats
('-c' and '--cc') are
not supported
in
119 directory diff mode
('-d' and '--dir-diff').
124 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
125 split(' ', substr($rawdiff[$i], 1));
126 my $src_path = $rawdiff[$i + 1];
129 if ($status =~ /^[CR]/) {
130 $dst_path = $rawdiff[$i + 2];
133 $dst_path = $src_path;
137 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
138 $submodule{$src_path}{left
} = $lsha1;
139 if ($lsha1 ne $rsha1) {
140 $submodule{$dst_path}{right
} = $rsha1;
142 $submodule{$dst_path}{right
} = "$rsha1-dirty";
147 if ($lmode eq $symlink_mode) {
148 $symlink{$src_path}{left
} =
149 $diffrepo->command_oneline('show', "$lsha1");
152 if ($rmode eq $symlink_mode) {
153 $symlink{$dst_path}{right
} =
154 $diffrepo->command_oneline('show', "$rsha1");
157 if ($lmode ne $null_mode and $status !~ /^C/) {
158 $lindex .= "$lmode $lsha1\t$src_path\0";
161 if ($rmode ne $null_mode) {
162 if ($rsha1 ne $null_sha1) {
163 $rindex .= "$rmode $rsha1\t$dst_path\0";
165 push(@working_tree, $dst_path);
170 # Setup temp directories
171 my $tmpdir = tempdir
('git-difftool.XXXXX', CLEANUP
=> 0, TMPDIR
=> 1);
172 my $ldir = "$tmpdir/left";
173 my $rdir = "$tmpdir/right";
174 mkpath
($ldir) or exit_cleanup
($tmpdir, 1);
175 mkpath
($rdir) or exit_cleanup
($tmpdir, 1);
177 # If $GIT_DIR is not set prior to calling 'git update-index' and
178 # 'git checkout-index', then those commands will fail if difftool
179 # is called from a directory other than the repo root.
180 my $must_unset_git_dir = 0;
181 if (not defined($ENV{GIT_DIR
})) {
182 $must_unset_git_dir = 1;
183 $ENV{GIT_DIR
} = $repo_path;
186 # Populate the left and right directories based on each index file
188 $ENV{GIT_INDEX_FILE
} = "$tmpdir/lindex";
190 $repo->command_input_pipe(qw(update-index -z --index-info));
191 print($inpipe $lindex);
192 $repo->command_close_pipe($inpipe, $ctx);
194 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
195 exit_cleanup
($tmpdir, $rc) if $rc != 0;
197 $ENV{GIT_INDEX_FILE
} = "$tmpdir/rindex";
199 $repo->command_input_pipe(qw(update-index -z --index-info));
200 print($inpipe $rindex);
201 $repo->command_close_pipe($inpipe, $ctx);
203 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
204 exit_cleanup
($tmpdir, $rc) if $rc != 0;
206 # If $GIT_DIR was explicitly set just for the update/checkout
207 # commands, then it should be unset before continuing.
208 delete($ENV{GIT_DIR
}) if ($must_unset_git_dir);
209 delete($ENV{GIT_INDEX_FILE
});
211 # Changes in the working tree need special treatment since they are
212 # not part of the index. Remove any trailing slash from $workdir
213 # before starting to avoid double slashes in symlink targets.
215 for my $file (@working_tree) {
216 my $dir = dirname
($file);
217 unless (-d
"$rdir/$dir") {
218 mkpath
("$rdir/$dir") or
219 exit_cleanup
($tmpdir, 1);
222 symlink("$workdir/$file", "$rdir/$file") or
223 exit_cleanup
($tmpdir, 1);
225 copy
("$workdir/$file", "$rdir/$file") or
226 exit_cleanup
($tmpdir, 1);
228 my $mode = stat("$workdir/$file")->mode;
229 chmod($mode, "$rdir/$file") or
230 exit_cleanup
($tmpdir, 1);
234 # Changes to submodules require special treatment. This loop writes a
235 # temporary file to both the left and right directories to show the
236 # change in the recorded SHA1 for the submodule.
237 for my $path (keys %submodule) {
239 if (defined($submodule{$path}{left
})) {
240 $ok = write_to_file
("$ldir/$path",
241 "Subproject commit $submodule{$path}{left}");
243 if (defined($submodule{$path}{right
})) {
244 $ok = write_to_file
("$rdir/$path",
245 "Subproject commit $submodule{$path}{right}");
247 exit_cleanup
($tmpdir, 1) if not $ok;
250 # Symbolic links require special treatment. The standard "git diff"
251 # shows only the link itself, not the contents of the link target.
252 # This loop replicates that behavior.
253 for my $path (keys %symlink) {
255 if (defined($symlink{$path}{left
})) {
256 $ok = write_to_file
("$ldir/$path",
257 $symlink{$path}{left
});
259 if (defined($symlink{$path}{right
})) {
260 $ok = write_to_file
("$rdir/$path",
261 $symlink{$path}{right
});
263 exit_cleanup
($tmpdir, 1) if not $ok;
266 return ($ldir, $rdir, $tmpdir, @working_tree);
274 # Make sure the path to the file exists
275 my $dir = dirname
($path);
277 mkpath
("$dir") or return 0;
280 # If the file already exists in that location, delete it. This
281 # is required in the case of symbolic links.
284 open(my $fh, '>', $path) or return 0;
293 # parse command-line options. all unrecognized options and arguments
294 # are passed through to the 'git diff' command.
296 difftool_cmd
=> undef,
302 symlinks
=> $^O
ne 'cygwin' &&
303 $^O
ne 'MSWin32' && $^O
ne 'msys',
306 GetOptions
('g|gui!' => \
$opts{gui
},
307 'd|dir-diff' => \
$opts{dirdiff
},
309 'prompt!' => \
$opts{prompt
},
310 'y' => sub { $opts{prompt
} = 0; },
311 'symlinks' => \
$opts{symlinks
},
312 'no-symlinks' => sub { $opts{symlinks
} = 0; },
313 't|tool:s' => \
$opts{difftool_cmd
},
314 'tool-help' => \
$opts{tool_help
},
315 'x|extcmd:s' => \
$opts{extcmd
});
317 if (defined($opts{help
})) {
320 if (defined($opts{tool_help
})) {
323 if (defined($opts{difftool_cmd
})) {
324 if (length($opts{difftool_cmd
}) > 0) {
325 $ENV{GIT_DIFF_TOOL
} = $opts{difftool_cmd
};
327 print "No <tool> given for --tool=<tool>\n";
331 if (defined($opts{extcmd
})) {
332 if (length($opts{extcmd
}) > 0) {
333 $ENV{GIT_DIFFTOOL_EXTCMD
} = $opts{extcmd
};
335 print "No <cmd> given for --extcmd=<cmd>\n";
340 my $guitool = Git
::config
('diff.guitool');
341 if (length($guitool) > 0) {
342 $ENV{GIT_DIFF_TOOL
} = $guitool;
346 # In directory diff mode, 'git-difftool--helper' is called once
347 # to compare the a/b directories. In file diff mode, 'git diff'
348 # will invoke a separate instance of 'git-difftool--helper' for
349 # each file that changed.
350 if (defined($opts{dirdiff
})) {
351 dir_diff
($opts{extcmd
}, $opts{symlinks
});
353 file_diff
($opts{prompt
});
359 my ($extcmd, $symlinks) = @_;
362 my $repo = Git
->repository();
363 my $workdir = find_worktree
($repo);
364 my ($a, $b, $tmpdir, @worktree) =
365 setup_dir_diff
($repo, $workdir, $symlinks);
367 if (defined($extcmd)) {
368 $rc = system($extcmd, $a, $b);
370 $ENV{GIT_DIFFTOOL_DIRDIFF
} = 'true';
371 $rc = system('git', 'difftool--helper', $a, $b);
373 # If the diff including working copy files and those
374 # files were modified during the diff, then the changes
375 # should be copied back to the working tree.
376 # Do not copy back files when symlinks are used and the
377 # external tool did not replace the original link with a file.
378 for my $file (@worktree) {
379 next if $symlinks && -l
"$b/$file";
380 next if ! -f
"$b/$file";
382 my $diff = compare
("$b/$file", "$workdir/$file");
385 } elsif ($diff == -1) {
386 my $errmsg = "warning: Could not compare ";
387 $errmsg += "'$b/$file' with '$workdir/$file'\n";
390 } elsif ($diff == 1) {
391 my $mode = stat("$b/$file")->mode;
392 copy
("$b/$file", "$workdir/$file") or
393 exit_cleanup
($tmpdir, 1);
395 chmod($mode, "$workdir/$file") or
396 exit_cleanup
($tmpdir, 1);
400 warn "warning: Temporary files exist in '$tmpdir'.\n";
401 warn "warning: You may want to cleanup or recover these.\n";
404 exit_cleanup
($tmpdir, $rc);
412 if (defined($prompt)) {
414 $ENV{GIT_DIFFTOOL_PROMPT
} = 'true';
416 $ENV{GIT_DIFFTOOL_NO_PROMPT
} = 'true';
420 $ENV{GIT_PAGER
} = '';
421 $ENV{GIT_EXTERNAL_DIFF
} = 'git-difftool--helper';
423 # ActiveState Perl for Win32 does not implement POSIX semantics of
424 # exec* system call. It just spawns the given executable and finishes
425 # the starting program, exiting with code 0.
426 # system will at least catch the errors returned by git diff,
427 # allowing the caller of git difftool better handling of failures.
428 my $rc = system('git', 'diff', @ARGV);
429 exit($rc | ($rc >> 8));