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

[PATCH 1/2] [GSOC] cat-file: fix --batch report changed-type bug

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 1, 2021, 14:35 UTC
Message-ID
<495cd90dbaf43e957d03edd2fdc7449b39eee53a.1622558157.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.967.git.1622558157.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

When `--batch` used with `--batch-all-objects`, with some format atoms like %(objectname), %(rest) or even no atoms may cause Git exit and report "object xxx changed type!?".

E.g. `git cat-file --batch="batman" --batch-all-objects`

This is because we did not get the object type through oid_object_info_extended(), it's composed of two situations:

1. Since object_info is empty, skip_object_info is
set to true, We skipped collecting the object type.
2. The formatting atom like %(objectname) does not require
oid_object_info_extended() to collect object types.

The correct way to deal with it is to swap the order of setting skip_object_info and setting typep. This will ensure that we must get the type of the object when using --batch.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/cat-file.c  | 13 +++++++------
 t/t1006-cat-file.sh | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 5ebf13359e83..02461bb5ea6f 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -512,12 +512,6 @@ static int batch_objects(struct batch_options *opt)
 	if (opt->cmdmode)
 		data.split_on_whitespace = 1;
 
-	if (opt->all_objects) {
-		struct object_info empty = OBJECT_INFO_INIT;
-		if (!memcmp(&data.info, &empty, sizeof(empty)))
-			data.skip_object_info = 1;
-	}
-
 	/*
 	 * If we are printing out the object, then always fill in the type,
 	 * since we will want to decide whether or not to stream.
@@ -525,6 +519,13 @@ static int batch_objects(struct batch_options *opt)
 	if (opt->print_contents)
 		data.info.typep = &data.type;
 
+	if (opt->all_objects) {
+		struct object_info empty = OBJECT_INFO_INIT;
+
+		if (!memcmp(&data.info, &empty, sizeof(empty)))
+			data.skip_object_info = 1;
+	}
+
 	if (opt->all_objects) {
 		struct object_cb_data cb;
 
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index 5d2dc99b74ad..1502a27142ba 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -586,4 +586,23 @@ test_expect_success 'cat-file --unordered works' '
 	test_cmp expect actual
 '
 
+test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
+	git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
+	git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
+	git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
+	cmp expect actual
+'
+
+test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
+	git -C all-two cat-file --batch="%(rest)" <objects >expect &&
+	git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
+	cmp expect actual
+'
+
+test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
+	git -C all-two cat-file --batch="batman" <objects >expect &&
+	git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
+	cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: Jeff King
Message 2 of 16 in “[GSOC] cat-file: fix --batch report changed-type bug”
  1. 0/2 [GSOC] cat-file: fix --batch report changed-type bugZheNing Hu via GitGitGadget, Jun 1, 2021
  2. 1/2 [GSOC] cat-file: fix --batch report changed-type bugZheNing Hu via GitGitGadget, Jun 1, 2021
  3. Jeff KingJun 1, 2021
  4. ZheNing HuJun 2, 2021
  5. Jeff KingJun 2, 2021
  6. ZheNing HuJun 3, 2021
  7. ZheNing HuJun 3, 2021
  8. Jeff KingJun 3, 2021
  9. 2/2 [GSOC] cat-file: merge two block into oneZheNing Hu via GitGitGadget, Jun 1, 2021
  10. Jeff KingJun 1, 2021
  11. ZheNing HuJun 2, 2021
  12. 0/2 [GSOC] cat-file: fix --batch report changed-type bugZheNing Hu via GitGitGadget, Jun 3, 2021
  13. 1/2 [GSOC] cat-file: handle trivial --batch format with --batch-all-objectsZheNing Hu via GitGitGadget, Jun 3, 2021
  14. 2/2 [GSOC] cat-file: merge two block into oneZheNing Hu via GitGitGadget, Jun 3, 2021
  15. Jeff KingJun 3, 2021
  16. Junio C HamanoJun 3, 2021

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.