object-file API: pass an enum to read_object_with_reference()

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 5, 2022 at 00:48 UTC 6aea6baeb3ece6c832dbdf1deed09f41aebf85c2
7 files changed +19 -17
builtin/cat-file.c
+7 -4
@@ -154,7 +154,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
154 break;
155
156 case 0:
157 - if (type_from_string(exp_type) == OBJ_BLOB) {
157 + {
158 + enum object_type exp_type_id = type_from_string(exp_type);
159 +
160 + if (exp_type_id == OBJ_BLOB) {
161 struct object_id blob_oid;
162 if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
163 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,
179 * fall-back to the usual case.
180 */
181 }
179 - buf = read_object_with_reference(the_repository,
180 - &oid, exp_type, &size, NULL);
182 + buf = read_object_with_reference(the_repository, &oid,
183 + exp_type_id, &size, NULL);
184 break;
182 -
185 + }
186 default:
187 die("git cat-file: unknown option: %s", exp_type);
188 }
builtin/fast-import.c
+3 -3
@@ -2483,7 +2483,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2483 unsigned long size;
2484 char *buf = read_object_with_reference(the_repository,
2485 &commit_oid,
2486 - commit_type, &size,
2486 + OBJ_COMMIT, &size,
2487 &commit_oid);
2488 if (!buf || size < the_hash_algo->hexsz + 6)
2489 die("Not a valid commit: %s", p);
@@ -2555,7 +2555,7 @@ static void parse_from_existing(struct branch *b)
2555 char *buf;
2556
2557 buf = read_object_with_reference(the_repository,
2558 - &b->oid, commit_type, &size,
2558 + &b->oid, OBJ_COMMIT, &size,
2559 &b->oid);
2560 parse_from_commit(b, buf, size);
2561 free(buf);
@@ -2651,7 +2651,7 @@ static struct hash_list *parse_merge(unsigned int *count)
2651 unsigned long size;
2652 char *buf = read_object_with_reference(the_repository,
2653 &n->oid,
2654 - commit_type,
2654 + OBJ_COMMIT,
2655 &size, &n->oid);
2656 if (!buf || size < the_hash_algo->hexsz + 6)
2657 die("Not a valid commit: %s", from);
builtin/grep.c
+2 -2
@@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt,
482 object_type = oid_object_info(subrepo, oid, NULL);
483 obj_read_unlock();
484 data = read_object_with_reference(subrepo,
485 - oid, tree_type,
485 + oid, OBJ_TREE,
486 &size, NULL);
487 if (!data)
488 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,
651 int hit, len;
652
653 data = read_object_with_reference(opt->repo,
654 - &obj->oid, tree_type,
654 + &obj->oid, OBJ_TREE,
655 &size, NULL);
656 if (!data)
657 die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
builtin/pack-objects.c
+1 -1
@@ -1802,7 +1802,7 @@ static void add_preferred_base(struct object_id *oid)
1802 return;
1803
1804 data = read_object_with_reference(the_repository, oid,
1805 - tree_type, &size, &tree_oid);
1805 + OBJ_TREE, &size, &tree_oid);
1806 if (!data)
1807 return;
1808
cache.h
+1 -1
@@ -1561,7 +1561,7 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char
1561
1562 void *read_object_with_reference(struct repository *r,
1563 const struct object_id *oid,
1564 - const char *required_type,
1564 + enum object_type required_type,
1565 unsigned long *size,
1566 struct object_id *oid_ret);
1567
object-file.c
+2 -3
@@ -1737,16 +1737,15 @@ void *read_object_file_extended(struct repository *r,
1737
1738 void *read_object_with_reference(struct repository *r,
1739 const struct object_id *oid,
1740 - const char *required_type_name,
1740 + enum object_type required_type,
1741 unsigned long *size,
1742 struct object_id *actual_oid_return)
1743 {
1744 - enum object_type type, required_type;
1744 + enum object_type type;
1745 void *buffer;
1746 unsigned long isize;
1747 struct object_id actual_oid;
1748
1749 - required_type = type_from_string(required_type_name);
1749 oidcpy(&actual_oid, oid);
1750 while (1) {
1751 int ref_length = -1;
tree-walk.c
+3 -3
@@ -89,7 +89,7 @@ void *fill_tree_descriptor(struct repository *r,
89 void *buf = NULL;
90
91 if (oid) {
92 - buf = read_object_with_reference(r, oid, tree_type, &size, NULL);
92 + buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL);
93 if (!buf)
94 die("unable to read tree %s", oid_to_hex(oid));
95 }
@@ -605,7 +605,7 @@ int get_tree_entry(struct repository *r,
605 unsigned long size;
606 struct object_id root;
607
608 - tree = read_object_with_reference(r, tree_oid, tree_type, &size, &root);
608 + tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root);
609 if (!tree)
610 return -1;
611
@@ -677,7 +677,7 @@ enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r,
677 unsigned long size;
678 tree = read_object_with_reference(r,
679 &current_tree_oid,
680 - tree_type, &size,
680 + OBJ_TREE, &size,
681 &root);
682 if (!tree)
683 goto done;