]> git.ipfire.org Git - thirdparty/git.git/blame - git-difftool.perl
attr: avoid calling find_basename() twice per path
[thirdparty/git.git] / git-difftool.perl
CommitLineData
7e0abcec 1#!/usr/bin/perl
f47f1e2c 2# Copyright (c) 2009, 2010 David Aguilar
7e0abcec 3# Copyright (c) 2012 Tim Henigan
5c38ea31
DA
4#
5# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
a904392e
DA
6# git-difftool--helper script.
7#
8# This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
7e0abcec 9# The GIT_DIFF* variables are exported for use by git-difftool--helper.
a904392e 10#
5c38ea31
DA
11# Any arguments that are unknown to this script are forwarded to 'git diff'.
12
d48b2841 13use 5.008;
5c38ea31
DA
14use strict;
15use warnings;
7c7584b9 16use File::Basename qw(dirname);
7e0abcec 17use File::Copy;
05df5326 18use File::Compare;
7c7584b9 19use File::Find;
7e0abcec 20use File::stat;
ceb1497a 21use File::Path qw(mkpath rmtree);
7e0abcec 22use File::Temp qw(tempdir);
3f94ff75
TH
23use Getopt::Long qw(:config pass_through);
24use Git;
5c38ea31
DA
25
26sub usage
27{
28360769 28 my $exitcode = shift;
5c38ea31 29 print << 'USAGE';
bf73fc21 30usage: git difftool [-t|--tool=<tool>] [--tool-help]
85089604
TH
31 [-x|--extcmd=<cmd>]
32 [-g|--gui] [--no-gui]
3f94ff75 33 [--prompt] [-y|--no-prompt]
7e0abcec 34 [-d|--dir-diff]
f47f1e2c 35 ['git diff' options]
5c38ea31 36USAGE
28360769 37 exit($exitcode);
5c38ea31
DA
38}
39
7e0abcec
TH
40sub find_worktree
41{
75cd7583
DA
42 my ($repo) = @_;
43
7e0abcec
TH
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
7c7584b9
DA
62sub filter_tool_scripts
63{
64 my ($tools) = @_;
65 if (-d $_) {
66 if ($_ ne ".") {
67 # Ignore files in subdirectories
68 $File::Find::prune = 1;
69 }
70 } else {
71 if ((-f $_) && ($_ ne "defaults")) {
72 push(@$tools, $_);
73 }
74 }
75}
76
bf73fc21
TH
77sub print_tool_help
78{
7c7584b9 79 my ($cmd, @found, @notfound, @tools);
bf73fc21
TH
80 my $gitpath = Git::exec_path();
81
7c7584b9 82 find(sub { filter_tool_scripts(\@tools) }, "$gitpath/mergetools");
bf73fc21
TH
83
84 foreach my $tool (@tools) {
85 $cmd = "TOOL_MODE=diff";
86 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
87 $cmd .= " && get_merge_tool_path $tool >/dev/null 2>&1";
88 $cmd .= " && can_diff >/dev/null 2>&1";
89 if (system('sh', '-c', $cmd) == 0) {
90 push(@found, $tool);
91 } else {
92 push(@notfound, $tool);
93 }
94 }
95
a4cd5be3
DA
96 print << 'EOF';
97'git difftool --tool=<tool>' may be set to one of the following:
98EOF
7c7584b9 99 print "\t$_\n" for (sort(@found));
bf73fc21 100
a4cd5be3
DA
101 print << 'EOF';
102
103The following tools are valid, but not currently available:
104EOF
7c7584b9 105 print "\t$_\n" for (sort(@notfound));
bf73fc21 106
a4cd5be3 107 print << 'EOF';
bf73fc21 108
a4cd5be3
DA
109NOTE: Some of the tools listed above only work in a windowed
110environment. If run in a terminal-only session, they will fail.
111EOF
bf73fc21
TH
112 exit(0);
113}
114
ceb1497a
DA
115sub exit_cleanup
116{
117 my ($tmpdir, $status) = @_;
118 my $errno = $!;
119 rmtree($tmpdir);
120 if ($status and $errno) {
121 my ($package, $file, $line) = caller();
122 warn "$file line $line: $errno\n";
123 }
124 exit($status | ($status >> 8));
125}
126
7e0abcec
TH
127sub setup_dir_diff
128{
1f229345 129 my ($repo, $workdir, $symlinks) = @_;
75cd7583 130
7e0abcec
TH
131 # Run the diff; exit immediately if no diff found
132 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
133 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
134 # by Git->repository->command*.
75cd7583 135 my $repo_path = $repo->repo_path();
a4cd5be3
DA
136 my %repo_args = (Repository => $repo_path, WorkingCopy => $workdir);
137 my $diffrepo = Git->repository(%repo_args);
138
139 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
140 my $diffrtn = $diffrepo->command_oneline(@gitargs);
ed36e5bd 141 exit(0) unless defined($diffrtn);
7e0abcec 142
7e0abcec
TH
143 # Build index info for left and right sides of the diff
144 my $submodule_mode = '160000';
145 my $symlink_mode = '120000';
146 my $null_mode = '0' x 6;
147 my $null_sha1 = '0' x 40;
148 my $lindex = '';
149 my $rindex = '';
150 my %submodule;
151 my %symlink;
75cd7583 152 my @working_tree = ();
7e0abcec
TH
153 my @rawdiff = split('\0', $diffrtn);
154
155 my $i = 0;
156 while ($i < $#rawdiff) {
157 if ($rawdiff[$i] =~ /^::/) {
a4cd5be3
DA
158 warn << 'EOF';
159Combined diff formats ('-c' and '--cc') are not supported in
160directory diff mode ('-d' and '--dir-diff').
161EOF
7e0abcec
TH
162 exit(1);
163 }
164
a4cd5be3
DA
165 my ($lmode, $rmode, $lsha1, $rsha1, $status) =
166 split(' ', substr($rawdiff[$i], 1));
7e0abcec
TH
167 my $src_path = $rawdiff[$i + 1];
168 my $dst_path;
169
170 if ($status =~ /^[CR]/) {
171 $dst_path = $rawdiff[$i + 2];
172 $i += 3;
173 } else {
174 $dst_path = $src_path;
175 $i += 2;
176 }
177
a4cd5be3 178 if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
7e0abcec
TH
179 $submodule{$src_path}{left} = $lsha1;
180 if ($lsha1 ne $rsha1) {
181 $submodule{$dst_path}{right} = $rsha1;
182 } else {
183 $submodule{$dst_path}{right} = "$rsha1-dirty";
184 }
185 next;
186 }
187
188 if ($lmode eq $symlink_mode) {
a4cd5be3
DA
189 $symlink{$src_path}{left} =
190 $diffrepo->command_oneline('show', "$lsha1");
7e0abcec
TH
191 }
192
193 if ($rmode eq $symlink_mode) {
a4cd5be3
DA
194 $symlink{$dst_path}{right} =
195 $diffrepo->command_oneline('show', "$rsha1");
7e0abcec
TH
196 }
197
a4cd5be3 198 if ($lmode ne $null_mode and $status !~ /^C/) {
7e0abcec
TH
199 $lindex .= "$lmode $lsha1\t$src_path\0";
200 }
201
202 if ($rmode ne $null_mode) {
203 if ($rsha1 ne $null_sha1) {
204 $rindex .= "$rmode $rsha1\t$dst_path\0";
205 } else {
206 push(@working_tree, $dst_path);
207 }
208 }
209 }
210
ceb1497a
DA
211 # Setup temp directories
212 my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
213 my $ldir = "$tmpdir/left";
214 my $rdir = "$tmpdir/right";
215 mkpath($ldir) or exit_cleanup($tmpdir, 1);
216 mkpath($rdir) or exit_cleanup($tmpdir, 1);
217
7e0abcec
TH
218 # If $GIT_DIR is not set prior to calling 'git update-index' and
219 # 'git checkout-index', then those commands will fail if difftool
220 # is called from a directory other than the repo root.
221 my $must_unset_git_dir = 0;
222 if (not defined($ENV{GIT_DIR})) {
223 $must_unset_git_dir = 1;
224 $ENV{GIT_DIR} = $repo_path;
225 }
226
227 # Populate the left and right directories based on each index file
228 my ($inpipe, $ctx);
229 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
a4cd5be3
DA
230 ($inpipe, $ctx) =
231 $repo->command_input_pipe(qw(update-index -z --index-info));
7e0abcec
TH
232 print($inpipe $lindex);
233 $repo->command_close_pipe($inpipe, $ctx);
ceb1497a 234
75cd7583 235 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
ceb1497a 236 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec
TH
237
238 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
a4cd5be3
DA
239 ($inpipe, $ctx) =
240 $repo->command_input_pipe(qw(update-index -z --index-info));
7e0abcec
TH
241 print($inpipe $rindex);
242 $repo->command_close_pipe($inpipe, $ctx);
ceb1497a 243
7e0abcec 244 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
ceb1497a 245 exit_cleanup($tmpdir, $rc) if $rc != 0;
7e0abcec
TH
246
247 # If $GIT_DIR was explicitly set just for the update/checkout
248 # commands, then it should be unset before continuing.
249 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
250 delete($ENV{GIT_INDEX_FILE});
251
252 # Changes in the working tree need special treatment since they are
253 # not part of the index
254 for my $file (@working_tree) {
255 my $dir = dirname($file);
256 unless (-d "$rdir/$dir") {
ceb1497a
DA
257 mkpath("$rdir/$dir") or
258 exit_cleanup($tmpdir, 1);
7e0abcec 259 }
1f229345 260 if ($symlinks) {
ceb1497a
DA
261 symlink("$workdir/$file", "$rdir/$file") or
262 exit_cleanup($tmpdir, 1);
1f229345 263 } else {
ceb1497a
DA
264 copy("$workdir/$file", "$rdir/$file") or
265 exit_cleanup($tmpdir, 1);
266
1f229345 267 my $mode = stat("$workdir/$file")->mode;
ceb1497a
DA
268 chmod($mode, "$rdir/$file") or
269 exit_cleanup($tmpdir, 1);
1f229345 270 }
7e0abcec
TH
271 }
272
273 # Changes to submodules require special treatment. This loop writes a
274 # temporary file to both the left and right directories to show the
275 # change in the recorded SHA1 for the submodule.
276 for my $path (keys %submodule) {
ceb1497a 277 my $ok;
7e0abcec 278 if (defined($submodule{$path}{left})) {
ceb1497a
DA
279 $ok = write_to_file("$ldir/$path",
280 "Subproject commit $submodule{$path}{left}");
7e0abcec
TH
281 }
282 if (defined($submodule{$path}{right})) {
ceb1497a
DA
283 $ok = write_to_file("$rdir/$path",
284 "Subproject commit $submodule{$path}{right}");
7e0abcec 285 }
ceb1497a 286 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
287 }
288
289 # Symbolic links require special treatment. The standard "git diff"
290 # shows only the link itself, not the contents of the link target.
291 # This loop replicates that behavior.
292 for my $path (keys %symlink) {
ceb1497a 293 my $ok;
7e0abcec 294 if (defined($symlink{$path}{left})) {
ceb1497a
DA
295 $ok = write_to_file("$ldir/$path",
296 $symlink{$path}{left});
7e0abcec
TH
297 }
298 if (defined($symlink{$path}{right})) {
ceb1497a
DA
299 $ok = write_to_file("$rdir/$path",
300 $symlink{$path}{right});
7e0abcec 301 }
ceb1497a 302 exit_cleanup($tmpdir, 1) if not $ok;
7e0abcec
TH
303 }
304
ceb1497a 305 return ($ldir, $rdir, $tmpdir, @working_tree);
7e0abcec
TH
306}
307
308sub write_to_file
309{
310 my $path = shift;
311 my $value = shift;
312
313 # Make sure the path to the file exists
314 my $dir = dirname($path);
315 unless (-d "$dir") {
ceb1497a 316 mkpath("$dir") or return 0;
7e0abcec
TH
317 }
318
319 # If the file already exists in that location, delete it. This
320 # is required in the case of symbolic links.
ceb1497a 321 unlink($path);
7e0abcec 322
ceb1497a 323 open(my $fh, '>', $path) or return 0;
7e0abcec
TH
324 print($fh $value);
325 close($fh);
ceb1497a
DA
326
327 return 1;
7e0abcec
TH
328}
329
75cd7583
DA
330sub main
331{
332 # parse command-line options. all unrecognized options and arguments
333 # are passed through to the 'git diff' command.
c9bdd505
DA
334 my %opts = (
335 difftool_cmd => undef,
336 dirdiff => undef,
337 extcmd => undef,
338 gui => undef,
339 help => undef,
340 prompt => undef,
190f5475
DA
341 symlinks => $^O ne 'cygwin' &&
342 $^O ne 'MSWin32' && $^O ne 'msys',
c9bdd505
DA
343 tool_help => undef,
344 );
345 GetOptions('g|gui!' => \$opts{gui},
346 'd|dir-diff' => \$opts{dirdiff},
347 'h' => \$opts{help},
348 'prompt!' => \$opts{prompt},
349 'y' => sub { $opts{prompt} = 0; },
1f229345
DA
350 'symlinks' => \$opts{symlinks},
351 'no-symlinks' => sub { $opts{symlinks} = 0; },
c9bdd505
DA
352 't|tool:s' => \$opts{difftool_cmd},
353 'tool-help' => \$opts{tool_help},
354 'x|extcmd:s' => \$opts{extcmd});
355
356 if (defined($opts{help})) {
75cd7583 357 usage(0);
5c38ea31 358 }
c9bdd505 359 if (defined($opts{tool_help})) {
75cd7583 360 print_tool_help();
3f94ff75 361 }
c9bdd505
DA
362 if (defined($opts{difftool_cmd})) {
363 if (length($opts{difftool_cmd}) > 0) {
364 $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
75cd7583
DA
365 } else {
366 print "No <tool> given for --tool=<tool>\n";
367 usage(1);
368 }
369 }
c9bdd505
DA
370 if (defined($opts{extcmd})) {
371 if (length($opts{extcmd}) > 0) {
372 $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
75cd7583
DA
373 } else {
374 print "No <cmd> given for --extcmd=<cmd>\n";
375 usage(1);
376 }
377 }
c9bdd505
DA
378 if ($opts{gui}) {
379 my $guitool = Git::config('diff.guitool');
75cd7583
DA
380 if (length($guitool) > 0) {
381 $ENV{GIT_DIFF_TOOL} = $guitool;
382 }
383 }
384
385 # In directory diff mode, 'git-difftool--helper' is called once
386 # to compare the a/b directories. In file diff mode, 'git diff'
387 # will invoke a separate instance of 'git-difftool--helper' for
388 # each file that changed.
c9bdd505 389 if (defined($opts{dirdiff})) {
1f229345 390 dir_diff($opts{extcmd}, $opts{symlinks});
75cd7583 391 } else {
c9bdd505 392 file_diff($opts{prompt});
3f94ff75
TH
393 }
394}
7e0abcec 395
75cd7583
DA
396sub dir_diff
397{
1f229345 398 my ($extcmd, $symlinks) = @_;
75cd7583 399 my $rc;
ceb1497a 400 my $error = 0;
75cd7583 401 my $repo = Git->repository();
75cd7583 402 my $workdir = find_worktree($repo);
ceb1497a
DA
403 my ($a, $b, $tmpdir, @worktree) =
404 setup_dir_diff($repo, $workdir, $symlinks);
405
7e0abcec
TH
406 if (defined($extcmd)) {
407 $rc = system($extcmd, $a, $b);
3f94ff75 408 } else {
7e0abcec
TH
409 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
410 $rc = system('git', 'difftool--helper', $a, $b);
d531174f 411 }
7e0abcec
TH
412 # If the diff including working copy files and those
413 # files were modified during the diff, then the changes
1f229345
DA
414 # should be copied back to the working tree.
415 # Do not copy back files when symlinks are used and the
416 # external tool did not replace the original link with a file.
417 for my $file (@worktree) {
418 next if $symlinks && -l "$b/$file";
283abb2c
DA
419 next if ! -f "$b/$file";
420
421 my $diff = compare("$b/$file", "$workdir/$file");
422 if ($diff == 0) {
423 next;
424 } elsif ($diff == -1) {
425 my $errmsg = "warning: Could not compare ";
426 $errmsg += "'$b/$file' with '$workdir/$file'\n";
427 warn $errmsg;
ceb1497a 428 $error = 1;
283abb2c 429 } elsif ($diff == 1) {
1f229345 430 my $mode = stat("$b/$file")->mode;
ceb1497a
DA
431 copy("$b/$file", "$workdir/$file") or
432 exit_cleanup($tmpdir, 1);
433
434 chmod($mode, "$workdir/$file") or
435 exit_cleanup($tmpdir, 1);
05df5326 436 }
7e0abcec 437 }
ceb1497a
DA
438 if ($error) {
439 warn "warning: Temporary files exist in '$tmpdir'.\n";
440 warn "warning: You may want to cleanup or recover these.\n";
441 exit(1);
442 } else {
443 exit_cleanup($tmpdir, $rc);
444 }
75cd7583
DA
445}
446
447sub file_diff
448{
449 my ($prompt) = @_;
450
7e0abcec
TH
451 if (defined($prompt)) {
452 if ($prompt) {
453 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
454 } else {
455 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
456 }
457 }
458
459 $ENV{GIT_PAGER} = '';
460 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
461
462 # ActiveState Perl for Win32 does not implement POSIX semantics of
463 # exec* system call. It just spawns the given executable and finishes
464 # the starting program, exiting with code 0.
465 # system will at least catch the errors returned by git diff,
466 # allowing the caller of git difftool better handling of failures.
467 my $rc = system('git', 'diff', @ARGV);
468 exit($rc | ($rc >> 8));
469}
75cd7583
DA
470
471main();