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