object-file: get rid of `the_repository` in `has_loose_object()`

We implicitly depend on `the_repository` in `has_loose_object()`. Refactor the function to accept an `odb_source` as input that should be checked for such a loose object. This refactoring changes semantics of the function to not check the whole object database for such a loose object anymore, but instead we now only check that single source. Existing callers thus need to loop through all sources manually now. While this change may seem illogical at first, whether or not an object exists in a specific format should be answered by the source using that format. As such, we can eventually convert this into a generic function `odb_source_has_object()` that simply checks whether a given object exists in an object source. And as we will know about the format that any given source uses it allows us to derive whether the object exists in a given format. This change also makes `has_loose_object_nonlocal()` obsolete. The only caller of this function is adapted so that it skips the primary object source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2025 at 06:56 UTC 931e8c9f5226d855922ab5b0ba04bafe4aa7382f
3 files changed +30 -17
builtin/pack-objects.c
+20 -4
@@ -1703,8 +1703,16 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1703 struct list_head *pos;
1704 struct multi_pack_index *m;
1705
1706 - if (!exclude && local && has_loose_object_nonlocal(oid))
1707 - return 0;
1706 + if (!exclude && local) {
1707 + /*
1708 + * Note that we start iterating at `sources->next` so that we
1709 + * skip the local object source.
1710 + */
1711 + struct odb_source *source = the_repository->objects->sources->next;
1712 + for (; source; source = source->next)
1713 + if (has_loose_object(source, oid))
1714 + return 0;
1715 + }
1716
1717 /*
1718 * If we already know the pack object lives in, start checks from that
@@ -3928,7 +3936,14 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
3936 } else {
3937 if (!want_object_in_pack_mtime(oid, 0, &pack, &offset, mtime))
3938 return;
3931 - if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3939 + if (!pack && type == OBJ_BLOB) {
3940 + struct odb_source *source = the_repository->objects->sources;
3941 + int found = 0;
3942 +
3943 + for (; !found && source; source = source->next)
3944 + if (has_loose_object(source, oid))
3945 + found = 1;
3946 +
3947 /*
3948 * If a traversed tree has a missing blob then we want
3949 * to avoid adding that missing object to our pack.
@@ -3942,7 +3957,8 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
3957 * limited to "ensure non-tip blobs which don't exist in
3958 * packs do exist via loose objects". Confused?
3959 */
3945 - return;
3960 + if (!found)
3961 + return;
3962 }
3963
3964 entry = create_object_entry(oid, type, pack_name_hash_fn(name),
object-file.c
+7 -9
@@ -121,14 +121,10 @@ static int check_and_freshen(const struct object_id *oid, int freshen)
121 check_and_freshen_nonlocal(oid, freshen);
122 }
123
124 -int has_loose_object_nonlocal(const struct object_id *oid)
124 +int has_loose_object(struct odb_source *source,
125 + const struct object_id *oid)
126 {
126 - return check_and_freshen_nonlocal(oid, 0);
127 -}
128 -
129 -int has_loose_object(const struct object_id *oid)
130 -{
131 - return check_and_freshen(oid, 0);
127 + return check_and_freshen_odb(source, oid, 0);
128 }
129
130 int format_object_header(char *str, size_t size, enum object_type type,
@@ -1103,8 +1099,10 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
1099 int hdrlen;
1100 int ret;
1101
1106 - if (has_loose_object(oid))
1107 - return 0;
1102 + for (struct odb_source *source = repo->objects->sources; source; source = source->next)
1103 + if (has_loose_object(source, oid))
1104 + return 0;
1105 +
1106 oi.typep = &type;
1107 oi.sizep = &len;
1108 oi.contentp = &buf;
object-file.h
+3 -4
@@ -45,13 +45,12 @@ const char *odb_loose_path(struct odb_source *source,
45 const struct object_id *oid);
46
47 /*
48 - * Return true iff an alternate object database has a loose object
48 + * Return true iff an object database source has a loose object
49 * with the specified name. This function does not respect replace
50 * references.
51 */
52 -int has_loose_object_nonlocal(const struct object_id *);
53 -
54 -int has_loose_object(const struct object_id *);
52 +int has_loose_object(struct odb_source *source,
53 + const struct object_id *oid);
54
55 void *map_loose_object(struct repository *r, const struct object_id *oid,
56 unsigned long *size);