Skip to content

Commit 1120c54

Browse files
committed
Merge branch 'jk/ext-diff-with-relative'
"git diff --relative" did not mix well with "git diff --ext-diff", which has been corrected. * jk/ext-diff-with-relative: diff: drop "name" parameter from prepare_temp_file() diff: clean up external-diff argv setup diff: use filespec path to set up tempfiles for ext-diff
2 parents af8a3bb + f034bb1 commit 1120c54

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

diff.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,7 +4213,6 @@ static void prep_temp_blob(struct index_state *istate,
42134213
}
42144214

42154215
static struct diff_tempfile *prepare_temp_file(struct repository *r,
4216-
const char *name,
42174216
struct diff_filespec *one)
42184217
{
42194218
struct diff_tempfile *temp = claim_diff_tempfile();
@@ -4231,18 +4230,18 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
42314230

42324231
if (!S_ISGITLINK(one->mode) &&
42334232
(!one->oid_valid ||
4234-
reuse_worktree_file(r->index, name, &one->oid, 1))) {
4233+
reuse_worktree_file(r->index, one->path, &one->oid, 1))) {
42354234
struct stat st;
4236-
if (lstat(name, &st) < 0) {
4235+
if (lstat(one->path, &st) < 0) {
42374236
if (errno == ENOENT)
42384237
goto not_a_valid_file;
4239-
die_errno("stat(%s)", name);
4238+
die_errno("stat(%s)", one->path);
42404239
}
42414240
if (S_ISLNK(st.st_mode)) {
42424241
struct strbuf sb = STRBUF_INIT;
4243-
if (strbuf_readlink(&sb, name, st.st_size) < 0)
4244-
die_errno("readlink(%s)", name);
4245-
prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
4242+
if (strbuf_readlink(&sb, one->path, st.st_size) < 0)
4243+
die_errno("readlink(%s)", one->path);
4244+
prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len,
42464245
(one->oid_valid ?
42474246
&one->oid : null_oid()),
42484247
(one->oid_valid ?
@@ -4251,7 +4250,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
42514250
}
42524251
else {
42534252
/* we can borrow from the file in the work tree */
4254-
temp->name = name;
4253+
temp->name = one->path;
42554254
if (!one->oid_valid)
42564255
oid_to_hex_r(temp->hex, null_oid());
42574256
else
@@ -4269,7 +4268,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
42694268
else {
42704269
if (diff_populate_filespec(r, one, NULL))
42714270
die("cannot read data blob for %s", one->path);
4272-
prep_temp_blob(r->index, name, temp,
4271+
prep_temp_blob(r->index, one->path, temp,
42734272
one->data, one->size,
42744273
&one->oid, one->mode);
42754274
}
@@ -4278,10 +4277,9 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
42784277

42794278
static void add_external_diff_name(struct repository *r,
42804279
struct strvec *argv,
4281-
const char *name,
42824280
struct diff_filespec *df)
42834281
{
4284-
struct diff_tempfile *temp = prepare_temp_file(r, name, df);
4282+
struct diff_tempfile *temp = prepare_temp_file(r, df);
42854283
strvec_push(argv, temp->name);
42864284
strvec_push(argv, temp->hex);
42874285
strvec_push(argv, temp->mode);
@@ -4308,11 +4306,9 @@ static void run_external_diff(const char *pgm,
43084306
strvec_push(&cmd.args, name);
43094307

43104308
if (one && two) {
4311-
add_external_diff_name(o->repo, &cmd.args, name, one);
4312-
if (!other)
4313-
add_external_diff_name(o->repo, &cmd.args, name, two);
4314-
else {
4315-
add_external_diff_name(o->repo, &cmd.args, other, two);
4309+
add_external_diff_name(o->repo, &cmd.args, one);
4310+
add_external_diff_name(o->repo, &cmd.args, two);
4311+
if (other) {
43164312
strvec_push(&cmd.args, other);
43174313
strvec_push(&cmd.args, xfrm_msg);
43184314
}
@@ -7034,7 +7030,7 @@ static char *run_textconv(struct repository *r,
70347030
struct strbuf buf = STRBUF_INIT;
70357031
int err = 0;
70367032

7037-
temp = prepare_temp_file(r, spec->path, spec);
7033+
temp = prepare_temp_file(r, spec);
70387034
strvec_push(&child.args, pgm);
70397035
strvec_push(&child.args, temp->name);
70407036

t/t4045-diff-relative.sh

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

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

0 commit comments

Comments
 (0)