From d203a5f0561e6de7e731c34d33b5e519cd8a26e4 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 15:58:59 -0600 Subject: [PATCH 1/4] log, rev-list: --simplify-by-decoration with paths drops a decorated merge left no parent Over histories of several roots whose commits share dates, git was seen to take a decorated merge that simplification leaves no parent, and that holds nothing the pathspec matches, for a root TREESAME to the empty tree: it is not listed (unless --show-pulls lists it; --first-parent, which leaves it one parent to decide by, keeps it), its children lose it as they lose such a root, it holds no parents for the parent counts, and ancestry for dropping parents goes no further than it. zig-git listed it, and the merges below it that kept it as a parent. Found alongside, by the same comparisons: - under --full-diff, a commit is diffed against the TREESAME roots it lost only when it is TREESAME to them, and a merge left no parent against the empty tree when it is TREESAME to none of them; - with --sparse and --first-parent, a later parent that is a TREESAME root is kept, as --sparse drops no root; - a pull --show-pulls keeps, shown as its parent, is shown once when that parent is already among the commit's parents. The test of every option now runs --simplify-by-decoration with paths over the makeHistory histories, and a new one over four more seeds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 13 ++++-- src/RevWalk.zig | 69 +++++++++++++++++++++++++------- tools/simplify_tests.zig | 85 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 145 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 453b342..fe6978b 100644 --- a/README.md +++ b/README.md @@ -320,7 +320,11 @@ do: `--show-pulls` and `--remove-empty` as `--simplify-merges` takes them (below), and with paths, where a commit is kept when it is decorated or changes them (a root, decorated or not, only when it holds something - they match); + they match, and likewise a decorated merge simplification leaves no + parent, which git takes for such a root — though `--show-pulls` still + lists it, and `--first-parent`, which leaves it one parent to decide + by, keeps it — so that its children lose it as they lose a root and + ancestry goes no further); - naming commits by the nearest tag, as `git describe` does: annotated tags, lightweight ones or every ref, `--match` and `--exclude`, git's choice among the names of one commit, and its depth, which is counted @@ -656,9 +660,12 @@ candidate list of parents, under every way of simplifying history: - under `--simplify-merges` and `--simplify-by-decoration`, with or without `--parents`, the parents they show, or, where a commit shows none, the root TREESAME to the empty tree that it reached (its whole - tree is then diffed against that root's); under `--full-diff`, the + tree is then diffed against that root's; a merge, only against those + of the roots it lost it is TREESAME to, and against the empty tree when + it is TREESAME to none); under `--full-diff`, the parents they show and the roots TREESAME to the empty tree they lost - that are not ancestors of the commit's other parents, in its order — with `--first-parent`, which decides by the + that are not ancestors of the commit's other parents and that the + commit is TREESAME to, in its order — with `--first-parent`, which decides by the first parent alone and shows the later ones replaced, that is the first parent even where it was dropped. diff --git a/src/RevWalk.zig b/src/RevWalk.zig index 1177987..d2d0ee8 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -190,7 +190,12 @@ //! shows. A pull `--show-pulls` keeps is listed but shown, in its //! children's parents, as its one parent when it holds one. With `--sparse` no commit with //! one parent is replaced, nor a merge left one parent only by dropping -//! ancestors, and no TREESAME root is dropped. +//! ancestors, and no TREESAME root is dropped. Under +//! `--simplify-by-decoration` with paths, a decorated merge left no +//! parent and holding nothing the pathspec matches is taken for a root +//! TREESAME to the empty tree: not listed (unless `--show-pulls` lists +//! it), dropped from its children's parents, holding no parents for the +//! parent counts, and with nothing past it for ancestry. //! - The sorts, `--children` and the parent counts use the parents //! simplification left; `--boundary` the rewritten ones, by which the //! boundary is also sorted. @@ -466,6 +471,11 @@ const SimState = struct { boundary_parents: Span = .{}, /// Kept by `--simplify-merges` only because `--show-pulls` lists it. pull_only: bool = false, + /// A decorated merge `--simplify-by-decoration` left no parents, + /// holding nothing the pathspec matches, which git takes for a root + /// TREESAME to the empty tree: its children lose it as they lose one, + /// and ancestry goes no further. + as_root: bool = false, /// What `--simplify-merges` replaces the commit with. replacement: Index = 0, replaced: bool = false, @@ -1141,6 +1151,8 @@ fn shownBySimplification(w: *const RevWalk, c: Index) bool { const s = w.options.simplify.?; const st = w.peekState(c); if (s.simplify_merges and !st.remains) return false; + // --show-pulls still lists a decorated merge taken for a root. + if (st.as_root and !s.show_pulls) return false; if (!st.treesame or st.pull) return true; if (s.sparse) return true; // Counted as the commit is listed: in a walk by generation number, @@ -1315,6 +1327,13 @@ fn onPath(w: *const RevWalk, c: Index) bool { return !w.excluded(c) or w.isBottom(c); } +/// Whether `--simplify-merges` drops `c` from its children's parents as a +/// root TREESAME to the empty tree. +fn isTreesameRoot(w: *const RevWalk, c: Index) bool { + const st = w.peekState(c); + return st.as_root or (w.simplifiedParents(c).len == 0 and st.treesame); +} + fn replacementOf(w: *const RevWalk, c: Index, in_list: *const std.AutoHashMapUnmanaged(Index, void)) Index { if (!in_list.contains(c)) return c; return w.peekState(c).replacement; @@ -1343,8 +1362,9 @@ fn simplifyOneMerge( // With --sparse they are shown as they are. const r = if (w.options.simplify.?.sparse) p else w.replacementOf(p, in_list); // A listed root TREESAME to the empty tree goes, as it does from - // the first parent's place. - if (in_list.contains(r) and w.simplifiedParents(r).len == 0 and w.peekState(r).treesame) continue; + // the first parent's place (but not with --sparse, which drops + // none). + if (!w.options.simplify.?.sparse and in_list.contains(r) and w.isTreesameRoot(r)) continue; // A parent replaced by one already there is shown once. if (std.mem.findScalar(Index, later.items, r) != null) continue; if (ps.len != 0 and w.replacementOf(ps[0], in_list) == r) continue; @@ -1400,9 +1420,8 @@ fn simplifyOneMerge( var step2: std.ArrayList(Parent) = .empty; defer step2.deinit(w.gpa); for (kept_anc.items) |e| { - const st = w.peekState(e.commit); // With --sparse, a commit with one parent keeps it. - const ts_root = in_list.contains(e.commit) and w.simplifiedParents(e.commit).len == 0 and st.treesame and + const ts_root = in_list.contains(e.commit) and w.isTreesameRoot(e.commit) and !(w.options.simplify.?.sparse and ps.len < 2); if (!ts_root) try step2.append(w.gpa, e); } @@ -1433,8 +1452,16 @@ fn simplifyOneMerge( step2.clearRetainingCapacity(); try step2.append(w.gpa, on_path.?); } + // A decorated merge left no parents, holding nothing the pathspec + // matches, git takes for a root TREESAME to the empty tree. + const as_root = if (w.options.simplify.?.decorated != null and w.options.simplify.?.pathspec.items.len != 0 and + ps.len > 1 and step2.items.len == 0) + try w.isEmpty(c, null) + else + false; const st = try w.simState(c); st.replaced = true; + st.as_root = as_root; // With --sparse, a merge with an excluded parent was seen to be // replaced or kept by rules not matched. if (w.options.simplify.?.sparse and list.items.len > 1) { @@ -1450,7 +1477,7 @@ fn simplifyOneMerge( }; break :blk (if (n == 0) list.items.len else n) != 1; }); - const kept = sparse_kept or step2.items.len != 1 or !(step2.items[0].same or (treesame_commit and !w.relevant(step2.items[0].commit))); + const kept = (sparse_kept or step2.items.len != 1 or !(step2.items[0].same or (treesame_commit and !w.relevant(step2.items[0].commit)))); // A pull kept only for --show-pulls is shown, in its children's // parents, as its one parent. st.pull_only = !kept and st.pull; @@ -1463,11 +1490,21 @@ fn simplifyOneMerge( try commits.appendSlice(w.gpa, later.items); const held_sp = try w.storeSpan(commits.items); for (commits.items[0..step2.items.len]) |*x| x.* = w.shownFor(x.*); + // A pull shown as the parent another parent is shows it once. + var n_shown: usize = 0; + for (commits.items, 0..) |x, i| { + if (std.mem.findScalar(Index, commits.items[0..n_shown], x) != null) continue; + commits.items[n_shown] = commits.items[i]; + n_shown += 1; + } + commits.shrinkRetainingCapacity(n_shown); const sp = try w.storeSpan(commits.items); - // Left none, the commit is diffed against the roots it lost; under - // --full-diff, against the parents it shows and the TREESAME - // roots it lost that are not ancestors of its other parents, in - // the order it had them, always. + // Left none, the commit is diffed against the roots it lost (a + // merge, only those it is TREESAME to, and against the empty tree + // when there are none); under --full-diff, against the parents it + // shows and the TREESAME roots it lost that are not ancestors of + // its other parents and that it is TREESAME to, in the order it + // had them. commits.clearRetainingCapacity(); // What git holds once it has simplified the merges, before it // shows the commit (when it drops the TREESAME roots and, with @@ -1487,7 +1524,7 @@ fn simplifyOneMerge( // counted and diffed against only when the commit shows no // parent; a merge losing one, git was seen to count and diff // by rules not matched, where those matter. - const made_root = w.store.parents(e.commit).len != 0; + const made_root = w.store.parents(e.commit).len != 0 and !w.peekState(e.commit).as_root; if (dropped_root and made_root and list.items.len > 1) { const o = w.options; if (step2.items.len == 0 or o.min_parents != 0 or o.max_parents != null or w.options.simplify.?.full_diff) @@ -1495,7 +1532,10 @@ fn simplifyOneMerge( } // With --sparse, git was seen to diff against the parents shown // alone, when there are any. - const lost = dropped_root and !is_ancestor and (step2.items.len == 0 or (!made_root and !w.options.simplify.?.sparse)); + const lost = dropped_root and !is_ancestor and (if (step2.items.len == 0) + list.items.len == 1 or e.same + else + !made_root and !w.options.simplify.?.sparse and e.same); // Whether git still holds such a root for a merge that shows // other parents was seen to vary by rules not matched. if (dropped_root and !is_ancestor and step2.items.len != 0 and list.items.len > 1) { @@ -1509,7 +1549,8 @@ fn simplifyOneMerge( // What git holds loses the TREESAME roots, unless nothing else // is left; a decorated one is not TREESAME there. const decorated_root = if (w.options.simplify.?.decorated) |d| d.has(d.context, e.commit) else false; - if (shown or (dropped_root and !is_ancestor and (step2.items.len == 0 or decorated_root))) try held.append(w.gpa, e.commit); + // A merge taken for a root holds none. + if (!as_root and (shown or (dropped_root and !is_ancestor and (step2.items.len == 0 or decorated_root)))) try held.append(w.gpa, e.commit); } try commits.appendSlice(w.gpa, later.items); const roots = try w.storeSpan(commits.items); @@ -1661,7 +1702,7 @@ fn reaches( while (r.stack.pop()) |x| { w.work.ancestry_steps += 1; // A commit --remove-empty made a root has no parents for git. - const onward: []const Index = if (cleared_roots and w.peekState(x).cleared) &.{} else if (simplified and in_list.contains(x)) blk: { + const onward: []const Index = if ((cleared_roots and w.peekState(x).cleared) or w.peekState(x).as_root) &.{} else if (simplified and in_list.contains(x)) blk: { const st = w.peekState(x); if (!st.replaced) break :blk w.store.parents(x); if (st.remains) break :blk w.span(st.merged_parents); diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 3b2cb1a..b3652eb 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -711,12 +711,9 @@ test "--simplify-merges and --simplify-by-decoration with every other option, as for ([_]bool{ false, true }) |graph| { if (graph) try repo.git(&.{ "commit-graph", "write", "--reachable" }); for (sets) |set| for ([_][]const []const u8{ &.{"main"}, &.{"b1..main"}, &.{ "--all", "--not", "b3" }, &.{"b2...main"} }) |range| for ([_][]const []const u8{ &.{"a"}, &.{"d"}, &.{ "m", "b" }, &.{} }) |spec| { - // Only --simplify-by-decoration simplifies without paths, and - // it is tried only without them here: with them, over these - // histories of several roots, it was seen to differ from git - // before any of these options, as `main` does. + // Only --simplify-by-decoration simplifies without paths. const by_decoration = std.mem.find(u8, set[1], "decoration") != null or std.mem.find(u8, set[2], "decoration") != null; - if ((spec.len == 0) != by_decoration) continue; + if (spec.len == 0 and !by_decoration) continue; var a: std.ArrayList([]const u8) = .empty; defer a.deinit(gpa); try a.appendSlice(gpa, set); @@ -763,6 +760,84 @@ test "--simplify-merges counts and shows the parents git holds" { } } +test "--simplify-by-decoration with paths over histories of several roots and shared dates" { + const gpa = testing.allocator; + for ([_]u64{ 21, 22, 23, 24 }) |seed| { + var repo = try Repo.init(gpa, testing.io); + defer repo.deinit(); + try makeHistory(&repo, seed, 90); + try repo.git(&.{ "tag", "-a", "-m", "x", "an", "main~3" }); + for ([_][]const []const u8{ + &.{ "rev-list", "--simplify-by-decoration", "--parents" }, + &.{ "rev-list", "--simplify-by-decoration", "--parents", "--show-pulls" }, + &.{ "rev-list", "--simplify-by-decoration", "--parents", "--no-merges" }, + &.{ "log", "--format=%h %p %d", "--simplify-by-decoration", "--raw", "--full-diff" }, + }) |set| for ([_][]const u8{ "main", "--all" }) |range| for ([_][]const u8{ "a", "m", "sub", "nothing" }) |path| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(gpa); + try a.appendSlice(gpa, set); + try a.appendSlice(gpa, &.{ range, "--", path }); + try sameUnlessRefused(&repo, a.items); + }; + } +} + +test "--simplify-by-decoration with paths takes a decorated merge left no parents for a root" { + // c2, decorated, merges two roots holding nothing f matches: git + // drops it as it drops such a root, from what it lists and from its + // children's parents, unless --show-pulls lists it or --first-parent + // leaves it one parent. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "x=1" }, + .{ .files = "y=1" }, + .{ .files = "x=1 y=1", .parents = &.{ 0, 1 } }, + .{ .files = "x=2 y=1", .parents = &.{2} }, + .{ .files = "x=2 y=1 f=1", .parents = &.{3} }, + .{ .files = "x=3 y=1 f=1", .parents = &.{4} }, + }); + for ([_][]const []const u8{ + &.{}, + &.{"--show-pulls"}, + &.{"--no-merges"}, + &.{"--first-parent"}, + &.{"--boundary"}, + &.{"--max-parents=0"}, + &.{ "--raw", "--full-diff" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "log", "--format=%s %p %d", "--decorate-refs=refs/tags/c2", "--decorate-refs=refs/tags/c3", "--simplify-by-decoration" }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "--", "f" }); + try same(&repo, a.items); + } +} + +test "--full-diff diffs a merge against the roots it lost only when it is TREESAME to them" { + // c2 loses root c0 and differs from it: diffed against c1 alone. c4 + // loses c0 and is TREESAME to it: diffed against both. c7, a merge + // of two roots it differs from, is diffed as a root. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "x=1" }, + .{ .files = "f=1" }, + .{ .files = "x=1 f=2", .parents = &.{ 0, 1 } }, + .{ .files = "f=2", .parents = &.{1} }, + .{ .files = "x=1", .parents = &.{ 3, 0 } }, + .{ .files = "x=2" }, + .{ .files = "x=1 y=1" }, + .{ .files = "x=2 y=1 f=1", .parents = &.{ 5, 6 } }, + .{ .files = "x=1 f=3", .parents = &.{ 2, 4, 7 } }, + }); + for ([_][]const u8{ "--simplify-merges", "--simplify-by-decoration" }) |mode| { + try same(&repo, &.{ "log", "--format=%s %p", "--raw", "--full-diff", mode, "--", "f" }); + try same(&repo, &.{ "log", "--format=%s %p", "--raw", mode, "--", "f" }); + } +} + test "a commit --skip passes in a walk with exclusions has its parents rewritten" { // c3 is skipped, then a boundary commit: git shows the parents it // rewrote before counting it, and diffs it against its own under -- knotmirror 0.0.1 From d2e5f837264ac77ceb5b336e0d8da063344db1d9 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 15:59:07 -0600 Subject: [PATCH 2/4] rev-list, log: a boundary commit read in resolving the revisions shows its parents A later parent under --first-parent that the walk never follows is a boundary commit git shows with no parents after --parents, unless git has read it. git was seen to have read the commits it goes through in resolving the revisions: HEAD~5 reads HEAD to HEAD~4, X^2 reads X, and a message search the commits it looks through; named by its ID, such a commit is not read. zig-git's resolver read them without the walk knowing, so a boundary commit among them was shown with no parents, with or without --skip, --merges, --min-parents, paths or a commit-graph. The resolver now records the commits it parses, and rev-list and log load them before walking. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 4 +++- src/Resolver.zig | 8 ++++++++ tools/rev_list.zig | 5 +++++ tools/simplify_tests.zig | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fe6978b..9e4a37f 100644 --- a/README.md +++ b/README.md @@ -673,7 +673,9 @@ A merge shown against each parent (`-m`, `--diff-merges=separate`) that has no entry left, nothing differing from any parent, is written once, as a commit with an empty diff, as git does. A boundary commit nothing has read (a later parent under `--first-parent` that neither -the walk nor a symmetric difference's search for merge bases read) +the walk, nor a symmetric difference's search for merge bases, nor +resolving the revisions — `HEAD~5` reads `HEAD` to `HEAD~4`, and a +message search the commits it looks through — read) shows its own parents in `%p` and `%P`, and none after `--parents` unless a diff was made for it; it is dated zero where the boundary is ordered by date. diff --git a/src/Resolver.zig b/src/Resolver.zig index d88c89c..e481054 100644 --- a/src/Resolver.zig +++ b/src/Resolver.zig @@ -89,6 +89,10 @@ parent_steps: bool = false, loud: bool = false, /// Where a bad setting read part way through is reported. setting_diag: ?*Diagnostics = null, +/// The commits read for their parents or dates in resolving revisions +/// (`~`, `^`, message searches), which git has parsed by +/// the time it walks: a `--boundary` commit among them shows its parents. +parsed: std.ArrayList(Id) = .empty, /// What resolving needs besides the repository. pub const Options = struct { @@ -279,6 +283,7 @@ pub fn deinit(r: *Resolver) void { r.clearAmbiguity(); r.lookup.deinit(); r.messages.deinit(r.gpa); + r.parsed.deinit(r.gpa); r.arena.deinit(); r.* = undefined; } @@ -1030,11 +1035,13 @@ fn grafts(r: *Resolver, diag: ?*Diagnostics) Error!*const Grafts { fn parentOf(r: *Resolver, id: Id, n: u64) Error!?Id { if ((try r.grafts(null)).lookup(id)) |g| { if (try r.kindOf(id) == null) return null; + try r.parsed.append(r.gpa, id); return if (n >= 1 and n <= g.parents.len) g.parents[@intCast(n - 1)] else null; } var o = try r.readObject(id) orelse return null; defer o.deinit(r.gpa); const c = Commit.parse(id.object_format, o.data) catch return null; + try r.parsed.append(r.gpa, id); var it = c.parents(); var i: u64 = 0; while (it.next()) |p| { @@ -1133,6 +1140,7 @@ fn enqueue(r: *Resolver, heap: *std.ArrayList(Queued), seen: *std.AutoHashMapUnm defer o.deinit(r.gpa); if (o.kind != .commit) return; const c = Commit.parse(id.object_format, o.data) catch return; + try r.parsed.append(r.gpa, id); const when: i64 = if (c.committer()) |who| (if (who.date()) |d| d.seconds else 0) else 0; order.* += 1; try heap.append(r.gpa, .{ .id = id, .date = when, .order = order.* }); diff --git a/tools/rev_list.zig b/tools/rev_list.zig index 2d63935..d060369 100644 --- a/tools/rev_list.zig +++ b/tools/rev_list.zig @@ -983,6 +983,11 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder if (searching) options.filter = .{ .context = &filter, .keep = Filter.keep }; if (filter.diffs != null or filter.follower != null) options.diff_filter = .{ .context = &filter, .keep = Filter.keepDiff }; + // What git read resolving the revisions it has read for the walk too. + for (revs.resolver.parsed.items) |id| { + var diag: git.Diagnostics = .{}; + h.store.load(try h.store.name(id), &diag) catch {}; + } var walk: RevWalk = .init(gpa, &h.store, options); defer walk.deinit(); filter.walk = &walk; diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index b3652eb..018efaa 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -838,6 +838,44 @@ test "--full-diff diffs a merge against the roots it lost only when it is TREESA } } +test "a --first-parent boundary commit read in resolving the revisions shows its parents" { + // c1, a later parent of c3 the walk never follows, is read finding + // HEAD~2, so that git knows its parents when it shows it as a + // boundary commit; named by its ID, it is not read. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "a=1" }, + .{ .files = "a=2", .parents = &.{0} }, + .{ .files = "a=1 b=1", .parents = &.{0} }, + .{ .files = "a=2 b=1", .parents = &.{ 2, 1 } }, + .{ .files = "a=2 b=2", .parents = &.{3} }, + .{ .files = "a=3 b=2", .parents = &.{ 1, 4 } }, + }); + const c0 = try repo.output(&.{ "rev-parse", "c0" }); + defer testing.allocator.free(c0); + const by_id = try std.fmt.allocPrint(testing.allocator, "^{s}", .{std.mem.trimEnd(u8, c0, "\n")}); + defer testing.allocator.free(by_id); + for ([_][]const u8{ "^HEAD~2", "^HEAD^~1", by_id }) |exclude| for ([_][]const []const u8{ + &.{}, + &.{"--skip=1"}, + &.{"--merges"}, + &.{"--min-parents=2"}, + &.{"--full-history"}, + &.{ "--", "a" }, + &.{ "--full-history", "--skip=2", "--", "a" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "rev-list", "--first-parent", "--boundary", "--parents", "c4", exclude }); + try a.appendSlice(testing.allocator, opts); + try same(&repo, a.items); + a.items[0] = "log"; + a.items[3] = "--format=%m %s %p"; + try same(&repo, a.items); + }; +} + test "a commit --skip passes in a walk with exclusions has its parents rewritten" { // c3 is skipped, then a boundary commit: git shows the parents it // rewrote before counting it, and diffs it against its own under -- knotmirror 0.0.1 From 3840237441f87ab98b442aa93d3e74fcbfd090bf Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 16:16:34 -0600 Subject: [PATCH 3/4] log, rev-list: --simplify-merges counts a merge's lost roots as git does Over more histories of the makeHistory shape, with exclusions and parent counts, two more rules of git's --simplify-merges (and so of --simplify-by-decoration with paths) were found by comparison: - a merge left no parent holds, for the parent counts and --boundary, only the TREESAME roots it lost that it is TREESAME to (none for a merge adding the paths to two roots), where zig-git held them all; - a merge that loses a parent for being an ancestor of another is TREESAME when it is TREESAME to every parent left on the path, so that one left a single parent on the path besides excluded ones is replaced by it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 7 +++++-- src/RevWalk.zig | 19 +++++++++++++++-- tools/simplify_tests.zig | 45 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9e4a37f..c690fb5 100644 --- a/README.md +++ b/README.md @@ -233,9 +233,12 @@ do: as it does) with every other option, a commit-graph or none, as git was seen to: an excluded parent counts as off the path, as under `--ancestry-path`, so that a commit TREESAME to its parents with one - left besides it is replaced by that one; git holds, once it has + left besides it is replaced by that one, and a merge that loses a + parent for being an ancestor of another is TREESAME when it is TREESAME + to every parent left on the path; git holds, once it has simplified the merges, the parents left and, only when none is left, - the roots TREESAME to the empty tree they lost (a decorated one, which + the roots TREESAME to the empty tree they lost (a merge, only those it + is TREESAME to; a decorated one, which is not TREESAME there, always), with the later parents unreplaced under `--first-parent`, and drops the roots and replaces the later parents diff --git a/src/RevWalk.zig b/src/RevWalk.zig index d2d0ee8..dbf67bb 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -1448,7 +1448,20 @@ fn simplifyOneMerge( // seen to be replaced or kept by rules not matched, apparently // depending on when git learned of the exclusions. if (on_path_count == 0 and step2.items.len > 1 and !w.options.onAncestryPath()) return error.UnsupportedMergeExclusion; - if (treesame_commit and on_path_count == 1 and on_path_count != step2.items.len) { + // A merge that lost a parent for being an ancestor of another was + // seen to be TREESAME when it is TREESAME to every parent left on the + // path (one an excluded parent was replaced by, which git also drops, + // no longer counting against it). + const recomputed = blk: { + if (kept_anc.items.len == list.items.len) break :blk treesame_commit; + var any = false; + for (kept_anc.items) |e| if (w.onPath(e.commit)) { + if (!e.same) break :blk false; + any = true; + }; + break :blk if (any) true else treesame_commit; + }; + if (recomputed and on_path_count == 1 and on_path_count != step2.items.len) { step2.clearRetainingCapacity(); try step2.append(w.gpa, on_path.?); } @@ -1550,7 +1563,9 @@ fn simplifyOneMerge( // is left; a decorated one is not TREESAME there. const decorated_root = if (w.options.simplify.?.decorated) |d| d.has(d.context, e.commit) else false; // A merge taken for a root holds none. - if (!as_root and (shown or (dropped_root and !is_ancestor and (step2.items.len == 0 or decorated_root)))) try held.append(w.gpa, e.commit); + // A merge left none holds only those it is TREESAME to. + const held_root = dropped_root and !is_ancestor and (if (step2.items.len == 0) list.items.len == 1 or e.same else decorated_root); + if (!as_root and (shown or held_root)) try held.append(w.gpa, e.commit); } try commits.appendSlice(w.gpa, later.items); const roots = try w.storeSpan(commits.items); diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 018efaa..cdb87c5 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -838,6 +838,51 @@ test "--full-diff diffs a merge against the roots it lost only when it is TREESA } } +test "--simplify-merges counts no roots for a merge left none it is TREESAME to" { + // c2 merges two roots and adds f: git counts it with no parents. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "x=1" }, + .{ .files = "y=1" }, + .{ .files = "x=1 y=1 f=1", .parents = &.{ 0, 1 } }, + .{ .files = "x=1 y=1 f=2", .parents = &.{2} }, + }); + for ([_][]const u8{ "--simplify-merges", "--simplify-by-decoration" }) |mode| for ([_][]const []const u8{ + &.{"--max-parents=0"}, + &.{"--min-parents=2"}, + &.{"--merges"}, + &.{ "--boundary", "-n", "1" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "rev-list", "--parents", mode }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "main", "--", "f" }); + try same(&repo, a.items); + }; +} + +test "--simplify-merges replaces a merge that lost an ancestor parent by the one it is TREESAME to" { + // c6 is TREESAME to c5 alone; c2 is excluded, and c4 is replaced by + // c1, excluded too and an ancestor of c2, so dropped: git then takes + // c6 for TREESAME and replaces it by c5. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "a=1" }, + .{ .files = "a=1 c=1", .parents = &.{0} }, + .{ .files = "a=2 c=1", .parents = &.{1} }, + .{ .files = "a=2 c=2", .parents = &.{2} }, + .{ .files = "a=1 b=1 c=1", .parents = &.{1} }, + .{ .files = "a=5", .parents = &.{0} }, + .{ .files = "a=5 b=1 c=1", .parents = &.{ 5, 2, 4 } }, + .{ .files = "a=6 b=1 c=1", .parents = &.{6} }, + }); + try same(&repo, &.{ "log", "--format=%s %p", "--simplify-merges", "--parents", "main", "^c3", "--", "a" }); + try same(&repo, &.{ "log", "--format=%s %p", "--simplify-by-decoration", "--decorate-refs=refs/tags/c7", "--parents", "main", "^c3", "--", "a" }); +} + test "a --first-parent boundary commit read in resolving the revisions shows its parents" { // c1, a later parent of c3 the walk never follows, is read finding // HEAD~2, so that git knows its parents when it shows it as a -- knotmirror 0.0.1 From 50ef994704d7a4de0c6244cc82dc85e662b16be8 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 16:32:20 -0600 Subject: [PATCH 4/4] log, rev-list: count a decorated merge taken for a root as git does A merge that loses a decorated merge --simplify-by-decoration took for a root, while it shows other parents, was refused with a parent count or --boundary, as for a real root. git was seen to hold it, for the parent counts and a boundary commit, only where the merge is TREESAME to it, so that case is now answered rather than refused. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 9 +++++---- src/RevWalk.zig | 14 +++++++++----- tools/simplify_tests.zig | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c690fb5..a64b1ad 100644 --- a/README.md +++ b/README.md @@ -326,8 +326,9 @@ do: they match, and likewise a decorated merge simplification leaves no parent, which git takes for such a root — though `--show-pulls` still lists it, and `--first-parent`, which leaves it one parent to decide - by, keeps it — so that its children lose it as they lose a root and - ancestry goes no further); + by, keeps it — so that its children lose it as they lose a root, + holding it for the parent counts only where they are TREESAME to it, + and ancestry goes no further); - naming commits by the nearest tag, as `git describe` does: annotated tags, lightweight ones or every ref, `--match` and `--exclude`, git's choice among the names of one commit, and its depth, which is counted @@ -918,8 +919,8 @@ deliberate or not yet matched: paths, `--sparse` or `--show-pulls` together with `--remove-empty`; `--sparse` with `--first-parent` and `--boundary`; a parent count or `--boundary` where a merge that shows other parents lost a root - TREESAME to the empty tree (git was seen to count it for some such - merges and not others); `log --full-diff` with `--show-pulls` where a + TREESAME to the empty tree, other than a decorated merge taken for one + (git was seen to count it for some such merges and not others); `log --full-diff` with `--show-pulls` where a commit shows a pull's parent in the pull's place; `--remove-empty` where a merge loses a parent `--remove-empty` made a root and either keeps no parent or a parent count or `--full-diff` diff --git a/src/RevWalk.zig b/src/RevWalk.zig index dbf67bb..66c7ede 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -194,8 +194,9 @@ //! `--simplify-by-decoration` with paths, a decorated merge left no //! parent and holding nothing the pathspec matches is taken for a root //! TREESAME to the empty tree: not listed (unless `--show-pulls` lists -//! it), dropped from its children's parents, holding no parents for the -//! parent counts, and with nothing past it for ancestry. +//! it), dropped from its children's parents (held for their parent +//! counts only by those TREESAME to it), holding no parents itself, and +//! with nothing past it for ancestry. //! - The sorts, `--children` and the parent counts use the parents //! simplification left; `--boundary` the rewritten ones, by which the //! boundary is also sorted. @@ -1550,8 +1551,9 @@ fn simplifyOneMerge( else !made_root and !w.options.simplify.?.sparse and e.same); // Whether git still holds such a root for a merge that shows - // other parents was seen to vary by rules not matched. - if (dropped_root and !is_ancestor and step2.items.len != 0 and list.items.len > 1) { + // other parents was seen to vary by rules not matched (but + // never to hold a decorated merge taken for a root). + if (dropped_root and !is_ancestor and step2.items.len != 0 and list.items.len > 1 and !w.peekState(e.commit).as_root) { const o = w.options; if (o.min_parents != 0 or o.max_parents != null or o.boundary) return error.UnsupportedLostRootMerge; } @@ -1564,7 +1566,9 @@ fn simplifyOneMerge( const decorated_root = if (w.options.simplify.?.decorated) |d| d.has(d.context, e.commit) else false; // A merge taken for a root holds none. // A merge left none holds only those it is TREESAME to. - const held_root = dropped_root and !is_ancestor and (if (step2.items.len == 0) list.items.len == 1 or e.same else decorated_root); + // A decorated merge taken for a root is held only by a commit + // TREESAME to it. + const held_root = dropped_root and !is_ancestor and (if (step2.items.len == 0) list.items.len == 1 or e.same else decorated_root and (e.same or !w.peekState(e.commit).as_root)); if (!as_root and (shown or held_root)) try held.append(w.gpa, e.commit); } try commits.appendSlice(w.gpa, later.items); diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index cdb87c5..8671476 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -815,6 +815,40 @@ test "--simplify-by-decoration with paths takes a decorated merge left no parent } } +test "--simplify-by-decoration counts a decorated merge taken for a root as a parent only where TREESAME to it" { + // c2 is taken for a root under f. c4 differs from it and shows c3 + // alone, counted as one parent; c5 is TREESAME to it and is counted + // with two. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importCommits(&repo, &.{ + .{ .files = "x=1" }, + .{ .files = "y=1" }, + .{ .files = "x=1 y=1", .parents = &.{ 0, 1 } }, + .{ .files = "f=5" }, + .{ .files = "x=1 y=1 f=6", .parents = &.{ 3, 2 } }, + .{ .files = "x=1 y=1", .parents = &.{ 3, 2 } }, + .{ .files = "x=2 y=1 f=7", .parents = &.{ 4, 5 } }, + }); + for ([_][]const []const u8{ + &.{}, + &.{"--min-parents=2"}, + &.{"--max-parents=1"}, + &.{"--merges"}, + &.{"--no-merges"}, + &.{"--boundary"}, + &.{ "--boundary", "-n", "2" }, + &.{ "--raw", "--full-diff" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "log", "--format=%s %p %d", "--decorate-refs=refs/tags/c2", "--simplify-by-decoration", "--parents" }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "--", "f" }); + try same(&repo, a.items); + } +} + test "--full-diff diffs a merge against the roots it lost only when it is TREESAME to them" { // c2 loses root c0 and differs from it: diffed against c1 alone. c4 // loses c0 and is TREESAME to it: diffed against both. c7, a merge -- knotmirror 0.0.1