From 52bcfc4dcaed8bc99db8b9d91b7553a141fdcf4b Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Thu, 8 Oct 2026 20:05:28 +0900 Subject: [PATCH] knot-maintenance: write splitted commitgraph append small commitgraph files as chain in most cases, and write unified commitgraph file on full repack. Signed-off-by: Seongmin Lee --- .../knot-maintenance/src/commitgraph.rs | 34 +++++++++++++ knot2/crates/knot-maintenance/src/lib.rs | 16 +++--- .../knot-maintenance/tests/commitgraph.rs | 49 +++++++++++++++++++ knot2/crates/knot-maintenance/tests/engine.rs | 16 +++--- 4 files changed, 102 insertions(+), 13 deletions(-) diff --git a/knot2/crates/knot-maintenance/src/commitgraph.rs b/knot2/crates/knot-maintenance/src/commitgraph.rs index 861d2058e..377845e73 100644 --- a/knot2/crates/knot-maintenance/src/commitgraph.rs +++ b/knot2/crates/knot-maintenance/src/commitgraph.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet}; use gix::ObjectId; +use gix::commitgraph::{Graph, write}; use gix::objs::tree::EntryKind as TreeEntryKind; use gix::objs::{CommitRef, Kind, TagRefIter, TreeRef}; use gix::prelude::FindExt; @@ -132,6 +133,39 @@ pub fn write(repo: &Repo) -> Result { Ok(true) } +pub fn write_layer(repo: &Repo) -> Result { + let kind = repo.object_format().kind(); + let odb = repo.git().objects.clone(); + let tips: Vec = repo + .references()? + .into_iter() + .filter_map(|record| peel_to_commit(&odb, record.target.object_id(), kind, MAX_PEEL_DEPTH)) + .collect(); + let info_dir = repo.objects_dir().join("info"); + let graph = Graph::from_info_dir(&info_dir).ok(); + let options = write::Options { + object_hash: kind, + bloom_settings: write::bloom_filter_settings( + graph.as_ref(), + true, + Some(BLOOM_HASH_VERSION), + ), + max_new_filters: None, + thread_limit: knot_resource::gix_thread_limit().map(knot_resource::ThreadCount::get), + split: write::Split::Merge { + size_multiple: 2, + max_commits: None, + }, + }; + match write::write_to_info_dir(&odb, &info_dir, graph, tips, options, None) { + Ok(outcome) => Ok(outcome.is_some()), + Err(error) => { + tracing::warn!(repo = %repo.path().display(), %error, "commit-graph layer write failed"); + Ok(false) + } + } +} + pub fn remove(repo: &Repo) -> Result<(), MaintError> { let path = graph_path(repo); match std::fs::remove_file(&path) { diff --git a/knot2/crates/knot-maintenance/src/lib.rs b/knot2/crates/knot-maintenance/src/lib.rs index 18a1c3d2e..bf8e0556c 100644 --- a/knot2/crates/knot-maintenance/src/lib.rs +++ b/knot2/crates/knot-maintenance/src/lib.rs @@ -273,12 +273,6 @@ pub fn run_repo( } }; - let commit_graph = if opts.commit_graph && snapshot.due(Task::CommitGraph) { - commitgraph::write(repo)? - } else { - false - }; - let full_repack = snapshot.due(Task::FullRepack); if full_repack { fsio::mark_full_repack(repo.git().git_dir(), now_seconds)?; @@ -338,6 +332,16 @@ pub fn run_repo( None => PruneReport::skipped(), }; + let commit_graph = if !opts.commit_graph { + false + } else if full_repack { + commitgraph::write(repo)? + } else if snapshot.due(Task::CommitGraph) { + commitgraph::write_layer(repo)? + } else { + false + }; + let multi_pack_index = if !opts.multi_pack_index { MidxStatus::Absent } else if snapshot.due(Task::MultiPackIndex) { diff --git a/knot2/crates/knot-maintenance/tests/commitgraph.rs b/knot2/crates/knot-maintenance/tests/commitgraph.rs index 1e1428b1a..d8bab7444 100644 --- a/knot2/crates/knot-maintenance/tests/commitgraph.rs +++ b/knot2/crates/knot-maintenance/tests/commitgraph.rs @@ -248,3 +248,52 @@ fn changed_path_filter_matches_git_for_high_byte_names() { commit(root, "café/résumé.txt", "2", "edit non-ascii"); build_and_compare(root, "sha1", &[b"BIDX", b"BDAT"]); } + +fn chain_layers(root: &Path) -> usize { + std::fs::read_to_string(root.join(".git/objects/info/commit-graphs/commit-graph-chain")) + .map_or(0, |chain| chain.lines().count()) +} + +fn maintain(root: &Path, at: knot_types::UnixSeconds) -> bool { + run_repo(&Repo::open(root).unwrap(), at, &options()) + .unwrap() + .commit_graph +} + +#[test] +fn later_passes_write_layers_and_the_full_repack_collapses_them() { + if skip() { + return; + } + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + let single = root.join(".git/objects/info/commit-graph"); + seed_history(root, "sha1"); + + assert!(maintain(root, now())); + assert!(single.exists()); + assert_eq!(chain_layers(root), 0); + + commit(root, "dir1/a.txt", "a2", "first layer"); + assert!(maintain(root, now())); + assert!(!single.exists(), "the single graph migrates into the chain"); + assert_eq!(chain_layers(root), 2); + let (ok, stderr) = graph_verify(root); + assert!(ok, "verify after the first layer: {stderr}"); + + commit(root, "dir2/d.txt", "d2", "second layer"); + assert!(maintain(root, now())); + assert_eq!(chain_layers(root), 2, "the small top layers merge"); + let (ok, stderr) = graph_verify(root); + assert!(ok, "verify after the second layer: {stderr}"); + ["dir1/a.txt", "dir2", "c.txt"].iter().for_each(|path| { + assert_eq!(path_log(root, path, true), path_log(root, path, false)); + }); + + let later = knot_types::UnixSeconds::new(now().get() + 432_000); + assert!(maintain(root, later)); + assert!(single.exists()); + assert!(!root.join(".git/objects/info/commit-graphs").exists()); + let (ok, stderr) = graph_verify(root); + assert!(ok, "verify after the full repack: {stderr}"); +} diff --git a/knot2/crates/knot-maintenance/tests/engine.rs b/knot2/crates/knot-maintenance/tests/engine.rs index 74071c499..03c778721 100644 --- a/knot2/crates/knot-maintenance/tests/engine.rs +++ b/knot2/crates/knot-maintenance/tests/engine.rs @@ -115,7 +115,8 @@ fn the_written_graph_verifies_and_accelerates_selection_through_an_octopus_merge let tip = commit(&repo, 6, vec![pair, octopus]); set_ref(&repo, "refs/heads/main", tip); - let graph = repo.git().git_dir().join("objects/info/commit-graph"); + let info = repo.git().git_dir().join("objects/info"); + let has_graph = || info.join("commit-graph").exists() || info.join("commit-graphs").exists(); let closure = |r: &Repo| -> HashSet { r.select_pack_objects(knot_git::Wants::new(&[tip]), knot_git::Haves::new(&[])) .unwrap() @@ -123,9 +124,9 @@ fn the_written_graph_verifies_and_accelerates_selection_through_an_octopus_merge .collect() }; let decode_closure = closure(&repo); - assert!(!graph.exists()); + assert!(!has_graph()); - assert!(run_repo(&repo, now(), &options()).unwrap().commit_graph && graph.exists()); + assert!(run_repo(&repo, now(), &options()).unwrap().commit_graph && has_graph()); let (ok, stderr) = git(&repo, &["commit-graph", "verify"]); assert!( ok, @@ -152,19 +153,20 @@ fn commit_graph_knob_lifecycle_skips_backfills_and_sweeps() { commit_graph: false, ..options() }; - let graph = repo.git().git_dir().join("objects/info/commit-graph"); + let info = repo.git().git_dir().join("objects/info"); + let has_graph = || info.join("commit-graph").exists() || info.join("commit-graphs").exists(); let first = run_repo(&repo, now(), &off).unwrap(); assert_eq!(first.repack.status, RepackStatus::Repacked); assert!( - !first.commit_graph && !graph.exists(), + !first.commit_graph && !has_graph(), "no graph is written when the knob is off" ); let reopened = Repo::open(repo.git().git_dir()).unwrap(); let backfill = run_repo(&reopened, now(), &options()).unwrap(); assert!( - backfill.commit_graph && graph.exists(), + backfill.commit_graph && has_graph(), "a packed repo with no graph backfills it" ); assert_eq!(backfill.repack.status, RepackStatus::Clean); @@ -178,7 +180,7 @@ fn commit_graph_knob_lifecycle_skips_backfills_and_sweeps() { let swept = Repo::open(repo.git().git_dir()).unwrap(); assert!(!run_repo(&swept, now(), &off).unwrap().commit_graph); assert!( - !graph.exists(), + !has_graph(), "flipping the knob off sweeps the orphan graph" ); } -- knotmirror 0.0.1