From a0f83e777660dbf7d9526c05d94fc920e459aed9 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Jan 2023 06:03:57 -0500 Subject: [PATCH 1/3] 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 cd676a5136 (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 Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff.c | 2 +- t/t4045-diff-relative.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index 1054a4b732..c78ea27033 100644 --- a/diff.c +++ b/diff.c @@ -4281,7 +4281,7 @@ static void add_external_diff_name(struct repository *r, const char *name, struct diff_filespec *df) { - struct diff_tempfile *temp = prepare_temp_file(r, name, df); + struct diff_tempfile *temp = prepare_temp_file(r, df->path, df); strvec_push(argv, temp->name); strvec_push(argv, temp->hex); strvec_push(argv, temp->mode); diff --git a/t/t4045-diff-relative.sh b/t/t4045-diff-relative.sh index fab351b48a..5e1bbb57e7 100755 --- a/t/t4045-diff-relative.sh +++ b/t/t4045-diff-relative.sh @@ -162,6 +162,35 @@ check_diff_relative_option subdir file2 true --no-relative --relative check_diff_relative_option . file2 false --no-relative --relative=subdir check_diff_relative_option . file2 true --no-relative --relative=subdir +test_expect_success 'external diff with --relative' ' + test_when_finished "git reset --hard" && + echo changed >file1 && + echo changed >subdir/file2 && + + write_script mydiff <<-\EOF && + # hacky pretend diff; the goal here is just to make sure we got + # passed sensible input that we _could_ diff, without relying on + # the specific output of a system diff tool. + echo "diff a/$1 b/$1" && + echo "--- a/$1" && + echo "+++ b/$1" && + echo "@@ -1 +0,0 @@" && + sed "s/^/-/" "$2" && + sed "s/^/+/" "$5" + EOF + + cat >expect <<-\EOF && + diff a/file2 b/file2 + --- a/file2 + +++ b/file2 + @@ -1 +0,0 @@ + -other content + +changed + EOF + GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual && + test_cmp expect actual +' + test_expect_success 'setup diff --relative unmerged' ' test_commit zero file0 && test_commit base subdir/file0 && From de8f14e1c01451e751da80fb6309bf4a371b5b2b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Jan 2023 06:04:18 -0500 Subject: [PATCH 2/3] diff: clean up external-diff argv setup Since the previous commit, setting up the tempfile for an external diff uses df->path from the diff_filespec, rather than the logical name. This means add_external_diff_name() does not need to take a "name" parameter at all, and we can drop it. And that in turn lets us simplify the conditional for handling renames (when the "other" name is non-NULL). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/diff.c b/diff.c index c78ea27033..330f090f6a 100644 --- a/diff.c +++ b/diff.c @@ -4278,7 +4278,6 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r, static void add_external_diff_name(struct repository *r, struct strvec *argv, - const char *name, struct diff_filespec *df) { struct diff_tempfile *temp = prepare_temp_file(r, df->path, df); @@ -4308,11 +4307,9 @@ static void run_external_diff(const char *pgm, strvec_push(&cmd.args, name); if (one && two) { - add_external_diff_name(o->repo, &cmd.args, name, one); - if (!other) - add_external_diff_name(o->repo, &cmd.args, name, two); - else { - add_external_diff_name(o->repo, &cmd.args, other, two); + add_external_diff_name(o->repo, &cmd.args, one); + add_external_diff_name(o->repo, &cmd.args, two); + if (other) { strvec_push(&cmd.args, other); strvec_push(&cmd.args, xfrm_msg); } From f034bb1cad862a678030cdf0ae833636f9d7dbca Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Jan 2023 06:05:00 -0500 Subject: [PATCH 3/3] diff: drop "name" parameter from prepare_temp_file() The prepare_temp_file() function takes a diff_filespec as well as a filename. But it is almost certainly an error to pass in a name that isn't the filespec's "path" parameter, since that is the only thing that reliably tells us how to find the content (and indeed, this was the source of a recently-fixed bug). So let's drop the redundant "name" parameter and just use one->path throughout the function. This simplifies the interface a little bit, and makes it impossible for calling code to get it wrong. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/diff.c b/diff.c index 330f090f6a..3117fce39b 100644 --- a/diff.c +++ b/diff.c @@ -4213,7 +4213,6 @@ static void prep_temp_blob(struct index_state *istate, } static struct diff_tempfile *prepare_temp_file(struct repository *r, - const char *name, struct diff_filespec *one) { struct diff_tempfile *temp = claim_diff_tempfile(); @@ -4231,18 +4230,18 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r, if (!S_ISGITLINK(one->mode) && (!one->oid_valid || - reuse_worktree_file(r->index, name, &one->oid, 1))) { + reuse_worktree_file(r->index, one->path, &one->oid, 1))) { struct stat st; - if (lstat(name, &st) < 0) { + if (lstat(one->path, &st) < 0) { if (errno == ENOENT) goto not_a_valid_file; - die_errno("stat(%s)", name); + die_errno("stat(%s)", one->path); } if (S_ISLNK(st.st_mode)) { struct strbuf sb = STRBUF_INIT; - if (strbuf_readlink(&sb, name, st.st_size) < 0) - die_errno("readlink(%s)", name); - prep_temp_blob(r->index, name, temp, sb.buf, sb.len, + if (strbuf_readlink(&sb, one->path, st.st_size) < 0) + die_errno("readlink(%s)", one->path); + prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len, (one->oid_valid ? &one->oid : null_oid()), (one->oid_valid ? @@ -4251,7 +4250,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r, } else { /* we can borrow from the file in the work tree */ - temp->name = name; + temp->name = one->path; if (!one->oid_valid) oid_to_hex_r(temp->hex, null_oid()); else @@ -4269,7 +4268,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r, else { if (diff_populate_filespec(r, one, NULL)) die("cannot read data blob for %s", one->path); - prep_temp_blob(r->index, name, temp, + prep_temp_blob(r->index, one->path, temp, one->data, one->size, &one->oid, one->mode); } @@ -4280,7 +4279,7 @@ static void add_external_diff_name(struct repository *r, struct strvec *argv, struct diff_filespec *df) { - struct diff_tempfile *temp = prepare_temp_file(r, df->path, df); + struct diff_tempfile *temp = prepare_temp_file(r, df); strvec_push(argv, temp->name); strvec_push(argv, temp->hex); strvec_push(argv, temp->mode); @@ -7034,7 +7033,7 @@ static char *run_textconv(struct repository *r, struct strbuf buf = STRBUF_INIT; int err = 0; - temp = prepare_temp_file(r, spec->path, spec); + temp = prepare_temp_file(r, spec); strvec_push(&child.args, pgm); strvec_push(&child.args, temp->name);