Merge branch 'mm/gc-safety-doc'
[gitweb.git] / git-difftool.perl
index 99b03949bf32bb7b0797303680d09fc197cf2cc8..df59bdfe97786b5d9f48777cfad80545ff6bbdc2 100755 (executable)
@@ -22,6 +22,7 @@
 use File::Temp qw(tempdir);
 use Getopt::Long qw(:config pass_through);
 use Git;
+use Git::I18N;
 
 sub usage
 {
@@ -100,7 +101,7 @@ sub changed_files
 
 sub setup_dir_diff
 {
-       my ($workdir, $symlinks) = @_;
+       my ($worktree, $symlinks) = @_;
        my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
        my $diffrtn = Git::command_oneline(@gitargs);
        exit(0) unless defined($diffrtn);
@@ -109,7 +110,7 @@ sub setup_dir_diff
        # changed files.  The paths returned by diff --raw are relative to the
        # top-level of the repository, but we defer changing directories so
        # that @ARGV can perform pathspec limiting in the current directory.
-       chdir($workdir);
+       chdir($worktree);
 
        # Build index info for left and right sides of the diff
        my $submodule_mode = '160000';
@@ -121,14 +122,14 @@ sub setup_dir_diff
        my $wtindex = '';
        my %submodule;
        my %symlink;
-       my @working_tree = ();
+       my @files = ();
        my %working_tree_dups = ();
        my @rawdiff = split('\0', $diffrtn);
 
        my $i = 0;
        while ($i < $#rawdiff) {
                if ($rawdiff[$i] =~ /^::/) {
-                       warn << 'EOF';
+                       warn __ <<'EOF';
 Combined diff formats ('-c' and '--cc') are not supported in
 directory diff mode ('-d' and '--dir-diff').
 EOF
@@ -173,14 +174,14 @@ sub setup_dir_diff
                }
 
                if ($rmode ne $null_mode) {
-                       # Avoid duplicate working_tree entries
+                       # Avoid duplicate entries
                        if ($working_tree_dups{$dst_path}++) {
                                next;
                        }
                        my ($use, $wt_sha1) =
                                use_wt_file($dst_path, $rsha1);
                        if ($use) {
-                               push @working_tree, $dst_path;
+                               push @files, $dst_path;
                                $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
                        } else {
                                $rindex .= "$rmode $rsha1\t$dst_path\0";
@@ -188,6 +189,10 @@ sub setup_dir_diff
                }
        }
 
+       # Go to the root of the worktree so that the left index files
+       # are properly setup -- the index is toplevel-relative.
+       chdir($worktree);
+
        # Setup temp directories
        my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
        my $ldir = "$tmpdir/left";
@@ -227,14 +232,14 @@ sub setup_dir_diff
 
        # Changes in the working tree need special treatment since they are
        # not part of the index.
-       for my $file (@working_tree) {
+       for my $file (@files) {
                my $dir = dirname($file);
                unless (-d "$rdir/$dir") {
                        mkpath("$rdir/$dir") or
                        exit_cleanup($tmpdir, 1);
                }
                if ($symlinks) {
-                       symlink("$workdir/$file", "$rdir/$file") or
+                       symlink("$worktree/$file", "$rdir/$file") or
                        exit_cleanup($tmpdir, 1);
                } else {
                        copy($file, "$rdir/$file") or
@@ -278,7 +283,7 @@ sub setup_dir_diff
                exit_cleanup($tmpdir, 1) if not $ok;
        }
 
-       return ($ldir, $rdir, $tmpdir, @working_tree);
+       return ($ldir, $rdir, $tmpdir, @files);
 }
 
 sub write_to_file
@@ -342,7 +347,7 @@ sub main
                if (length($opts{difftool_cmd}) > 0) {
                        $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
                } else {
-                       print "No <tool> given for --tool=<tool>\n";
+                       print __("No <tool> given for --tool=<tool>\n");
                        usage(1);
                }
        }
@@ -350,7 +355,7 @@ sub main
                if (length($opts{extcmd}) > 0) {
                        $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
                } else {
-                       print "No <cmd> given for --extcmd=<cmd>\n";
+                       print __("No <cmd> given for --extcmd=<cmd>\n");
                        usage(1);
                }
        }
@@ -388,9 +393,9 @@ sub dir_diff
        my $error = 0;
        my $repo = Git->repository();
        my $repo_path = $repo->repo_path();
-       my $workdir = $repo->wc_path();
-       $workdir =~ s|/$||; # Avoid double slashes in symlink targets
-       my ($a, $b, $tmpdir, @worktree) = setup_dir_diff($workdir, $symlinks);
+       my $worktree = $repo->wc_path();
+       $worktree =~ s|/$||; # Avoid double slashes in symlink targets
+       my ($a, $b, $tmpdir, @files) = setup_dir_diff($worktree, $symlinks);
 
        if (defined($extcmd)) {
                $rc = system($extcmd, $a, $b);
@@ -411,24 +416,24 @@ sub dir_diff
        my %tmp_modified;
        my $indices_loaded = 0;
 
-       for my $file (@worktree) {
+       for my $file (@files) {
                next if $symlinks && -l "$b/$file";
                next if ! -f "$b/$file";
 
                if (!$indices_loaded) {
                        %wt_modified = changed_files(
-                               $repo_path, "$tmpdir/wtindex", $workdir);
+                               $repo_path, "$tmpdir/wtindex", $worktree);
                        %tmp_modified = changed_files(
                                $repo_path, "$tmpdir/wtindex", $b);
                        $indices_loaded = 1;
                }
 
                if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
-                       my $errmsg = "warning: Both files modified: ";
-                       $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
-                       $errmsg .= "warning: Working tree file has been left.\n";
-                       $errmsg .= "warning:\n";
-                       warn $errmsg;
+                       warn sprintf(__(
+                               "warning: Both files modified:\n" .
+                               "'%s/%s' and '%s/%s'.\n" .
+                               "warning: Working tree file has been left.\n" .
+                               "warning:\n"), $worktree, $file, $b, $file);
                        $error = 1;
                } elsif (exists $tmp_modified{$file}) {
                        my $mode = stat("$b/$file")->mode;
@@ -440,8 +445,9 @@ sub dir_diff
                }
        }
        if ($error) {
-               warn "warning: Temporary files exist in '$tmpdir'.\n";
-               warn "warning: You may want to cleanup or recover these.\n";
+               warn sprintf(__(
+                       "warning: Temporary files exist in '%s'.\n" .
+                       "warning: You may want to cleanup or recover these.\n"), $tmpdir);
                exit(1);
        } else {
                exit_cleanup($tmpdir, $rc);