git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] diff-files --raw: handle intent-to-add files correctly

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 11, 2020, 12:38 UTC
Message-ID
<6d423928512e62c91ef1ae9b469aa8cc361ed080.1591879139.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.654.git.1591879139.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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 https://github.com/git-for-windows/git/issues/2677
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 diff-lib.c             | 14 ++++++++++++++
 t/t4000-diff-format.sh | 10 ++++++++++
 2 files changed, 24 insertions(+)
diff --git a/diff-lib.c b/diff-lib.c
index 15bb45776e4..4af8f811ae8 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -223,6 +223,20 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 					       the_hash_algo->empty_blob, 0,
 					       ce->name, 0);
 				continue;
+			} else if (ce_intent_to_add(ce) &&
+				   !(revs->diffopt.output_format &
+				     ~(DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))) {
+				struct object_id oid;
+				int ret = lstat(ce->name, &st);
+
+				if (ret < 0)
+					oidclr(&oid);
+				else
+					ret = index_path(istate, &oid,
+						 ce->name, &st, 0);
+				diff_addremove(&revs->diffopt, '+', ce->ce_mode,
+					       &oid, ret >= 0, ce->name, 0);
+				continue;
 			}
 
 			changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
diff --git a/t/t4000-diff-format.sh b/t/t4000-diff-format.sh
index e5116a76a1c..48ff4e250b5 100755
--- a/t/t4000-diff-format.sh
+++ b/t/t4000-diff-format.sh
@@ -89,4 +89,14 @@ test_expect_success 'git diff-files --patch --no-patch does not show the patch'
 	test_must_be_empty err
 '
 
+test_expect_success 'git diff-files --raw handles intent-to-add files correctly' '
+	echo 123 >ita &&
+	git add -N ita &&
+	printf ":000000 100644 %s %s A\\tita\n" \
+		$ZERO_OID $(git hash-object --stdin <ita) >expect &&
+	git diff-files --raw ita >actual &&
+	test_cmp expect actual
+'
+
+
 test_done
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 2 of 37 in “Fix difftool problem with intent-to-add files”
  1. 0/3 Fix difftool problem with intent-to-add filesJohannes Schindelin via GitGitGadget, Jun 11, 2020
  2. 2/3 diff-files --raw: handle intent-to-add files correctlyJohannes Schindelin via GitGitGadget, Jun 11, 2020
  3. 1/3 diff-files: fix incorrect usage of an empty treeJohannes Schindelin via GitGitGadget, Jun 11, 2020
  4. 3/3 difftool -d: ensure that intent-to-add files are handled correctlyJohannes Schindelin via GitGitGadget, Jun 11, 2020
  5. 0/3 Fix difftool problem with intent-to-add filesJohannes Schindelin via GitGitGadget, Jun 23, 2020
  6. 2/3 diff-files: fix incorrect usage of an empty treeJohannes Schindelin via GitGitGadget, Jun 23, 2020
  7. Junio C HamanoJun 24, 2020
  8. 3/3 difftool -d: ensure that intent-to-add files are handled correctlyJohannes Schindelin via GitGitGadget, Jun 23, 2020
  9. 1/3 diff-files --raw: handle intent-to-add files correctlyJohannes Schindelin via GitGitGadget, Jun 23, 2020
  10. Junio C HamanoJun 24, 2020
  11. Johannes SchindelinJun 24, 2020
  12. Junio C HamanoJun 24, 2020
  13. Junio C HamanoJun 24, 2020
  14. Johannes SchindelinJun 25, 2020
  15. Srinidhi KaushikJun 24, 2020
  16. Johannes SchindelinJun 24, 2020
  17. Junio C HamanoJun 24, 2020
  18. 0/3 Fix difftool problem with intent-to-add filesJohannes Schindelin via GitGitGadget, Jun 24, 2020
  19. 1/3 diff-files --raw: handle intent-to-add files correctlyJohannes Schindelin via GitGitGadget, Jun 24, 2020
  20. 3/3 difftool -d: ensure that intent-to-add files are handled correctlyJohannes Schindelin via GitGitGadget, Jun 24, 2020
  21. 2/3 diff-files: fix incorrect usage of an empty treeJohannes Schindelin via GitGitGadget, Jun 24, 2020
  22. 0/2 Fix difftool problem with intent-to-add filesJohannes Schindelin via GitGitGadget, Jun 25, 2020
  23. 1/2 diff-files --raw: show correct post-image of intent-to-add filesJohannes Schindelin via GitGitGadget, Jun 25, 2020
  24. Junio C HamanoJun 25, 2020
  25. Johannes SchindelinJul 1, 2020
  26. Junio C HamanoJul 1, 2020
  27. Junio C HamanoJun 25, 2020
  28. Johannes SchindelinJul 1, 2020
  29. Srinidhi KaushikJun 26, 2020
  30. 2/2 difftool -d: ensure that intent-to-add files are handled correctlyJohannes Schindelin via GitGitGadget, Jun 25, 2020
  31. Junio C HamanoJun 25, 2020
  32. Johannes SchindelinJul 1, 2020
  33. Junio C HamanoJul 1, 2020
  34. Johannes SchindelinJul 1, 2020
  35. 0/2 Fix difftool problem with intent-to-add filesJohannes Schindelin via GitGitGadget, Jul 1, 2020
  36. 1/2 diff-files --raw: show correct post-image of intent-to-add filesJohannes Schindelin via GitGitGadget, Jul 1, 2020
  37. 2/2 difftool -d: ensure that intent-to-add files are handled correctlyJohannes Schindelin via GitGitGadget, Jul 1, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.