t9902: add a few basic completion tests
[git/jrn.git] / git-difftool.perl
blobc0798540adee04cb7cfb6fd04ef3541a1fcbb762
1 #!/usr/bin/perl
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'.
13 use 5.008;
14 use strict;
15 use warnings;
16 use File::Basename qw(dirname);
17 use File::Copy;
18 use File::Compare;
19 use File::Find;
20 use File::stat;
21 use File::Path qw(mkpath);
22 use File::Temp qw(tempdir);
23 use Getopt::Long qw(:config pass_through);
24 use Git;
26 my @tools;
27 my @working_tree;
28 my $rc;
29 my $repo = Git->repository();
30 my $repo_path = $repo->repo_path();
32 sub usage
34 my $exitcode = shift;
35 print << 'USAGE';
36 usage: git difftool [-t|--tool=<tool>] [--tool-help]
37 [-x|--extcmd=<cmd>]
38 [-g|--gui] [--no-gui]
39 [--prompt] [-y|--no-prompt]
40 [-d|--dir-diff]
41 ['git diff' options]
42 USAGE
43 exit($exitcode);
46 sub find_worktree
48 # Git->repository->wc_path() does not honor changes to the working
49 # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
50 # config variable.
51 my $worktree;
52 my $env_worktree = $ENV{GIT_WORK_TREE};
53 my $core_worktree = Git::config('core.worktree');
55 if (defined($env_worktree) and (length($env_worktree) > 0)) {
56 $worktree = $env_worktree;
57 } elsif (defined($core_worktree) and (length($core_worktree) > 0)) {
58 $worktree = $core_worktree;
59 } else {
60 $worktree = $repo->wc_path();
63 return $worktree;
66 my $workdir = find_worktree();
68 sub filter_tool_scripts
70 if (-d $_) {
71 if ($_ ne ".") {
72 # Ignore files in subdirectories
73 $File::Find::prune = 1;
75 } else {
76 if ((-f $_) && ($_ ne "defaults")) {
77 push(@tools, $_);
82 sub print_tool_help
84 my ($cmd, @found, @notfound);
85 my $gitpath = Git::exec_path();
87 find(\&filter_tool_scripts, "$gitpath/mergetools");
89 foreach my $tool (@tools) {
90 $cmd = "TOOL_MODE=diff";
91 $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
92 $cmd .= " && get_merge_tool_path $tool >/dev/null 2>&1";
93 $cmd .= " && can_diff >/dev/null 2>&1";
94 if (system('sh', '-c', $cmd) == 0) {
95 push(@found, $tool);
96 } else {
97 push(@notfound, $tool);
101 print "'git difftool --tool=<tool>' may be set to one of the following:\n";
102 print "\t$_\n" for (sort(@found));
104 print "\nThe following tools are valid, but not currently available:\n";
105 print "\t$_\n" for (sort(@notfound));
107 print "\nNOTE: Some of the tools listed above only work in a windowed\n";
108 print "environment. If run in a terminal-only session, they will fail.\n";
110 exit(0);
113 sub setup_dir_diff
115 # Run the diff; exit immediately if no diff found
116 # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
117 # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
118 # by Git->repository->command*.
119 my $diffrepo = Git->repository(Repository => $repo_path, WorkingCopy => $workdir);
120 my $diffrtn = $diffrepo->command_oneline('diff', '--raw', '--no-abbrev', '-z', @ARGV);
121 exit(0) if (length($diffrtn) == 0);
123 # Setup temp directories
124 my $tmpdir = tempdir('git-diffall.XXXXX', CLEANUP => 1, TMPDIR => 1);
125 my $ldir = "$tmpdir/left";
126 my $rdir = "$tmpdir/right";
127 mkpath($ldir) or die $!;
128 mkpath($rdir) or die $!;
130 # Build index info for left and right sides of the diff
131 my $submodule_mode = '160000';
132 my $symlink_mode = '120000';
133 my $null_mode = '0' x 6;
134 my $null_sha1 = '0' x 40;
135 my $lindex = '';
136 my $rindex = '';
137 my %submodule;
138 my %symlink;
139 my @rawdiff = split('\0', $diffrtn);
141 my $i = 0;
142 while ($i < $#rawdiff) {
143 if ($rawdiff[$i] =~ /^::/) {
144 print "Combined diff formats ('-c' and '--cc') are not supported in directory diff mode.\n";
145 exit(1);
148 my ($lmode, $rmode, $lsha1, $rsha1, $status) = split(' ', substr($rawdiff[$i], 1));
149 my $src_path = $rawdiff[$i + 1];
150 my $dst_path;
152 if ($status =~ /^[CR]/) {
153 $dst_path = $rawdiff[$i + 2];
154 $i += 3;
155 } else {
156 $dst_path = $src_path;
157 $i += 2;
160 if (($lmode eq $submodule_mode) or ($rmode eq $submodule_mode)) {
161 $submodule{$src_path}{left} = $lsha1;
162 if ($lsha1 ne $rsha1) {
163 $submodule{$dst_path}{right} = $rsha1;
164 } else {
165 $submodule{$dst_path}{right} = "$rsha1-dirty";
167 next;
170 if ($lmode eq $symlink_mode) {
171 $symlink{$src_path}{left} = $diffrepo->command_oneline('show', "$lsha1");
174 if ($rmode eq $symlink_mode) {
175 $symlink{$dst_path}{right} = $diffrepo->command_oneline('show', "$rsha1");
178 if (($lmode ne $null_mode) and ($status !~ /^C/)) {
179 $lindex .= "$lmode $lsha1\t$src_path\0";
182 if ($rmode ne $null_mode) {
183 if ($rsha1 ne $null_sha1) {
184 $rindex .= "$rmode $rsha1\t$dst_path\0";
185 } else {
186 push(@working_tree, $dst_path);
191 # If $GIT_DIR is not set prior to calling 'git update-index' and
192 # 'git checkout-index', then those commands will fail if difftool
193 # is called from a directory other than the repo root.
194 my $must_unset_git_dir = 0;
195 if (not defined($ENV{GIT_DIR})) {
196 $must_unset_git_dir = 1;
197 $ENV{GIT_DIR} = $repo_path;
200 # Populate the left and right directories based on each index file
201 my ($inpipe, $ctx);
202 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
203 ($inpipe, $ctx) = $repo->command_input_pipe(qw/update-index -z --index-info/);
204 print($inpipe $lindex);
205 $repo->command_close_pipe($inpipe, $ctx);
206 $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
207 exit($rc | ($rc >> 8)) if ($rc != 0);
209 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
210 ($inpipe, $ctx) = $repo->command_input_pipe(qw/update-index -z --index-info/);
211 print($inpipe $rindex);
212 $repo->command_close_pipe($inpipe, $ctx);
213 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
214 exit($rc | ($rc >> 8)) if ($rc != 0);
216 # If $GIT_DIR was explicitly set just for the update/checkout
217 # commands, then it should be unset before continuing.
218 delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
219 delete($ENV{GIT_INDEX_FILE});
221 # Changes in the working tree need special treatment since they are
222 # not part of the index
223 for my $file (@working_tree) {
224 my $dir = dirname($file);
225 unless (-d "$rdir/$dir") {
226 mkpath("$rdir/$dir") or die $!;
228 copy("$workdir/$file", "$rdir/$file") or die $!;
229 chmod(stat("$workdir/$file")->mode, "$rdir/$file") or die $!;
232 # Changes to submodules require special treatment. This loop writes a
233 # temporary file to both the left and right directories to show the
234 # change in the recorded SHA1 for the submodule.
235 for my $path (keys %submodule) {
236 if (defined($submodule{$path}{left})) {
237 write_to_file("$ldir/$path", "Subproject commit $submodule{$path}{left}");
239 if (defined($submodule{$path}{right})) {
240 write_to_file("$rdir/$path", "Subproject commit $submodule{$path}{right}");
244 # Symbolic links require special treatment. The standard "git diff"
245 # shows only the link itself, not the contents of the link target.
246 # This loop replicates that behavior.
247 for my $path (keys %symlink) {
248 if (defined($symlink{$path}{left})) {
249 write_to_file("$ldir/$path", $symlink{$path}{left});
251 if (defined($symlink{$path}{right})) {
252 write_to_file("$rdir/$path", $symlink{$path}{right});
256 return ($ldir, $rdir);
259 sub write_to_file
261 my $path = shift;
262 my $value = shift;
264 # Make sure the path to the file exists
265 my $dir = dirname($path);
266 unless (-d "$dir") {
267 mkpath("$dir") or die $!;
270 # If the file already exists in that location, delete it. This
271 # is required in the case of symbolic links.
272 unlink("$path");
274 open(my $fh, '>', "$path") or die $!;
275 print($fh $value);
276 close($fh);
279 # parse command-line options. all unrecognized options and arguments
280 # are passed through to the 'git diff' command.
281 my ($difftool_cmd, $dirdiff, $extcmd, $gui, $help, $prompt, $tool_help);
282 GetOptions('g|gui!' => \$gui,
283 'd|dir-diff' => \$dirdiff,
284 'h' => \$help,
285 'prompt!' => \$prompt,
286 'y' => sub { $prompt = 0; },
287 't|tool:s' => \$difftool_cmd,
288 'tool-help' => \$tool_help,
289 'x|extcmd:s' => \$extcmd);
291 if (defined($help)) {
292 usage(0);
294 if (defined($tool_help)) {
295 print_tool_help();
297 if (defined($difftool_cmd)) {
298 if (length($difftool_cmd) > 0) {
299 $ENV{GIT_DIFF_TOOL} = $difftool_cmd;
300 } else {
301 print "No <tool> given for --tool=<tool>\n";
302 usage(1);
305 if (defined($extcmd)) {
306 if (length($extcmd) > 0) {
307 $ENV{GIT_DIFFTOOL_EXTCMD} = $extcmd;
308 } else {
309 print "No <cmd> given for --extcmd=<cmd>\n";
310 usage(1);
313 if ($gui) {
314 my $guitool = '';
315 $guitool = Git::config('diff.guitool');
316 if (length($guitool) > 0) {
317 $ENV{GIT_DIFF_TOOL} = $guitool;
321 # In directory diff mode, 'git-difftool--helper' is called once
322 # to compare the a/b directories. In file diff mode, 'git diff'
323 # will invoke a separate instance of 'git-difftool--helper' for
324 # each file that changed.
325 if (defined($dirdiff)) {
326 my ($a, $b) = setup_dir_diff();
327 if (defined($extcmd)) {
328 $rc = system($extcmd, $a, $b);
329 } else {
330 $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
331 $rc = system('git', 'difftool--helper', $a, $b);
334 exit($rc | ($rc >> 8)) if ($rc != 0);
336 # If the diff including working copy files and those
337 # files were modified during the diff, then the changes
338 # should be copied back to the working tree
339 for my $file (@working_tree) {
340 if (-e "$b/$file" && compare("$b/$file", "$workdir/$file")) {
341 copy("$b/$file", "$workdir/$file") or die $!;
342 chmod(stat("$b/$file")->mode, "$workdir/$file") or die $!;
345 } else {
346 if (defined($prompt)) {
347 if ($prompt) {
348 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
349 } else {
350 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
354 $ENV{GIT_PAGER} = '';
355 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
357 # ActiveState Perl for Win32 does not implement POSIX semantics of
358 # exec* system call. It just spawns the given executable and finishes
359 # the starting program, exiting with code 0.
360 # system will at least catch the errors returned by git diff,
361 # allowing the caller of git difftool better handling of failures.
362 my $rc = system('git', 'diff', @ARGV);
363 exit($rc | ($rc >> 8));