odb: allow `odb_find_source()` to fail

When trying to locate a source for an unknown object directory we will die right away. In subsequent patches we will add new callsites though that want to handle this situation gracefully instead. Refactor the function to return a `NULL` pointer if the source could not be found and adapt the callsites to die instead. Introduce a new wrapper `odb_find_source_or_die()` that continues to die in case the source could not be found. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 11, 2025 at 15:46 UTC 0d61933b8f9a0392310196578e1374283496843c
4 files changed +14 -5
builtin/commit-graph.c
+2 -2
@@ -101,7 +101,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
101 if (opts.progress)
102 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
103
104 - source = odb_find_source(the_repository->objects, opts.obj_dir);
104 + source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
105 graph_name = get_commit_graph_filename(source);
106 chain_name = get_commit_graph_chain_filename(source);
107 if (open_commit_graph(graph_name, &fd, &st))
@@ -289,7 +289,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,
289 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
290 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
291
292 - source = odb_find_source(the_repository->objects, opts.obj_dir);
292 + source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
293
294 if (opts.reachable) {
295 if (write_commit_graph_reachable(source, flags, &write_opts))
midx-write.c
+1 -1
@@ -916,7 +916,7 @@ cleanup:
916 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
917 const char *object_dir)
918 {
919 - struct odb_source *source = odb_find_source(r->objects, object_dir);
919 + struct odb_source *source = odb_find_source_or_die(r->objects, object_dir);
920 return get_multi_pack_index(source);
921 }
922
odb.c
+6
@@ -464,6 +464,12 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
464 free(obj_dir_real);
465 strbuf_release(&odb_path_real);
466
467 + return source;
468 +}
469 +
470 +struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir)
471 +{
472 + struct odb_source *source = odb_find_source(odb, obj_dir);
473 if (!source)
474 die(_("could not find object directory matching %s"), obj_dir);
475 return source;
odb.h
+5 -2
@@ -186,11 +186,14 @@ struct object_database *odb_new(struct repository *repo);
186 void odb_clear(struct object_database *o);
187
188 /*
189 - * Find source by its object directory path. Dies in case the source couldn't
190 - * be found.
189 + * Find source by its object directory path. Returns a `NULL` pointer in case
190 + * the source could not be found.
191 */
192 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
193
194 +/* Same as `odb_find_source()`, but dies in case the source doesn't exist. */
195 +struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir);
196 +
197 /*
198 * Replace the current writable object directory with the specified temporary
199 * object directory; returns the former primary source.