treewide: drop uses of `for_each_{loose,packed}_object()`

We're using `for_each_loose_object()` and `for_each_packed_object()` at a couple of callsites to enumerate all loose and packed objects, respectively. These functions will be removed in a subsequent commit in favor of the newly introduced `odb_source_loose_for_each_object()` and `packfile_store_for_each_object()` replacements. Prepare for this by refactoring the sites accordingly. Note that ideally, we'd convert all callsites to use the generic `odb_for_each_object()` function already. But for some callers this is not possible (yet), and it would require some significant refactorings to make this work. Converting these site will thus be deferred to a later patch series. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jan 26, 2026 at 10:51 UTC 317ea9a6c3c134a1bcdee49bbbbf1731c17b967a
2 files changed +59 -19
builtin/cat-file.c
+28 -6
@@ -806,11 +806,14 @@ struct for_each_object_payload {
806 void *payload;
807 };
808
809 -static int batch_one_object_loose(const struct object_id *oid,
810 - const char *path UNUSED,
811 - void *_payload)
809 +static int batch_one_object_oi(const struct object_id *oid,
810 + struct object_info *oi,
811 + void *_payload)
812 {
813 struct for_each_object_payload *payload = _payload;
814 + if (oi && oi->whence == OI_PACKED)
815 + return payload->callback(oid, oi->u.packed.pack, oi->u.packed.offset,
816 + payload->payload);
817 return payload->callback(oid, NULL, 0, payload->payload);
818 }
819
@@ -846,8 +849,21 @@ static void batch_each_object(struct batch_options *opt,
849 .payload = _payload,
850 };
851 struct bitmap_index *bitmap = prepare_bitmap_git(the_repository);
852 + struct odb_source *source;
853
850 - for_each_loose_object(the_repository->objects, batch_one_object_loose, &payload, 0);
854 + /*
855 + * TODO: we still need to tap into implementation details of the object
856 + * database sources. Ideally, we should extend `odb_for_each_object()`
857 + * to handle object filters itself so that we can move the filtering
858 + * logic into the individual sources.
859 + */
860 + odb_prepare_alternates(the_repository->objects);
861 + for (source = the_repository->objects->sources; source; source = source->next) {
862 + int ret = odb_source_loose_for_each_object(source, NULL, batch_one_object_oi,
863 + &payload, flags);
864 + if (ret)
865 + break;
866 + }
867
868 if (bitmap && !for_each_bitmapped_object(bitmap, &opt->objects_filter,
869 batch_one_object_bitmapped, &payload)) {
@@ -861,8 +877,14 @@ static void batch_each_object(struct batch_options *opt,
877 &payload, flags);
878 }
879 } else {
864 - for_each_packed_object(the_repository, batch_one_object_packed,
865 - &payload, flags);
880 + struct object_info oi = { 0 };
881 +
882 + for (source = the_repository->objects->sources; source; source = source->next) {
883 + int ret = packfile_store_for_each_object(source->packfiles, &oi,
884 + batch_one_object_oi, &payload, flags);
885 + if (ret)
886 + break;
887 + }
888 }
889
890 free_bitmap_index(bitmap);
commit-graph.c
+31 -13
@@ -1479,30 +1479,38 @@ static int write_graph_chunk_bloom_data(struct hashfile *f,
1479 return 0;
1480 }
1481
1482 +static int add_packed_commits_oi(const struct object_id *oid,
1483 + struct object_info *oi,
1484 + void *data)
1485 +{
1486 + struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1487 +
1488 + if (ctx->progress)
1489 + display_progress(ctx->progress, ++ctx->progress_done);
1490 +
1491 + if (*oi->typep != OBJ_COMMIT)
1492 + return 0;
1493 +
1494 + oid_array_append(&ctx->oids, oid);
1495 + set_commit_pos(ctx->r, oid);
1496 +
1497 + return 0;
1498 +}
1499 +
1500 static int add_packed_commits(const struct object_id *oid,
1501 struct packed_git *pack,
1502 uint32_t pos,
1503 void *data)
1504 {
1487 - struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1505 enum object_type type;
1506 off_t offset = nth_packed_object_offset(pack, pos);
1507 struct object_info oi = OBJECT_INFO_INIT;
1508
1492 - if (ctx->progress)
1493 - display_progress(ctx->progress, ++ctx->progress_done);
1494 -
1509 oi.typep = &type;
1510 if (packed_object_info(pack, offset, &oi) < 0)
1511 die(_("unable to get type of object %s"), oid_to_hex(oid));
1512
1499 - if (type != OBJ_COMMIT)
1500 - return 0;
1501 -
1502 - oid_array_append(&ctx->oids, oid);
1503 - set_commit_pos(ctx->r, oid);
1504 -
1505 - return 0;
1513 + return add_packed_commits_oi(oid, &oi, data);
1514 }
1515
1516 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
@@ -1959,13 +1967,23 @@ static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1967
1968 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1969 {
1970 + struct odb_source *source;
1971 + enum object_type type;
1972 + struct object_info oi = {
1973 + .typep = &type,
1974 + };
1975 +
1976 if (ctx->report_progress)
1977 ctx->progress = start_delayed_progress(
1978 ctx->r,
1979 _("Finding commits for commit graph among packed objects"),
1980 ctx->approx_nr_objects);
1967 - for_each_packed_object(ctx->r, add_packed_commits, ctx,
1968 - ODB_FOR_EACH_OBJECT_PACK_ORDER);
1981 +
1982 + odb_prepare_alternates(ctx->r->objects);
1983 + for (source = ctx->r->objects->sources; source; source = source->next)
1984 + packfile_store_for_each_object(source->packfiles, &oi, add_packed_commits_oi,
1985 + ctx, ODB_FOR_EACH_OBJECT_PACK_ORDER);
1986 +
1987 if (ctx->progress_done < ctx->approx_nr_objects)
1988 display_progress(ctx->progress, ctx->approx_nr_objects);
1989 stop_progress(&ctx->progress);