Git development
 help / color / mirror / Atom feed
* [PATCH 00/12] Introduce pluggable commit graphs
@ 2026-11-16 13:07 Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 01/12] commit-graph: simplify linking commit graph chains Patrick Steinhardt
                   ` (11 more replies)
  0 siblings, 12 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

Hi,

this patch series introduces pluggable commit graphs such that an object
database backend is in full control over where exactly the commit graph
is being read from or written to.

An alternative form of this would have been to abstract the overall
functionality of a commit graph and instead expose its functionality.
But that would have been a much larger undertaking, so for now I decided
to go the easier step and retain the data structure itself. But that
does not preclude a subsequent step eventually to abstract the data
structure even further if we ever wanted to.

Meanwhile, with this change all logic to derive storage paths for commit
graphs moves into the "files" backend.

This series is built on fbd2d1e765d (The 18th batch, 2026-11-09) with
ps/commit-graph-write-refactor at a9993c1000d (commit-graph: modernize
function names, 2026-11-04) merged into it.

Thanks!

Patrick

---
Patrick Steinhardt (12):
      commit-graph: simplify linking commit graph chains
      commit-graph: simplify reading a single commit graph
      commit-graph: expose function to validate a commit graph chain
      commit-graph: decide mergeable and chainable layers by depth
      commit-graph: introduce common exit for `write_commit_graph_file()`
      commit-graph: derive layer infos from the passed-in chain
      commit-graph: split out check whether writing commit graphs is allowed
      commit-graph: split out logic to write a new commit graph layer
      commit-graph: mark incomplete commit graph chains
      odb/source: make commit graphs pluggable
      odb/source-files: move logic to derive commit graph paths
      odb/source-files: internalize logic to derive commit graph paths

 builtin/commit-graph.c     |  87 ++++---
 builtin/fetch.c            |   4 +-
 builtin/gc.c               |   3 +-
 commit-graph.c             | 585 +++++++--------------------------------------
 commit-graph.h             |  74 +++---
 odb.c                      |   6 +
 odb.h                      |  10 +
 odb/source-files.c         | 478 ++++++++++++++++++++++++++++++++++++
 odb/source-files.h         |  12 +
 odb/source.h               |  52 ++++
 t/helper/test-read-graph.c |   3 +-
 11 files changed, 740 insertions(+), 574 deletions(-)


---
base-commit: 0c2ef2cc2023eab2fe8bc54d542fe4f93314f186
change-id: 20261110-pks-odb-pluggable-commit-graphs-e9dc9674f0ae


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 01/12] commit-graph: simplify linking commit graph chains
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 20:53   ` Junio C Hamano
  2026-11-16 13:07 ` [PATCH 02/12] commit-graph: simplify reading a single commit graph Patrick Steinhardt
                   ` (10 subsequent siblings)
  11 siblings, 1 reply; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

When we load a commit graph chain we verify for every layer that the
hash of the base commit graph matches what the current tip has in its
base graphs chunk. To do so we pass both the chain itself as well as the
array of object IDs that we have read from the commit-graph chain file
to `add_graph_to_chain()`.

But the array of object IDs is redundant information: we have already
loaded and linked the base layers, and as part of that we have stored
their respective hashes in the structure itself. Consequently, we can
already derive the hashes by walking the chain of base layers.

Refactor the function to only take the new tip and base layers as input
and derive all the other information ad-hoc and make the function
public. This prepares for a subsequent commit, where we are about to
move the logic that creates commit graph chains into the object database
backend.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 52 ++++++++++++++++++++++++----------------------------
 commit-graph.h |  8 ++++++++
 2 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index da65cd3deb9..94c761907a0 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -575,50 +575,48 @@ static void validate_mixed_bloom_settings(struct commit_graph *g)
 	}
 }
 
-static int add_graph_to_chain(struct commit_graph *g,
-			      struct commit_graph *chain,
-			      struct object_id *oids,
-			      int n)
+int commit_graph_link_base(struct commit_graph *g,
+			   struct commit_graph *base)
 {
-	struct commit_graph *cur_g = chain;
+	struct commit_graph *layer;
+	size_t n = 0;
+
+	for (layer = base; layer; layer = layer->base_graph)
+		n++;
 
 	if (n && !g->chunk_base_graphs) {
 		warning(_("commit-graph has no base graphs chunk"));
-		return 0;
+		return -1;
 	}
 
 	if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) {
 		warning(_("commit-graph base graphs chunk is too small"));
-		return 0;
+		return -1;
 	}
 
-	while (n) {
+	for (layer = base; layer; layer = layer->base_graph) {
 		n--;
-
-		if (!cur_g ||
-		    !oideq(&oids[n], &cur_g->oid) ||
-		    !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
+		if (!hasheq(layer->oid.hash,
+			    g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
 			    g->hash_algo)) {
 			warning(_("commit-graph chain does not match"));
-			return 0;
+			return -1;
 		}
-
-		cur_g = cur_g->base_graph;
 	}
 
-	if (chain) {
-		if (unsigned_add_overflows(chain->num_commits,
-					   chain->num_commits_in_base)) {
+	if (base) {
+		if (unsigned_add_overflows(base->num_commits,
+					   base->num_commits_in_base)) {
 			warning(_("commit count in base graph too high: %"PRIuMAX),
-				(uintmax_t)chain->num_commits_in_base);
-			return 0;
+				(uintmax_t)base->num_commits_in_base);
+			return -1;
 		}
-		g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
+		g->num_commits_in_base = base->num_commits + base->num_commits_in_base;
 	}
 
-	g->base_graph = chain;
+	g->base_graph = base;
 
-	return 1;
+	return 0;
 }
 
 int open_commit_graph_chain(const char *chain_file,
@@ -652,20 +650,19 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 {
 	struct commit_graph *graph_chain = NULL;
 	struct strbuf line = STRBUF_INIT;
-	struct object_id *oids;
 	int i = 0, valid = 1, count;
 	FILE *fp = xfdopen(fd, "r");
 
 	count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
-	CALLOC_ARRAY(oids, count);
 
 	for (i = 0; i < count; i++) {
 		struct odb_source_files *files;
+		struct object_id oid;
 
 		if (strbuf_getline_lf(&line, fp) == EOF)
 			break;
 
-		if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) {
+		if (get_oid_hex_algop(line.buf, &oid, odb->repo->hash_algo)) {
 			warning(_("invalid commit-graph chain: line '%s' not a hash"),
 				line.buf);
 			valid = 0;
@@ -682,7 +679,7 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 			free(graph_name);
 
 			if (g) {
-				if (add_graph_to_chain(g, graph_chain, oids, i)) {
+				if (!commit_graph_link_base(g, graph_chain)) {
 					graph_chain = g;
 					valid = 1;
 				} else {
@@ -702,7 +699,6 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 	validate_mixed_generation_chain(graph_chain);
 	validate_mixed_bloom_settings(graph_chain);
 
-	free(oids);
 	fclose(fp);
 	strbuf_release(&line);
 
diff --git a/commit-graph.h b/commit-graph.h
index 2156499a688..f0e7cc93461 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -128,6 +128,14 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
 struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 						   int fd, struct stat *st,
 						   int *incomplete_chain);
+
+/*
+ * Link the commit graph layer `g` on top of the chain `base`. `base` may be
+ * NULL if `g` is the root of a new chain. Returns 0 on success, a negative
+ * error code otherwise.
+ */
+int commit_graph_link_base(struct commit_graph *g,
+			   struct commit_graph *base);
 struct commit_graph *read_commit_graph_one(struct repository *repo,
 					   const char *dir);
 

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 02/12] commit-graph: simplify reading a single commit graph
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 01/12] commit-graph: simplify linking commit graph chains Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 03/12] commit-graph: expose function to validate a commit graph chain Patrick Steinhardt
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

In a subsequent commit we're about to move the logic to locate commit
graphs and commit graph chains into the object database backend. With
that change, the responsibility of the commit-graph subsystem changes so
that it only provides the functionality to read and write commit graphs,
whereas the backend owns the locations those are read from and written
to.

The function `load_commit_graph_one_fd_st()` is responsible for loading
a single commit graph layer from disk. It receives as input the file
descriptor and the directory it is being read from. While the former
makes sense, the latter doesn't: we already populate the graph's file
name in `load_commit_graph_one()`, so we populate paths in two different
locations.

Besides that, the actual paths are about to become a responsibility of
the backend. So arguably, generic functionality that loads the commit
graph should not be exposed to it.

Stop passing the directory path and instead populate it in
`load_commit_graph_one()`, which will later on be moved into the "files"
backend. While at it, rename `load_commit_graph_one_fd_st()` to
`commit_graph_read()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/commit-graph.c |  2 +-
 commit-graph.c         | 15 +++++++--------
 commit-graph.h         | 12 +++++-------
 3 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 8194a528406..2a5a75dc22f 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -111,7 +111,7 @@ static int cmd_commit_graph_verify(int argc,
 	if (opened == OPENED_NONE)
 		return 0;
 	else if (opened == OPENED_GRAPH)
-		graph = load_commit_graph_one_fd_st(repo, dir->abspath, fd, &st);
+		graph = commit_graph_read(repo, fd, &st);
 	else
 		graph = load_commit_graph_chain_fd_st(repo->objects, fd, &st,
 						      &incomplete_chain);
diff --git a/commit-graph.c b/commit-graph.c
index 94c761907a0..c427a6f90f7 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -257,9 +257,8 @@ int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
 	return 1;
 }
 
-struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
-						 const char *dir,
-						 int fd, struct stat *st)
+struct commit_graph *commit_graph_read(struct repository *repo,
+				       int fd, struct stat *st)
 {
 	void *graph_map;
 	size_t graph_size;
@@ -276,9 +275,7 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
 	close(fd);
 
 	ret = commit_graph_parse(repo, graph_map, graph_size);
-	if (ret)
-		ret->dir = absolute_pathdup(dir);
-	else
+	if (!ret)
 		munmap(graph_map, graph_size);
 
 	return ret;
@@ -508,9 +505,11 @@ static struct commit_graph *load_commit_graph_one(struct repository *repo,
 	if (!open_ok)
 		return NULL;
 
-	g = load_commit_graph_one_fd_st(repo, dir, fd, &st);
-	if (g)
+	g = commit_graph_read(repo, fd, &st);
+	if (g) {
+		g->dir = absolute_pathdup(dir);
 		g->filename = absolute_pathdup(graph_file);
+	}
 
 	return g;
 }
diff --git a/commit-graph.h b/commit-graph.h
index f0e7cc93461..8660c7fe698 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -117,14 +117,12 @@ struct commit_graph {
 };
 
 /*
- * 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.
+ * Read a single commit graph layer from the given file descriptor. The file
+ * descriptor will be closed regardless of whether loading succeeds. Returns
+ * NULL on error.
  */
-struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
-						 const char *dir,
-						 int fd, struct stat *st);
+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,
 						   int *incomplete_chain);

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 03/12] commit-graph: expose function to validate a commit graph chain
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 01/12] commit-graph: simplify linking commit graph chains Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 02/12] commit-graph: simplify reading a single commit graph Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 04/12] commit-graph: decide mergeable and chainable layers by depth Patrick Steinhardt
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

When we have loaded a commit graph chain we need to validate whether all
of its layers have been written with compatible settings. If that's not
the case, then some functionality of the chain needs to be restricted as
it would otherwise yield inconsistent results.

In a subsequent commit we'll move loading of commit graphs into the
object database backend, and consequently the backend will have to
perform this validation. Expose this functionality to prepare for this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 9 +++++++--
 commit-graph.h | 7 +++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index c427a6f90f7..9464522feb5 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -574,6 +574,12 @@ static void validate_mixed_bloom_settings(struct commit_graph *g)
 	}
 }
 
+void commit_graph_validate_chain(struct commit_graph *g)
+{
+	validate_mixed_generation_chain(g);
+	validate_mixed_bloom_settings(g);
+}
+
 int commit_graph_link_base(struct commit_graph *g,
 			   struct commit_graph *base)
 {
@@ -695,8 +701,7 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 		}
 	}
 
-	validate_mixed_generation_chain(graph_chain);
-	validate_mixed_bloom_settings(graph_chain);
+	commit_graph_validate_chain(graph_chain);
 
 	fclose(fp);
 	strbuf_release(&line);
diff --git a/commit-graph.h b/commit-graph.h
index 8660c7fe698..dc4f6194c72 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -134,6 +134,13 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
  */
 int commit_graph_link_base(struct commit_graph *g,
 			   struct commit_graph *base);
+
+/*
+ * Validate the chain of commit graph layers to ensure that all layers are
+ * compatible with one another. This may disable generation data and Bloom
+ * filters in case they aren't.
+ */
+void commit_graph_validate_chain(struct commit_graph *g);
 struct commit_graph *read_commit_graph_one(struct repository *repo,
 					   const char *dir);
 

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 04/12] commit-graph: decide mergeable and chainable layers by depth
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (2 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 03/12] commit-graph: expose function to validate a commit graph chain Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 05/12] commit-graph: introduce common exit for `write_commit_graph_file()` Patrick Steinhardt
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

When writing a split commit graph, `split_graph_merge_strategy()`
decides which layers of the existing chain get merged into the new
layer and which layer the new one builds upon. This is decided on two
simple rules:

  - A layer may be merged in case it is part of the same local object
    directory.

  - A layer may be added to the chain in case it is either a split
    commit graph layer, regardless of whether it is local or not. Or in
    case it is local, we may also add it to our chain in case it is a
    non-split commit graph, in which case we convert it to a split one.

These checks require us to know about where exactly these commit graphs
are stored on disk as we compare directories of the respective files
with one another to figure out whether or not a layer is local. So as a
consequence, this policy becomes backend-specific once the backend
controls where exactly commit graphs are stored.

Refactor the code so that we instead tell the merge strategy how many of
the topmost layers are mergeable and chainable, respectively, which will
let the backends decide on that property.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 51 ++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 40 insertions(+), 11 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index 9464522feb5..7f7146e752a 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1151,6 +1151,8 @@ struct write_commit_graph_context {
 	uint64_t progress_cnt;
 
 	char *base_graph_name;
+	size_t mergeable_layers;
+	size_t chainable_layers;
 	int num_commit_graphs_before;
 	int num_commit_graphs_after;
 	char **commit_graph_filenames_before;
@@ -2283,6 +2285,7 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 	enum commit_graph_split_flags flags = ctx->opts->split_flags;
 	int max_commits = ctx->opts->max_commits;
 	int size_mult = 2;
+	size_t depth = 0;
 	uint32_t i;
 
 	if (ctx->opts->size_multiple)
@@ -2299,7 +2302,7 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 	    flags != COMMIT_GRAPH_SPLIT_REPLACE) {
 		while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
 			    (max_commits && num_commits > max_commits))) {
-			if (strcmp(g->dir, ctx->dir))
+			if (depth >= ctx->mergeable_layers)
 				break;
 
 			if (unsigned_add_overflows(num_commits, g->num_commits))
@@ -2309,6 +2312,7 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 				    (uintmax_t)g->num_commits);
 			num_commits += g->num_commits;
 			g = g->base_graph;
+			depth++;
 
 			ctx->num_commit_graphs_after--;
 		}
@@ -2320,16 +2324,13 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 		BUG("split_graph_merge_strategy: num_commit_graphs_after "
 		    "should be 1 with --split=replace");
 
-	if (ctx->num_commit_graphs_after == 2) {
-		char *old_graph_name = get_commit_graph_filename(g->dir);
-
-		if (!strcmp(g->filename, old_graph_name) &&
-		    strcmp(g->dir, ctx->dir)) {
-			ctx->num_commit_graphs_after = 1;
-			ctx->new_base_graph = NULL;
-		}
-
-		free(old_graph_name);
+	/*
+	 * If we cannot add the new base to our chain we instead merge it and
+	 * all of its base layers.
+	 */
+	if (ctx->new_base_graph && depth >= ctx->chainable_layers) {
+		ctx->num_commit_graphs_after = 1;
+		ctx->new_base_graph = NULL;
 	}
 
 	CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
@@ -2560,6 +2561,7 @@ int commit_graph_write(struct repository *r,
 	struct oidset reachable_commits = OIDSET_INIT;
 	struct topo_level_slab topo_levels;
 	struct commit_graph *g;
+	bool mergeable = true;
 
 	if (!!opts->pack_indexes + !!opts->commits + !!(opts->flags & COMMIT_GRAPH_WRITE_REACHABLE) > 1)
 		BUG("attempting to write commit-graph with mutually incompatible options");
@@ -2597,6 +2599,33 @@ int commit_graph_write(struct repository *r,
 	for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
 		chain->topo_levels = &topo_levels;
 
+	/*
+	 * We only consider merging layers that belong to our own object
+	 * directory. Furthermore, we can only chain on top of either:
+	 *
+	 *   - A split commit graph layer, regardless of whether or not it is
+	 *     local.
+	 *
+	 *   - A local non-split commit graph, in which case we'll convert that
+	 *     layer into a split commit graph layer.
+	 */
+	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, ctx.dir);
+
+		free(path);
+
+		if (!is_local && !is_split)
+			break;
+		if (!is_local)
+			mergeable = false;
+
+		if (mergeable)
+			ctx.mergeable_layers++;
+		ctx.chainable_layers++;
+	}
+
 	if (opts->flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
 		ctx.changed_paths = 1;
 	if (!(opts->flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 05/12] commit-graph: introduce common exit for `write_commit_graph_file()`
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (3 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 04/12] commit-graph: decide mergeable and chainable layers by depth Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 06/12] commit-graph: derive layer infos from the passed-in chain Patrick Steinhardt
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

In the next commit we're going to refactor `write_commit_graph_file()`.
As part of that refactoring we'll perform a couple more allocations in
this function that need to be cleaned up on exit.

Prepare for this by introducing a common exit path now already, as that
will make the diff a tiny bit easier to read.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index 7f7146e752a..c146f20f68f 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -2099,6 +2099,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	struct strbuf progress_title = STRBUF_INIT;
 	struct chunkfile *cf;
 	unsigned char file_hash[GIT_MAX_RAWSZ];
+	int ret;
 
 	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
 		struct strbuf tmp_file = STRBUF_INIT;
@@ -2112,9 +2113,9 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	}
 
 	if (safe_create_leading_directories(ctx->r, ctx->graph_name)) {
-		error(_("unable to create leading directories of %s"),
-			ctx->graph_name);
-		return -1;
+		ret = error(_("unable to create leading directories of %s"),
+			    ctx->graph_name);
+		goto out;
 	}
 
 	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
@@ -2126,14 +2127,14 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
 		graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
 		if (!graph_layer) {
-			error(_("unable to create temporary graph layer"));
-			return -1;
+			ret = error(_("unable to create temporary graph layer"));
+			goto out;
 		}
 
 		if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) {
-			error(_("unable to adjust shared permissions for '%s'"),
-			      get_tempfile_path(graph_layer));
-			return -1;
+			ret = error(_("unable to adjust shared permissions for '%s'"),
+				    get_tempfile_path(graph_layer));
+			goto out;
 		}
 
 		f = hashfd(ctx->r->hash_algo,
@@ -2227,8 +2228,8 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 		int result;
 
 		if (!chainf) {
-			error(_("unable to open commit-graph chain file"));
-			return -1;
+			ret = error(_("unable to open commit-graph chain file"));
+			goto out;
 		}
 
 		if (ctx->base_graph_name) {
@@ -2243,8 +2244,8 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 				result = rename(ctx->base_graph_name, dest);
 
 				if (result) {
-					error(_("failed to rename base commit-graph file"));
-					return -1;
+					ret = error(_("failed to rename base commit-graph file"));
+					goto out;
 				}
 			}
 		} else {
@@ -2267,14 +2268,17 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
 
 		if (result) {
-			error(_("failed to rename temporary commit-graph file"));
-			return -1;
+			ret = error(_("failed to rename temporary commit-graph file"));
+			goto out;
 		}
 	}
 
 	commit_lock_file(&lk);
+	ret = 0;
 
-	return 0;
+out:
+	rollback_lock_file(&lk);
+	return ret;
 }
 
 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 06/12] commit-graph: derive layer infos from the passed-in chain
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (4 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 05/12] commit-graph: introduce common exit for `write_commit_graph_file()` Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 07/12] commit-graph: split out check whether writing commit graphs is allowed Patrick Steinhardt
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

In addition to writing the new commit-graph layer, we also need to do a
bit of bookkeeping when writing a split commit graph.

  - We track the list of hashes that the new chain is supposed to
    consist of.

  - We track the list of filenames part of the new chain so that we
    don't prune them.

  - We track the list of filenames that will be merged so that we can
    bump their mtime.

  - We track the name of the old non-split commit-graph so that we can
    rename it in case it is being converted into a split commit-graph.

All of this information is passed in by the caller via a couple of
arrays in the write context, but it is in fact redundant: we already
have all information available via the passed-in commit graph.

Refactor the code accordingly. This will make it easier to move this
logic into the "files" backend as we can now cleanly split out the
path-dependent logic from `write_commit_graph_file()`.

While the logic should be mostly equivalent, there's one change in
behaviour: in the preimage, we call `expire_commit_graphs()` regardless
of whether or not writing the new commit graph file succeeds, but now we
only do so on success. This is arguably an improvement though, as
expiring commit graphs even when we haven't written any new ones is
somewhat puzzling.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 227 ++++++++++++++++++++++++---------------------------------
 1 file changed, 97 insertions(+), 130 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index c146f20f68f..caf40fd6b01 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -25,6 +25,7 @@
 #include "bloom.h"
 #include "commit-slab.h"
 #include "shallow.h"
+#include "string-list.h"
 #include "json-writer.h"
 #include "trace2.h"
 #include "tree.h"
@@ -1150,14 +1151,10 @@ struct write_commit_graph_context {
 	int progress_done;
 	uint64_t progress_cnt;
 
-	char *base_graph_name;
 	size_t mergeable_layers;
 	size_t chainable_layers;
 	int num_commit_graphs_before;
 	int num_commit_graphs_after;
-	char **commit_graph_filenames_before;
-	char **commit_graph_filenames_after;
-	char **commit_graph_hash_after;
 	uint32_t new_num_commits_in_base;
 	struct commit_graph *new_base_graph;
 
@@ -2089,14 +2086,24 @@ static int write_graph_chunk_base(struct hashfile *f,
 	return 0;
 }
 
+static void mark_commit_graphs(struct commit_graph *tip,
+			       struct commit_graph *base);
+static void expire_commit_graphs(const char *dirpath,
+				 struct string_list *kept,
+				 timestamp_t expire_time);
+
 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 {
-	uint32_t i;
 	struct hashfile *f;
-	struct tempfile *graph_layer; /* when COMMIT_GRAPH_WRITE_SPLIT is non-zero */
+	struct tempfile *graph_layer = NULL; /* when COMMIT_GRAPH_WRITE_SPLIT is non-zero */
 	struct lock_file lk = LOCK_INIT;
 	const unsigned hashsz = ctx->r->hash_algo->rawsz;
 	struct strbuf progress_title = STRBUF_INIT;
+	struct string_list chain_hashes = STRING_LIST_INIT_DUP;
+	struct string_list kept = STRING_LIST_INIT_DUP;
+	struct commit_graph *base = ctx->new_base_graph, *chain;
+	char *base_rename_from = NULL, *base_rename_to = NULL;
+	char *new_path = NULL;
 	struct chunkfile *cf;
 	unsigned char file_hash[GIT_MAX_RAWSZ];
 	int ret;
@@ -2206,78 +2213,102 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	stop_progress(&ctx->progress);
 	strbuf_release(&progress_title);
 
-	if ((ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) &&
-	    ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
-		char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
-		char *new_base_name = get_split_graph_filename(ctx->new_base_graph->dir, new_base_hash);
+	finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
+			  CSUM_HASH_IN_STREAM | CSUM_FSYNC);
+	free_chunkfile(cf);
+
+	/*
+	 * Snapshot everything we need from the current chain before closing
+	 * it, as we must not have any layers mapped when replacing them.
+	 */
+	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		const char *new_hash = hash_to_hex_algop(file_hash, ctx->r->hash_algo);
+
+		new_path = get_split_graph_filename(ctx->dir, new_hash);
+
+		string_list_append(&chain_hashes, new_hash);
+		for (chain = base; chain; chain = chain->base_graph)
+			string_list_append(&chain_hashes, oid_to_hex(&chain->oid));
 
-		free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
-		free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
-		ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
-		ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
+		/*
+		 * In case we have a non-split commit graph we need to convert
+		 * it into the base layer of the new split commit graph by
+		 * renaming it.
+		 */
+		if (base) {
+			base_rename_to = get_split_graph_filename(base->dir, oid_to_hex(&base->oid));
+			if (strcmp(base->filename, base_rename_to))
+				base_rename_from = xstrdup(base->filename);
+			else
+				FREE_AND_NULL(base_rename_to);
+		}
+
+		for (chain = base; chain; chain = chain->base_graph) {
+			if (chain == base && base_rename_to)
+				string_list_append(&kept, base_rename_to);
+			else
+				string_list_append(&kept, chain->filename);
+		}
+		string_list_append(&kept, new_path);
+
+		mark_commit_graphs(ctx->r->objects->commit_graph, base);
 	}
 
 	commit_graph_close(ctx->r->objects);
-	finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
-			  CSUM_HASH_IN_STREAM | CSUM_FSYNC);
-	free_chunkfile(cf);
 
 	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
 		FILE *chainf = fdopen_lock_file(&lk, "w");
-		char *final_graph_name;
-		int result;
 
 		if (!chainf) {
 			ret = error(_("unable to open commit-graph chain file"));
 			goto out;
 		}
 
-		if (ctx->base_graph_name) {
-			const char *dest;
-			int idx = ctx->num_commit_graphs_after - 1;
-			if (ctx->num_commit_graphs_after > 1)
-				idx--;
-
-			dest = ctx->commit_graph_filenames_after[idx];
-
-			if (strcmp(ctx->base_graph_name, dest)) {
-				result = rename(ctx->base_graph_name, dest);
-
-				if (result) {
-					ret = error(_("failed to rename base commit-graph file"));
-					goto out;
-				}
+		if (base_rename_from) {
+			if (rename(base_rename_from, base_rename_to)) {
+				ret = error(_("failed to rename base commit-graph file"));
+				goto out;
 			}
-		} else {
+		} else if (!base) {
 			char *graph_name = get_commit_graph_filename(ctx->dir);
 			unlink(graph_name);
 			free(graph_name);
 		}
 
-		free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
-		ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] =
-			xstrdup(hash_to_hex_algop(file_hash, ctx->r->hash_algo));
-		final_graph_name = get_split_graph_filename(ctx->dir,
-					ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
-		free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
-		ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
-
-		result = rename_tempfile(&graph_layer, final_graph_name);
-
-		for (i = 0; i < ctx->num_commit_graphs_after; i++)
-			fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
-
-		if (result) {
+		if (rename_tempfile(&graph_layer, new_path)) {
 			ret = error(_("failed to rename temporary commit-graph file"));
 			goto out;
 		}
+
+		/*
+		 * Chain hashes are ordered newest-first, but the chain file is
+		 * oldest-first so we iterate in reverse.
+		 */
+		for (size_t i = chain_hashes.nr; i; i--)
+			fprintf(chainf, "%s\n", chain_hashes.items[i - 1].string);
 	}
 
 	commit_lock_file(&lk);
+
+	if (!(ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
+		char *chain_name = get_commit_graph_chain_filename(ctx->dir);
+		unlink(chain_name);
+		free(chain_name);
+	}
+
+	expire_commit_graphs(ctx->dir, &kept,
+			     ctx->opts->expire_time ? ctx->opts->expire_time : time(NULL));
+
 	ret = 0;
 
 out:
 	rollback_lock_file(&lk);
+	delete_tempfile(&graph_layer);
+	string_list_clear(&chain_hashes, 0);
+	string_list_clear(&kept, 0);
+	free(base_rename_from);
+	free(base_rename_to);
+	free(new_path);
 	return ret;
 }
 
@@ -2290,7 +2321,6 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 	int max_commits = ctx->opts->max_commits;
 	int size_mult = 2;
 	size_t depth = 0;
-	uint32_t i;
 
 	if (ctx->opts->size_multiple)
 		size_mult = ctx->opts->size_multiple;
@@ -2337,30 +2367,12 @@ static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 		ctx->new_base_graph = NULL;
 	}
 
-	CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
-	CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
-
-	for (i = 0; i < ctx->num_commit_graphs_after &&
-		    i < ctx->num_commit_graphs_before; i++)
-		ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
-
-	i = ctx->num_commit_graphs_before - 1;
-	g = graph_to_merge;
-
-	while (g) {
-		if (i < ctx->num_commit_graphs_after)
-			ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
-
-		/*
-		 * If the topmost remaining layer has generation data chunk, the
-		 * resultant layer also has generation data chunk.
-		 */
-		if (i == ctx->num_commit_graphs_after - 2)
-			ctx->write_generation_data = !!g->chunk_generation_data;
-
-		i--;
-		g = g->base_graph;
-	}
+	/*
+	 * If the topmost remaining layer has generation data chunk, the
+	 * resultant layer also has generation data chunk.
+	 */
+	if (ctx->new_base_graph)
+		ctx->write_generation_data = !!ctx->new_base_graph->chunk_generation_data;
 }
 
 static void merge_commit_graph(struct write_commit_graph_context *ctx,
@@ -2464,48 +2476,37 @@ static void merge_commit_graphs(struct write_commit_graph_context *ctx,
 		ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
 	}
 
-	if (ctx->new_base_graph)
-		ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
-
 	sort_and_scan_merged_commits(ctx);
 }
 
-static void mark_commit_graphs(struct write_commit_graph_context *ctx)
+static void mark_commit_graphs(struct commit_graph *tip,
+			       struct commit_graph *base)
 {
-	uint32_t i;
 	time_t now = time(NULL);
 
-	for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
+	for (struct commit_graph *g = tip; g && g != base; g = g->base_graph) {
 		struct stat st;
 		struct utimbuf updated_time;
 
-		if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
+		if (stat(g->filename, &st) < 0)
 			continue;
 
 		updated_time.actime = st.st_atime;
 		updated_time.modtime = now;
-		utime(ctx->commit_graph_filenames_before[i], &updated_time);
+		utime(g->filename, &updated_time);
 	}
 }
 
-static void expire_commit_graphs(struct write_commit_graph_context *ctx)
+static void expire_commit_graphs(const char *dirpath,
+				 struct string_list *kept,
+				 timestamp_t expire_time)
 {
 	struct strbuf path = STRBUF_INIT;
 	DIR *dir;
 	struct dirent *de;
 	size_t dirnamelen;
-	timestamp_t expire_time = time(NULL);
-
-	if (ctx->opts->expire_time)
-		expire_time = ctx->opts->expire_time;
-	if (!(ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
-		char *chain_file_name = get_commit_graph_chain_filename(ctx->dir);
-		unlink(chain_file_name);
-		free(chain_file_name);
-		ctx->num_commit_graphs_after = 0;
-	}
 
-	strbuf_addstr(&path, ctx->dir);
+	strbuf_addstr(&path, dirpath);
 	strbuf_addstr(&path, "/info/commit-graphs");
 	dir = opendir(path.buf);
 
@@ -2516,7 +2517,6 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
 	dirnamelen = path.len;
 	while ((de = readdir(dir)) != NULL) {
 		struct stat st;
-		uint32_t i, found = 0;
 
 		strbuf_setlen(&path, dirnamelen);
 		strbuf_addstr(&path, de->d_name);
@@ -2529,20 +2529,12 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
 		if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
 			continue;
 
-		for (i = 0; i < ctx->num_commit_graphs_after; i++) {
-			if (!strcmp(ctx->commit_graph_filenames_after[i],
-				    path.buf)) {
-				found = 1;
-				break;
-			}
-		}
-
-		if (!found)
+		if (!unsorted_string_list_has_string(kept, path.buf))
 			unlink(path.buf);
 	}
 
 out:
-	if(dir)
+	if (dir)
 		closedir(dir);
 	strbuf_release(&path);
 }
@@ -2652,14 +2644,6 @@ int commit_graph_write(struct repository *r,
 		for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
 			ctx.num_commit_graphs_before++;
 
-		if (ctx.num_commit_graphs_before) {
-			ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
-			i = ctx.num_commit_graphs_before;
-
-			for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
-				ctx.commit_graph_filenames_before[--i] = xstrdup(chain->filename);
-		}
-
 		replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
 	}
 
@@ -2727,14 +2711,8 @@ int commit_graph_write(struct repository *r,
 	if (ctx.changed_paths)
 		deinit_bloom_filters();
 
-	if (ctx.opts->flags & COMMIT_GRAPH_WRITE_SPLIT)
-		mark_commit_graphs(&ctx);
-
-	expire_commit_graphs(&ctx);
-
 cleanup:
 	free(ctx.graph_name);
-	free(ctx.base_graph_name);
 	free(ctx.dir);
 	commit_stack_clear(&ctx.commits);
 	oidset_clear(&reachable_commits);
@@ -2750,17 +2728,6 @@ int commit_graph_write(struct repository *r,
 		}
 	}
 
-	for (i = 0; i < ctx.num_commit_graphs_before; i++)
-		free(ctx.commit_graph_filenames_before[i]);
-	free(ctx.commit_graph_filenames_before);
-
-	for (i = 0; i < ctx.num_commit_graphs_after; i++) {
-		free(ctx.commit_graph_filenames_after[i]);
-		free(ctx.commit_graph_hash_after[i]);
-	}
-	free(ctx.commit_graph_filenames_after);
-	free(ctx.commit_graph_hash_after);
-
 	return res;
 }
 

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 07/12] commit-graph: split out check whether writing commit graphs is allowed
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (5 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 06/12] commit-graph: derive layer infos from the passed-in chain Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 08/12] commit-graph: split out logic to write a new commit graph layer Patrick Steinhardt
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

In a subsequent commit we're about to split out the logic to write a
commit graph into the object database backends, which will need to
determine whether writing a graph is enabled in the first place. There
can be various reasons though why we cannot write a new graph, and we
certainly don't want to repeat this logic across all potential writers.

Extract the logic into a new function to prepare for this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 37 ++++++++++++++++++++++++-------------
 commit-graph.h |  3 +++
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index caf40fd6b01..c673c0424da 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -2539,6 +2539,29 @@ static void expire_commit_graphs(const char *dirpath,
 	strbuf_release(&path);
 }
 
+bool commit_graph_write_allowed(struct repository *r)
+{
+	prepare_repo_settings(r);
+
+	if (!r->settings.core_commit_graph) {
+		warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
+		return false;
+	}
+
+	if (!commit_graph_compatible(r))
+		return false;
+
+	if (r->settings.commit_graph_changed_paths_version < -1 ||
+	    r->settings.commit_graph_changed_paths_version > 2) {
+		warning(_("attempting to write a commit-graph, but "
+			  "'commitGraph.changedPathsVersion' (%d) is not supported"),
+			r->settings.commit_graph_changed_paths_version);
+		return false;
+	}
+
+	return true;
+}
+
 int commit_graph_write(struct repository *r,
 		       const char *dir,
 		       const struct commit_graph_write_options *opts)
@@ -2562,20 +2585,8 @@ int commit_graph_write(struct repository *r,
 	if (!!opts->pack_indexes + !!opts->commits + !!(opts->flags & COMMIT_GRAPH_WRITE_REACHABLE) > 1)
 		BUG("attempting to write commit-graph with mutually incompatible options");
 
-	prepare_repo_settings(r);
-	if (!r->settings.core_commit_graph) {
-		warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
+	if (!commit_graph_write_allowed(r))
 		return 0;
-	}
-	if (!commit_graph_compatible(r))
-		return 0;
-	if (r->settings.commit_graph_changed_paths_version < -1
-	    || r->settings.commit_graph_changed_paths_version > 2) {
-		warning(_("attempting to write a commit-graph, but "
-			  "'commitGraph.changedPathsVersion' (%d) is not supported"),
-			r->settings.commit_graph_changed_paths_version);
-		return 0;
-	}
 
 	ctx.dir = absolute_pathdup(dir);
 
diff --git a/commit-graph.h b/commit-graph.h
index dc4f6194c72..e2fc0c8da4c 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -198,6 +198,9 @@ struct commit_graph_write_options {
 	.max_new_filters = -1, \
 }
 
+/* Check whether the repository's configuration allows writing commit graphs. */
+bool commit_graph_write_allowed(struct repository *r);
+
 /*
  * The commit_graph_write* methods return zero on success
  * and a negative value on failure. Note that if the repository

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 08/12] commit-graph: split out logic to write a new commit graph layer
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (6 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 07/12] commit-graph: split out check whether writing commit graphs is allowed Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 09/12] commit-graph: mark incomplete commit graph chains Patrick Steinhardt
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

The logic to write a new commit graph is mostly contained in
`write_commit_graph_file()`. This function has two different
responsibilities:

  - It writes the content of the new commit graph layer.

  - It manages the location of the newly written files, as well as the
    deletion of old files.

While the former is generic functionality, the latter is specific to the
actual backend once we make commit graphs pluggable.

Separate these two concerns by introducing `commit_graph_write_layer()`,
which is only responsible for writing the content of the new file.
The backend-specific handling of paths remain the responsibility of
`commit_graph_write()`, which allows us to move that logic into the
backend in a subsequent commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c | 247 ++++++++++++++++++++++++++++++++-------------------------
 commit-graph.h |  27 +++++++
 2 files changed, 166 insertions(+), 108 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index c673c0424da..7bb6b112b2e 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1140,8 +1140,6 @@ struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit
 
 struct write_commit_graph_context {
 	struct repository *r;
-	char *dir;
-	char *graph_name;
 	struct oid_array oids;
 	struct commit_stack commits;
 	int num_extra_edges;
@@ -2092,68 +2090,16 @@ static void expire_commit_graphs(const char *dirpath,
 				 struct string_list *kept,
 				 timestamp_t expire_time);
 
-static int write_commit_graph_file(struct write_commit_graph_context *ctx)
+static int write_commit_graph_file(struct write_commit_graph_context *ctx,
+				   int fd, const char *path,
+				   unsigned char *file_hash)
 {
-	struct hashfile *f;
-	struct tempfile *graph_layer = NULL; /* when COMMIT_GRAPH_WRITE_SPLIT is non-zero */
-	struct lock_file lk = LOCK_INIT;
 	const unsigned hashsz = ctx->r->hash_algo->rawsz;
 	struct strbuf progress_title = STRBUF_INIT;
-	struct string_list chain_hashes = STRING_LIST_INIT_DUP;
-	struct string_list kept = STRING_LIST_INIT_DUP;
-	struct commit_graph *base = ctx->new_base_graph, *chain;
-	char *base_rename_from = NULL, *base_rename_to = NULL;
-	char *new_path = NULL;
+	struct hashfile *f;
 	struct chunkfile *cf;
-	unsigned char file_hash[GIT_MAX_RAWSZ];
-	int ret;
-
-	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		struct strbuf tmp_file = STRBUF_INIT;
-
-		strbuf_addf(&tmp_file,
-			    "%s/info/commit-graphs/tmp_graph_XXXXXX",
-			    ctx->dir);
-		ctx->graph_name = strbuf_detach(&tmp_file, NULL);
-	} else {
-		ctx->graph_name = get_commit_graph_filename(ctx->dir);
-	}
-
-	if (safe_create_leading_directories(ctx->r, ctx->graph_name)) {
-		ret = error(_("unable to create leading directories of %s"),
-			    ctx->graph_name);
-		goto out;
-	}
-
-	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		char *lock_name = get_commit_graph_chain_filename(ctx->dir);
-
-		repo_hold_lock_file_for_update_mode(ctx->r, &lk, lock_name,
-						    LOCK_DIE_ON_ERROR, 0444);
-		free(lock_name);
-
-		graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
-		if (!graph_layer) {
-			ret = error(_("unable to create temporary graph layer"));
-			goto out;
-		}
-
-		if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) {
-			ret = error(_("unable to adjust shared permissions for '%s'"),
-				    get_tempfile_path(graph_layer));
-			goto out;
-		}
-
-		f = hashfd(ctx->r->hash_algo,
-			   get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
-	} else {
-		repo_hold_lock_file_for_update_mode(ctx->r, &lk,
-						    ctx->graph_name,
-						    LOCK_DIE_ON_ERROR, 0444);
-		f = hashfd(ctx->r->hash_algo,
-			   get_lock_file_fd(&lk), get_lock_file_path(&lk));
-	}
 
+	f = hashfd(ctx->r->hash_algo, fd, path);
 	cf = init_chunkfile(f);
 
 	add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
@@ -2217,14 +2163,114 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 			  CSUM_HASH_IN_STREAM | CSUM_FSYNC);
 	free_chunkfile(cf);
 
+	return 0;
+}
+
+int commit_graph_write(struct repository *r,
+		       const char *dir,
+		       const struct commit_graph_write_options *opts)
+{
+	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;
+	size_t mergeable_layers = 0, chainable_layers = 0;
+	unsigned char hash[GIT_MAX_RAWSZ];
+	bool mergeable = true;
+	const char *out_path;
+	int out_fd;
+	int ret;
+
+	if (!commit_graph_write_allowed(r))
+		return 0;
+
+	abs_dir = absolute_pathdup(dir);
+
+	g = prepare_commit_graph(r);
+
+	/*
+	 * We only consider merging layers that belong to our own object
+	 * directory. Furthermore, we can only chain on top of either:
+	 *
+	 *   - A split commit graph layer, regardless of whether or not it is
+	 *     local.
+	 *
+	 *   - A local non-split commit graph, in which case we'll convert that
+	 *     layer into a split commit graph layer.
+	 */
+	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);
+
+		free(path);
+
+		if (!is_local && !is_split)
+			break;
+		if (!is_local)
+			mergeable = false;
+
+		if (mergeable)
+			mergeable_layers++;
+		chainable_layers++;
+	}
+
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		graph_name = xstrfmt("%s/info/commit-graphs/tmp_graph_XXXXXX", abs_dir);
+	} else {
+		graph_name = get_commit_graph_filename(abs_dir);
+	}
+
+	if (safe_create_leading_directories(r, graph_name)) {
+		ret = error(_("unable to create leading directories of %s"),
+			    graph_name);
+		goto out;
+	}
+
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		char *chain_name = get_commit_graph_chain_filename(abs_dir);
+
+		repo_hold_lock_file_for_update_mode(r, &lk, chain_name,
+						    LOCK_DIE_ON_ERROR, 0444);
+		free(chain_name);
+
+		graph_layer = mks_tempfile_m(graph_name, 0444);
+		if (!graph_layer) {
+			ret = error(_("unable to create temporary graph layer"));
+			goto out;
+		}
+
+		if (adjust_shared_perm(r, get_tempfile_path(graph_layer))) {
+			ret = error(_("unable to adjust shared permissions for '%s'"),
+				    get_tempfile_path(graph_layer));
+			goto out;
+		}
+
+		out_fd = get_tempfile_fd(graph_layer);
+		out_path = get_tempfile_path(graph_layer);
+	} else {
+		repo_hold_lock_file_for_update_mode(r, &lk, graph_name,
+						    LOCK_DIE_ON_ERROR, 0444);
+		out_fd = get_lock_file_fd(&lk);
+		out_path = get_lock_file_path(&lk);
+	}
+
+	ret = commit_graph_write_layer(r, opts, mergeable_layers, chainable_layers,
+				       out_fd, out_path, &base, hash);
+	if (ret <= 0)
+		goto out;
+
 	/*
 	 * Snapshot everything we need from the current chain before closing
 	 * it, as we must not have any layers mapped when replacing them.
 	 */
-	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		const char *new_hash = hash_to_hex_algop(file_hash, ctx->r->hash_algo);
+	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(ctx->dir, new_hash);
+		new_path = get_split_graph_filename(abs_dir, new_hash);
 
 		string_list_append(&chain_hashes, new_hash);
 		for (chain = base; chain; chain = chain->base_graph)
@@ -2251,12 +2297,12 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 		}
 		string_list_append(&kept, new_path);
 
-		mark_commit_graphs(ctx->r->objects->commit_graph, base);
+		mark_commit_graphs(g, base);
 	}
 
-	commit_graph_close(ctx->r->objects);
+	commit_graph_close(r->objects);
 
-	if (ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
 		FILE *chainf = fdopen_lock_file(&lk, "w");
 
 		if (!chainf) {
@@ -2270,7 +2316,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 				goto out;
 			}
 		} else if (!base) {
-			char *graph_name = get_commit_graph_filename(ctx->dir);
+			char *graph_name = get_commit_graph_filename(abs_dir);
 			unlink(graph_name);
 			free(graph_name);
 		}
@@ -2290,14 +2336,14 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
 	commit_lock_file(&lk);
 
-	if (!(ctx->opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
-		char *chain_name = get_commit_graph_chain_filename(ctx->dir);
+	if (!(opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
+		char *chain_name = get_commit_graph_chain_filename(abs_dir);
 		unlink(chain_name);
 		free(chain_name);
 	}
 
-	expire_commit_graphs(ctx->dir, &kept,
-			     ctx->opts->expire_time ? ctx->opts->expire_time : time(NULL));
+	expire_commit_graphs(abs_dir, &kept,
+			     opts->expire_time ? opts->expire_time : time(NULL));
 
 	ret = 0;
 
@@ -2308,7 +2354,9 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 	string_list_clear(&kept, 0);
 	free(base_rename_from);
 	free(base_rename_to);
+	free(graph_name);
 	free(new_path);
+	free(abs_dir);
 	return ret;
 }
 
@@ -2562,13 +2610,19 @@ bool commit_graph_write_allowed(struct repository *r)
 	return true;
 }
 
-int commit_graph_write(struct repository *r,
-		       const char *dir,
-		       const struct commit_graph_write_options *opts)
+int commit_graph_write_layer(struct repository *r,
+			     const struct commit_graph_write_options *opts,
+			     size_t mergeable_layers,
+			     size_t chainable_layers,
+			     int fd, const char *path,
+			     struct commit_graph **base,
+			     unsigned char *hash)
 {
 	struct write_commit_graph_context ctx = {
 		.r = r,
 		.opts = opts,
+		.mergeable_layers = mergeable_layers,
+		.chainable_layers = chainable_layers,
 		.total_bloom_filter_data_size = 0,
 		.write_generation_data = (get_configured_generation_version(r) == 2),
 		.num_generation_data_overflows = 0,
@@ -2580,16 +2634,17 @@ int commit_graph_write(struct repository *r,
 	struct oidset reachable_commits = OIDSET_INIT;
 	struct topo_level_slab topo_levels;
 	struct commit_graph *g;
-	bool mergeable = true;
 
 	if (!!opts->pack_indexes + !!opts->commits + !!(opts->flags & COMMIT_GRAPH_WRITE_REACHABLE) > 1)
 		BUG("attempting to write commit-graph with mutually incompatible options");
+	if (chainable_layers < mergeable_layers)
+		BUG("attempting to write commit-graph with more mergeable than chainable layers");
+
+	*base = NULL;
 
 	if (!commit_graph_write_allowed(r))
 		return 0;
 
-	ctx.dir = absolute_pathdup(dir);
-
 	bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
 	bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
 						      bloom_settings.bits_per_entry);
@@ -2606,33 +2661,6 @@ int commit_graph_write(struct repository *r,
 	for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
 		chain->topo_levels = &topo_levels;
 
-	/*
-	 * We only consider merging layers that belong to our own object
-	 * directory. Furthermore, we can only chain on top of either:
-	 *
-	 *   - A split commit graph layer, regardless of whether or not it is
-	 *     local.
-	 *
-	 *   - A local non-split commit graph, in which case we'll convert that
-	 *     layer into a split commit graph layer.
-	 */
-	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, ctx.dir);
-
-		free(path);
-
-		if (!is_local && !is_split)
-			break;
-		if (!is_local)
-			mergeable = false;
-
-		if (mergeable)
-			ctx.mergeable_layers++;
-		ctx.chainable_layers++;
-	}
-
 	if (opts->flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
 		ctx.changed_paths = 1;
 	if (!(opts->flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
@@ -2717,21 +2745,24 @@ int commit_graph_write(struct repository *r,
 	if (ctx.changed_paths)
 		compute_bloom_filters(&ctx);
 
-	res = write_commit_graph_file(&ctx);
+	res = write_commit_graph_file(&ctx, fd, path, hash);
 
 	if (ctx.changed_paths)
 		deinit_bloom_filters();
 
+	if (!res) {
+		*base = ctx.new_base_graph;
+		res = 1;
+	}
+
 cleanup:
-	free(ctx.graph_name);
-	free(ctx.dir);
 	commit_stack_clear(&ctx.commits);
 	oidset_clear(&reachable_commits);
 	oid_array_clear(&ctx.oids);
 	clear_topo_level_slab(&topo_levels);
 
-	if (ctx.r->objects->commit_graph) {
-		struct commit_graph *g = ctx.r->objects->commit_graph;
+	if (r->objects->commit_graph) {
+		struct commit_graph *g = r->objects->commit_graph;
 
 		while (g) {
 			g->topo_levels = NULL;
diff --git a/commit-graph.h b/commit-graph.h
index e2fc0c8da4c..1cc0d097321 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -201,6 +201,33 @@ struct commit_graph_write_options {
 /* Check whether the repository's configuration allows writing commit graphs. */
 bool commit_graph_write_allowed(struct repository *r);
 
+/*
+ * Write a single commit-graph layer to the given file descriptor. When writing
+ * a split commit graph then the repository's loaded commit graph will be used
+ * as the base. This can be tweaked:
+ *
+ *   - `mergeable_layers` controls how many of the topmost layers may be merged
+ *     together.
+ *
+ *   - `chainable_layers` controls how many of the topmost layers may be used
+ *     as base. In case the picked base is not chainable, then it and all of
+ *     its bases are merged.
+ *
+ * On success, `*base` is set to the layer of the currently-loaded chain that
+ * the new layer builds upon, or NULL if the new layer is standalone. `hash`
+ * receives the trailing checksum of the new layer.
+ *
+ * Returns 1 in case a new layer has been written, 0 in case there was nothing
+ * to write, a negative error code otherwise.
+ */
+int commit_graph_write_layer(struct repository *r,
+			     const struct commit_graph_write_options *opts,
+			     size_t mergeable_layers,
+			     size_t chainable_layers,
+			     int fd, const char *path,
+			     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

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 09/12] commit-graph: mark incomplete commit graph chains
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (7 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 08/12] commit-graph: split out logic to write a new commit graph layer Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 10/12] odb/source: make commit graphs pluggable Patrick Steinhardt
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

When loading a commit graph chain it may happen that we only find a
subset of its layers. Such a commit graph is still usable though in the
case where we have the base and layers building on top of it, so we
still return such an incomplete commit graph to the caller.

While valid and usable, we of course want to warn about such commit
graphs in `git commit-graph verify`, which is why there is an
`incomplete` out-parameter in `load_commit_graph_chain_fd_st()`. In a
subsequent commit though we want to hide this function in favor of a
pluggable interface, but carrying such a flag there feels wrong.

Instead, have the function set a new `incomplete` field in the commit
graph structure itself.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/commit-graph.c | 9 ++++-----
 commit-graph.c         | 9 ++++-----
 commit-graph.h         | 4 ++--
 3 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 2a5a75dc22f..5ecce4b6caa 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -66,7 +66,6 @@ static int cmd_commit_graph_verify(int argc,
 	enum commit_graph_verify_flags flags = isatty(2) ? COMMIT_GRAPH_VERIFY_PROGRESS : 0;
 	int fd;
 	struct stat st;
-	int incomplete_chain = 0;
 	int ret;
 	struct option options[] = {
 		OPT_BIT(0, "shallow", &flags,
@@ -113,20 +112,20 @@ static int cmd_commit_graph_verify(int argc,
 	else if (opened == OPENED_GRAPH)
 		graph = commit_graph_read(repo, fd, &st);
 	else
-		graph = load_commit_graph_chain_fd_st(repo->objects, fd, &st,
-						      &incomplete_chain);
+		graph = load_commit_graph_chain_fd_st(repo->objects, fd, &st);
 
 	if (!graph)
 		return 1;
 
 	ret = commit_graph_verify(graph, flags);
-	commit_graph_free(graph);
 
-	if (incomplete_chain) {
+	if (graph->incomplete) {
 		error("one or more commit-graph chain files could not be loaded");
 		ret |= 1;
 	}
 
+	commit_graph_free(graph);
+
 	return ret;
 }
 
diff --git a/commit-graph.c b/commit-graph.c
index 7bb6b112b2e..0ae9f586f21 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -651,8 +651,7 @@ int open_commit_graph_chain(const char *chain_file,
 }
 
 struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
-						   int fd, struct stat *st,
-						   int *incomplete_chain)
+						   int fd, struct stat *st)
 {
 	struct commit_graph *graph_chain = NULL;
 	struct strbuf line = STRBUF_INIT;
@@ -703,11 +702,12 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
 	}
 
 	commit_graph_validate_chain(graph_chain);
+	if (graph_chain)
+		graph_chain->incomplete = !valid;
 
 	fclose(fp);
 	strbuf_release(&line);
 
-	*incomplete_chain = !valid;
 	return graph_chain;
 }
 
@@ -720,9 +720,8 @@ static struct commit_graph *load_commit_graph_chain(struct repository *repo,
 	struct commit_graph *g = NULL;
 
 	if (open_commit_graph_chain(chain_file, &fd, &st, repo->hash_algo)) {
-		int incomplete;
 		/* ownership of fd is taken over by load function */
-		g = load_commit_graph_chain_fd_st(repo->objects, fd, &st, &incomplete);
+		g = load_commit_graph_chain_fd_st(repo->objects, fd, &st);
 	}
 
 	free(chain_file);
diff --git a/commit-graph.h b/commit-graph.h
index 1cc0d097321..3b5e0eb2d73 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -93,6 +93,7 @@ struct commit_graph {
 	struct object_id oid;
 	char *dir;
 	char *filename;
+	bool incomplete;
 
 	uint32_t num_commits_in_base;
 	unsigned int read_generation_data;
@@ -124,8 +125,7 @@ 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,
-						   int *incomplete_chain);
+						   int fd, struct stat *st);
 
 /*
  * Link the commit graph layer `g` on top of the chain `base`. `base` may be

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 10/12] odb/source: make commit graphs pluggable
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (8 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 09/12] commit-graph: mark incomplete commit graph chains Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 11/12] odb/source-files: move logic to derive commit graph paths Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 12/12] odb/source-files: internalize " Patrick Steinhardt
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

The location where commit graphs are read from and written to is
hardcoded to ".git/objects/info". This indeed makes sense for the
"files" backend, which also stores all of its other information in these
directories. But other backends may want to store commit graphs
differently, if they even want to support them at all.

Give backends control over how commit graphs are stored by making the
ability to both read and write commit graphs pluggable. Adapt callers to
use the new, backend-generic functions where possible. The one exception
is "builtin/commit-graph.c", whose "--object-dir=" option allows the
user to specify the target object directory. This is highly specific to
the "files" backend though, and consequently we only support that option
there.

Note that the logic that locates commit graphs is still hosted by
"commit-graph.c" right now. This will be fixed in a subsequent commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/commit-graph.c     | 25 +++++++++++++++-------
 builtin/fetch.c            |  4 +---
 builtin/gc.c               |  3 +--
 commit-graph.c             | 13 ++++--------
 odb.c                      |  6 ++++++
 odb.h                      | 10 +++++++++
 odb/source-files.c         | 27 ++++++++++++++++++++++++
 odb/source.h               | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 t/helper/test-read-graph.c |  3 +--
 9 files changed, 119 insertions(+), 24 deletions(-)

diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 5ecce4b6caa..6ee644d8487 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -277,23 +277,27 @@ static int cmd_commit_graph_write(int argc,
 
 	if (!!(write_opts.flags & COMMIT_GRAPH_WRITE_REACHABLE) + opts.stdin_packs + opts.stdin_commits > 1)
 		die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
-	if (!opts.obj_dir)
-		opts.obj_dir = repo_get_object_directory(repo);
 	if (!opts.enable_changed_paths)
 		write_opts.flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
 	if (opts.enable_changed_paths == 1 ||
 	    git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
 		write_opts.flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
 
-	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 (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 (opts.stdin_packs) {
 		struct strbuf packname = STRBUF_INIT;
 		size_t dirlen;
 
-		strbuf_addf(&packname, "%s/pack/", dir->abspath);
+		if (dir)
+			strbuf_addf(&packname, "%s/pack/", dir->abspath);
+		else
+			strbuf_addf(&packname, "%s/pack/",
+				    repo_get_object_directory(repo));
 		dirlen = packname.len;
 
 		while (strbuf_getline(&buf, stdin) != EOF) {
@@ -323,8 +327,13 @@ static int cmd_commit_graph_write(int argc,
 		stop_progress(&progress);
 	}
 
-	if (commit_graph_write(repo, dir->abspath, &write_opts))
-		result = 1;
+	if (dir) {
+		if (commit_graph_write(repo, dir->abspath, &write_opts))
+			result = 1;
+	} else {
+		if (odb_write_commit_graph(repo->objects, &write_opts))
+			result = 1;
+	}
 
 cleanup:
 	string_list_clear(&pack_indexes, 0);
diff --git a/builtin/fetch.c b/builtin/fetch.c
index c646bc6698d..b7606016817 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -2978,9 +2978,7 @@ int cmd_fetch(int argc,
 		if (graph_write_mode != GRAPH_WRITE_SKIP) {
 			trace2_region_enter("fetch", "write-commit-graph",
 					    the_repository);
-			commit_graph_write(the_repository,
-					   the_repository->objects->source->path,
-					   &write_opts);
+			odb_write_commit_graph(the_repository->objects, &write_opts);
 			trace2_region_leave("fetch", "write-commit-graph",
 					    the_repository);
 		}
diff --git a/builtin/gc.c b/builtin/gc.c
index 68cb04a672d..f9a15912d0e 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -741,8 +741,7 @@ int cmd_gc(int argc,
 		if (!opts.quiet && !daemonized)
 			write_opts.flags |= COMMIT_GRAPH_WRITE_PROGRESS;
 
-		commit_graph_write(the_repository, the_repository->objects->source->path,
-				   &write_opts);
+		odb_write_commit_graph(the_repository->objects, &write_opts);
 	}
 
 	if (!daemonized) {
diff --git a/commit-graph.c b/commit-graph.c
index 0ae9f586f21..7fea94689a1 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -42,7 +42,7 @@ void git_test_write_commit_graph_or_die(struct repository *repo)
 	if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
 		opts.flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
 
-	if (commit_graph_write(repo, repo->objects->source->path, &opts))
+	if (odb_write_commit_graph(repo->objects, &opts))
 		die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
 }
 
@@ -747,8 +747,6 @@ struct commit_graph *read_commit_graph_one(struct repository *repo,
  */
 struct commit_graph *prepare_commit_graph(struct repository *r)
 {
-	struct odb_source_files *files;
-
 	/*
 	 * Early return if there is no object database or if the commit graph is
 	 * disabled.
@@ -778,12 +776,9 @@ struct commit_graph *prepare_commit_graph(struct repository *r)
 	if (!commit_graph_compatible(r))
 		return NULL;
 
-	files = odb_source_files_downcast(r->objects->source);
-	for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
-		r->objects->commit_graph = read_commit_graph_one(r, dir->abspath);
-		if (r->objects->commit_graph)
-			break;
-	}
+	if (odb_source_read_commit_graph(r->objects->source,
+					 &r->objects->commit_graph))
+		return NULL;
 
 	return r->objects->commit_graph;
 }
diff --git a/odb.c b/odb.c
index 5dd277761b1..ec5064c3cd0 100644
--- a/odb.c
+++ b/odb.c
@@ -839,3 +839,9 @@ int odb_fsck(struct object_database *odb, struct odb_fsck_options *options)
 {
 	return odb_source_fsck(odb->source, options);
 }
+
+int odb_write_commit_graph(struct object_database *odb,
+			   const struct commit_graph_write_options *opts)
+{
+	return odb_source_write_commit_graph(odb->source, opts);
+}
diff --git a/odb.h b/odb.h
index 857637f9805..099ba998faa 100644
--- a/odb.h
+++ b/odb.h
@@ -221,6 +221,16 @@ struct odb_fsck_options {
  */
 int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
 
+struct commit_graph_write_options;
+
+/*
+ * Write a new commit graph layer into the primary object source. The source
+ * is in control of where and how the layer is stored. Returns 0 on success, a
+ * negative error code otherwise.
+ */
+int odb_write_commit_graph(struct object_database *odb,
+			   const struct commit_graph_write_options *opts);
+
 /*
  * Iterate through all alternates of the database and execute the provided
  * callback function for each of them. Stop iterating once the callback
diff --git a/odb/source-files.c b/odb/source-files.c
index 0bf15031c87..5ad27fd4bc7 100644
--- a/odb/source-files.c
+++ b/odb/source-files.c
@@ -2,6 +2,7 @@
 #include "abspath.h"
 #include "blob.h"
 #include "chdir-notify.h"
+#include "commit-graph.h"
 #include "config.h"
 #include "gettext.h"
 #include "hex.h"
@@ -1230,6 +1231,30 @@ static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
 	return 0;
 }
 
+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);
+
+	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)
+			continue;
+		*out = g;
+		return 0;
+	}
+
+	return 1;
+}
+
+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);
+}
+
 static int fsck_commit_graph(struct odb_source_files *files,
 			     struct odb_files_dir *dir,
 			     struct odb_fsck_options *opts)
@@ -1335,6 +1360,8 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
 	files->base.optimize = odb_source_files_optimize;
 	files->base.optimize_required = odb_source_files_optimize_required;
 	files->base.generate_pack = odb_source_files_generate_pack;
+	files->base.read_commit_graph = odb_source_files_read_commit_graph;
+	files->base.write_commit_graph = odb_source_files_write_commit_graph;
 
 	/*
 	 * Ideally, we would only ever store absolute paths in the source. This
diff --git a/odb/source.h b/odb/source.h
index 7b56f51b827..bc9604f7df9 100644
--- a/odb/source.h
+++ b/odb/source.h
@@ -1,6 +1,7 @@
 #ifndef ODB_SOURCE_H
 #define ODB_SOURCE_H
 
+#include "gettext.h"
 #include "hashmap.h"
 #include "object.h"
 #include "odb.h"
@@ -32,6 +33,8 @@ enum odb_source_type {
  */
 const char *odb_source_type_to_name(enum odb_source_type type);
 
+struct commit_graph;
+struct commit_graph_write_options;
 struct object_id;
 struct odb_stream;
 struct strbuf;
@@ -285,6 +288,29 @@ struct odb_source {
 	 */
 	int (*fsck)(struct odb_source *source,
 		    struct odb_fsck_options *options);
+
+	/*
+	 * This callback is expected to read the commit graph of the source,
+	 * if any. This callback is optional. Sources that do not support
+	 * commit graphs shall leave it unset.
+	 *
+	 * The callback is expected to return 0 on success, 1 in case no commit
+	 * graph exists and a negative error code otherwise. On success, `out`
+	 * must be set to the commit graph.
+	 */
+	int (*read_commit_graph)(struct odb_source *source,
+				 struct commit_graph **out);
+
+	/*
+	 * This callback is expected to write a new commit graph layer with
+	 * the given options. This callback is optional. Sources that do not
+	 * support commit graphs shall leave it unset.
+	 *
+	 * The callback is expected to return 0 on success, a negative error
+	 * code otherwise.
+	 */
+	int (*write_commit_graph)(struct odb_source *source,
+				  const struct commit_graph_write_options *opts);
 };
 
 /*
@@ -540,4 +566,30 @@ static inline int odb_source_fsck(struct odb_source *source,
 	return source->fsck(source, opts);
 }
 
+/*
+ * Read the commit graph of the given source. Returns 0 on success, 1 in case
+ * no commit graph exists, and a negative error code otherwise.
+ */
+static inline int odb_source_read_commit_graph(struct odb_source *source,
+					       struct commit_graph **out)
+{
+	if (!source->read_commit_graph)
+		return 1;
+	return source->read_commit_graph(source, out);
+}
+
+/*
+ * Write a new commit graph layer into the given source. Returns 0 on success,
+ * a negative error code otherwise. Sources that do not support commit graphs
+ * cause an error.
+ */
+static inline int odb_source_write_commit_graph(struct odb_source *source,
+						const struct commit_graph_write_options *opts)
+{
+	if (!source->write_commit_graph)
+		return error(_("object source '%s' does not support commit graphs"),
+			     odb_source_type_to_name(source->type));
+	return source->write_commit_graph(source, opts);
+}
+
 #endif
diff --git a/t/helper/test-read-graph.c b/t/helper/test-read-graph.c
index 9642e55b8c0..d6174bb55f4 100644
--- a/t/helper/test-read-graph.c
+++ b/t/helper/test-read-graph.c
@@ -85,8 +85,7 @@ int cmd__read_graph(int argc, const char **argv)
 
 	prepare_repo_settings(the_repository);
 
-	graph = read_commit_graph_one(the_repository, source->path);
-	if (!graph) {
+	if (odb_source_read_commit_graph(source, &graph)) {
 		ret = 1;
 		goto done;
 	}

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 11/12] odb/source-files: move logic to derive commit graph paths
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (9 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 10/12] odb/source: make commit graphs pluggable Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  2026-11-16 13:07 ` [PATCH 12/12] odb/source-files: internalize " Patrick Steinhardt
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

Move the logic that derives commit graph paths from the generic
commit-graph subsystem into the "files" backend.

This is mostly an exact move, and we still carry declarations in
"commit-graph.h", so that it becomes trivial to see that nothing else
has changed. The only exception are three sites where we now generate
warnings because of -Wsign-compare.

The next commit will refactor moved code so that we can drop those
declarations.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 commit-graph.c     | 437 -----------------------------------------------------
 commit-graph.h     |   2 -
 odb/source-files.c | 428 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 428 insertions(+), 439 deletions(-)

diff --git a/commit-graph.c b/commit-graph.c
index 7fea94689a1..8daadf56e88 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1,13 +1,11 @@
 #define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "config.h"
 #include "csum-file.h"
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
-#include "lockfile.h"
 #include "packfile.h"
 #include "commit.h"
 #include "object.h"
@@ -25,7 +23,6 @@
 #include "bloom.h"
 #include "commit-slab.h"
 #include "shallow.h"
-#include "string-list.h"
 #include "json-writer.h"
 #include "trace2.h"
 #include "tree.h"
@@ -201,23 +198,6 @@ static int commit_gen_cmp(const void *va, const void *vb)
 	return 0;
 }
 
-char *get_commit_graph_filename(const char *dir)
-{
-	return xstrfmt("%s/info/commit-graph", dir);
-}
-
-static char *get_split_graph_filename(const char *dir,
-				      const char *oid_hex)
-{
-	return xstrfmt("%s/info/commit-graphs/graph-%s.graph", dir,
-		       oid_hex);
-}
-
-char *get_commit_graph_chain_filename(const char *dir)
-{
-	return xstrfmt("%s/info/commit-graphs/commit-graph-chain", dir);
-}
-
 static struct commit_graph *alloc_commit_graph(void)
 {
 	struct commit_graph *g = xcalloc(1, sizeof(*g));
@@ -246,18 +226,6 @@ static int commit_graph_compatible(struct repository *r)
 	return 1;
 }
 
-int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
-{
-	*fd = git_open(graph_file);
-	if (*fd < 0)
-		return 0;
-	if (fstat(*fd, st)) {
-		close(*fd);
-		return 0;
-	}
-	return 1;
-}
-
 struct commit_graph *commit_graph_read(struct repository *repo,
 				       int fd, struct stat *st)
 {
@@ -494,37 +462,6 @@ struct commit_graph *commit_graph_parse(struct repository *r,
 	return NULL;
 }
 
-static struct commit_graph *load_commit_graph_one(struct repository *repo,
-						  const char *dir,
-						  const char *graph_file)
-{
-	struct stat st;
-	int fd;
-	struct commit_graph *g;
-	int open_ok = open_commit_graph(graph_file, &fd, &st);
-
-	if (!open_ok)
-		return NULL;
-
-	g = commit_graph_read(repo, fd, &st);
-	if (g) {
-		g->dir = absolute_pathdup(dir);
-		g->filename = absolute_pathdup(graph_file);
-	}
-
-	return g;
-}
-
-static struct commit_graph *load_commit_graph_v1(struct repository *repo,
-						 const char *dir)
-{
-	char *graph_name = get_commit_graph_filename(dir);
-	struct commit_graph *g = load_commit_graph_one(repo, dir, graph_name);
-	free(graph_name);
-
-	return g;
-}
-
 /*
  * returns 1 if and only if all graphs in the chain have
  * corrected commit dates stored in the generation_data chunk.
@@ -625,120 +562,6 @@ int commit_graph_link_base(struct commit_graph *g,
 	return 0;
 }
 
-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)
-		return 0;
-	if (fstat(*fd, st)) {
-		close(*fd);
-		return 0;
-	}
-	if (st->st_size < hash_algo->hexsz) {
-		close(*fd);
-		if (!st->st_size) {
-			/* treat empty files the same as missing */
-			errno = ENOENT;
-		} else {
-			warning(_("commit-graph chain file too small"));
-			errno = EINVAL;
-		}
-		return 0;
-	}
-	return 1;
-}
-
-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;
-	int i = 0, valid = 1, count;
-	FILE *fp = xfdopen(fd, "r");
-
-	count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
-
-	for (i = 0; i < count; i++) {
-		struct odb_source_files *files;
-		struct object_id oid;
-
-		if (strbuf_getline_lf(&line, fp) == EOF)
-			break;
-
-		if (get_oid_hex_algop(line.buf, &oid, odb->repo->hash_algo)) {
-			warning(_("invalid commit-graph chain: line '%s' not a hash"),
-				line.buf);
-			valid = 0;
-			break;
-		}
-
-		valid = 0;
-
-		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);
-
-			free(graph_name);
-
-			if (g) {
-				if (!commit_graph_link_base(g, graph_chain)) {
-					graph_chain = g;
-					valid = 1;
-				} else {
-					commit_graph_free(g);
-				}
-
-				break;
-			}
-		}
-
-		if (!valid) {
-			warning(_("unable to find all commit-graph files"));
-			break;
-		}
-	}
-
-	commit_graph_validate_chain(graph_chain);
-	if (graph_chain)
-		graph_chain->incomplete = !valid;
-
-	fclose(fp);
-	strbuf_release(&line);
-
-	return graph_chain;
-}
-
-static struct commit_graph *load_commit_graph_chain(struct repository *repo,
-						    const char *dir)
-{
-	char *chain_file = get_commit_graph_chain_filename(dir);
-	struct stat st;
-	int fd;
-	struct commit_graph *g = NULL;
-
-	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);
-	}
-
-	free(chain_file);
-	return g;
-}
-
-struct commit_graph *read_commit_graph_one(struct repository *repo,
-					   const char *dir)
-{
-	struct commit_graph *g = load_commit_graph_v1(repo, dir);
-
-	if (!g)
-		g = load_commit_graph_chain(repo, dir);
-
-	return g;
-}
-
 /*
  * Return 1 if commit_graph is non-NULL, and 0 otherwise.
  *
@@ -2078,12 +1901,6 @@ static int write_graph_chunk_base(struct hashfile *f,
 	return 0;
 }
 
-static void mark_commit_graphs(struct commit_graph *tip,
-			       struct commit_graph *base);
-static void expire_commit_graphs(const char *dirpath,
-				 struct string_list *kept,
-				 timestamp_t expire_time);
-
 static int write_commit_graph_file(struct write_commit_graph_context *ctx,
 				   int fd, const char *path,
 				   unsigned char *file_hash)
@@ -2160,200 +1977,6 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx,
 	return 0;
 }
 
-int commit_graph_write(struct repository *r,
-		       const char *dir,
-		       const struct commit_graph_write_options *opts)
-{
-	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;
-	size_t mergeable_layers = 0, chainable_layers = 0;
-	unsigned char hash[GIT_MAX_RAWSZ];
-	bool mergeable = true;
-	const char *out_path;
-	int out_fd;
-	int ret;
-
-	if (!commit_graph_write_allowed(r))
-		return 0;
-
-	abs_dir = absolute_pathdup(dir);
-
-	g = prepare_commit_graph(r);
-
-	/*
-	 * We only consider merging layers that belong to our own object
-	 * directory. Furthermore, we can only chain on top of either:
-	 *
-	 *   - A split commit graph layer, regardless of whether or not it is
-	 *     local.
-	 *
-	 *   - A local non-split commit graph, in which case we'll convert that
-	 *     layer into a split commit graph layer.
-	 */
-	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);
-
-		free(path);
-
-		if (!is_local && !is_split)
-			break;
-		if (!is_local)
-			mergeable = false;
-
-		if (mergeable)
-			mergeable_layers++;
-		chainable_layers++;
-	}
-
-	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		graph_name = xstrfmt("%s/info/commit-graphs/tmp_graph_XXXXXX", abs_dir);
-	} else {
-		graph_name = get_commit_graph_filename(abs_dir);
-	}
-
-	if (safe_create_leading_directories(r, graph_name)) {
-		ret = error(_("unable to create leading directories of %s"),
-			    graph_name);
-		goto out;
-	}
-
-	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		char *chain_name = get_commit_graph_chain_filename(abs_dir);
-
-		repo_hold_lock_file_for_update_mode(r, &lk, chain_name,
-						    LOCK_DIE_ON_ERROR, 0444);
-		free(chain_name);
-
-		graph_layer = mks_tempfile_m(graph_name, 0444);
-		if (!graph_layer) {
-			ret = error(_("unable to create temporary graph layer"));
-			goto out;
-		}
-
-		if (adjust_shared_perm(r, get_tempfile_path(graph_layer))) {
-			ret = error(_("unable to adjust shared permissions for '%s'"),
-				    get_tempfile_path(graph_layer));
-			goto out;
-		}
-
-		out_fd = get_tempfile_fd(graph_layer);
-		out_path = get_tempfile_path(graph_layer);
-	} else {
-		repo_hold_lock_file_for_update_mode(r, &lk, graph_name,
-						    LOCK_DIE_ON_ERROR, 0444);
-		out_fd = get_lock_file_fd(&lk);
-		out_path = get_lock_file_path(&lk);
-	}
-
-	ret = commit_graph_write_layer(r, opts, mergeable_layers, chainable_layers,
-				       out_fd, out_path, &base, hash);
-	if (ret <= 0)
-		goto out;
-
-	/*
-	 * Snapshot everything we need from the current chain before closing
-	 * it, as we must not have any layers mapped when replacing them.
-	 */
-	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);
-
-		string_list_append(&chain_hashes, new_hash);
-		for (chain = base; chain; chain = chain->base_graph)
-			string_list_append(&chain_hashes, oid_to_hex(&chain->oid));
-
-		/*
-		 * In case we have a non-split commit graph we need to convert
-		 * it into the base layer of the new split commit graph by
-		 * renaming it.
-		 */
-		if (base) {
-			base_rename_to = get_split_graph_filename(base->dir, oid_to_hex(&base->oid));
-			if (strcmp(base->filename, base_rename_to))
-				base_rename_from = xstrdup(base->filename);
-			else
-				FREE_AND_NULL(base_rename_to);
-		}
-
-		for (chain = base; chain; chain = chain->base_graph) {
-			if (chain == base && base_rename_to)
-				string_list_append(&kept, base_rename_to);
-			else
-				string_list_append(&kept, chain->filename);
-		}
-		string_list_append(&kept, new_path);
-
-		mark_commit_graphs(g, base);
-	}
-
-	commit_graph_close(r->objects);
-
-	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
-		FILE *chainf = fdopen_lock_file(&lk, "w");
-
-		if (!chainf) {
-			ret = error(_("unable to open commit-graph chain file"));
-			goto out;
-		}
-
-		if (base_rename_from) {
-			if (rename(base_rename_from, base_rename_to)) {
-				ret = error(_("failed to rename base commit-graph file"));
-				goto out;
-			}
-		} else if (!base) {
-			char *graph_name = get_commit_graph_filename(abs_dir);
-			unlink(graph_name);
-			free(graph_name);
-		}
-
-		if (rename_tempfile(&graph_layer, new_path)) {
-			ret = error(_("failed to rename temporary commit-graph file"));
-			goto out;
-		}
-
-		/*
-		 * Chain hashes are ordered newest-first, but the chain file is
-		 * oldest-first so we iterate in reverse.
-		 */
-		for (size_t i = chain_hashes.nr; i; i--)
-			fprintf(chainf, "%s\n", chain_hashes.items[i - 1].string);
-	}
-
-	commit_lock_file(&lk);
-
-	if (!(opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
-		char *chain_name = get_commit_graph_chain_filename(abs_dir);
-		unlink(chain_name);
-		free(chain_name);
-	}
-
-	expire_commit_graphs(abs_dir, &kept,
-			     opts->expire_time ? opts->expire_time : time(NULL));
-
-	ret = 0;
-
-out:
-	rollback_lock_file(&lk);
-	delete_tempfile(&graph_layer);
-	string_list_clear(&chain_hashes, 0);
-	string_list_clear(&kept, 0);
-	free(base_rename_from);
-	free(base_rename_to);
-	free(graph_name);
-	free(new_path);
-	free(abs_dir);
-	return ret;
-}
-
 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
 				       struct commit_graph *graph_to_merge)
 {
@@ -2521,66 +2144,6 @@ static void merge_commit_graphs(struct write_commit_graph_context *ctx,
 	sort_and_scan_merged_commits(ctx);
 }
 
-static void mark_commit_graphs(struct commit_graph *tip,
-			       struct commit_graph *base)
-{
-	time_t now = time(NULL);
-
-	for (struct commit_graph *g = tip; g && g != base; g = g->base_graph) {
-		struct stat st;
-		struct utimbuf updated_time;
-
-		if (stat(g->filename, &st) < 0)
-			continue;
-
-		updated_time.actime = st.st_atime;
-		updated_time.modtime = now;
-		utime(g->filename, &updated_time);
-	}
-}
-
-static void expire_commit_graphs(const char *dirpath,
-				 struct string_list *kept,
-				 timestamp_t expire_time)
-{
-	struct strbuf path = STRBUF_INIT;
-	DIR *dir;
-	struct dirent *de;
-	size_t dirnamelen;
-
-	strbuf_addstr(&path, dirpath);
-	strbuf_addstr(&path, "/info/commit-graphs");
-	dir = opendir(path.buf);
-
-	if (!dir)
-		goto out;
-
-	strbuf_addch(&path, '/');
-	dirnamelen = path.len;
-	while ((de = readdir(dir)) != NULL) {
-		struct stat st;
-
-		strbuf_setlen(&path, dirnamelen);
-		strbuf_addstr(&path, de->d_name);
-
-		if (stat(path.buf, &st) < 0)
-			continue;
-
-		if (st.st_mtime > expire_time)
-			continue;
-		if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
-			continue;
-
-		if (!unsorted_string_list_has_string(kept, path.buf))
-			unlink(path.buf);
-	}
-
-out:
-	if (dir)
-		closedir(dir);
-	strbuf_release(&path);
-}
-
 bool commit_graph_write_allowed(struct repository *r)
 {
 	prepare_repo_settings(r);
diff --git a/commit-graph.h b/commit-graph.h
index 3b5e0eb2d73..3295498fd34 100644
--- a/commit-graph.h
+++ b/commit-graph.h
@@ -141,8 +141,6 @@ int commit_graph_link_base(struct commit_graph *g,
  * filters in case they aren't.
  */
 void commit_graph_validate_chain(struct commit_graph *g);
-struct commit_graph *read_commit_graph_one(struct repository *repo,
-					   const char *dir);
 
 struct repo_settings;
 
diff --git a/odb/source-files.c b/odb/source-files.c
index 5ad27fd4bc7..90cda52b48b 100644
--- a/odb/source-files.c
+++ b/odb/source-files.c
@@ -1231,6 +1231,180 @@ static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
 	return 0;
 }
 
+char *get_commit_graph_filename(const char *dir)
+{
+	return xstrfmt("%s/info/commit-graph", dir);
+}
+
+static char *get_split_graph_filename(const char *dir,
+				      const char *oid_hex)
+{
+	return xstrfmt("%s/info/commit-graphs/graph-%s.graph", dir,
+		       oid_hex);
+}
+
+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)
+{
+	*fd = git_open(graph_file);
+	if (*fd < 0)
+		return 0;
+	if (fstat(*fd, st)) {
+		close(*fd);
+		return 0;
+	}
+	return 1;
+}
+
+static struct commit_graph *load_commit_graph_one(struct repository *repo,
+						  const char *dir,
+						  const char *graph_file)
+{
+	struct stat st;
+	int fd;
+	struct commit_graph *g;
+	int open_ok = open_commit_graph(graph_file, &fd, &st);
+
+	if (!open_ok)
+		return NULL;
+
+	g = commit_graph_read(repo, fd, &st);
+	if (g) {
+		g->dir = absolute_pathdup(dir);
+		g->filename = absolute_pathdup(graph_file);
+	}
+
+	return g;
+}
+
+static struct commit_graph *load_commit_graph_v1(struct repository *repo,
+						 const char *dir)
+{
+	char *graph_name = get_commit_graph_filename(dir);
+	struct commit_graph *g = load_commit_graph_one(repo, dir, graph_name);
+	free(graph_name);
+
+	return g;
+}
+
+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)
+		return 0;
+	if (fstat(*fd, st)) {
+		close(*fd);
+		return 0;
+	}
+	if (st->st_size < (ssize_t) hash_algo->hexsz) {
+		close(*fd);
+		if (!st->st_size) {
+			/* treat empty files the same as missing */
+			errno = ENOENT;
+		} else {
+			warning(_("commit-graph chain file too small"));
+			errno = EINVAL;
+		}
+		return 0;
+	}
+	return 1;
+}
+
+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;
+	int i = 0, valid = 1, count;
+	FILE *fp = xfdopen(fd, "r");
+
+	count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
+
+	for (i = 0; i < count; i++) {
+		struct odb_source_files *files;
+		struct object_id oid;
+
+		if (strbuf_getline_lf(&line, fp) == EOF)
+			break;
+
+		if (get_oid_hex_algop(line.buf, &oid, odb->repo->hash_algo)) {
+			warning(_("invalid commit-graph chain: line '%s' not a hash"),
+				line.buf);
+			valid = 0;
+			break;
+		}
+
+		valid = 0;
+
+		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);
+
+			free(graph_name);
+
+			if (g) {
+				if (!commit_graph_link_base(g, graph_chain)) {
+					graph_chain = g;
+					valid = 1;
+				} else {
+					commit_graph_free(g);
+				}
+
+				break;
+			}
+		}
+
+		if (!valid) {
+			warning(_("unable to find all commit-graph files"));
+			break;
+		}
+	}
+
+	commit_graph_validate_chain(graph_chain);
+	if (graph_chain)
+		graph_chain->incomplete = !valid;
+
+	fclose(fp);
+	strbuf_release(&line);
+
+	return graph_chain;
+}
+
+static struct commit_graph *load_commit_graph_chain(struct repository *repo,
+						    const char *dir)
+{
+	char *chain_file = get_commit_graph_chain_filename(dir);
+	struct stat st;
+	int fd;
+	struct commit_graph *g = NULL;
+
+	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);
+	}
+
+	free(chain_file);
+	return g;
+}
+
+static struct commit_graph *read_commit_graph_one(struct repository *repo,
+						  const char *dir)
+{
+	struct commit_graph *g = load_commit_graph_v1(repo, dir);
+
+	if (!g)
+		g = load_commit_graph_chain(repo, dir);
+
+	return g;
+}
+
 static int odb_source_files_read_commit_graph(struct odb_source *source,
 					      struct commit_graph **out)
 {
@@ -1248,6 +1422,260 @@ static int odb_source_files_read_commit_graph(struct odb_source *source,
 	return 1;
 }
 
+static void mark_commit_graphs(struct commit_graph *tip,
+			       struct commit_graph *base)
+{
+	time_t now = time(NULL);
+
+	for (struct commit_graph *g = tip; g && g != base; g = g->base_graph) {
+		struct stat st;
+		struct utimbuf updated_time;
+
+		if (stat(g->filename, &st) < 0)
+			continue;
+
+		updated_time.actime = st.st_atime;
+		updated_time.modtime = now;
+		utime(g->filename, &updated_time);
+	}
+}
+
+static void expire_commit_graphs(const char *dirpath,
+				 struct string_list *kept,
+				 timestamp_t expire_time)
+{
+	struct strbuf path = STRBUF_INIT;
+	DIR *dir;
+	struct dirent *de;
+	size_t dirnamelen;
+
+	strbuf_addstr(&path, dirpath);
+	strbuf_addstr(&path, "/info/commit-graphs");
+	dir = opendir(path.buf);
+
+	if (!dir)
+		goto out;
+
+	strbuf_addch(&path, '/');
+	dirnamelen = path.len;
+	while ((de = readdir(dir)) != NULL) {
+		struct stat st;
+
+		strbuf_setlen(&path, dirnamelen);
+		strbuf_addstr(&path, de->d_name);
+
+		if (stat(path.buf, &st) < 0)
+			continue;
+
+		if ((timestamp_t) st.st_mtime > expire_time)
+			continue;
+		if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
+			continue;
+
+		if (!unsorted_string_list_has_string(kept, path.buf))
+			unlink(path.buf);
+	}
+
+out:
+	if (dir)
+		closedir(dir);
+	strbuf_release(&path);
+}
+
+int commit_graph_write(struct repository *r,
+		       const char *dir,
+		       const struct commit_graph_write_options *opts)
+{
+	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;
+	size_t mergeable_layers = 0, chainable_layers = 0;
+	unsigned char hash[GIT_MAX_RAWSZ];
+	bool mergeable = true;
+	const char *out_path;
+	int out_fd;
+	int ret;
+
+	if (!commit_graph_write_allowed(r))
+		return 0;
+
+	abs_dir = absolute_pathdup(dir);
+
+	g = prepare_commit_graph(r);
+
+	/*
+	 * We only consider merging layers that belong to our own object
+	 * directory. Furthermore, we can only chain on top of either:
+	 *
+	 *   - A split commit graph layer, regardless of whether or not it is
+	 *     local.
+	 *
+	 *   - A local non-split commit graph, in which case we'll convert that
+	 *     layer into a split commit graph layer.
+	 */
+	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);
+
+		free(path);
+
+		if (!is_local && !is_split)
+			break;
+		if (!is_local)
+			mergeable = false;
+
+		if (mergeable)
+			mergeable_layers++;
+		chainable_layers++;
+	}
+
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		graph_name = xstrfmt("%s/info/commit-graphs/tmp_graph_XXXXXX", abs_dir);
+	} else {
+		graph_name = get_commit_graph_filename(abs_dir);
+	}
+
+	if (safe_create_leading_directories(r, graph_name)) {
+		ret = error(_("unable to create leading directories of %s"),
+			    graph_name);
+		goto out;
+	}
+
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		char *chain_name = get_commit_graph_chain_filename(abs_dir);
+
+		repo_hold_lock_file_for_update_mode(r, &lk, chain_name,
+						    LOCK_DIE_ON_ERROR, 0444);
+		free(chain_name);
+
+		graph_layer = mks_tempfile_m(graph_name, 0444);
+		if (!graph_layer) {
+			ret = error(_("unable to create temporary graph layer"));
+			goto out;
+		}
+
+		if (adjust_shared_perm(r, get_tempfile_path(graph_layer))) {
+			ret = error(_("unable to adjust shared permissions for '%s'"),
+				    get_tempfile_path(graph_layer));
+			goto out;
+		}
+
+		out_fd = get_tempfile_fd(graph_layer);
+		out_path = get_tempfile_path(graph_layer);
+	} else {
+		repo_hold_lock_file_for_update_mode(r, &lk, graph_name,
+						    LOCK_DIE_ON_ERROR, 0444);
+		out_fd = get_lock_file_fd(&lk);
+		out_path = get_lock_file_path(&lk);
+	}
+
+	ret = commit_graph_write_layer(r, opts, mergeable_layers, chainable_layers,
+				       out_fd, out_path, &base, hash);
+	if (ret <= 0)
+		goto out;
+
+	/*
+	 * Snapshot everything we need from the current chain before closing
+	 * it, as we must not have any layers mapped when replacing them.
+	 */
+	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);
+
+		string_list_append(&chain_hashes, new_hash);
+		for (chain = base; chain; chain = chain->base_graph)
+			string_list_append(&chain_hashes, oid_to_hex(&chain->oid));
+
+		/*
+		 * In case we have a non-split commit graph we need to convert
+		 * it into the base layer of the new split commit graph by
+		 * renaming it.
+		 */
+		if (base) {
+			base_rename_to = get_split_graph_filename(base->dir, oid_to_hex(&base->oid));
+			if (strcmp(base->filename, base_rename_to))
+				base_rename_from = xstrdup(base->filename);
+			else
+				FREE_AND_NULL(base_rename_to);
+		}
+
+		for (chain = base; chain; chain = chain->base_graph) {
+			if (chain == base && base_rename_to)
+				string_list_append(&kept, base_rename_to);
+			else
+				string_list_append(&kept, chain->filename);
+		}
+		string_list_append(&kept, new_path);
+
+		mark_commit_graphs(g, base);
+	}
+
+	commit_graph_close(r->objects);
+
+	if (opts->flags & COMMIT_GRAPH_WRITE_SPLIT) {
+		FILE *chainf = fdopen_lock_file(&lk, "w");
+
+		if (!chainf) {
+			ret = error(_("unable to open commit-graph chain file"));
+			goto out;
+		}
+
+		if (base_rename_from) {
+			if (rename(base_rename_from, base_rename_to)) {
+				ret = error(_("failed to rename base commit-graph file"));
+				goto out;
+			}
+		} else if (!base) {
+			char *graph_name = get_commit_graph_filename(abs_dir);
+			unlink(graph_name);
+			free(graph_name);
+		}
+
+		if (rename_tempfile(&graph_layer, new_path)) {
+			ret = error(_("failed to rename temporary commit-graph file"));
+			goto out;
+		}
+
+		/*
+		 * Chain hashes are ordered newest-first, but the chain file is
+		 * oldest-first so we iterate in reverse.
+		 */
+		for (size_t i = chain_hashes.nr; i; i--)
+			fprintf(chainf, "%s\n", chain_hashes.items[i - 1].string);
+	}
+
+	commit_lock_file(&lk);
+
+	if (!(opts->flags & COMMIT_GRAPH_WRITE_SPLIT)) {
+		char *chain_name = get_commit_graph_chain_filename(abs_dir);
+		unlink(chain_name);
+		free(chain_name);
+	}
+
+	expire_commit_graphs(abs_dir, &kept,
+			     opts->expire_time ? opts->expire_time : (timestamp_t) time(NULL));
+
+	ret = 0;
+
+out:
+	rollback_lock_file(&lk);
+	delete_tempfile(&graph_layer);
+	string_list_clear(&chain_hashes, 0);
+	string_list_clear(&kept, 0);
+	free(base_rename_from);
+	free(base_rename_to);
+	free(graph_name);
+	free(new_path);
+	free(abs_dir);
+	return ret;
+}
+
 static int odb_source_files_write_commit_graph(struct odb_source *source,
 					       const struct commit_graph_write_options *opts)
 {

-- 
2.56.1.763.g16177fe143.dirty


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 12/12] odb/source-files: internalize logic to derive commit graph paths
  2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
                   ` (10 preceding siblings ...)
  2026-11-16 13:07 ` [PATCH 11/12] odb/source-files: move logic to derive commit graph paths Patrick Steinhardt
@ 2026-11-16 13:07 ` Patrick Steinhardt
  11 siblings, 0 replies; 14+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 13:07 UTC (permalink / raw)
  To: git

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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 01/12] commit-graph: simplify linking commit graph chains
  2026-11-16 13:07 ` [PATCH 01/12] commit-graph: simplify linking commit graph chains Patrick Steinhardt
@ 2026-11-16 20:53   ` Junio C Hamano
  0 siblings, 0 replies; 14+ messages in thread
From: Junio C Hamano @ 2026-11-16 20:53 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: git

Patrick Steinhardt <ps@pks.im> writes:

> When we load a commit graph chain we verify for every layer that the
> hash of the base commit graph matches what the current tip has in its
> base graphs chunk. To do so we pass both the chain itself as well as the
> array of object IDs that we have read from the commit-graph chain file
> to `add_graph_to_chain()`.
>
> But the array of object IDs is redundant information: we have already
> loaded and linked the base layers, and as part of that we have stored
> their respective hashes in the structure itself. Consequently, we can
> already derive the hashes by walking the chain of base layers.
>
> Refactor the function to only take the new tip and base layers as input
> and derive all the other information ad-hoc and make the function
> public. This prepares for a subsequent commit, where we are about to
> move the logic that creates commit graph chains into the object database
> backend.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  commit-graph.c | 52 ++++++++++++++++++++++++----------------------------
>  commit-graph.h |  8 ++++++++
>  2 files changed, 32 insertions(+), 28 deletions(-)
>
> diff --git a/commit-graph.c b/commit-graph.c
> index da65cd3deb9..94c761907a0 100644
> --- a/commit-graph.c
> +++ b/commit-graph.c
> @@ -575,50 +575,48 @@ static void validate_mixed_bloom_settings(struct commit_graph *g)
>  	}
>  }
>  
> -static int add_graph_to_chain(struct commit_graph *g,
> -			      struct commit_graph *chain,
> -			      struct object_id *oids,
> -			      int n)
> +int commit_graph_link_base(struct commit_graph *g,
> +			   struct commit_graph *base)

Nice simplification, I guess.

>  {
> -	struct commit_graph *cur_g = chain;
> +	struct commit_graph *layer;
> +	size_t n = 0;
> +
> +	for (layer = base; layer; layer = layer->base_graph)
> +		n++;

We chase the pointers to count the number of layers.  We no longer
count in signed int, which is probably a big plus in the sanitation
department.

>  	if (n && !g->chunk_base_graphs) {
>  		warning(_("commit-graph has no base graphs chunk"));
> -		return 0;
> +		return -1;
>  	}

It might be obvious when reading (and writing) this patch, but it is
a welcome change to standardize on "0 means happy" convention.
Should the fact that we now use more common error reporting
convention be mentioned in the log message?

>  	if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) {
>  		warning(_("commit-graph base graphs chunk is too small"));
> -		return 0;
> +		return -1;
>  	}
>  
> -	while (n) {
> +	for (layer = base; layer; layer = layer->base_graph) {
>  		n--;
> -
> -		if (!cur_g ||
> -		    !oideq(&oids[n], &cur_g->oid) ||
> -		    !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
> +		if (!hasheq(layer->oid.hash,
> +			    g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
>  			    g->hash_algo)) {

We no longer check cur_g->oid is what we expect, because we lost the
array that holds the value to compare.  Would this make cur_g->oid a
write-only member that is only computed and written but never used
other than reporting purposes?  In commit-graph.c before this step,
I think this is the only place that reads ->oid for purposes other
than passing it to diagnostics like die/warn.

We seem to still be making sure in load_commit_graph_chain_fd_st()
that the line we read into local oid there is a hash, so perhaps it
is possible to validate g->oid against this oid after we load g?

> @@ -682,7 +679,7 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
>  			free(graph_name);
>  
>  			if (g) {
> -				if (add_graph_to_chain(g, graph_chain, oids, i)) {
> +				if (!commit_graph_link_base(g, graph_chain)) {
>  					graph_chain = g;
>  					valid = 1;
>  				} else {

> +
> +/*
> + * Link the commit graph layer `g` on top of the chain `base`. `base` may be
> + * NULL if `g` is the root of a new chain. Returns 0 on success, a negative
> + * error code otherwise.
> + */
> +int commit_graph_link_base(struct commit_graph *g,
> +			   struct commit_graph *base);
>  struct commit_graph *read_commit_graph_one(struct repository *repo,
>  					   const char *dir);

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-11-16 20:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 13:07 [PATCH 00/12] Introduce pluggable commit graphs Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 01/12] commit-graph: simplify linking commit graph chains Patrick Steinhardt
2026-11-16 20:53   ` Junio C Hamano
2026-11-16 13:07 ` [PATCH 02/12] commit-graph: simplify reading a single commit graph Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 03/12] commit-graph: expose function to validate a commit graph chain Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 04/12] commit-graph: decide mergeable and chainable layers by depth Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 05/12] commit-graph: introduce common exit for `write_commit_graph_file()` Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 06/12] commit-graph: derive layer infos from the passed-in chain Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 07/12] commit-graph: split out check whether writing commit graphs is allowed Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 08/12] commit-graph: split out logic to write a new commit graph layer Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 09/12] commit-graph: mark incomplete commit graph chains Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 10/12] odb/source: make commit graphs pluggable Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 11/12] odb/source-files: move logic to derive commit graph paths Patrick Steinhardt
2026-11-16 13:07 ` [PATCH 12/12] odb/source-files: internalize " Patrick Steinhardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox