commit-graph: stop depending on struct odb_source

To read or write a commit graph we require access to the repository that
the graph should be read from or written for as well as the object
directory to derive its location. Instead of passing in these two bits
of information explicitly though, we pass in a `struct odb_source`,
which carries with it both of these pieces of information.

But ultimately, this is somewhat flawed as we now assume that the source
even has an on-disk representation, and furthermore we assume that the
commit graphs would live in ".git/objects/info". This is true for the
"files" backend, but it's not necessarily true for any other backend
that we may eventually want to introduce. So eventually, we'll want to
evolve the commit-graph subsystem to become agnostic of the backend's
layout and let the backend itself decide where to read a commit graph
from or where to write it to.

We're not there yet to do that switch, but the current design is already
causing issues for the intent of this patch series where we want to move
alternates into the "files" backend.

Convert the subsystem to take a repository plus an object directory path
instead. This unblocks moving around alternates, and it's also a step
in the right direction for moving commit graphs into the source in a
later series.

Note that we previously compared the `struct odb_source` pointers of two
commit graphs to figure out whether they were located in the same object
directory, whereas we now have to compare their paths. Callers may pass
in those paths in different forms though, for example relative to the
current working directory or as absolute paths. Convert the object
directory into an absolute path both when loading and when writing
commit graphs so that the comparisons become more robust.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt authored and Junio C Hamano committed 2026-10-08 07:31:18 -07:00
1 parent 778d260c31
commit 623d2c7125
9 files changed
+102 -72

No files matched your search

+23 -8
View File
@@ -21,7 +21,7 @@
* call this method outside of a builtin, and only if you know what
* you are doing!
*/
void git_test_write_commit_graph_or_die(struct odb_source *source);
void git_test_write_commit_graph_or_die(struct repository *repo);
struct commit;
struct bloom_filter_settings;
@@ -29,8 +29,8 @@ struct repository;
struct object_database;
struct string_list;
char *get_commit_graph_filename(struct odb_source *source);
char *get_commit_graph_chain_filename(struct odb_source *source);
char *get_commit_graph_filename(const char *dir);
char *get_commit_graph_chain_filename(const char *dir);
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);
@@ -85,12 +85,13 @@ struct commit_graph {
const unsigned char *data;
size_t data_len;
struct repository *repo;
const struct git_hash_algo *hash_algo;
unsigned char num_chunks;
uint32_t num_commits;
struct object_id oid;
char *dir;
char *filename;
struct odb_source *odb_source;
uint32_t num_commits_in_base;
unsigned int read_generation_data;
@@ -114,12 +115,20 @@ struct commit_graph {
struct bloom_filter_settings *bloom_filter_settings;
};
struct commit_graph *load_commit_graph_one_fd_st(struct odb_source *source,
/*
* Load commit graphs from the given object directory `dir`. The directory may
* be given as a relative path; it is canonicalized internally so that graphs
* loaded from the same directory compare equal regardless of how the caller
* spelled the path.
*/
struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
const char *dir,
int fd, struct stat *st);
struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
int fd, struct stat *st,
int *incomplete_chain);
struct commit_graph *read_commit_graph_one(struct odb_source *source);
struct commit_graph *read_commit_graph_one(struct repository *repo,
const char *dir);
struct repo_settings;
@@ -171,11 +180,17 @@ struct commit_graph_opts {
* 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 write_commit_graph_reachable(struct odb_source *source,
int write_commit_graph_reachable(struct repository *repo,
const char *dir,
enum commit_graph_write_flags flags,
const struct commit_graph_opts *opts);
int write_commit_graph(struct odb_source *source,
int write_commit_graph(struct repository *r,
const char *dir,
const struct string_list *pack_indexes,
struct oidset *commits,
enum commit_graph_write_flags flags,