odb/source-packed: wire up `count_objects()` callback

Move `packfile_store_count_objects()` from "packfile.c" into "odb/source-packed.c" and wire it up as the `count_objects()` callback of the "packed" source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 17, 2026 at 08:39 UTC 6b7484fcb683211eafd82a7ba4b4470d553a62b6
4 files changed +34 -42
odb/source-files.c
+1 -1
@@ -103,7 +103,7 @@ static int odb_source_files_count_objects(struct odb_source *source,
103 unsigned long count;
104 int ret;
105
106 - ret = packfile_store_count_objects(files->packed, flags, &count);
106 + ret = odb_source_count_objects(&files->packed->base, flags, &count);
107 if (ret < 0)
108 goto out;
109
odb/source-packed.c
+33
@@ -338,6 +338,38 @@ out:
338 return ret;
339 }
340
341 +static int odb_source_packed_count_objects(struct odb_source *source,
342 + enum odb_count_objects_flags flags UNUSED,
343 + unsigned long *out)
344 +{
345 + struct odb_source_packed *packed = odb_source_packed_downcast(source);
346 + struct packfile_list_entry *e;
347 + struct multi_pack_index *m;
348 + unsigned long count = 0;
349 + int ret;
350 +
351 + m = get_multi_pack_index(&packed->files->base);
352 + if (m)
353 + count += m->num_objects + m->num_objects_in_base;
354 +
355 + for (e = packfile_store_get_packs(packed); e; e = e->next) {
356 + if (e->pack->multi_pack_index)
357 + continue;
358 + if (open_pack_index(e->pack)) {
359 + ret = -1;
360 + goto out;
361 + }
362 +
363 + count += e->pack->num_objects;
364 + }
365 +
366 + *out = count;
367 + ret = 0;
368 +
369 +out:
370 + return ret;
371 +}
372 +
373 void (*report_garbage)(unsigned seen_bits, const char *path);
374
375 static void report_helper(const struct string_list *list,
@@ -549,6 +581,7 @@ struct odb_source_packed *odb_source_packed_new(struct odb_source_files *parent)
581 packed->base.read_object_info = odb_source_packed_read_object_info;
582 packed->base.read_object_stream = odb_source_packed_read_object_stream;
583 packed->base.for_each_object = odb_source_packed_for_each_object;
584 + packed->base.count_objects = odb_source_packed_count_objects;
585
586 if (!is_absolute_path(parent->base.path))
587 chdir_notify_register(NULL, odb_source_packed_reparent, packed);
packfile.c
-31
@@ -866,37 +866,6 @@ struct packfile_list_entry *packfile_store_get_packs(struct odb_source_packed *s
866 return store->packs.head;
867 }
868
869 -int packfile_store_count_objects(struct odb_source_packed *store,
870 - enum odb_count_objects_flags flags UNUSED,
871 - unsigned long *out)
872 -{
873 - struct packfile_list_entry *e;
874 - struct multi_pack_index *m;
875 - unsigned long count = 0;
876 - int ret;
877 -
878 - m = get_multi_pack_index(&store->files->base);
879 - if (m)
880 - count += m->num_objects + m->num_objects_in_base;
881 -
882 - for (e = packfile_store_get_packs(store); e; e = e->next) {
883 - if (e->pack->multi_pack_index)
884 - continue;
885 - if (open_pack_index(e->pack)) {
886 - ret = -1;
887 - goto out;
888 - }
889 -
890 - count += e->pack->num_objects;
891 - }
892 -
893 - *out = count;
894 - ret = 0;
895 -
896 -out:
897 - return ret;
898 -}
899 -
869 unsigned long unpack_object_header_buffer(const unsigned char *buf,
870 unsigned long len, enum object_type *type, size_t *sizep)
871 {
packfile.h
-10
@@ -141,16 +141,6 @@ enum kept_pack_type {
141 KEPT_PACK_IN_CORE_OPEN = (1 << 2),
142 };
143
144 -/*
145 - * Count the number objects contained in the given packfile store. If
146 - * successful, the number of objects will be written to the `out` pointer.
147 - *
148 - * Return 0 on success, a negative error code otherwise.
149 - */
150 -int packfile_store_count_objects(struct odb_source_packed *store,
151 - enum odb_count_objects_flags flags,
152 - unsigned long *out);
153 -
144 /*
145 * Retrieve the cache of kept packs from the given packfile store. Accepts a
146 * combination of `kept_pack_type` flags. The cache is computed on demand and