Skip to content

Commit f242a03

Browse files
davvidgitster
authored andcommitted
difftool: chdir as early as possible
Make difftool chdir to the top-level of the repository as soon as it can so that we can simplify how paths are handled. Replace construction of absolute paths via string concatenation with relative paths wherever possible. The bulk of the code no longer needs to use absolute paths. Signed-off-by: David Aguilar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6e3e2a commit f242a03

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

git-difftool.perl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ sub exit_cleanup
5959

6060
sub use_wt_file
6161
{
62-
my ($workdir, $file, $sha1) = @_;
62+
my ($file, $sha1) = @_;
6363
my $null_sha1 = '0' x 40;
6464

65-
if (-l "$workdir/$file" || ! -e _) {
65+
if (-l $file || ! -e _) {
6666
return (0, $null_sha1);
6767
}
6868

69-
my $wt_sha1 = Git::command_oneline('hash-object', "$workdir/$file");
69+
my $wt_sha1 = Git::command_oneline('hash-object', $file);
7070
my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
7171
return ($use, $wt_sha1);
7272
}
@@ -105,6 +105,12 @@ sub setup_dir_diff
105105
my $diffrtn = Git::command_oneline(@gitargs);
106106
exit(0) unless defined($diffrtn);
107107

108+
# Go to the root of the worktree now that we've captured the list of
109+
# changed files. The paths returned by diff --raw are relative to the
110+
# top-level of the repository, but we defer changing directories so
111+
# that @ARGV can perform pathspec limiting in the current directory.
112+
chdir($workdir);
113+
108114
# Build index info for left and right sides of the diff
109115
my $submodule_mode = '160000';
110116
my $symlink_mode = '120000';
@@ -172,7 +178,7 @@ sub setup_dir_diff
172178
next;
173179
}
174180
my ($use, $wt_sha1) =
175-
use_wt_file($workdir, $dst_path, $rsha1);
181+
use_wt_file($dst_path, $rsha1);
176182
if ($use) {
177183
push @working_tree, $dst_path;
178184
$wtindex .= "$rmode $wt_sha1\t$dst_path\0";
@@ -182,10 +188,6 @@ sub setup_dir_diff
182188
}
183189
}
184190

185-
# Go to the root of the worktree so that the left index files
186-
# are properly setup -- the index is toplevel-relative.
187-
chdir($workdir);
188-
189191
# Setup temp directories
190192
my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
191193
my $ldir = "$tmpdir/left";
@@ -235,10 +237,10 @@ sub setup_dir_diff
235237
symlink("$workdir/$file", "$rdir/$file") or
236238
exit_cleanup($tmpdir, 1);
237239
} else {
238-
copy("$workdir/$file", "$rdir/$file") or
240+
copy($file, "$rdir/$file") or
239241
exit_cleanup($tmpdir, 1);
240242

241-
my $mode = stat("$workdir/$file")->mode;
243+
my $mode = stat($file)->mode;
242244
chmod($mode, "$rdir/$file") or
243245
exit_cleanup($tmpdir, 1);
244246
}
@@ -430,10 +432,10 @@ sub dir_diff
430432
$error = 1;
431433
} elsif (exists $tmp_modified{$file}) {
432434
my $mode = stat("$b/$file")->mode;
433-
copy("$b/$file", "$workdir/$file") or
435+
copy("$b/$file", $file) or
434436
exit_cleanup($tmpdir, 1);
435437

436-
chmod($mode, "$workdir/$file") or
438+
chmod($mode, $file) or
437439
exit_cleanup($tmpdir, 1);
438440
}
439441
}

0 commit comments

Comments
 (0)