[PATCH 12/12] odb/source-files: internalize logic to derive commit graph paths
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Nov 16, 2026, 13:07 UTC
- Message-ID
- <20261116-pks-odb-pluggable-commit-graphs-v1-12-a83fc1ff0808@pks.im>
- In-Reply-To
- <20261116-pks-odb-pluggable-commit-graphs-v1-0-a83fc1ff0808@pks.im>
In the preceding commit we have moved the logic to derive commit graph paths into the "files" backend. We couldn't yet make the logic private to the backend though because we still use it when we verify commit graphs.
Refactor the code and expose two new functions that are specific to the "files" backend to read and write a commit graph into a specific object directory. These functions are only for use for existing callers in git-commit-graph(1), so adapt that one caller to use those functions.
This converts the last caller outside of the "files" backend to stop caring about exact commit graph paths anymore. Drop the declarations for functions that derive such paths and make them private.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- builtin/commit-graph.c | 57 +++++++----------- commit-graph.h | 21 ------- odb/source-files.c | 157 ++++++++++++++++++++++++++++--------------------- odb/source-files.h | 12 ++++ 4 files changed, 123 insertions(+), 124 deletions(-)
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c index 6ee644d8487..d9d3cdd335a 100644 --- a/builtin/commit-graph.c +++ b/builtin/commit-graph.c @@ -59,13 +59,7 @@ static int cmd_commit_graph_verify(int argc, { struct opts_commit_graph opts = { 0 }; struct commit_graph *graph = NULL; - struct odb_files_dir *dir = NULL; - char *graph_name; - char *chain_name; - enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE; enum commit_graph_verify_flags flags = isatty(2) ? COMMIT_GRAPH_VERIFY_PROGRESS : 0; - int fd; - struct stat st; int ret; struct option options[] = { OPT_BIT(0, "shallow", &flags, @@ -85,37 +79,27 @@ static int cmd_commit_graph_verify(int argc, if (argc) usage_with_options(builtin_commit_graph_verify_usage, options); - if (!opts.obj_dir) - opts.obj_dir = repo_get_object_directory(repo); - - dir = odb_source_files_find_dir(repo->objects, opts.obj_dir); - if (!dir) - die(_("could not find object directory matching %s"), opts.obj_dir); - - graph_name = get_commit_graph_filename(dir->abspath); - chain_name = get_commit_graph_chain_filename(dir->abspath); - if (open_commit_graph(graph_name, &fd, &st)) - opened = OPENED_GRAPH; - else if (errno != ENOENT) - die_errno(_("Could not open commit-graph '%s'"), graph_name); - else if (open_commit_graph_chain(chain_name, &fd, &st, - repo->hash_algo)) - opened = OPENED_CHAIN; - else if (errno != ENOENT) - die_errno(_("could not open commit-graph chain '%s'"), chain_name); - - FREE_AND_NULL(graph_name); - FREE_AND_NULL(chain_name); - - if (opened == OPENED_NONE) - return 0; - else if (opened == OPENED_GRAPH) - graph = commit_graph_read(repo, fd, &st); - else - graph = load_commit_graph_chain_fd_st(repo->objects, fd, &st); + if (repo->objects->source->type == ODB_SOURCE_FILES) { + struct odb_source_files *files = odb_source_files_downcast(repo->objects->source); + struct odb_files_dir *dir = files->dirs; + + if (opts.obj_dir) { + dir = odb_source_files_find_dir(repo->objects, opts.obj_dir); + if (!dir) + die(_("could not find object directory matching %s"), opts.obj_dir); + } - if (!graph) + ret = odb_source_files_read_commit_graph_dir(files, dir, &graph); + } else { + if (opts.obj_dir) + die(_("--object-dir= is not supported with %s backend"), + odb_source_type_to_name(repo->objects->source->type)); + ret = odb_source_read_commit_graph(repo->objects->source, &graph); + } + if (ret < 0) return 1; + if (ret > 0) + return 0; ret = commit_graph_verify(graph, flags); @@ -328,7 +312,8 @@ static int cmd_commit_graph_write(int argc, } if (dir) { - if (commit_graph_write(repo, dir->abspath, &write_opts)) + struct odb_source_files *files = odb_source_files_downcast(repo->objects->source); + if (odb_source_files_write_commit_graph_dir(files, dir, &write_opts)) result = 1; } else { if (odb_write_commit_graph(repo->objects, &write_opts)) diff --git a/commit-graph.h b/commit-graph.h index 3295498fd34..a84227c7a6d 100644 --- a/commit-graph.h +++ b/commit-graph.h @@ -29,12 +29,7 @@ struct repository; struct object_database; struct string_list; -char *get_commit_graph_filename(const char *dir); -char *get_commit_graph_chain_filename(const char *dir); struct commit_graph *prepare_commit_graph(struct repository *r); -int open_commit_graph(const char *graph_file, int *fd, struct stat *st); -int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st, - const struct git_hash_algo *hash_algo); /* * Given a commit struct, try to fill the commit struct info, including: @@ -124,8 +119,6 @@ struct commit_graph { */ struct commit_graph *commit_graph_read(struct repository *repo, int fd, struct stat *st); -struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, - int fd, struct stat *st); /* * Link the commit graph layer `g` on top of the chain `base`. `base` may be @@ -226,20 +219,6 @@ int commit_graph_write_layer(struct repository *r, struct commit_graph **base, unsigned char *hash); -/* - * The commit_graph_write* methods return zero on success - * and a negative value on failure. Note that if the repository - * is not compatible with the commit-graph feature, then the - * methods will return 0 without writing a commit-graph. - * - * The object directory `dir` may be given as a relative path; it is - * canonicalized internally so that it compares equal to the directory of - * graphs that have already been loaded. - */ -int commit_graph_write(struct repository *r, - const char *dir, - const struct commit_graph_write_options *opts); - enum commit_graph_verify_flags { COMMIT_GRAPH_VERIFY_SHALLOW = (1 << 0), COMMIT_GRAPH_VERIFY_PROGRESS = (1 << 1), diff --git a/odb/source-files.c b/odb/source-files.c index 90cda52b48b..16d331e4f28 100644 --- a/odb/source-files.c +++ b/odb/source-files.c @@ -1231,7 +1231,7 @@ static int odb_source_files_generate_pack(struct odb_source *source UNUSED, return 0; } -char *get_commit_graph_filename(const char *dir) +static char *get_commit_graph_filename(const char *dir) { return xstrfmt("%s/info/commit-graph", dir); } @@ -1243,12 +1243,12 @@ static char *get_split_graph_filename(const char *dir, oid_hex); } -char *get_commit_graph_chain_filename(const char *dir) +static char *get_commit_graph_chain_filename(const char *dir) { return xstrfmt("%s/info/commit-graphs/commit-graph-chain", dir); } -int open_commit_graph(const char *graph_file, int *fd, struct stat *st) +static int open_commit_graph(const char *graph_file, int *fd, struct stat *st) { *fd = git_open(graph_file); if (*fd < 0) @@ -1260,40 +1260,45 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st) return 1; } -static struct commit_graph *load_commit_graph_one(struct repository *repo, - const char *dir, - const char *graph_file) +static int load_commit_graph_one(struct repository *repo, + const char *dir, + const char *graph_file, + struct commit_graph **out) { struct stat st; int fd; struct commit_graph *g; - int open_ok = open_commit_graph(graph_file, &fd, &st); - if (!open_ok) - return NULL; + if (!open_commit_graph(graph_file, &fd, &st)) { + if (errno == ENOENT) + return 1; + return error_errno(_("Could not open commit-graph '%s'"), + graph_file); + } g = commit_graph_read(repo, fd, &st); - if (g) { - g->dir = absolute_pathdup(dir); - g->filename = absolute_pathdup(graph_file); - } + if (!g) + return -1; - return g; + g->dir = absolute_pathdup(dir); + g->filename = absolute_pathdup(graph_file); + *out = g; + return 0; } -static struct commit_graph *load_commit_graph_v1(struct repository *repo, - const char *dir) +static int load_commit_graph_v1(struct repository *repo, + const char *dir, + struct commit_graph **out) { char *graph_name = get_commit_graph_filename(dir); - struct commit_graph *g = load_commit_graph_one(repo, dir, graph_name); + int ret = load_commit_graph_one(repo, dir, graph_name, out); free(graph_name); - - return g; + return ret; } -int open_commit_graph_chain(const char *chain_file, - int *fd, struct stat *st, - const struct git_hash_algo *hash_algo) +static int open_commit_graph_chain(const char *chain_file, + int *fd, struct stat *st, + const struct git_hash_algo *hash_algo) { *fd = git_open(chain_file); if (*fd < 0) @@ -1316,8 +1321,8 @@ int open_commit_graph_chain(const char *chain_file, return 1; } -struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, - int fd, struct stat *st) +static struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, + int fd, struct stat *st) { struct commit_graph *graph_chain = NULL; struct strbuf line = STRBUF_INIT; @@ -1345,20 +1350,25 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, files = odb_source_files_downcast(odb->source); for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { char *graph_name = get_split_graph_filename(dir->abspath, line.buf); - struct commit_graph *g = load_commit_graph_one(odb->repo, dir->abspath, graph_name); + struct commit_graph *g = NULL; + int ret; + ret = load_commit_graph_one(odb->repo, dir->abspath, graph_name, &g); free(graph_name); - if (g) { - if (!commit_graph_link_base(g, graph_chain)) { - graph_chain = g; - valid = 1; - } else { - commit_graph_free(g); - } - + if (ret > 0) + continue; + if (ret < 0) break; + + if (!commit_graph_link_base(g, graph_chain)) { + graph_chain = g; + valid = 1; + } else { + commit_graph_free(g); } + + break; } if (!valid) { @@ -1377,49 +1387,64 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb, return graph_chain; } -static struct commit_graph *load_commit_graph_chain(struct repository *repo, - const char *dir) +static int load_commit_graph_chain(struct repository *repo, + const char *dir, + struct commit_graph **out) { char *chain_file = get_commit_graph_chain_filename(dir); struct stat st; int fd; - struct commit_graph *g = NULL; + int ret; - if (open_commit_graph_chain(chain_file, &fd, &st, repo->hash_algo)) { - /* ownership of fd is taken over by load function */ - g = load_commit_graph_chain_fd_st(repo->objects, fd, &st); + if (!open_commit_graph_chain(chain_file, &fd, &st, repo->hash_algo)) { + if (errno == ENOENT) + ret = 1; + else + ret = error_errno(_("could not open commit-graph chain '%s'"), + chain_file); + goto out; } + /* ownership of fd is taken over by load function */ + *out = load_commit_graph_chain_fd_st(repo->objects, fd, &st); + ret = *out ? 0 : -1; + +out: free(chain_file); - return g; + return ret; } -static struct commit_graph *read_commit_graph_one(struct repository *repo, - const char *dir) +int odb_source_files_read_commit_graph_dir(struct odb_source_files *files, + struct odb_files_dir *dir, + struct commit_graph **out) { - struct commit_graph *g = load_commit_graph_v1(repo, dir); + struct repository *repo = files->base.odb->repo; + int ret; - if (!g) - g = load_commit_graph_chain(repo, dir); + ret = load_commit_graph_v1(repo, dir->abspath, out); + if (ret > 0) + ret = load_commit_graph_chain(repo, dir->abspath, out); - return g; + return ret; } static int odb_source_files_read_commit_graph(struct odb_source *source, struct commit_graph **out) { struct odb_source_files *files = odb_source_files_downcast(source); + bool error = false; for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { - struct commit_graph *g = read_commit_graph_one(files->base.odb->repo, - dir->abspath); - if (!g) + int ret = odb_source_files_read_commit_graph_dir(files, dir, out); + if (ret) { + if (ret < 0) + error = true; continue; - *out = g; + } return 0; } - return 1; + return error ? -1 : 1; } static void mark_commit_graphs(struct commit_graph *tip, @@ -1482,17 +1507,18 @@ static void expire_commit_graphs(const char *dirpath, strbuf_release(&path); } -int commit_graph_write(struct repository *r, - const char *dir, - const struct commit_graph_write_options *opts) +int odb_source_files_write_commit_graph_dir(struct odb_source_files *files, + struct odb_files_dir *dir, + const struct commit_graph_write_options *opts) { + struct repository *r = files->base.odb->repo; struct string_list chain_hashes = STRING_LIST_INIT_DUP; struct string_list kept = STRING_LIST_INIT_DUP; struct tempfile *graph_layer = NULL; struct lock_file lk = LOCK_INIT; struct commit_graph *g, *chain, *base = NULL; char *base_rename_from = NULL, *base_rename_to = NULL; - char *graph_name = NULL, *abs_dir = NULL, *new_path = NULL; + char *graph_name = NULL, *new_path = NULL; size_t mergeable_layers = 0, chainable_layers = 0; unsigned char hash[GIT_MAX_RAWSZ]; bool mergeable = true; @@ -1503,8 +1529,6 @@ int commit_graph_write(struct repository *r, if (!commit_graph_write_allowed(r)) return 0; - abs_dir = absolute_pathdup(dir); - g = prepare_commit_graph(r); /* @@ -1520,7 +1544,7 @@ int commit_graph_write(struct repository *r, for (struct commit_graph *layer = g; layer; layer = layer->base_graph) { char *path = get_commit_graph_filename(layer->dir); bool is_split = !!strcmp(layer->filename, path); - bool is_local = !strcmp(layer->dir, abs_dir); + bool is_local = !strcmp(layer->dir, dir->abspath); free(path); @@ -1535,9 +1559,9 @@ int commit_graph_write(struct repository *r, } if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) { - graph_name = xstrfmt("%s/info/commit-graphs/tmp_graph_XXXXXX", abs_dir); + graph_name = xstrfmt("%s/info/commit-graphs/tmp_graph_XXXXXX", dir->abspath); } else { - graph_name = get_commit_graph_filename(abs_dir); + graph_name = get_commit_graph_filename(dir->abspath); } if (safe_create_leading_directories(r, graph_name)) { @@ -1547,7 +1571,7 @@ int commit_graph_write(struct repository *r, } if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) { - char *chain_name = get_commit_graph_chain_filename(abs_dir); + char *chain_name = get_commit_graph_chain_filename(dir->abspath); repo_hold_lock_file_for_update_mode(r, &lk, chain_name, LOCK_DIE_ON_ERROR, 0444); @@ -1586,7 +1610,7 @@ int commit_graph_write(struct repository *r, if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) { const char *new_hash = hash_to_hex_algop(hash, r->hash_algo); - new_path = get_split_graph_filename(abs_dir, new_hash); + new_path = get_split_graph_filename(dir->abspath, new_hash); string_list_append(&chain_hashes, new_hash); for (chain = base; chain; chain = chain->base_graph) @@ -1632,7 +1656,7 @@ int commit_graph_write(struct repository *r, goto out; } } else if (!base) { - char *graph_name = get_commit_graph_filename(abs_dir); + char *graph_name = get_commit_graph_filename(dir->abspath); unlink(graph_name); free(graph_name); } @@ -1653,12 +1677,12 @@ int commit_graph_write(struct repository *r, commit_lock_file(&lk); if (!(opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) { - char *chain_name = get_commit_graph_chain_filename(abs_dir); + char *chain_name = get_commit_graph_chain_filename(dir->abspath); unlink(chain_name); free(chain_name); } - expire_commit_graphs(abs_dir, &kept, + expire_commit_graphs(dir->abspath, &kept, opts->expire_time ? opts->expire_time : (timestamp_t) time(NULL)); ret = 0; @@ -1672,7 +1696,6 @@ int commit_graph_write(struct repository *r, free(base_rename_to); free(graph_name); free(new_path); - free(abs_dir); return ret; } @@ -1680,7 +1703,7 @@ static int odb_source_files_write_commit_graph(struct odb_source *source, const struct commit_graph_write_options *opts) { struct odb_source_files *files = odb_source_files_downcast(source); - return commit_graph_write(source->odb->repo, files->dirs->abspath, opts); + return odb_source_files_write_commit_graph_dir(files, files->dirs, opts); } static int fsck_commit_graph(struct odb_source_files *files, diff --git a/odb/source-files.h b/odb/source-files.h index f779e67a1db..71ccacd8d90 100644 --- a/odb/source-files.h +++ b/odb/source-files.h @@ -106,4 +106,16 @@ static inline struct odb_source_files *odb_source_files_downcast(struct odb_sour */ struct odb_files_dir *odb_source_files_find_dir(struct object_database *odb, const char *obj_dir); +/* + * Read and write a commit graph via a specific object directory. These + * functions should not be used, but we have to provide this ability to support + * the "--object-dir=" option for git-commit-graph(1). + */ +int odb_source_files_read_commit_graph_dir(struct odb_source_files *files, + struct odb_files_dir *dir, + struct commit_graph **out); +int odb_source_files_write_commit_graph_dir(struct odb_source_files *files, + struct odb_files_dir *dir, + const struct commit_graph_write_options *opts); + #endif
-- 2.56.1.763.g16177fe143.dirty