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>
Patrick Steinhardt committed
Jan 26, 2026 at 10:51 UTC
cc47e3d38c5be2969df3dba6814ee0e685a07de2
1 file changed
+12
-45
builtin/fsck.c
+12
-45
@@ -218,15 +218,17 @@ static int mark_used(struct object *obj, enum object_type type UNUSED,
218
return 0;
219
}
220
221
-static void mark_unreachable_referents(const struct object_id *oid)
221
+static int mark_unreachable_referents(const struct object_id *oid,
222
+ struct object_info *oi UNUSED,
223
+ void *data UNUSED)
224
{
225
struct fsck_options options = FSCK_OPTIONS_DEFAULT;
226
struct object *obj = lookup_object(the_repository, oid);
227
228
if (!obj || !(obj->flags & HAS_OBJ))
227
- return; /* not part of our original set */
229
+ return 0; /* not part of our original set */
230
if (obj->flags & REACHABLE)
229
- return; /* reachable objects already traversed */
231
+ return 0; /* reachable objects already traversed */
232
233
/*
234
* 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)
245
fsck_walk(obj, NULL, &options);
246
if (obj->type == OBJ_TREE)
247
free_tree_buffer((struct tree *)obj);
246
-}
248
248
-static int mark_loose_unreachable_referents(const struct object_id *oid,
249
- const char *path UNUSED,
250
- void *data UNUSED)
251
-{
252
- mark_unreachable_referents(oid);
253
- return 0;
254
-}
255
-
256
-static int mark_packed_unreachable_referents(const struct object_id *oid,
257
- struct packed_git *pack UNUSED,
258
- uint32_t pos UNUSED,
259
- void *data UNUSED)
260
-{
261
- mark_unreachable_referents(oid);
249
return 0;
250
}
251
@@ -394,12 +381,8 @@ static void check_connectivity(void)
381
* and ignore any that weren't present in our earlier
382
* traversal.
383
*/
397
- for_each_loose_object(the_repository->objects,
398
- mark_loose_unreachable_referents, NULL, 0);
399
- for_each_packed_object(the_repository,
400
- mark_packed_unreachable_referents,
401
- NULL,
402
- 0);
384
+ odb_for_each_object(the_repository->objects, NULL,
385
+ mark_unreachable_referents, NULL, 0);
386
}
387
388
/* 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,
831
fsck_resolve_undo(istate, index_path);
832
}
833
851
-static void mark_object_for_connectivity(const struct object_id *oid)
834
+static int mark_object_for_connectivity(const struct object_id *oid,
835
+ struct object_info *oi UNUSED,
836
+ void *cb_data UNUSED)
837
{
838
struct object *obj = lookup_unknown_object(the_repository, oid);
839
obj->flags |= HAS_OBJ;
855
-}
856
-
857
-static int mark_loose_for_connectivity(const struct object_id *oid,
858
- const char *path UNUSED,
859
- void *data UNUSED)
860
-{
861
- mark_object_for_connectivity(oid);
862
- return 0;
863
-}
864
-
865
-static int mark_packed_for_connectivity(const struct object_id *oid,
866
- struct packed_git *pack UNUSED,
867
- uint32_t pos UNUSED,
868
- void *data UNUSED)
869
-{
870
- mark_object_for_connectivity(oid);
840
return 0;
841
}
842
@@ -1001,10 +970,8 @@ int cmd_fsck(int argc,
970
fsck_refs(the_repository);
971
972
if (connectivity_only) {
1004
- for_each_loose_object(the_repository->objects,
1005
- mark_loose_for_connectivity, NULL, 0);
1006
- for_each_packed_object(the_repository,
1007
- mark_packed_for_connectivity, NULL, 0);
973
+ odb_for_each_object(the_repository->objects, NULL,
974
+ mark_object_for_connectivity, NULL, 0);
975
} else {
976
odb_prepare_alternates(the_repository->objects);
977
for (source = the_repository->objects->sources; source; source = source->next)