odb: introduce `odb_prepare()`

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>

Patrick Steinhardt committed Jun 22, 2026 at 10:47 UTC b7fe8f06728f4d39d9b84454588185b384695902
3 files changed +20 -15
builtin/grep.c
+2 -7
@@ -1356,13 +1356,8 @@ int cmd_grep(int argc,
1356 if (recurse_submodules)
1357 repo_read_gitmodules(the_repository, 1);
1358
1359 - if (startup_info->have_repository) {
1360 - struct odb_source *source;
1361 -
1362 - odb_prepare_alternates(the_repository->objects);
1363 - for (source = the_repository->objects->sources; source; source = source->next)
1364 - odb_source_prepare(source, 0);
1365 - }
1359 + if (startup_info->have_repository)
1360 + odb_prepare(the_repository->objects, 0);
1361
1362 start_threads(&opt);
1363 } else {
odb.c
+12 -6
@@ -1070,7 +1070,7 @@ void odb_free(struct object_database *o)
1070 free(o);
1071 }
1072
1073 -void odb_reprepare(struct object_database *o)
1073 +void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1074 {
1075 struct odb_source *source;
1076
@@ -1082,13 +1082,19 @@ void odb_reprepare(struct object_database *o)
1082 * the linked list, so existing odbs will continue to exist for
1083 * the lifetime of the process.
1084 */
1085 - o->loaded_alternates = 0;
1086 - odb_prepare_alternates(o);
1085 + if (flags & ODB_PREPARE_FLUSH_CACHES) {
1086 + o->loaded_alternates = 0;
1087 + o->object_count_valid = 0;
1088 + }
1089
1090 + odb_prepare_alternates(o);
1091 for (source = o->sources; source; source = source->next)
1089 - odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
1090 -
1091 - o->object_count_valid = 0;
1092 + odb_source_prepare(source, flags);
1093
1094 obj_read_unlock();
1095 }
1096 +
1097 +void odb_reprepare(struct object_database *o)
1098 +{
1099 + odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1100 +}
odb.h
+6 -2
@@ -133,9 +133,13 @@ enum odb_prepare_flags {
133 };
134
135 /*
136 - * Clear caches, reload alternates and then reload object sources so that new
137 - * objects may become accessible.
136 + * Prepare the object database for use. Calling this function is generally not
137 + * needed, but can be useful in case the caller wants to pre-open individual
138 + * sources.
139 */
140 +void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
141 +
142 +/* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
143 void odb_reprepare(struct object_database *o);
144
145 /*