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