]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: get rid of `the_repository` in loose object iterators
authorPatrick Steinhardt <ps@pks.im>
Thu, 17 Jul 2025 04:56:39 +0000 (06:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Jul 2025 05:16:17 +0000 (22:16 -0700)
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>
builtin/cat-file.c
builtin/count-objects.c
builtin/fsck.c
builtin/gc.c
builtin/pack-objects.c
builtin/prune.c
object-file.c
object-file.h
prune-packed.c
reachable.c

index 2492a0b6f39ac3d372850aae66fd23be586c805e..aa1498aa60f2bb9d8165dae6b9d6f32781b53f48 100644 (file)
@@ -848,7 +848,7 @@ static void batch_each_object(struct batch_options *opt,
        };
        struct bitmap_index *bitmap = prepare_bitmap_git(the_repository);
 
-       for_each_loose_object(batch_one_object_loose, &payload, 0);
+       for_each_loose_object(the_repository->objects, batch_one_object_loose, &payload, 0);
 
        if (bitmap && !for_each_bitmapped_object(bitmap, &opt->objects_filter,
                                                 batch_one_object_bitmapped, &payload)) {
index f687647931e0e21189e75fdfeb20ad6b3fcb40ea..e70a01c628e55f1f98282abce135eae650146834 100644 (file)
@@ -117,7 +117,7 @@ int cmd_count_objects(int argc,
                report_linked_checkout_garbage(the_repository);
        }
 
-       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
+       for_each_loose_file_in_source(the_repository->objects->sources,
                                      count_loose, count_cruft, NULL, NULL);
 
        if (verbose) {
index 0084cf7400bd46ba7d8b72a908a67c3536abc907..f0854ce5d847d3aeb4415e2ee9835a495b600f01 100644 (file)
@@ -393,7 +393,8 @@ static void check_connectivity(void)
                 * and ignore any that weren't present in our earlier
                 * traversal.
                 */
-               for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
+               for_each_loose_object(the_repository->objects,
+                                     mark_loose_unreachable_referents, NULL, 0);
                for_each_packed_object(the_repository,
                                       mark_packed_unreachable_referents,
                                       NULL,
@@ -687,7 +688,7 @@ static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
        return 0;
 }
 
-static void fsck_object_dir(const char *path)
+static void fsck_source(struct odb_source *source)
 {
        struct progress *progress = NULL;
        struct for_each_loose_cb cb_data = {
@@ -701,8 +702,8 @@ static void fsck_object_dir(const char *path)
                progress = start_progress(the_repository,
                                          _("Checking object directories"), 256);
 
-       for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
-                                     &cb_data);
+       for_each_loose_file_in_source(source, fsck_loose,
+                                     fsck_cruft, fsck_subdir, &cb_data);
        display_progress(progress, 256);
        stop_progress(&progress);
 }
@@ -994,13 +995,14 @@ int cmd_fsck(int argc,
                fsck_refs(the_repository);
 
        if (connectivity_only) {
-               for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
+               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);
        } else {
                odb_prepare_alternates(the_repository->objects);
                for (source = the_repository->objects->sources; source; source = source->next)
-                       fsck_object_dir(source->path);
+                       fsck_source(source);
 
                if (check_full) {
                        struct packed_git *p;
index 21bd44e1645eacda890f059e2b97031d36ef652d..6eefefc63d20adeb3fd33922082983c27c492c4e 100644 (file)
@@ -1301,7 +1301,7 @@ static int loose_object_auto_condition(struct gc_config *cfg UNUSED)
        if (loose_object_auto_limit < 0)
                return 1;
 
-       return for_each_loose_file_in_objdir(the_repository->objects->sources->path,
+       return for_each_loose_file_in_source(the_repository->objects->sources,
                                             loose_object_count,
                                             NULL, NULL, &count);
 }
@@ -1336,7 +1336,7 @@ static int pack_loose(struct maintenance_run_opts *opts)
         * Do not start pack-objects process
         * if there are no loose objects.
         */
-       if (!for_each_loose_file_in_objdir(r->objects->sources->path,
+       if (!for_each_loose_file_in_source(r->objects->sources,
                                           bail_on_loose,
                                           NULL, NULL, NULL))
                return 0;
@@ -1376,11 +1376,9 @@ static int pack_loose(struct maintenance_run_opts *opts)
        else if (data.batch_size > 0)
                data.batch_size--; /* Decrease for equality on limit. */
 
-       for_each_loose_file_in_objdir(r->objects->sources->path,
+       for_each_loose_file_in_source(r->objects->sources,
                                      write_loose_object_to_stdin,
-                                     NULL,
-                                     NULL,
-                                     &data);
+                                     NULL, NULL, &data);
 
        fclose(data.in);
 
index e8e85d8278bba682a872916cff99d256eed9fce3..9e85293730be1aa999f601951a35f3c24d9c628e 100644 (file)
@@ -4342,9 +4342,8 @@ static int add_loose_object(const struct object_id *oid, const char *path,
  */
 static void add_unreachable_loose_objects(void)
 {
-       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
-                                     add_loose_object,
-                                     NULL, NULL, NULL);
+       for_each_loose_file_in_source(the_repository->objects->sources,
+                                     add_loose_object, NULL, NULL, NULL);
 }
 
 static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
index 339017c7ccf37c7e3df49643f3fdfb54a885c544..bf5d3bb152cf0ef9cf12a30649a6c40c00b99311 100644 (file)
@@ -200,7 +200,7 @@ int cmd_prune(int argc,
                revs.exclude_promisor_objects = 1;
        }
 
-       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
+       for_each_loose_file_in_source(the_repository->objects->sources,
                                      prune_object, prune_cruft, prune_subdir, &revs);
 
        prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
index bd93f17dcfe6f6d1fdf911ac5d070713db20a572..b894379d22cc6f1a499d9a5490bdca7a8a63d379 100644 (file)
@@ -1388,7 +1388,7 @@ static int for_each_file_in_obj_subdir(unsigned int subdir_nr,
        return r;
 }
 
-int for_each_loose_file_in_objdir(const char *path,
+int for_each_loose_file_in_source(struct odb_source *source,
                                  each_loose_object_fn obj_cb,
                                  each_loose_cruft_fn cruft_cb,
                                  each_loose_subdir_fn subdir_cb,
@@ -1397,11 +1397,10 @@ int for_each_loose_file_in_objdir(const char *path,
        struct strbuf buf = STRBUF_INIT;
        int r;
 
-       strbuf_addstr(&buf, path);
+       strbuf_addstr(&buf, source->path);
        for (int i = 0; i < 256; i++) {
-               r = for_each_file_in_obj_subdir(i, &buf, the_repository->hash_algo,
-                                               obj_cb, cruft_cb,
-                                               subdir_cb, data);
+               r = for_each_file_in_obj_subdir(i, &buf, source->odb->repo->hash_algo,
+                                               obj_cb, cruft_cb, subdir_cb, data);
                if (r)
                        break;
        }
@@ -1410,14 +1409,15 @@ int for_each_loose_file_in_objdir(const char *path,
        return r;
 }
 
-int for_each_loose_object(each_loose_object_fn cb, void *data,
+int for_each_loose_object(struct object_database *odb,
+                         each_loose_object_fn cb, void *data,
                          enum for_each_object_flags flags)
 {
        struct odb_source *source;
 
-       odb_prepare_alternates(the_repository->objects);
-       for (source = the_repository->objects->sources; source; source = source->next) {
-               int r = for_each_loose_file_in_objdir(source->path, cb, NULL,
+       odb_prepare_alternates(odb);
+       for (source = odb->sources; source; source = source->next) {
+               int r = for_each_loose_file_in_source(source, cb, NULL,
                                                      NULL, data);
                if (r)
                        return r;
index d52b335e85ba795f97b9e34977aba9397eec315a..1b1ab95423d55c706f829cbdc91e1ff5eb835b49 100644 (file)
@@ -86,7 +86,7 @@ typedef int each_loose_cruft_fn(const char *basename,
 typedef int each_loose_subdir_fn(unsigned int nr,
                                 const char *path,
                                 void *data);
-int for_each_loose_file_in_objdir(const char *path,
+int for_each_loose_file_in_source(struct odb_source *source,
                                  each_loose_object_fn obj_cb,
                                  each_loose_cruft_fn cruft_cb,
                                  each_loose_subdir_fn subdir_cb,
@@ -99,7 +99,8 @@ int for_each_loose_file_in_objdir(const char *path,
  *
  * Any flags specific to packs are ignored.
  */
-int for_each_loose_object(each_loose_object_fn, void *,
+int for_each_loose_object(struct object_database *odb,
+                         each_loose_object_fn, void *,
                          enum for_each_object_flags flags);
 
 
index 92fb4fbb0ed3d1e399af5ff3b1314bf17e990aeb..d49dc11957c7a6b9a9ee9ba5cbca94e1780f1bcb 100644 (file)
@@ -40,7 +40,7 @@ void prune_packed_objects(int opts)
                progress = start_delayed_progress(the_repository,
                                                  _("Removing duplicate objects"), 256);
 
-       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
+       for_each_loose_file_in_source(the_repository->objects->sources,
                                      prune_object, NULL, prune_subdir, &opts);
 
        /* Ensure we show 100% before finishing progress */
index e984b68a0c41e1064ea3a4adfa818dab7e33b8c6..5706ccaede33206dbcfbdc8ee5e79ec68a85de14 100644 (file)
@@ -319,7 +319,7 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
        oidset_init(&data.extra_recent_oids, 0);
        data.extra_recent_oids_loaded = 0;
 
-       r = for_each_loose_object(add_recent_loose, &data,
+       r = for_each_loose_object(the_repository->objects, add_recent_loose, &data,
                                  FOR_EACH_OBJECT_LOCAL_ONLY);
        if (r)
                goto done;