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

[PATCH v3 12/12] object-file API: pass an enum to read_object_with_reference()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Feb 4, 2022, 23:48 UTC
Message-ID
<patch-v3-12.12-d2d355e6bb8-20220204T234435Z-avarab@gmail.com>
In-Reply-To
<cover-v3-00.12-00000000000-20220204T234435Z-avarab@gmail.com>

Change the read_object_with_reference() function to take an "enum object_type". It was not prepared to handle an arbitrary "const char *type", as it was itself calling type_from_string().

Let's change the only caller that passes in user data to use type_from_string(), and convert the rest to use e.g. "OBJ_TREE" instead of "tree_type".

The "cat-file" caller is not on the codepath that handles"--allow-unknown", so the type_from_string() there is safe. Its use of type_from_string() doesn't functionally differ from that of the pre-image.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/cat-file.c     | 11 +++++++----
 builtin/fast-import.c  |  6 +++---
 builtin/grep.c         |  4 ++--
 builtin/pack-objects.c |  2 +-
 cache.h                |  2 +-
 object-file.c          |  5 ++---
 tree-walk.c            |  6 +++---
 7 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index d94050e6c18..3c5bc505e0a 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -154,7 +154,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 		break;
 
 	case 0:
-		if (type_from_string(exp_type) == OBJ_BLOB) {
+	{
+		enum object_type exp_type_id = type_from_string(exp_type);
+
+		if (exp_type_id == OBJ_BLOB) {
 			struct object_id blob_oid;
 			if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
 				char *buffer = read_object_file(&oid, &type,
@@ -176,10 +179,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 			 * fall-back to the usual case.
 			 */
 		}
-		buf = read_object_with_reference(the_repository,
-						 &oid, exp_type, &size, NULL);
+		buf = read_object_with_reference(the_repository, &oid,
+						 exp_type_id, &size, NULL);
 		break;
-
+	}
 	default:
 		die("git cat-file: unknown option: %s", exp_type);
 	}
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 123df7d9a53..c52e807f56e 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -2483,7 +2483,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
 		unsigned long size;
 		char *buf = read_object_with_reference(the_repository,
 						       &commit_oid,
-						       commit_type, &size,
+						       OBJ_COMMIT, &size,
 						       &commit_oid);
 		if (!buf || size < the_hash_algo->hexsz + 6)
 			die("Not a valid commit: %s", p);
@@ -2555,7 +2555,7 @@ static void parse_from_existing(struct branch *b)
 		char *buf;
 
 		buf = read_object_with_reference(the_repository,
-						 &b->oid, commit_type, &size,
+						 &b->oid, OBJ_COMMIT, &size,
 						 &b->oid);
 		parse_from_commit(b, buf, size);
 		free(buf);
@@ -2651,7 +2651,7 @@ static struct hash_list *parse_merge(unsigned int *count)
 			unsigned long size;
 			char *buf = read_object_with_reference(the_repository,
 							       &n->oid,
-							       commit_type,
+							       OBJ_COMMIT,
 							       &size, &n->oid);
 			if (!buf || size < the_hash_algo->hexsz + 6)
 				die("Not a valid commit: %s", from);
diff --git a/builtin/grep.c b/builtin/grep.c
index 9e34a820ad4..75e07b5623a 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt,
 		object_type = oid_object_info(subrepo, oid, NULL);
 		obj_read_unlock();
 		data = read_object_with_reference(subrepo,
-						  oid, tree_type,
+						  oid, OBJ_TREE,
 						  &size, NULL);
 		if (!data)
 			die(_("unable to read tree (%s)"), oid_to_hex(oid));
@@ -651,7 +651,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
 		int hit, len;
 
 		data = read_object_with_reference(opt->repo,
-						  &obj->oid, tree_type,
+						  &obj->oid, OBJ_TREE,
 						  &size, NULL);
 		if (!data)
 			die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ba2006f2212..c4df3df3141 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1802,7 +1802,7 @@ static void add_preferred_base(struct object_id *oid)
 		return;
 
 	data = read_object_with_reference(the_repository, oid,
-					  tree_type, &size, &tree_oid);
+					  OBJ_TREE, &size, &tree_oid);
 	if (!data)
 		return;
 
diff --git a/cache.h b/cache.h
index 2027803c49a..0e7fc8b7c2f 100644
--- a/cache.h
+++ b/cache.h
@@ -1561,7 +1561,7 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
 
 void *read_object_with_reference(struct repository *r,
 				 const struct object_id *oid,
-				 const char *required_type,
+				 enum object_type required_type,
 				 unsigned long *size,
 				 struct object_id *oid_ret);
 
diff --git a/object-file.c b/object-file.c
index c75c12dd0ba..6cbdb836cb7 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1737,16 +1737,15 @@ void *read_object_file_extended(struct repository *r,
 
 void *read_object_with_reference(struct repository *r,
 				 const struct object_id *oid,
-				 const char *required_type_name,
+				 enum object_type required_type,
 				 unsigned long *size,
 				 struct object_id *actual_oid_return)
 {
-	enum object_type type, required_type;
+	enum object_type type;
 	void *buffer;
 	unsigned long isize;
 	struct object_id actual_oid;
 
-	required_type = type_from_string(required_type_name);
 	oidcpy(&actual_oid, oid);
 	while (1) {
 		int ref_length = -1;
diff --git a/tree-walk.c b/tree-walk.c
index 3a94959d64a..506234b4b81 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -89,7 +89,7 @@ void *fill_tree_descriptor(struct repository *r,
 	void *buf = NULL;
 
 	if (oid) {
-		buf = read_object_with_reference(r, oid, tree_type, &size, NULL);
+		buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL);
 		if (!buf)
 			die("unable to read tree %s", oid_to_hex(oid));
 	}
@@ -605,7 +605,7 @@ int get_tree_entry(struct repository *r,
 	unsigned long size;
 	struct object_id root;
 
-	tree = read_object_with_reference(r, tree_oid, tree_type, &size, &root);
+	tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root);
 	if (!tree)
 		return -1;
 
@@ -677,7 +677,7 @@ enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r,
 			unsigned long size;
 			tree = read_object_with_reference(r,
 							  &current_tree_oid,
-							  tree_type, &size,
+							  OBJ_TREE, &size,
 							  &root);
 			if (!tree)
 				goto done;
-- 
2.35.1.940.ge7a5b4b05f2
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 45 of 46 in “object-file API: pass object enums, tidy up streaming interface”
  1. 00/10 object-file API: pass object enums, tidy up streaming interfaceÆvar Arnfjörð Bjarmason, Feb 1, 2022
  2. 01/10 object-file.c: split up declaration of unrelated variablesÆvar Arnfjörð Bjarmason, Feb 1, 2022
  3. 02/10 object-file API: return "void", not "int" from hash_object_file()Ævar Arnfjörð Bjarmason, Feb 1, 2022
  4. 03/10 object-file API: add a format_object_header() functionÆvar Arnfjörð Bjarmason, Feb 1, 2022
  5. 04/10 object-file API: have write_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 1, 2022
  6. Junio C HamanoFeb 1, 2022
  7. 05/10 object-file API: provide a hash_object_file_oideq()Ævar Arnfjörð Bjarmason, Feb 1, 2022
  8. Junio C HamanoFeb 1, 2022
  9. Ævar Arnfjörð BjarmasonFeb 1, 2022
  10. 06/10 object-file API: replace some use of check_object_signature()Ævar Arnfjörð Bjarmason, Feb 1, 2022
  11. Junio C HamanoFeb 1, 2022
  12. 08/10 object-file API: replace check_object_signature() with stream_*Ævar Arnfjörð Bjarmason, Feb 1, 2022
  13. 07/10 object-file API: have hash_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 1, 2022
  14. 09/10 object-file.c: add a literal version of write_object_file_prepare()Ævar Arnfjörð Bjarmason, Feb 1, 2022
  15. 10/10 object-file API: pass an enum to read_object_with_reference()Ævar Arnfjörð Bjarmason, Feb 1, 2022
  16. 00/11 object-file API: pass object enums, tidy up streaming interfaceÆvar Arnfjörð Bjarmason, Feb 4, 2022
  17. 02/11 object-file API: return "void", not "int" from hash_object_file()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  18. 03/11 object-file API: add a format_object_header() functionÆvar Arnfjörð Bjarmason, Feb 4, 2022
  19. 04/11 object-file API: have write_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 4, 2022
  20. Junio C HamanoFeb 4, 2022
  21. 01/11 object-file.c: split up declaration of unrelated variablesÆvar Arnfjörð Bjarmason, Feb 4, 2022
  22. 05/11 object API: correct "buf" v.s. "map" mismatch in *.c and *.hÆvar Arnfjörð Bjarmason, Feb 4, 2022
  23. Junio C HamanoFeb 4, 2022
  24. 06/11 object API: make check_object_signature() oideq()-like, move docsÆvar Arnfjörð Bjarmason, Feb 4, 2022
  25. Junio C HamanoFeb 4, 2022
  26. 07/11 object-file API: split up and simplify check_object_signature()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  27. 08/11 object API: rename hash_object_file_literally() to write_*()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  28. 09/11 object-file API: have hash_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 4, 2022
  29. 10/11 object-file.c: add a literal version of write_object_file_prepare()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  30. 11/11 object-file API: pass an enum to read_object_with_reference()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  31. 00/12 object-file API: pass object enums, tidy up streaming interfaceÆvar Arnfjörð Bjarmason, Feb 4, 2022
  32. 01/12 object-file.c: split up declaration of unrelated variablesÆvar Arnfjörð Bjarmason, Feb 4, 2022
  33. 02/12 object-file API: return "void", not "int" from hash_object_file()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  34. 03/12 object-file API: add a format_object_header() functionÆvar Arnfjörð Bjarmason, Feb 4, 2022
  35. Jiang XinFeb 17, 2022
  36. Ævar Arnfjörð BjarmasonFeb 17, 2022
  37. Jiang XinMar 1, 2022
  38. 04/12 object-file API: have write_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 4, 2022
  39. 05/12 object API: correct "buf" v.s. "map" mismatch in *.c and *.hÆvar Arnfjörð Bjarmason, Feb 4, 2022
  40. 06/12 object API docs: move check_object_signature() docs to cache.hÆvar Arnfjörð Bjarmason, Feb 4, 2022
  41. 09/12 object API: rename hash_object_file_literally() to write_*()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  42. 08/12 object-file API: split up and simplify check_object_signature()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  43. 07/12 object API users + docs: check <0, not !0 with check_object_signature()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  44. 10/12 object-file API: have hash_object_file() take "enum object_type"Ævar Arnfjörð Bjarmason, Feb 4, 2022
  45. 12/12 object-file API: pass an enum to read_object_with_reference()Ævar Arnfjörð Bjarmason, Feb 4, 2022
  46. 11/12 object-file.c: add a literal version of write_object_file_prepare()Ævar Arnfjörð Bjarmason, Feb 4, 2022

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.