git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 08/12] commit-graph: split out logic to write a new commit graph layer

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 16, 2026, 13:07 UTC
Message-ID
<20261116-pks-odb-pluggable-commit-graphs-v1-8-a83fc1ff0808@pks.im>
In-Reply-To
<20261116-pks-odb-pluggable-commit-graphs-v1-0-a83fc1ff0808@pks.im>

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
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 10 of 14 in “Introduce pluggable commit graphs”
  1. 00/12 Introduce pluggable commit graphsPatrick Steinhardt, Nov 16, 2026
  2. 01/12 commit-graph: simplify linking commit graph chainsPatrick Steinhardt, Nov 16, 2026
  3. Junio C HamanoNov 16, 2026
  4. 02/12 commit-graph: simplify reading a single commit graphPatrick Steinhardt, Nov 16, 2026
  5. 03/12 commit-graph: expose function to validate a commit graph chainPatrick Steinhardt, Nov 16, 2026
  6. 04/12 commit-graph: decide mergeable and chainable layers by depthPatrick Steinhardt, Nov 16, 2026
  7. 05/12 commit-graph: introduce common exit for `write_commit_graph_file()`Patrick Steinhardt, Nov 16, 2026
  8. 06/12 commit-graph: derive layer infos from the passed-in chainPatrick Steinhardt, Nov 16, 2026
  9. 07/12 commit-graph: split out check whether writing commit graphs is allowedPatrick Steinhardt, Nov 16, 2026
  10. 08/12 commit-graph: split out logic to write a new commit graph layerPatrick Steinhardt, Nov 16, 2026
  11. 09/12 commit-graph: mark incomplete commit graph chainsPatrick Steinhardt, Nov 16, 2026
  12. 10/12 odb/source: make commit graphs pluggablePatrick Steinhardt, Nov 16, 2026
  13. 11/12 odb/source-files: move logic to derive commit graph pathsPatrick Steinhardt, Nov 16, 2026
  14. 12/12 odb/source-files: internalize logic to derive commit graph pathsPatrick Steinhardt, Nov 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.