aboutsummaryrefslogtreecommitdiff
path: root/object-file.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2026-03-12 09:42:59 +0100
committerJunio C Hamano <gitster@pobox.com>2026-03-12 08:38:42 -0700
commit2b24db1110150138ac1e09bc60d9ae5245909625 (patch)
tree1661f7964868da60d42883b5ebb0376e8ea17bde /object-file.c
parent222fddeaa44b633eea345996735b4f7893eb71ec (diff)
downloadgit-2b24db1110150138ac1e09bc60d9ae5245909625.tar.xz
object-file: generalize counting objects
Generalize the function introduced in the preceding commit to not only be able to approximate the number of loose objects, but to also provide an accurate count. The behaviour can be toggled via a new flag. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'object-file.c')
-rw-r--r--object-file.c59
1 files changed, 38 insertions, 21 deletions
diff --git a/object-file.c b/object-file.c
index da67e3c9ff..569ce6eaed 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1868,40 +1868,57 @@ int odb_source_loose_for_each_object(struct odb_source *source,
NULL, NULL, &data);
}
-int odb_source_loose_approximate_object_count(struct odb_source *source,
- unsigned long *out)
+static int count_loose_object(const struct object_id *oid UNUSED,
+ struct object_info *oi UNUSED,
+ void *payload)
+{
+ unsigned long *count = payload;
+ (*count)++;
+ return 0;
+}
+
+int odb_source_loose_count_objects(struct odb_source *source,
+ enum odb_count_objects_flags flags,
+ unsigned long *out)
{
const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2;
- unsigned long count = 0;
- struct dirent *ent;
char *path = NULL;
DIR *dir = NULL;
int ret;
- path = xstrfmt("%s/17", source->path);
+ if (flags & ODB_COUNT_OBJECTS_APPROXIMATE) {
+ unsigned long count = 0;
+ struct dirent *ent;
- dir = opendir(path);
- if (!dir) {
- if (errno == ENOENT) {
- *out = 0;
- ret = 0;
+ path = xstrfmt("%s/17", source->path);
+
+ dir = opendir(path);
+ if (!dir) {
+ if (errno == ENOENT) {
+ *out = 0;
+ ret = 0;
+ goto out;
+ }
+
+ ret = error_errno("cannot open object shard '%s'", path);
goto out;
}
- ret = error_errno("cannot open object shard '%s'", path);
- goto out;
- }
+ while ((ent = readdir(dir)) != NULL) {
+ if (strspn(ent->d_name, "0123456789abcdef") != hexsz ||
+ ent->d_name[hexsz] != '\0')
+ continue;
+ count++;
+ }
- while ((ent = readdir(dir)) != NULL) {
- if (strspn(ent->d_name, "0123456789abcdef") != hexsz ||
- ent->d_name[hexsz] != '\0')
- continue;
- count++;
+ *out = count * 256;
+ ret = 0;
+ } else {
+ *out = 0;
+ ret = odb_source_loose_for_each_object(source, NULL, count_loose_object,
+ out, 0);
}
- *out = count * 256;
- ret = 0;
-
out:
if (dir)
closedir(dir);