diff options
Diffstat (limited to 'git-difftool.perl')
-rwxr-xr-x | git-difftool.perl | 135 |
1 files changed, 52 insertions, 83 deletions
diff --git a/git-difftool.perl b/git-difftool.perl index e57d3d1295..959822d5f3 100755 --- a/git-difftool.perl +++ b/git-difftool.perl @@ -37,37 +37,11 @@ USAGE exit($exitcode); } -sub find_worktree -{ - my ($repo) = @_; - - # Git->repository->wc_path() does not honor changes to the working - # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree' - # config variable. - my $worktree; - my $env_worktree = $ENV{GIT_WORK_TREE}; - my $core_worktree = Git::config('core.worktree'); - - if (defined($env_worktree) and (length($env_worktree) > 0)) { - $worktree = $env_worktree; - } elsif (defined($core_worktree) and (length($core_worktree) > 0)) { - $worktree = $core_worktree; - } else { - $worktree = $repo->wc_path(); - } - - return $worktree; -} - sub print_tool_help { - my $cmd = 'TOOL_MODE=diff'; - $cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"'; - $cmd .= ' && show_tool_help'; - # See the comment at the bottom of file_diff() for the reason behind # using system() followed by exit() instead of exec(). - my $rc = system('sh', '-c', $cmd); + my $rc = system(qw(git mergetool --tool-help=diff)); exit($rc | ($rc >> 8)); } @@ -85,16 +59,14 @@ sub exit_cleanup sub use_wt_file { - my ($repo, $workdir, $file, $sha1) = @_; + my ($workdir, $file, $sha1) = @_; my $null_sha1 = '0' x 40; - if (! -e "$workdir/$file") { - # If the file doesn't exist in the working tree, we cannot - # use it. + if (-l "$workdir/$file" || ! -e _) { return (0, $null_sha1); } - my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file"); + my $wt_sha1 = Git::command_oneline('hash-object', "$workdir/$file"); my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1); return ($use, $wt_sha1); } @@ -103,20 +75,17 @@ sub changed_files { my ($repo_path, $index, $worktree) = @_; $ENV{GIT_INDEX_FILE} = $index; - $ENV{GIT_WORK_TREE} = $worktree; - my $must_unset_git_dir = 0; - if (not defined($ENV{GIT_DIR})) { - $must_unset_git_dir = 1; - $ENV{GIT_DIR} = $repo_path; - } - my @refreshargs = qw/update-index --really-refresh -q --unmerged/; - my @gitargs = qw/diff-files --name-only -z/; + my @gitargs = ('--git-dir', $repo_path, '--work-tree', $worktree); + my @refreshargs = ( + @gitargs, 'update-index', + '--really-refresh', '-q', '--unmerged'); try { Git::command_oneline(@refreshargs); } catch Git::Error::Command with {}; - my $line = Git::command_oneline(@gitargs); + my @diffargs = (@gitargs, 'diff-files', '--name-only', '-z'); + my $line = Git::command_oneline(@diffargs); my @files; if (defined $line) { @files = split('\0', $line); @@ -125,26 +94,15 @@ sub changed_files } delete($ENV{GIT_INDEX_FILE}); - delete($ENV{GIT_WORK_TREE}); - delete($ENV{GIT_DIR}) if ($must_unset_git_dir); return map { $_ => 1 } @files; } sub setup_dir_diff { - my ($repo, $workdir, $symlinks) = @_; - - # Run the diff; exit immediately if no diff found - # 'Repository' and 'WorkingCopy' must be explicitly set to insure that - # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used - # by Git->repository->command*. - my $repo_path = $repo->repo_path(); - my %repo_args = (Repository => $repo_path, WorkingCopy => $workdir); - my $diffrepo = Git->repository(%repo_args); - + my ($workdir, $symlinks) = @_; my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV); - my $diffrtn = $diffrepo->command_oneline(@gitargs); + my $diffrtn = Git::command_oneline(@gitargs); exit(0) unless defined($diffrtn); # Build index info for left and right sides of the diff @@ -158,6 +116,7 @@ sub setup_dir_diff my %submodule; my %symlink; my @working_tree = (); + my %working_tree_dups = (); my @rawdiff = split('\0', $diffrtn); my $i = 0; @@ -195,12 +154,12 @@ EOF if ($lmode eq $symlink_mode) { $symlink{$src_path}{left} = - $diffrepo->command_oneline('show', "$lsha1"); + Git::command_oneline('show', $lsha1); } if ($rmode eq $symlink_mode) { $symlink{$dst_path}{right} = - $diffrepo->command_oneline('show', "$rsha1"); + Git::command_oneline('show', $rsha1); } if ($lmode ne $null_mode and $status !~ /^C/) { @@ -208,8 +167,12 @@ EOF } if ($rmode ne $null_mode) { - my ($use, $wt_sha1) = use_wt_file($repo, $workdir, - $dst_path, $rsha1); + # Avoid duplicate working_tree entries + if ($working_tree_dups{$dst_path}++) { + next; + } + my ($use, $wt_sha1) = + use_wt_file($workdir, $dst_path, $rsha1); if ($use) { push @working_tree, $dst_path; $wtindex .= "$rmode $wt_sha1\t$dst_path\0"; @@ -219,6 +182,10 @@ EOF } } + # Go to the root of the worktree so that the left index files + # are properly setup -- the index is toplevel-relative. + chdir($workdir); + # Setup temp directories my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1); my $ldir = "$tmpdir/left"; @@ -226,44 +193,34 @@ EOF mkpath($ldir) or exit_cleanup($tmpdir, 1); mkpath($rdir) or exit_cleanup($tmpdir, 1); - # If $GIT_DIR is not set prior to calling 'git update-index' and - # 'git checkout-index', then those commands will fail if difftool - # is called from a directory other than the repo root. - my $must_unset_git_dir = 0; - if (not defined($ENV{GIT_DIR})) { - $must_unset_git_dir = 1; - $ENV{GIT_DIR} = $repo_path; - } - # Populate the left and right directories based on each index file my ($inpipe, $ctx); $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex"; ($inpipe, $ctx) = - $repo->command_input_pipe(qw(update-index -z --index-info)); + Git::command_input_pipe('update-index', '-z', '--index-info'); print($inpipe $lindex); - $repo->command_close_pipe($inpipe, $ctx); + Git::command_close_pipe($inpipe, $ctx); my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/"); exit_cleanup($tmpdir, $rc) if $rc != 0; $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex"; ($inpipe, $ctx) = - $repo->command_input_pipe(qw(update-index -z --index-info)); + Git::command_input_pipe('update-index', '-z', '--index-info'); print($inpipe $rindex); - $repo->command_close_pipe($inpipe, $ctx); + Git::command_close_pipe($inpipe, $ctx); $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/"); exit_cleanup($tmpdir, $rc) if $rc != 0; $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex"; ($inpipe, $ctx) = - $repo->command_input_pipe(qw(update-index --info-only -z --index-info)); + Git::command_input_pipe('update-index', '--info-only', '-z', '--index-info'); print($inpipe $wtindex); - $repo->command_close_pipe($inpipe, $ctx); + Git::command_close_pipe($inpipe, $ctx); # If $GIT_DIR was explicitly set just for the update/checkout # commands, then it should be unset before continuing. - delete($ENV{GIT_DIR}) if ($must_unset_git_dir); delete($ENV{GIT_INDEX_FILE}); # Changes in the working tree need special treatment since they are @@ -293,7 +250,7 @@ EOF # temporary file to both the left and right directories to show the # change in the recorded SHA1 for the submodule. for my $path (keys %submodule) { - my $ok; + my $ok = 0; if (defined($submodule{$path}{left})) { $ok = write_to_file("$ldir/$path", "Subproject commit $submodule{$path}{left}"); @@ -309,7 +266,7 @@ EOF # shows only the link itself, not the contents of the link target. # This loop replicates that behavior. for my $path (keys %symlink) { - my $ok; + my $ok = 0; if (defined($symlink{$path}{left})) { $ok = write_to_file("$ldir/$path", $symlink{$path}{left}); @@ -360,6 +317,7 @@ sub main symlinks => $^O ne 'cygwin' && $^O ne 'MSWin32' && $^O ne 'msys', tool_help => undef, + trust_exit_code => undef, ); GetOptions('g|gui!' => \$opts{gui}, 'd|dir-diff' => \$opts{dirdiff}, @@ -370,6 +328,8 @@ sub main 'no-symlinks' => sub { $opts{symlinks} = 0; }, 't|tool:s' => \$opts{difftool_cmd}, 'tool-help' => \$opts{tool_help}, + 'trust-exit-code' => \$opts{trust_exit_code}, + 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; }, 'x|extcmd:s' => \$opts{extcmd}); if (defined($opts{help})) { @@ -401,6 +361,15 @@ sub main } } + if (!defined $opts{trust_exit_code}) { + $opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode'); + } + if ($opts{trust_exit_code}) { + $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true'; + } else { + $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false'; + } + # In directory diff mode, 'git-difftool--helper' is called once # to compare the a/b directories. In file diff mode, 'git diff' # will invoke a separate instance of 'git-difftool--helper' for @@ -418,9 +387,9 @@ sub dir_diff my $rc; my $error = 0; my $repo = Git->repository(); - my $workdir = find_worktree($repo); - my ($a, $b, $tmpdir, @worktree) = - setup_dir_diff($repo, $workdir, $symlinks); + my $repo_path = $repo->repo_path(); + my $workdir = $repo->wc_path(); + my ($a, $b, $tmpdir, @worktree) = setup_dir_diff($workdir, $symlinks); if (defined($extcmd)) { $rc = system($extcmd, $a, $b); @@ -446,10 +415,10 @@ sub dir_diff next if ! -f "$b/$file"; if (!$indices_loaded) { - %wt_modified = changed_files($repo->repo_path(), - "$tmpdir/wtindex", "$workdir"); - %tmp_modified = changed_files($repo->repo_path(), - "$tmpdir/wtindex", "$b"); + %wt_modified = changed_files( + $repo_path, "$tmpdir/wtindex", $workdir); + %tmp_modified = changed_files( + $repo_path, "$tmpdir/wtindex", $b); $indices_loaded = 1; } |