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