packfile: extract logic to count number of objects
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>
Patrick Steinhardt committed
Mar 12, 2026 at 09:42 UTC
dd587cd59e1575f2d5698cb45b42644e1df9b835
2 files changed
+44
-10
packfile.c
+35
-10
@@ -1101,6 +1101,36 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
1101
return store->packs.head;
1102
}
1103
1104
+int packfile_store_count_objects(struct packfile_store *store,
1105
+ unsigned long *out)
1106
+{
1107
+ struct packfile_list_entry *e;
1108
+ struct multi_pack_index *m;
1109
+ unsigned long count = 0;
1110
+ int ret;
1111
+
1112
+ m = get_multi_pack_index(store->source);
1113
+ if (m)
1114
+ count += m->num_objects + m->num_objects_in_base;
1115
+
1116
+ for (e = packfile_store_get_packs(store); e; e = e->next) {
1117
+ if (e->pack->multi_pack_index)
1118
+ continue;
1119
+ if (open_pack_index(e->pack)) {
1120
+ ret = -1;
1121
+ goto out;
1122
+ }
1123
+
1124
+ count += e->pack->num_objects;
1125
+ }
1126
+
1127
+ *out = count;
1128
+ ret = 0;
1129
+
1130
+out:
1131
+ return ret;
1132
+}
1133
+
1134
/*
1135
* Give a fast, rough count of the number of objects in the repository. This
1136
* 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)
1143
if (!r->objects->approximate_object_count_valid) {
1144
struct odb_source *source;
1145
unsigned long count = 0;
1116
- struct packed_git *p;
1146
1147
odb_prepare_alternates(r->objects);
1119
-
1148
for (source = r->objects->sources; source; source = source->next) {
1121
- struct multi_pack_index *m = get_multi_pack_index(source);
1122
- if (m)
1123
- count += m->num_objects + m->num_objects_in_base;
1124
- }
1149
+ struct odb_source_files *files = odb_source_files_downcast(source);
1150
+ unsigned long c;
1151
1126
- repo_for_each_pack(r, p) {
1127
- if (p->multi_pack_index || open_pack_index(p))
1128
- continue;
1129
- count += p->num_objects;
1152
+ if (!packfile_store_count_objects(files->packed, &c))
1153
+ count += c;
1154
}
1155
+
1156
r->objects->approximate_object_count = count;
1157
r->objects->approximate_object_count_valid = 1;
1158
}
packfile.h
+9
@@ -268,6 +268,15 @@ enum kept_pack_type {
268
KEPT_PACK_IN_CORE = (1 << 1),
269
};
270
271
+/*
272
+ * Count the number objects contained in the given packfile store. If
273
+ * successful, the number of objects will be written to the `out` pointer.
274
+ *
275
+ * Return 0 on success, a negative error code otherwise.
276
+ */
277
+int packfile_store_count_objects(struct packfile_store *store,
278
+ unsigned long *out);
279
+
280
/*
281
* Retrieve the cache of kept packs from the given packfile store. Accepts a
282
* combination of `kept_pack_type` flags. The cache is computed on demand and