Skip to content

Commit dd587cd

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 6daeb66 commit dd587cd

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

packfile.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,36 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
11011101
return store->packs.head;
11021102
}
11031103

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+
11041134
/*
11051135
* Give a fast, rough count of the number of objects in the repository. This
11061136
* 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)
11131143
if (!r->objects->approximate_object_count_valid) {
11141144
struct odb_source *source;
11151145
unsigned long count = 0;
1116-
struct packed_git *p;
11171146

11181147
odb_prepare_alternates(r->objects);
1119-
11201148
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;
11251151

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;
11301154
}
1155+
11311156
r->objects->approximate_object_count = count;
11321157
r->objects->approximate_object_count_valid = 1;
11331158
}

packfile.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ enum kept_pack_type {
268268
KEPT_PACK_IN_CORE = (1 << 1),
269269
};
270270

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+
271280
/*
272281
* Retrieve the cache of kept packs from the given packfile store. Accepts a
273282
* combination of `kept_pack_type` flags. The cache is computed on demand and

0 commit comments

Comments
 (0)