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