From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Justin Tobler <jltobler@gmail.com>,
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Subject: [PATCH v3 9/9] commit-graph: derive layer info from the passed-in chain
Date: Fri, 04 Dec 2026 07:53:44 +0100 [thread overview]
Message-ID: <20261204-pks-odb-pluggable-commit-graphs-v3-9-85dafc8ce396@pks.im> (raw)
In-Reply-To: <20261204-pks-odb-pluggable-commit-graphs-v3-0-85dafc8ce396@pks.im>
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 | 228 ++++++++++++++++++++++++---------------------------------
1 file changed, 97 insertions(+), 131 deletions(-)
diff --git a/commit-graph.c b/commit-graph.c
index 3d7011ee95c..27d17a1161e 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"
@@ -1156,8 +1157,6 @@ struct write_commit_graph_context {
int progress_done;
uint64_t progress_cnt;
- char *base_graph_name;
-
/*
* Number of topmost layers of the existing chain that may be merged
* into the new layer, and that the new layer may be built on top of,
@@ -1168,9 +1167,6 @@ struct write_commit_graph_context {
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;
@@ -2102,14 +2098,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;
@@ -2219,78 +2225,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));
+
+ /*
+ * 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);
- 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;
+ 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;
}
@@ -2303,7 +2333,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;
@@ -2350,30 +2379,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,
@@ -2477,48 +2488,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);
@@ -2529,7 +2529,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);
@@ -2542,20 +2541,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);
}
@@ -2667,14 +2658,6 @@ int commit_graph_write(struct repository *r,
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;
}
@@ -2742,14 +2725,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);
@@ -2765,17 +2742,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.98.0.rc0.245.g55e557da3a.dirty
prev parent reply other threads:[~2026-12-04 6:54 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
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-20 11:15 ` 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 ` [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
2026-11-20 12:22 ` [PATCH v2 0/7] Introduce pluggable commit graphs (pt.1) Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 1/7] commit-graph: verify all trailer hashes against the chain file Patrick Steinhardt
2026-12-02 22:29 ` Justin Tobler
2026-12-03 7:31 ` Patrick Steinhardt
2026-12-03 9:12 ` Patrick Steinhardt
2026-12-03 9:13 ` Kaartic Sivaraam
2026-12-03 13:18 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 2/7] commit-graph: simplify linking commit graph chains Patrick Steinhardt
2026-12-02 22:54 ` Justin Tobler
2026-12-03 10:03 ` Kaartic Sivaraam
2026-12-03 13:18 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 3/7] commit-graph: simplify reading a single commit graph Patrick Steinhardt
2026-12-03 10:37 ` Kaartic Sivaraam
2026-12-03 13:19 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 4/7] commit-graph: expose function to validate a commit graph chain Patrick Steinhardt
2026-12-03 11:01 ` Kaartic Sivaraam
2026-12-03 13:19 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 5/7] commit-graph: decide mergeable and chainable layers by depth Patrick Steinhardt
2026-12-02 23:38 ` Justin Tobler
2026-12-03 7:31 ` Patrick Steinhardt
2026-12-03 13:38 ` Kaartic Sivaraam
2026-12-03 17:16 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 6/7] commit-graph: introduce common exit for `write_commit_graph_file()` Patrick Steinhardt
2026-12-03 13:50 ` Kaartic Sivaraam
2026-12-03 17:16 ` Patrick Steinhardt
2026-11-20 12:22 ` [PATCH v2 7/7] commit-graph: derive layer infos from the passed-in chain Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 0/9] Introduce pluggable commit graphs (pt.1) Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 1/9] t: deduplicate `graph_read_expect()` implementations Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 2/9] t/helper: report number of layers in "read-graph" Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 3/9] commit-graph: verify all trailer hashes against the chain file Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 4/9] commit-graph: simplify linking commit graph chains Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 5/9] commit-graph: simplify reading a single commit graph Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 6/9] commit-graph: expose function to finalize a commit graph chain Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 7/9] commit-graph: decide mergeable and chainable layers by depth Patrick Steinhardt
2026-12-04 6:53 ` [PATCH v3 8/9] commit-graph: introduce common exit for `write_commit_graph_file()` Patrick Steinhardt
2026-12-04 6:53 ` Patrick Steinhardt [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204-pks-odb-pluggable-commit-graphs-v3-9-85dafc8ce396@pks.im \
--to=ps@pks.im \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jltobler@gmail.com \
--cc=kaartic.sivaraam@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox