builtin/fsck: refactor to use odb_for_each_object()

In git-fsck(1) we have two callsites where we iterate over all objects
via `for_each_loose_object()` and `for_each_packed_object()`. Both of
these are trivially convertible with `odb_for_each_object()`.

Refactor these callsites accordingly.

Note that `odb_for_each_object()` may iterate over the same object
multiple times, for example when it exists both in packed and loose
format. But this has already been the case beforehand, so this does not
result in a change in behaviour.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2026-01-26 10:51:24 +01:00
committed by Junio C Hamano
parent df2fbdfa55
commit cc47e3d38c

View File

@@ -218,15 +218,17 @@ static int mark_used(struct object *obj, enum object_type type UNUSED,
return 0;
}
static void mark_unreachable_referents(const struct object_id *oid)
static int mark_unreachable_referents(const struct object_id *oid,
struct object_info *oi UNUSED,
void *data UNUSED)
{
struct fsck_options options = FSCK_OPTIONS_DEFAULT;
struct object *obj = lookup_object(the_repository, oid);
if (!obj || !(obj->flags & HAS_OBJ))
return; /* not part of our original set */
return 0; /* not part of our original set */
if (obj->flags & REACHABLE)
return; /* reachable objects already traversed */
return 0; /* reachable objects already traversed */
/*
* Avoid passing OBJ_NONE to fsck_walk, which will parse the object
@@ -243,22 +245,7 @@ static void mark_unreachable_referents(const struct object_id *oid)
fsck_walk(obj, NULL, &options);
if (obj->type == OBJ_TREE)
free_tree_buffer((struct tree *)obj);
}
static int mark_loose_unreachable_referents(const struct object_id *oid,
const char *path UNUSED,
void *data UNUSED)
{
mark_unreachable_referents(oid);
return 0;
}
static int mark_packed_unreachable_referents(const struct object_id *oid,
struct packed_git *pack UNUSED,
uint32_t pos UNUSED,
void *data UNUSED)
{
mark_unreachable_referents(oid);
return 0;
}
@@ -394,12 +381,8 @@ static void check_connectivity(void)
* and ignore any that weren't present in our earlier
* traversal.
*/
for_each_loose_object(the_repository->objects,
mark_loose_unreachable_referents, NULL, 0);
for_each_packed_object(the_repository,
mark_packed_unreachable_referents,
NULL,
0);
odb_for_each_object(the_repository->objects, NULL,
mark_unreachable_referents, NULL, 0);
}
/* Look up all the requirements, warn about missing objects.. */
@@ -848,26 +831,12 @@ static void fsck_index(struct index_state *istate, const char *index_path,
fsck_resolve_undo(istate, index_path);
}
static void mark_object_for_connectivity(const struct object_id *oid)
static int mark_object_for_connectivity(const struct object_id *oid,
struct object_info *oi UNUSED,
void *cb_data UNUSED)
{
struct object *obj = lookup_unknown_object(the_repository, oid);
obj->flags |= HAS_OBJ;
}
static int mark_loose_for_connectivity(const struct object_id *oid,
const char *path UNUSED,
void *data UNUSED)
{
mark_object_for_connectivity(oid);
return 0;
}
static int mark_packed_for_connectivity(const struct object_id *oid,
struct packed_git *pack UNUSED,
uint32_t pos UNUSED,
void *data UNUSED)
{
mark_object_for_connectivity(oid);
return 0;
}
@@ -1001,10 +970,8 @@ int cmd_fsck(int argc,
fsck_refs(the_repository);
if (connectivity_only) {
for_each_loose_object(the_repository->objects,
mark_loose_for_connectivity, NULL, 0);
for_each_packed_object(the_repository,
mark_packed_for_connectivity, NULL, 0);
odb_for_each_object(the_repository->objects, NULL,
mark_object_for_connectivity, NULL, 0);
} else {
odb_prepare_alternates(the_repository->objects);
for (source = the_repository->objects->sources; source; source = source->next)