object-file: get rid of `the_repository` in loose object iterators

The iterators for loose objects still rely on `the_repository`. Refactor them: - `for_each_loose_file_in_objdir()` is refactored so that the caller is now expected to pass an `odb_source` as parameter instead of the path to that source. Furthermore, it is renamed accordingly to `for_each_loose_file_in_source()`. - `for_each_loose_object()` is refactored to take in an object database now and calls the above function in a loop. This allows us to get rid of the global dependency. Adjust callers accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2025 at 06:56 UTC d81712ce65f7ee59ce88c8b74f09b6e6456a6f3c
10 files changed +31 -31
builtin/cat-file.c
+1 -1
@@ -848,7 +848,7 @@ static void batch_each_object(struct batch_options *opt,
848 };
849 struct bitmap_index *bitmap = prepare_bitmap_git(the_repository);
850
851 - for_each_loose_object(batch_one_object_loose, &payload, 0);
851 + for_each_loose_object(the_repository->objects, batch_one_object_loose, &payload, 0);
852
853 if (bitmap && !for_each_bitmapped_object(bitmap, &opt->objects_filter,
854 batch_one_object_bitmapped, &payload)) {
builtin/count-objects.c
+1 -1
@@ -117,7 +117,7 @@ int cmd_count_objects(int argc,
117 report_linked_checkout_garbage(the_repository);
118 }
119
120 - for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
120 + for_each_loose_file_in_source(the_repository->objects->sources,
121 count_loose, count_cruft, NULL, NULL);
122
123 if (verbose) {
builtin/fsck.c
+8 -6
@@ -393,7 +393,8 @@ static void check_connectivity(void)
393 * and ignore any that weren't present in our earlier
394 * traversal.
395 */
396 - for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
396 + for_each_loose_object(the_repository->objects,
397 + mark_loose_unreachable_referents, NULL, 0);
398 for_each_packed_object(the_repository,
399 mark_packed_unreachable_referents,
400 NULL,
@@ -687,7 +688,7 @@ static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
688 return 0;
689 }
690
690 -static void fsck_object_dir(const char *path)
691 +static void fsck_source(struct odb_source *source)
692 {
693 struct progress *progress = NULL;
694 struct for_each_loose_cb cb_data = {
@@ -701,8 +702,8 @@ static void fsck_object_dir(const char *path)
702 progress = start_progress(the_repository,
703 _("Checking object directories"), 256);
704
704 - for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
705 - &cb_data);
705 + for_each_loose_file_in_source(source, fsck_loose,
706 + fsck_cruft, fsck_subdir, &cb_data);
707 display_progress(progress, 256);
708 stop_progress(&progress);
709 }
@@ -994,13 +995,14 @@ int cmd_fsck(int argc,
995 fsck_refs(the_repository);
996
997 if (connectivity_only) {
997 - for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
998 + for_each_loose_object(the_repository->objects,
999 + mark_loose_for_connectivity, NULL, 0);
1000 for_each_packed_object(the_repository,
1001 mark_packed_for_connectivity, NULL, 0);
1002 } else {
1003 odb_prepare_alternates(the_repository->objects);
1004 for (source = the_repository->objects->sources; source; source = source->next)
1003 - fsck_object_dir(source->path);
1005 + fsck_source(source);
1006
1007 if (check_full) {
1008 struct packed_git *p;
builtin/gc.c
+4 -6
@@ -1301,7 +1301,7 @@ static int loose_object_auto_condition(struct gc_config *cfg UNUSED)
1301 if (loose_object_auto_limit < 0)
1302 return 1;
1303
1304 - return for_each_loose_file_in_objdir(the_repository->objects->sources->path,
1304 + return for_each_loose_file_in_source(the_repository->objects->sources,
1305 loose_object_count,
1306 NULL, NULL, &count);
1307 }
@@ -1336,7 +1336,7 @@ static int pack_loose(struct maintenance_run_opts *opts)
1336 * Do not start pack-objects process
1337 * if there are no loose objects.
1338 */
1339 - if (!for_each_loose_file_in_objdir(r->objects->sources->path,
1339 + if (!for_each_loose_file_in_source(r->objects->sources,
1340 bail_on_loose,
1341 NULL, NULL, NULL))
1342 return 0;
@@ -1376,11 +1376,9 @@ static int pack_loose(struct maintenance_run_opts *opts)
1376 else if (data.batch_size > 0)
1377 data.batch_size--; /* Decrease for equality on limit. */
1378
1379 - for_each_loose_file_in_objdir(r->objects->sources->path,
1379 + for_each_loose_file_in_source(r->objects->sources,
1380 write_loose_object_to_stdin,
1381 - NULL,
1382 - NULL,
1383 - &data);
1381 + NULL, NULL, &data);
1382
1383 fclose(data.in);
1384
builtin/pack-objects.c
+2 -3
@@ -4342,9 +4342,8 @@ static int add_loose_object(const struct object_id *oid, const char *path,
4342 */
4343 static void add_unreachable_loose_objects(void)
4344 {
4345 - for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
4346 - add_loose_object,
4347 - NULL, NULL, NULL);
4345 + for_each_loose_file_in_source(the_repository->objects->sources,
4346 + add_loose_object, NULL, NULL, NULL);
4347 }
4348
4349 static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
builtin/prune.c
+1 -1
@@ -200,7 +200,7 @@ int cmd_prune(int argc,
200 revs.exclude_promisor_objects = 1;
201 }
202
203 - for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
203 + for_each_loose_file_in_source(the_repository->objects->sources,
204 prune_object, prune_cruft, prune_subdir, &revs);
205
206 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
object-file.c
+9 -9
@@ -1388,7 +1388,7 @@ static int for_each_file_in_obj_subdir(unsigned int subdir_nr,
1388 return r;
1389 }
1390
1391 -int for_each_loose_file_in_objdir(const char *path,
1391 +int for_each_loose_file_in_source(struct odb_source *source,
1392 each_loose_object_fn obj_cb,
1393 each_loose_cruft_fn cruft_cb,
1394 each_loose_subdir_fn subdir_cb,
@@ -1397,11 +1397,10 @@ int for_each_loose_file_in_objdir(const char *path,
1397 struct strbuf buf = STRBUF_INIT;
1398 int r;
1399
1400 - strbuf_addstr(&buf, path);
1400 + strbuf_addstr(&buf, source->path);
1401 for (int i = 0; i < 256; i++) {
1402 - r = for_each_file_in_obj_subdir(i, &buf, the_repository->hash_algo,
1403 - obj_cb, cruft_cb,
1404 - subdir_cb, data);
1402 + r = for_each_file_in_obj_subdir(i, &buf, source->odb->repo->hash_algo,
1403 + obj_cb, cruft_cb, subdir_cb, data);
1404 if (r)
1405 break;
1406 }
@@ -1410,14 +1409,15 @@ int for_each_loose_file_in_objdir(const char *path,
1409 return r;
1410 }
1411
1413 -int for_each_loose_object(each_loose_object_fn cb, void *data,
1412 +int for_each_loose_object(struct object_database *odb,
1413 + each_loose_object_fn cb, void *data,
1414 enum for_each_object_flags flags)
1415 {
1416 struct odb_source *source;
1417
1418 - odb_prepare_alternates(the_repository->objects);
1419 - for (source = the_repository->objects->sources; source; source = source->next) {
1420 - int r = for_each_loose_file_in_objdir(source->path, cb, NULL,
1418 + odb_prepare_alternates(odb);
1419 + for (source = odb->sources; source; source = source->next) {
1420 + int r = for_each_loose_file_in_source(source, cb, NULL,
1421 NULL, data);
1422 if (r)
1423 return r;
object-file.h
+3 -2
@@ -86,7 +86,7 @@ typedef int each_loose_cruft_fn(const char *basename,
86 typedef int each_loose_subdir_fn(unsigned int nr,
87 const char *path,
88 void *data);
89 -int for_each_loose_file_in_objdir(const char *path,
89 +int for_each_loose_file_in_source(struct odb_source *source,
90 each_loose_object_fn obj_cb,
91 each_loose_cruft_fn cruft_cb,
92 each_loose_subdir_fn subdir_cb,
@@ -99,7 +99,8 @@ int for_each_loose_file_in_objdir(const char *path,
99 *
100 * Any flags specific to packs are ignored.
101 */
102 -int for_each_loose_object(each_loose_object_fn, void *,
102 +int for_each_loose_object(struct object_database *odb,
103 + each_loose_object_fn, void *,
104 enum for_each_object_flags flags);
105
106
prune-packed.c
+1 -1
@@ -40,7 +40,7 @@ void prune_packed_objects(int opts)
40 progress = start_delayed_progress(the_repository,
41 _("Removing duplicate objects"), 256);
42
43 - for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
43 + for_each_loose_file_in_source(the_repository->objects->sources,
44 prune_object, NULL, prune_subdir, &opts);
45
46 /* Ensure we show 100% before finishing progress */
reachable.c
+1 -1
@@ -319,7 +319,7 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
319 oidset_init(&data.extra_recent_oids, 0);
320 data.extra_recent_oids_loaded = 0;
321
322 - r = for_each_loose_object(add_recent_loose, &data,
322 + r = for_each_loose_object(the_repository->objects, add_recent_loose, &data,
323 FOR_EACH_OBJECT_LOCAL_ONLY);
324 if (r)
325 goto done;