odb: rename `read_object_with_reference()`

Rename `read_object_with_reference()` to `odb_read_object_peeled()` to match other functions related to the object database and our modern coding guidelines. Furthermore though, the old name didn't really describe very well what this function actually does, which is to walk down any commit and tag objects until an object of the required type has been found. This is generally referred to as "peeling", so the new name should be way more descriptive. No compatibility wrapper is introduced as the function is not used a lot throughout our codebase. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 1, 2025 at 14:22 UTC 841a03b4046ab81276743b4d7e727b1658f805da
8 files changed +37 -45
Documentation/user-manual.adoc
+2 -2
@@ -4301,11 +4301,11 @@ Now, for the meat:
4301
4302 -----------------------------------------------------------------------------
4303 case 0:
4304 - buf = read_object_with_reference(sha1, argv[1], &size, NULL);
4304 + buf = odb_read_object_peeled(r->objects, sha1, argv[1], &size, NULL);
4305 -----------------------------------------------------------------------------
4306
4307 This is how you read a blob (actually, not only a blob, but any type of
4308 -object). To know how the function `read_object_with_reference()` actually
4308 +object). To know how the function `odb_read_object_peeled()` actually
4309 works, find the source code for it (something like `git grep
4310 read_object_with | grep ":[a-z]"` in the Git repository), and read
4311 the source.
builtin/cat-file.c
+2 -2
@@ -246,8 +246,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
246 * fall-back to the usual case.
247 */
248 }
249 - buf = read_object_with_reference(the_repository, &oid,
250 - exp_type_id, &size, NULL);
249 + buf = odb_read_object_peeled(the_repository->objects, &oid,
250 + exp_type_id, &size, NULL);
251
252 if (use_mailmap) {
253 size_t s = size;
builtin/fast-import.c
+8 -11
@@ -2535,10 +2535,9 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2535 oidcpy(&commit_oid, &commit_oe->idx.oid);
2536 } else if (!repo_get_oid(the_repository, p, &commit_oid)) {
2537 unsigned long size;
2538 - char *buf = read_object_with_reference(the_repository,
2539 - &commit_oid,
2540 - OBJ_COMMIT, &size,
2541 - &commit_oid);
2538 + char *buf = odb_read_object_peeled(the_repository->objects,
2539 + &commit_oid, OBJ_COMMIT, &size,
2540 + &commit_oid);
2541 if (!buf || size < the_hash_algo->hexsz + 6)
2542 die("Not a valid commit: %s", p);
2543 free(buf);
@@ -2604,9 +2603,8 @@ static void parse_from_existing(struct branch *b)
2603 unsigned long size;
2604 char *buf;
2605
2607 - buf = read_object_with_reference(the_repository,
2608 - &b->oid, OBJ_COMMIT, &size,
2609 - &b->oid);
2606 + buf = odb_read_object_peeled(the_repository->objects, &b->oid,
2607 + OBJ_COMMIT, &size, &b->oid);
2608 parse_from_commit(b, buf, size);
2609 free(buf);
2610 }
@@ -2699,10 +2697,9 @@ static struct hash_list *parse_merge(unsigned int *count)
2697 oidcpy(&n->oid, &oe->idx.oid);
2698 } else if (!repo_get_oid(the_repository, from, &n->oid)) {
2699 unsigned long size;
2702 - char *buf = read_object_with_reference(the_repository,
2703 - &n->oid,
2704 - OBJ_COMMIT,
2705 - &size, &n->oid);
2700 + char *buf = odb_read_object_peeled(the_repository->objects,
2701 + &n->oid, OBJ_COMMIT,
2702 + &size, &n->oid);
2703 if (!buf || size < the_hash_algo->hexsz + 6)
2704 die("Not a valid commit: %s", from);
2705 free(buf);
builtin/grep.c
+3 -6
@@ -522,9 +522,7 @@ static int grep_submodule(struct grep_opt *opt,
522 obj_read_lock();
523 object_type = odb_read_object_info(subrepo->objects, oid, NULL);
524 obj_read_unlock();
525 - data = read_object_with_reference(subrepo,
526 - oid, OBJ_TREE,
527 - &size, NULL);
525 + data = odb_read_object_peeled(subrepo->objects, oid, OBJ_TREE, &size, NULL);
526 if (!data)
527 die(_("unable to read tree (%s)"), oid_to_hex(oid));
528
@@ -705,9 +703,8 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
703 struct strbuf base;
704 int hit, len;
705
708 - data = read_object_with_reference(opt->repo,
709 - &obj->oid, OBJ_TREE,
710 - &size, NULL);
706 + data = odb_read_object_peeled(opt->repo->objects, &obj->oid,
707 + OBJ_TREE, &size, NULL);
708 if (!data)
709 die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid));
710
builtin/pack-objects.c
+2 -2
@@ -2055,8 +2055,8 @@ static void add_preferred_base(struct object_id *oid)
2055 if (window <= num_preferred_base++)
2056 return;
2057
2058 - data = read_object_with_reference(the_repository, oid,
2059 - OBJ_TREE, &size, &tree_oid);
2058 + data = odb_read_object_peeled(the_repository->objects, oid,
2059 + OBJ_TREE, &size, &tree_oid);
2060 if (!data)
2061 return;
2062
odb.c
+9 -8
@@ -905,11 +905,11 @@ void *odb_read_object(struct object_database *odb,
905 return data;
906 }
907
908 -void *read_object_with_reference(struct repository *r,
909 - const struct object_id *oid,
910 - enum object_type required_type,
911 - unsigned long *size,
912 - struct object_id *actual_oid_return)
908 +void *odb_read_object_peeled(struct object_database *odb,
909 + const struct object_id *oid,
910 + enum object_type required_type,
911 + unsigned long *size,
912 + struct object_id *actual_oid_return)
913 {
914 enum object_type type;
915 void *buffer;
@@ -921,7 +921,7 @@ void *read_object_with_reference(struct repository *r,
921 int ref_length = -1;
922 const char *ref_type = NULL;
923
924 - buffer = odb_read_object(r->objects, &actual_oid, &type, &isize);
924 + buffer = odb_read_object(odb, &actual_oid, &type, &isize);
925 if (!buffer)
926 return NULL;
927 if (type == required_type) {
@@ -941,9 +941,10 @@ void *read_object_with_reference(struct repository *r,
941 }
942 ref_length = strlen(ref_type);
943
944 - if (ref_length + r->hash_algo->hexsz > isize ||
944 + if (ref_length + odb->repo->hash_algo->hexsz > isize ||
945 memcmp(buffer, ref_type, ref_length) ||
946 - get_oid_hex_algop((char *) buffer + ref_length, &actual_oid, r->hash_algo)) {
946 + get_oid_hex_algop((char *) buffer + ref_length, &actual_oid,
947 + odb->repo->hash_algo)) {
948 free(buffer);
949 return NULL;
950 }
odb.h
+7 -8
@@ -274,6 +274,12 @@ void *odb_read_object(struct object_database *odb,
274 enum object_type *type,
275 unsigned long *size);
276
277 +void *odb_read_object_peeled(struct object_database *odb,
278 + const struct object_id *oid,
279 + enum object_type required_type,
280 + unsigned long *size,
281 + struct object_id *oid_ret);
282 +
283 /*
284 * Add an object file to the in-memory object store, without writing it
285 * to disk.
@@ -382,7 +388,7 @@ void odb_assert_oid_type(struct object_database *odb,
388 /*
389 * Enabling the object read lock allows multiple threads to safely call the
390 * following functions in parallel: odb_read_object(),
385 - * read_object_with_reference(), odb_read_object_info() and odb().
391 + * odb_read_object_peeled(), odb_read_object_info() and odb().
392 *
393 * obj_read_lock() and obj_read_unlock() may also be used to protect other
394 * section which cannot execute in parallel with object reading. Since the used
@@ -431,13 +437,6 @@ enum for_each_object_flags {
437 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS = (1<<4),
438 };
439
434 -
435 -void *read_object_with_reference(struct repository *r,
436 - const struct object_id *oid,
437 - enum object_type required_type,
438 - unsigned long *size,
439 - struct object_id *oid_ret);
440 -
440 /* Compatibility wrappers, to be removed once Git 2.51 has been released. */
441 #include "repository.h"
442
tree-walk.c
+4 -6
@@ -90,7 +90,7 @@ void *fill_tree_descriptor(struct repository *r,
90 void *buf = NULL;
91
92 if (oid) {
93 - buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL);
93 + buf = odb_read_object_peeled(r->objects, oid, OBJ_TREE, &size, NULL);
94 if (!buf)
95 die(_("unable to read tree (%s)"), oid_to_hex(oid));
96 }
@@ -611,7 +611,7 @@ int get_tree_entry(struct repository *r,
611 unsigned long size;
612 struct object_id root;
613
614 - tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root);
614 + tree = odb_read_object_peeled(r->objects, tree_oid, OBJ_TREE, &size, &root);
615 if (!tree)
616 return -1;
617
@@ -681,10 +681,8 @@ enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r,
681 void *tree;
682 struct object_id root;
683 unsigned long size;
684 - tree = read_object_with_reference(r,
685 - &current_tree_oid,
686 - OBJ_TREE, &size,
687 - &root);
684 + tree = odb_read_object_peeled(r->objects, &current_tree_oid,
685 + OBJ_TREE, &size, &root);
686 if (!tree)
687 goto done;
688