Skip to content

Commit 6d42392

Browse files
committed
diff-files --raw: handle intent-to-add files correctly
In `run_diff_files()`, files that have been staged with the intention to add are queued without a valid OID in the `diff_filepair`. When the output mode is, say, `DIFF_FORMAT_PATCH`, the `diff_fill_oid_info()` function, called from `run_diff()`, will remedy that situation by reading the file contents from disk. However, when the output mode is `DIFF_FORMAT_RAW`, that does not hold true, and the output will contain a bogus OID (and the flag `M` for "modified" instead of the correct `A` for "added"). As a consequence, `git difftool -d` (which relies on `git diff-files --raw`'s output) does not work correctly. Let's fix this specifically by imitating `diff_fill_oid_info()`. Note: we can only do that for diff formats that do not actually need the file contents, such as `DIFF_FORMAT_PATCH`: `run_diff()` would try to read the blob contents, but that blob might not have been written to Git's object database. This fixes git-for-windows#2677 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9c96c43 commit 6d42392

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

diff-lib.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
223223
the_hash_algo->empty_blob, 0,
224224
ce->name, 0);
225225
continue;
226+
} else if (ce_intent_to_add(ce) &&
227+
!(revs->diffopt.output_format &
228+
~(DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))) {
229+
struct object_id oid;
230+
int ret = lstat(ce->name, &st);
231+
232+
if (ret < 0)
233+
oidclr(&oid);
234+
else
235+
ret = index_path(istate, &oid,
236+
ce->name, &st, 0);
237+
diff_addremove(&revs->diffopt, '+', ce->ce_mode,
238+
&oid, ret >= 0, ce->name, 0);
239+
continue;
226240
}
227241

228242
changed = match_stat_with_submodule(&revs->diffopt, ce, &st,

t/t4000-diff-format.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ test_expect_success 'git diff-files --patch --no-patch does not show the patch'
8989
test_must_be_empty err
9090
'
9191

92+
test_expect_success 'git diff-files --raw handles intent-to-add files correctly' '
93+
echo 123 >ita &&
94+
git add -N ita &&
95+
printf ":000000 100644 %s %s A\\tita\n" \
96+
$ZERO_OID $(git hash-object --stdin <ita) >expect &&
97+
git diff-files --raw ita >actual &&
98+
test_cmp expect actual
99+
'
100+
101+
92102
test_done

0 commit comments

Comments
 (0)