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