]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: introduce `odb_prepare()`
authorPatrick Steinhardt <ps@pks.im>
Mon, 22 Jun 2026 08:47:55 +0000 (10:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Jun 2026 17:41:03 +0000 (10:41 -0700)
Introduce `odb_prepare()` as a simple wrapper to prepare alternates and
then prepare each individual source. Adapt git-grep(1) to use it.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/grep.c
odb.c
odb.h

index 7361bf071ed1623a2df5e8dc16ec5be172ea596e..a7252d56a11ee0bbedfb12432b0e884c16923caf 100644 (file)
@@ -1356,13 +1356,8 @@ int cmd_grep(int argc,
                if (recurse_submodules)
                        repo_read_gitmodules(the_repository, 1);
 
-               if (startup_info->have_repository) {
-                       struct odb_source *source;
-
-                       odb_prepare_alternates(the_repository->objects);
-                       for (source = the_repository->objects->sources; source; source = source->next)
-                               odb_source_prepare(source, 0);
-               }
+               if (startup_info->have_repository)
+                       odb_prepare(the_repository->objects, 0);
 
                start_threads(&opt);
        } else {
diff --git a/odb.c b/odb.c
index 7b45390e129680f79988121bfab8840276a338f4..11414c49a83f5a74c0bde8f937acaa1a11bd2187 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -1070,7 +1070,7 @@ void odb_free(struct object_database *o)
        free(o);
 }
 
-void odb_reprepare(struct object_database *o)
+void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
 {
        struct odb_source *source;
 
@@ -1082,13 +1082,19 @@ void odb_reprepare(struct object_database *o)
         * the linked list, so existing odbs will continue to exist for
         * the lifetime of the process.
         */
-       o->loaded_alternates = 0;
-       odb_prepare_alternates(o);
+       if (flags & ODB_PREPARE_FLUSH_CACHES) {
+               o->loaded_alternates = 0;
+               o->object_count_valid = 0;
+       }
 
+       odb_prepare_alternates(o);
        for (source = o->sources; source; source = source->next)
-               odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
-
-       o->object_count_valid = 0;
+               odb_source_prepare(source, flags);
 
        obj_read_unlock();
 }
+
+void odb_reprepare(struct object_database *o)
+{
+       odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
+}
diff --git a/odb.h b/odb.h
index c14c9030e47648ae3d0c2accaa7ea1fc101d1b37..b1c0f3767b2e68b9c7be6f44b37a8a95169b2e88 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -133,9 +133,13 @@ enum odb_prepare_flags {
 };
 
 /*
- * Clear caches, reload alternates and then reload object sources so that new
- * objects may become accessible.
+ * Prepare the object database for use. Calling this function is generally not
+ * needed, but can be useful in case the caller wants to pre-open individual
+ * sources.
  */
+void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
+
+/* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
 void odb_reprepare(struct object_database *o);
 
 /*