Skip to content

Commit 0d61933

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 595bef7 commit 0d61933

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

builtin/commit-graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix,
101101
if (opts.progress)
102102
flags |= COMMIT_GRAPH_WRITE_PROGRESS;
103103

104-
source = odb_find_source(the_repository->objects, opts.obj_dir);
104+
source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
105105
graph_name = get_commit_graph_filename(source);
106106
chain_name = get_commit_graph_chain_filename(source);
107107
if (open_commit_graph(graph_name, &fd, &st))
@@ -289,7 +289,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,
289289
git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
290290
flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
291291

292-
source = odb_find_source(the_repository->objects, opts.obj_dir);
292+
source = odb_find_source_or_die(the_repository->objects, opts.obj_dir);
293293

294294
if (opts.reachable) {
295295
if (write_commit_graph_reachable(source, flags, &write_opts))

midx-write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
916916
static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
917917
const char *object_dir)
918918
{
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);
920920
return get_multi_pack_index(source);
921921
}
922922

odb.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
464464
free(obj_dir_real);
465465
strbuf_release(&odb_path_real);
466466

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);
467473
if (!source)
468474
die(_("could not find object directory matching %s"), obj_dir);
469475
return source;

odb.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,14 @@ struct object_database *odb_new(struct repository *repo);
186186
void odb_clear(struct object_database *o);
187187

188188
/*
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.
191191
*/
192192
struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
193193

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+
194197
/*
195198
* Replace the current writable object directory with the specified temporary
196199
* object directory; returns the former primary source.

0 commit comments

Comments
 (0)