Skip to content

Commit a0f83e7

Browse files
peffgitster
authored andcommitted
diff: use filespec path to set up tempfiles for ext-diff
When we're going to run an external diff, we have to make the contents of the pre- and post-images available either by dumping them to a tempfile, or by pointing at a valid file in the worktree. The logic of this is all handled by prepare_temp_file(), and we just pass in the filename and the diff_filespec. But there's a gotcha here. The "filename" we have is a logical filename and not necessarily a path on disk or in the repository. This matters in at least one case: when using "--relative", we may have a name like "foo", even though the file content is found at "subdir/foo". As a result, we look for the wrong path, fail to find "foo", and claim that the file has been deleted (passing "/dev/null" to the external diff, rather than the correct worktree path). We can fix this by passing the pathname from the diff_filespec, which should always be a full repository path (and that's what we want even if reusing a worktree file, since we're always operating from the top-level of the working tree). The breakage seems to go all the way back to cd676a5 (diff --relative: output paths as relative to the current subdirectory, 2008-02-12). As far as I can tell, before then "name" would always have been the same as the filespec's "path". There are two related cases I looked at that aren't buggy: 1. the only other caller of prepare_temp_file() is run_textconv(). But it always passes the filespec's path field, so it's OK. 2. I wondered if file renames/copies might cause similar confusion. But they don't, because run_external_diff() receives two names in that case: "name" and "other", which correspond to the two sides of the diff. And we did correctly pass "other" when handling the post-image side. Barring the use of "--relative", that would always match "two->path", the path of the second filespec (and the rename destination). So the only bug is just the interaction with external diff drivers and --relative. Reported-by: Carl Baldwin <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c48035d commit a0f83e7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4281,7 +4281,7 @@ static void add_external_diff_name(struct repository *r,
42814281
const char *name,
42824282
struct diff_filespec *df)
42834283
{
4284-
struct diff_tempfile *temp = prepare_temp_file(r, name, df);
4284+
struct diff_tempfile *temp = prepare_temp_file(r, df->path, df);
42854285
strvec_push(argv, temp->name);
42864286
strvec_push(argv, temp->hex);
42874287
strvec_push(argv, temp->mode);

t/t4045-diff-relative.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ check_diff_relative_option subdir file2 true --no-relative --relative
162162
check_diff_relative_option . file2 false --no-relative --relative=subdir
163163
check_diff_relative_option . file2 true --no-relative --relative=subdir
164164

165+
test_expect_success 'external diff with --relative' '
166+
test_when_finished "git reset --hard" &&
167+
echo changed >file1 &&
168+
echo changed >subdir/file2 &&
169+
170+
write_script mydiff <<-\EOF &&
171+
# hacky pretend diff; the goal here is just to make sure we got
172+
# passed sensible input that we _could_ diff, without relying on
173+
# the specific output of a system diff tool.
174+
echo "diff a/$1 b/$1" &&
175+
echo "--- a/$1" &&
176+
echo "+++ b/$1" &&
177+
echo "@@ -1 +0,0 @@" &&
178+
sed "s/^/-/" "$2" &&
179+
sed "s/^/+/" "$5"
180+
EOF
181+
182+
cat >expect <<-\EOF &&
183+
diff a/file2 b/file2
184+
--- a/file2
185+
+++ b/file2
186+
@@ -1 +0,0 @@
187+
-other content
188+
+changed
189+
EOF
190+
GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual &&
191+
test_cmp expect actual
192+
'
193+
165194
test_expect_success 'setup diff --relative unmerged' '
166195
test_commit zero file0 &&
167196
test_commit base subdir/file0 &&

0 commit comments

Comments
 (0)