]> git.ipfire.org Git - thirdparty/git.git/commitdiff
packfile: extract logic to count number of objects
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Mar 2026 08:42:57 +0000 (09:42 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Mar 2026 15:38:42 +0000 (08:38 -0700)
In a subsequent commit we're about to introduce a new
`odb_source_count_objects()` function so that we can make the logic
pluggable. Prepare for this change by extracting the logic that we have
to count packed objects into a standalone function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
packfile.c
packfile.h

index 215a23e42be0fe374467bc5971806e5d13457b91..1ee5dd3da35c071017e7efbfc03edf51fa8c8e4a 100644 (file)
@@ -1101,6 +1101,36 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
        return store->packs.head;
 }
 
+int packfile_store_count_objects(struct packfile_store *store,
+                                unsigned long *out)
+{
+       struct packfile_list_entry *e;
+       struct multi_pack_index *m;
+       unsigned long count = 0;
+       int ret;
+
+       m = get_multi_pack_index(store->source);
+       if (m)
+               count += m->num_objects + m->num_objects_in_base;
+
+       for (e = packfile_store_get_packs(store); e; e = e->next) {
+               if (e->pack->multi_pack_index)
+                       continue;
+               if (open_pack_index(e->pack)) {
+                       ret = -1;
+                       goto out;
+               }
+
+               count += e->pack->num_objects;
+       }
+
+       *out = count;
+       ret = 0;
+
+out:
+       return ret;
+}
+
 /*
  * Give a fast, rough count of the number of objects in the repository. This
  * ignores loose objects completely. If you have a lot of them, then either
@@ -1113,21 +1143,16 @@ unsigned long repo_approximate_object_count(struct repository *r)
        if (!r->objects->approximate_object_count_valid) {
                struct odb_source *source;
                unsigned long count = 0;
-               struct packed_git *p;
 
                odb_prepare_alternates(r->objects);
-
                for (source = r->objects->sources; source; source = source->next) {
-                       struct multi_pack_index *m = get_multi_pack_index(source);
-                       if (m)
-                               count += m->num_objects + m->num_objects_in_base;
-               }
+                       struct odb_source_files *files = odb_source_files_downcast(source);
+                       unsigned long c;
 
-               repo_for_each_pack(r, p) {
-                       if (p->multi_pack_index || open_pack_index(p))
-                               continue;
-                       count += p->num_objects;
+                       if (!packfile_store_count_objects(files->packed, &c))
+                               count += c;
                }
+
                r->objects->approximate_object_count = count;
                r->objects->approximate_object_count_valid = 1;
        }
index 8b04a258a7b9d54b6156e88218022424d2231035..1da8c729cba131ae7e130081e8910a70e9a6ae6a 100644 (file)
@@ -268,6 +268,15 @@ enum kept_pack_type {
        KEPT_PACK_IN_CORE = (1 << 1),
 };
 
+/*
+ * Count the number objects contained in the given packfile store. If
+ * successful, the number of objects will be written to the `out` pointer.
+ *
+ * Return 0 on success, a negative error code otherwise.
+ */
+int packfile_store_count_objects(struct packfile_store *store,
+                                unsigned long *out);
+
 /*
  * Retrieve the cache of kept packs from the given packfile store. Accepts a
  * combination of `kept_pack_type` flags. The cache is computed on demand and