diff --git a/src/merge_base.zig b/src/merge_base.zig index e56ab96..96dfdcb 100644 --- a/src/merge_base.zig +++ b/src/merge_base.zig @@ -22,9 +22,16 @@ //! Which commits the painting reads matters beyond its answer: `RevWalk` //! excludes through every commit that has been read, and a commit-graph //! that is corrupt past a point git never reads is no failure. So the -//! painting queues a commit only when it paints it with something new, -//! and reads it whole only when it comes out of the queue; until then a -//! commit the commit-graph lists is known only by its dates. +//! painting queues a commit only when it paints it with something new. +//! Without a commit-graph it reads the commit whole only when it comes +//! out of the queue, and stops when only stale commits are queued. With +//! one, git reads each commit as it queues it, and stops as soon as no +//! commit queued could lead to another candidate: none painted from both +//! sides and not stale, not one from each side, and none from one side +//! whose generation number is above that of a commit painted from the +//! other. That was found from what `rev-list A...B` then excludes, and +//! from the commit-graph lookups `GIT_COMMIT_GRAPH_PARANOIA` makes git +//! do, seen in the loose-object directories it lists. const std = @import("std"); const Allocator = std.mem.Allocator; @@ -75,6 +82,9 @@ const Painter = struct { /// queues a commit by generation number first, read without its /// parents, and reads the rest only if it comes out of the queue. fn push(p: *Painter, c: Index, diag: ?*Diagnostics) Error!void { + // With a commit-graph git reads a commit as it queues it, which + // tells it the generation number. + if (p.store.graph.layers.len != 0) try p.store.load(c, diag); const d = try p.store.peek(c, diag); const generation = if (p.store.graph.layers.len != 0) d.generation else 0; try p.queue.append(p.gpa, .{ .generation = generation, .time = d.time, .sequence = p.sequence, .commit = c }); @@ -112,6 +122,40 @@ const Painter = struct { return false; } + /// Whether painting could still find a candidate: a commit queued + /// that is painted from both sides and not stale; one queued from each + /// side; or one queued from one side that could still reach a commit + /// painted from the other, which has a lower generation number. + fn mayFindMore(p: *Painter) bool { + var highest: [2]?u64 = .{ null, null }; + for (p.queue.items) |q| { + const f = p.flag(q.commit) & (first_side | second_side | stale); + if (f == first_side | second_side) return true; + const side: usize = if (f == first_side) 0 else if (f == second_side) 1 else continue; + const g = p.store.generation(q.commit); + highest[side] = @max(highest[side] orelse 0, g); + } + if (highest[0] != null and highest[1] != null) return true; + const side: usize = if (highest[0] != null) 0 else if (highest[1] != null) 1 else return false; + const other: u8 = if (side == 0) second_side else first_side; + const top = highest[side].?; + var it = p.flags.iterator(); + while (it.next()) |e| { + if (e.value_ptr.* & (first_side | second_side | stale) != other) continue; + if (top == Commits.infinite_generation or p.store.generation(e.key_ptr.*) < top) return true; + } + return false; + } + + /// Whether to go on painting: with a commit-graph, git stops once no + /// new candidate can be found; without one, once every commit queued + /// is stale. + fn goOn(p: *Painter) bool { + if (p.queue.items.len == 0) return false; + if (p.store.graph.layers.len != 0) return p.mayFindMore(); + return p.anyFresh(); + } + /// Paints from `one` and `twos`, and returns the candidates no stale /// paint reached, in the order found. fn paint(p: *Painter, one: Index, twos: []const Index, out: *std.ArrayList(Index), diag: ?*Diagnostics) Error!void { @@ -125,7 +169,7 @@ const Painter = struct { } var found: std.ArrayList(Index) = .empty; defer found.deinit(p.gpa); - while (p.queue.items.len != 0 and p.anyFresh()) { + while (p.goOn()) { const c = p.pop(); try p.store.load(c, diag); var f = p.flag(c) & (first_side | second_side | stale); -- knotmirror 0.0.1 From 39ee3b31dcf66598b61262a5e606e0b883ff40b9 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 2 Nov 2026 19:40:18 -0600 Subject: [PATCH 2/3] Walk orderings by generation number as git does with a commit-graph With a commit-graph, git walks --topo-order, --date-order and --author-date-order by generation number, in three interleaved walks (explore, indegree, topological). Model them step by step, down to how their queues order commits not yet read and leave a taken entry in place for the next push, and drop the refusals of --first-parent, --since, --until, --since-as-filter and exclusions with paths in those walks. With paths, --first-parent keeps a merge's other parents only when it is not TREESAME to its first, --sparse compares merges, rewriting rewrites every parent, and --full-history counts relevant parents as each commit is listed. --author-date-order is refused in the rare walk where git queues a commit whose author date it has not read. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 39 ++-- src/RevWalk.zig | 455 +++++++++++++++++++++++++++++++++++---- tools/grebe_tests.zig | 27 +-- tools/history.zig | 3 +- tools/rev_list.zig | 10 +- tools/simplify_tests.zig | 13 +- tools/walk_tests.zig | 71 +++++- 7 files changed, 531 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 435f1ec..9072b4d 100644 --- a/README.md +++ b/README.md @@ -166,8 +166,12 @@ do: `--boundary`, the sides of a symmetric difference and children — every rule found by comparing a model with git over thousands of generated histories, merges, octopus merges and criss-cross merges included — - and, with a commit-graph, the exact list git's ordered walks give for a - range, however the dates in it run; + and, with a commit-graph, git's walk of `--topo-order`, + `--date-order` and `--author-date-order` by generation number, its + three interleaved walks modelled step by step, down to the order its + queues give commits they have not yet read, so that `--first-parent`, + `--since`, `--until`, `--since-as-filter`, exclusions and paths list + what git lists however the dates run; - merge bases as `git merge-base` finds them, `--all` in git's order for criss-cross histories, `--octopus`, `--independent`, `--is-ancestor` and `--fork-point` from the reflog. @@ -616,14 +620,14 @@ deliberate or not yet matched: - `zig-git ls-files --debug` and `-f` are refused when `core.fsmonitor` is set, since git asks the monitor before it answers them, and long options cannot be abbreviated as git allows; -- with a commit-graph, git orders `--topo-order`, `--date-order` and - `--author-date-order` walks by generation number; with exclusions that - lists exactly the commits the range holds (except with `--children`, - `--right-only` or `--simplify-merges`), which is matched, but with - `--first-parent`, `--since`, `--until` or `--since-as-filter`, and with - exclusions under paths, it lists something not matched, so those - combinations are refused there; without them, and in the default - order, the walk is the same either way; +- with a commit-graph, an ordering with `--first-parent`, `--since`, + `--until` or `--since-as-filter` is refused together with `--children`, + `--left-only`, `--right-only`, `--ancestry-path`, `--simplify-merges` + or `--simplify-by-decoration`, where git orders the whole list rather + than walking by generation number and its list was not matched; and + walking by generation number, `--author-date-order` is refused when a + commit is queued before git has read its author date, which git then + orders by a rule not matched (about one walk in two thousand); - `--ancestry-path=` is refused, and so is `--children` with `--boundary` for a symmetric difference, whose boundary commits' children depend on which commits git's merge-base search happened to @@ -638,13 +642,14 @@ deliberate or not yet matched: - the `attr` pathspec magic is refused, where git reads attributes from the working tree, and so is the internal `prefix` magic; - with paths, `zig-git rev-list` refuses what was seen to list commits - git's rules did not account for: `--ancestry-path`; `--first-parent` - with `--parents`, `--children`, `--boundary`, an ordering or - `--remove-empty`; `--parents` with `--full-history` and `--reverse` or - `--skip` in a walk without exclusions or an ordering; `--remove-empty` - with `--boundary`, with `--reverse` and `--parents`, or in a repository - with changed-path Bloom filters, where git was seen to list other - commits with the filters than without them; and `--simplify-merges` + git's rules did not account for: `--ancestry-path`; outside a walk by + generation number, `--first-parent` with `--parents`, `--children`, + `--boundary`, an ordering or `--remove-empty`, and `--remove-empty` + with `--boundary` or with `--reverse` and `--parents`; `--parents` with + `--full-history` and `--reverse` or `--skip` in a walk without + exclusions or an ordering; `--remove-empty` in a repository with + changed-path Bloom filters, where git was seen to list other commits + with the filters than without them; and `--simplify-merges` with excluded commits, `--remove-empty`, `--sparse`, `--first-parent`, `--boundary`, `--show-pulls`, a parent count or a commit-graph; - `--follow` is refused across a merge (at the first one the walk diff --git a/src/RevWalk.zig b/src/RevWalk.zig index 20147ef..3b5331d 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -56,22 +56,57 @@ //! **`--max-count`** come after them, and **`--reverse`** after all of //! it, boundary commits included. //! -//! With a commit-graph, git orders `--topo-order`, `--date-order` and -//! `--author-date-order` walks by generation number. That lists the same -//! commits in the same order as the rules above for a walk without -//! exclusions. With them, the list is exact: the stop rule's tolerance -//! of clocks that run backwards is not needed, and nothing an excluded -//! commit reaches is listed, however its dates run, whether or not the -//! graph lists every commit and in one file or a split chain; the list -//! is then sorted as above. So the walk goes on until no queued commit -//! could reach a listed one by generation number. (The range of -//! hundreds of commits git was once seen to list nothing for is this: -//! everything in it was reachable from the excluded commit, through -//! dates far out of order.) With `--children`, `--right-only` or -//! `--simplify-merges`, though, git lists what the walk without a graph -//! lists. With `--first-parent`, `--since`, `--until` or -//! `--since-as-filter`, or with exclusions and paths, git lists what has -//! not been matched, and those combinations are refused. +//! With a commit-graph, git walks `--topo-order`, `--date-order` and +//! `--author-date-order` by generation number, unless `--children`, +//! `--left-only`, `--right-only`, `--ancestry-path`, `--simplify-merges` +//! or `--simplify-by-decoration` makes it work out the whole list first +//! (then the rules above apply, except that a walk with exclusions goes +//! on until no queued commit could reach a listed one by generation +//! number, the stop rule not being needed). The walk by generation +//! number was found the same way, counting alongside the output how many +//! commits each of its three walks took, as git's trace2 `topo_walk` +//! statistics report them: +//! +//! - **The explore walk** takes commits from a queue ordered by +//! generation number, then commit date, highest first, then first +//! queued. It reads each commit, excludes it if it is older than +//! `--since`, processes it as the walk without a graph does (excluding +//! an excluded commit's parents, simplifying it when limited to paths) +//! and queues every parent it has then that it has not queued before. +//! A commit not yet read is compared as one of infinite generation +//! number and no date, and is not moved when it is read while it +//! waits; taking the first entry leaves it in place until the queue is +//! next used, when a push takes its place. A commit the graph does not +//! list has an infinite generation number. +//! - **The indegree walk** takes commits from a queue ordered the same +//! way, explores down to each one's generation number, and counts it +//! as a child of its parents — of its first parent only, with +//! `--first-parent` — queueing each parent the first time. +//! - **The topological walk** starts once children have been counted down +//! to the lowest generation number of the starting points, from those +//! no other reached: newest first with `--topo-order` (the first given +//! first among equal dates), into a queue by commit or author date +//! otherwise. It takes a commit, processes it, and for each parent not +//! excluded by then — with `--first-parent`, the first such parent +//! only, a later one when the first is excluded — counts children down +//! to that parent's generation number and counts the commit out; a +//! parent left waiting for no child is queued, on a stack with +//! `--topo-order` and newest first, first in first out, by date +//! otherwise. git reads an author date as it explores a commit (or +//! reads a starting point); a commit queued by author date before that +//! is ordered otherwise, by a rule not matched, and is refused. A +//! commit older than `--since` is never queued. +//! - A commit taken is listed unless it is excluded or older than +//! `--since` by then, or the filters keep it out; `--skip`, +//! `--max-count`, `--boundary` and `--reverse` then work as above. +//! +//! So nothing an excluded commit reaches is listed, as its generation +//! numbers make sure the exclusion arrives first; but a commit older than +//! `--since` excludes what it reaches only once explored, and the walk +//! goes only as deep as it needs to. With paths, a commit is simplified +//! as it is explored, or earlier when rewriting reaches it, TREESAME +//! being decided once with the exclusions known then, and the relevant +//! parents that `--full-history` counts are counted as it is listed. //! //! **History simplification**, for a walk limited to paths, was found the //! same way, over histories whose commits change files, and agrees with @@ -84,7 +119,8 @@ //! - A merge's parents are **relevant** when they are not excluded, or are //! excluded starting points; a commit with one parent always counts it, //! and relevance is counted over every parent even with -//! `--first-parent`, which compares the first alone. By default the walk +//! `--first-parent`, which compares the first alone and keeps the +//! others unless the merge is TREESAME to it. By default the walk //! follows only the first relevant parent a merge is TREESAME to, and //! the merge is TREESAME. Otherwise it follows every parent, and the //! commit is TREESAME when it is TREESAME to every relevant parent — or, @@ -93,10 +129,12 @@ //! what it had when none is. //! - A commit is listed unless it is TREESAME, with `--sparse`, or with //! `--full-history` and rewritten parents when it has more than one -//! relevant parent. `--sparse` compares no commit with one parent. +//! relevant parent, counted as it is listed. `--sparse` compares no +//! commit with one parent, and compares merges as the rest does. //! - **Rewriting** (`--parents`, `--children`, `--simplify-merges`, never //! with `--sparse`) happens as each commit is listed, before `--skip` -//! counts it: each parent is followed, processing what it reaches, until +//! counts it: each parent, every one even with `--first-parent`, is +//! followed, processing what it reaches, until //! one is excluded, not TREESAME, a pull, or a merge — through which it //! goes on when the merge has exactly one relevant parent — and a root //! that is TREESAME is dropped. git lets go of a commit's parents once it @@ -171,6 +209,9 @@ bloom_key_counts: []u32 = &.{}, /// How often the changed-path Bloom filters were asked, and what they /// said. bloom_statistics: BloomStatistics = .{}, +/// What the generation walk counted, as git's trace2 `topo_walk` +/// statistics count it; null when the walk was not by generation number. +topo_statistics: ?TopoStatistics = null, /// Each commit's source, by commit number; `no_source` for none. sources: std.ArrayList(u32) = .empty, @@ -180,7 +221,8 @@ const seen_flag: u8 = 1; const excluded_flag: u8 = 2; const left_flag: u8 = 4; const right_flag: u8 = 8; -const listed_flag: u8 = 16; +/// Queued for the generation walk's exploring. +const explored_flag: u8 = 16; const shown_flag: u8 = 32; /// An excluded starting point, which git counts as a relevant parent. const bottom_flag: u8 = 64; @@ -301,6 +343,13 @@ pub const BloomStatistics = struct { false_positive: u64 = 0, }; +/// How many commits each of the generation walk's three walks took. +pub const TopoStatistics = struct { + explored: u64, + counted: u64, + walked: u64, +}; + const SimState = struct { simplified: bool = false, treesame: bool = false, @@ -351,9 +400,13 @@ pub const Entry = struct { /// What walking can fail with: reading a commit failed, and when a parent /// could not be read, `failed_child` names the commit that named it; or /// `UnsupportedGraphOrdering`: an ordering with `--first-parent`, -/// `--since`, `--until`, `--since-as-filter`, or exclusions and paths, -/// in a repository with a commit-graph, where git walks by generation -/// number and lists what has not been matched. +/// `--since`, `--until` or `--since-as-filter` together with +/// `--children`, `--left-only`, `--right-only`, `--ancestry-path`, +/// `--simplify-merges` or `--simplify-by-decoration`, in a repository with a +/// commit-graph, where git's list was not matched; or +/// `UnsupportedAuthorDateOrdering`: `--author-date-order` walked by +/// generation number, when a commit is queued before git has read its +/// author date, which git then orders as if it had none, not matched. /// /// With paths, comparing trees can fail with `TreeDiff.Error`; and /// `UnsupportedParentRewrite` is rewritten parents with paths and @@ -361,7 +414,7 @@ pub const Entry = struct { /// a walk without exclusions or an ordering, where git rewrites some /// parents in ways not matched and, with `--first-parent`, stops walking /// part-way. -pub const Error = Commits.Error || TreeDiff.Error || error{ UnsupportedGraphOrdering, UnsupportedParentRewrite, FilterFailed }; +pub const Error = Commits.Error || TreeDiff.Error || error{ UnsupportedGraphOrdering, UnsupportedAuthorDateOrdering, UnsupportedParentRewrite, FilterFailed }; const Queued = struct { time: i64, sequence: u64, commit: Index }; @@ -742,6 +795,9 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { var same_bits: std.ArrayList(Index) = .empty; defer same_bits.deinit(w.gpa); var compared = false; + // Whether the commit was found TREESAME to the one parent it follows, + // which then is its only parent. + var followed_one = false; decide: { if (excluded_commit) { try chosen.appendSlice(w.gpa, parents.items); @@ -756,7 +812,7 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { // Relevance is counted over every parent, even with --first-parent, // where only the first is compared. const single = real.len == 1; - if (s.sparse and parents.items.len == 1) { + if (s.sparse and real.len == 1 and parents.items.len == 1) { try chosen.appendSlice(w.gpa, parents.items); break :decide; } @@ -780,6 +836,7 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { if (s.show_pulls and i > 0 and first_same == false) pull = true; relevant_list.clearRetainingCapacity(); try relevant_list.append(w.gpa, p); + followed_one = true; break :decide; } if (d and s.remove_empty and try w.isEmpty(p, diag) and !try w.isEmpty(c, diag)) { @@ -803,8 +860,9 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { // relevant ones, is TREESAME to all it compared: none. treesame = if (has_relevant) all_relevant_same else all_same; } - // With --first-parent the other parents stay as they are. - if (w.options.first_parent and real.len > 1 and !excluded_commit and !(try w.simState(c)).cleared) { + // With --first-parent the other parents stay as they are, unless the + // commit is TREESAME to its first. + if (w.options.first_parent and real.len > 1 and !excluded_commit and !followed_one and !(try w.simState(c)).cleared) { try chosen.appendSlice(w.gpa, real[1..]); } const parents_span = try w.storeSpan(chosen.items); @@ -849,6 +907,18 @@ fn reconsider(w: *RevWalk, list: []const Index) Allocator.Error!void { } } +/// How many of a commit's parents are relevant now; a single parent +/// always is. +fn relevantNow(w: *const RevWalk, c: Index) usize { + const real = w.store.parents(c); + if (real.len == 1) return 1; + var n: usize = 0; + for (real) |p| { + if (w.relevant(p)) n += 1; + } + return n; +} + /// Whether a commit the walk reached is listed, as far as simplification /// is concerned. fn shownBySimplification(w: *const RevWalk, c: Index) bool { @@ -858,13 +928,15 @@ fn shownBySimplification(w: *const RevWalk, c: Index) bool { if (s.simplify_merges and !s.sparse and !st.remains) return false; if (!st.treesame or st.pull) return true; if (s.sparse) return true; - if (w.rewriting() and s.full_history and st.relevant_count > 1) return true; + // Counted as the commit is listed: in a walk by generation number, + // the exclusions known by then. + if (w.rewriting() and s.full_history and w.relevantNow(c) > 1) return true; return false; } /// The parents of listed commit `c` rewritten to the nearest commits that -/// are listed or excluded, as `--parents` shows them; the parents beyond -/// the first stay as they are with `--first-parent`. Reading ahead may +/// are listed or excluded, as `--parents` shows them, every parent even +/// with `--first-parent`. Reading ahead may /// process commits before they come out of the queue, as git does. fn rewriteParents(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { if ((try w.simState(c)).rewritten) return; @@ -874,10 +946,11 @@ fn rewriteParents(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { if (s.simplify_merges) { try out.appendSlice(w.gpa, w.simplifiedParents(c)); } else { - const n = w.walkParents(c).len; + // Every parent is rewritten, even with `--first-parent`. + const n = w.simplifiedParents(c).len; var i: usize = 0; while (i < n) : (i += 1) { - var p = w.walkParents(c)[i]; + var p = w.simplifiedParents(c)[i]; while (true) { try w.loadParent(c, p, diag); try w.process(p, diag); @@ -901,10 +974,6 @@ fn rewriteParents(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { p = q[0]; } } - if (w.options.first_parent) { - const all = w.simplifiedParents(c); - if (all.len > 1) try out.appendSlice(w.gpa, all[1..]); - } } var kept: usize = 0; for (out.items, 0..) |p, i| { @@ -1495,17 +1564,321 @@ fn addBoundary(w: *RevWalk) Error!void { for (found.items) |c| try w.output.append(w.gpa, .{ .commit = c, .boundary = true }); } +// -- the generation walk ----------------------------------------------------------- + +/// What the generation walk keeps: the queues of its three walks and how +/// many children each commit is still waiting on. +const GenWalk = struct { + explore: GenQueue = .{}, + indegree_queue: GenQueue = .{}, + /// One more than the number of children each commit is waiting on, for + /// commits the indegree walk has reached; zero for the rest. + indegree: std.ArrayList(i32) = .empty, + /// How deep, by generation number, the indegree walk has gone. + depth: u64 = Commits.infinite_generation, + topo_stack: std.ArrayList(Index) = .empty, + topo_heap: std.ArrayList(Queued) = .empty, + /// With `--author-date-order`, the commits whose author dates git + /// has read: those explored, and the starting points. + authors_read: std.AutoHashMapUnmanaged(Index, void) = .empty, + sequence: u64 = 0, + /// How many commits each walk took, as git's trace2 `topo_walk` + /// statistics count them. + explored: u64 = 0, + counted: u64 = 0, + walked: u64 = 0, + + fn deinit(g: *GenWalk, gpa: Allocator) void { + g.explore.items.deinit(gpa); + g.indegree_queue.items.deinit(gpa); + g.indegree.deinit(gpa); + g.topo_stack.deinit(gpa); + g.topo_heap.deinit(gpa); + g.authors_read.deinit(gpa); + } +}; + +/// A queue of the explore or indegree walk: a binary heap, highest +/// generation number first, then newest commit date, then first in. A +/// commit's place is decided by what the store knows of it when two +/// entries are compared, and a commit not yet read has an infinite +/// generation number and no date; reading it while it waits changes what +/// it is compared with, not where it is. Taking the first entry leaves it +/// in place until the queue is next used, and a push then takes its place. +const GenQueue = struct { + items: std.ArrayList(GenEntry) = .empty, + taken: bool = false, +}; + +const GenEntry = struct { sequence: u64, commit: Index }; + +/// A commit's generation number as the generation walk sees it: infinite +/// before it is read, as for a commit the commit-graph does not list. +fn generationNow(w: *const RevWalk, c: Index) u64 { + if (!w.store.isLoaded(c)) return Commits.infinite_generation; + return w.store.generation(c); +} + +fn genBefore(w: *const RevWalk, q: *const GenQueue, a: GenEntry, b: GenEntry) bool { + _ = q; + const ga = w.generationNow(a.commit); + const gb = w.generationNow(b.commit); + if (ga != gb) return ga > gb; + const da: i64 = if (w.store.isLoaded(a.commit)) w.store.time(a.commit) else 0; + const db: i64 = if (w.store.isLoaded(b.commit)) w.store.time(b.commit) else 0; + if (da != db) return da > db; + return a.sequence < b.sequence; +} + +fn genSiftDown(w: *const RevWalk, q: *GenQueue, start: usize) void { + const items = q.items.items; + var i = start; + while (true) { + const l = 2 * i + 1; + if (l >= items.len) break; + var best = l; + if (l + 1 < items.len and w.genBefore(q, items[l + 1], items[l])) best = l + 1; + if (!w.genBefore(q, items[best], items[i])) break; + std.mem.swap(GenEntry, &items[i], &items[best]); + i = best; + } +} + +/// Removes the entry taken last, if it is still in place. +fn genSettle(w: *const RevWalk, q: *GenQueue) void { + if (!q.taken) return; + q.taken = false; + const last = q.items.pop().?; + if (q.items.items.len == 0) return; + q.items.items[0] = last; + w.genSiftDown(q, 0); +} + +fn genPush(w: *RevWalk, g: *GenWalk, q: *GenQueue, c: Index) Allocator.Error!void { + const e: GenEntry = .{ .sequence = g.sequence, .commit = c }; + g.sequence += 1; + if (q.taken) { + q.taken = false; + q.items.items[0] = e; + w.genSiftDown(q, 0); + return; + } + try q.items.append(w.gpa, e); + const items = q.items.items; + var i = items.len - 1; + while (i > 0) { + const parent = (i - 1) / 2; + if (!w.genBefore(q, items[i], items[parent])) break; + std.mem.swap(GenEntry, &items[i], &items[parent]); + i = parent; + } +} + +fn genTop(w: *const RevWalk, q: *GenQueue) ?Index { + w.genSettle(q); + if (q.items.items.len == 0) return null; + return q.items.items[0].commit; +} + +fn genTake(w: *const RevWalk, q: *GenQueue) ?Index { + const top = w.genTop(q) orelse return null; + q.taken = true; + return top; +} + +fn indegreeOf(w: *RevWalk, g: *GenWalk, c: Index) Allocator.Error!*i32 { + const n = w.store.count(); + if (g.indegree.items.len < n) try g.indegree.appendNTimes(w.gpa, 0, n - g.indegree.items.len); + return &g.indegree.items[c]; +} + +/// The parents a commit has now: all of its own when it is excluded or +/// the walk is not limited to paths, and what simplification left +/// otherwise, the parents past the first included with `--first-parent`. +fn currentParents(w: *const RevWalk, c: Index) []const Index { + if (!w.excluded(c) and w.pruning()) return w.simplifiedParents(c); + return w.store.parents(c); +} + +/// Explores, as git's explore walk does, every queued commit of +/// generation `depth` or more: reads it, excludes it if it is older than +/// `--since`, processes it and queues every parent it has now. +fn exploreTo(w: *RevWalk, g: *GenWalk, depth: u64, diag: ?*Diagnostics) Error!void { + while (w.genTop(&g.explore)) |top| { + if (w.generationNow(top) < depth) break; + const c = w.genTake(&g.explore).?; + g.explored += 1; + try w.store.load(c, diag); + if (w.options.order == .author_date) try g.authors_read.put(w.gpa, c, {}); + if (w.options.since) |s| if (w.store.time(c) < s) try w.set(c, excluded_flag); + try w.process(c, diag); + var i: usize = 0; + while (i < w.currentParents(c).len) : (i += 1) { + const p = w.currentParents(c)[i]; + if (w.flag(p) & explored_flag != 0) continue; + try w.set(p, explored_flag); + try w.genPush(g, &g.explore, p); + } + } +} + +/// Counts children, as git's indegree walk does, for every queued commit +/// of generation `depth` or more, exploring as deep as each first. +fn indegreesTo(w: *RevWalk, g: *GenWalk, depth: u64, diag: ?*Diagnostics) Error!void { + while (w.genTop(&g.indegree_queue)) |top| { + if (w.generationNow(top) < depth) break; + const c = w.genTake(&g.indegree_queue).?; + g.counted += 1; + try w.exploreTo(g, w.generationNow(c), diag); + var i: usize = 0; + while (true) : (i += 1) { + const ps = w.currentParents(c); + if (i >= ps.len or (w.options.first_parent and i > 0)) break; + const p = ps[i]; + try w.loadParent(c, p, diag); + const d = try w.indegreeOf(g, p); + if (d.* != 0) { + d.* += 1; + } else { + d.* = 2; + try w.genPush(g, &g.indegree_queue, p); + } + } + } + g.depth = depth; +} + +fn topoPush(w: *RevWalk, g: *GenWalk, c: Index) Error!void { + if (w.options.since) |s| if (w.store.time(c) < s) return; + if (w.options.order == .topo) return g.topo_stack.append(w.gpa, c); + // git reads a commit's author date as it explores it, and orders one + // it has not read as if it had none: an order not matched. + if (w.options.order == .author_date and !g.authors_read.contains(c)) return error.UnsupportedAuthorDateOrdering; + try heapPush(w.gpa, &g.topo_heap, .{ .time = try w.sortTime(c, w.options.order), .sequence = g.sequence, .commit = c }); + g.sequence += 1; +} + +fn topoPop(w: *RevWalk, g: *GenWalk) ?Index { + if (w.options.order == .topo) return g.topo_stack.pop(); + return heapPop(&g.topo_heap); +} + +/// What git does with a commit as it comes out of the topological walk: +/// processes it, and counts it out of the first parent not excluded, or +/// of every parent not excluded without `--first-parent`, queueing the +/// ones no other child is waiting for. That parent is the first one past +/// an excluded first parent, too, even with `--first-parent`, and the +/// indegree walk counted only first parents then. +fn expandTopo(w: *RevWalk, g: *GenWalk, c: Index, diag: ?*Diagnostics) Error!void { + try w.process(c, diag); + var i: usize = 0; + while (i < w.currentParents(c).len) : (i += 1) { + const p = w.currentParents(c)[i]; + if (w.excluded(p)) continue; + try w.loadParent(c, p, diag); + const gp = w.generationNow(p); + if (gp < g.depth) try w.indegreesTo(g, gp, diag); + const d = try w.indegreeOf(g, p); + d.* -= 1; + if (d.* == 1) try w.topoPush(g, p); + if (w.options.first_parent) break; + } +} + +/// Whether git walks by generation number, as it does with an ordering in +/// a repository with a commit-graph unless `--children`, `--left-only`, +/// `--right-only`, `--ancestry-path`, `--simplify-merges` or +/// `--simplify-by-decoration` needs the whole list first. +pub fn walksByGeneration(o: Options, has_commit_graph: bool) bool { + if (o.order == .default or !has_commit_graph) return false; + if (o.children or o.left_only or o.right_only or o.ancestry_path or o.ancestry_commits.len != 0) return false; + if (o.simplify) |s| if (s.simplify_merges or s.decorated != null) return false; + return true; +} + +fn usesGenerations(w: *const RevWalk) bool { + return walksByGeneration(w.options, w.store.graph.layers.len != 0); +} + +/// Lists, as git does with generation numbers, what the top of this file +/// describes. +fn generationWalk(w: *RevWalk, diag: ?*Diagnostics) Error!void { + var g: GenWalk = .{}; + defer g.deinit(w.gpa); + + var starts: std.ArrayList(Index) = .empty; + defer starts.deinit(w.gpa); + for (w.starts.items) |s| { + if (std.mem.findScalar(Index, starts.items, s.commit) == null) try starts.append(w.gpa, s.commit); + } + if (starts.items.len == 0) return; + var lowest: u64 = Commits.infinite_generation; + for (starts.items) |c| { + // git has read the starting points' author dates already, in + // reading the commits it was given. + if (w.options.order == .author_date) try g.authors_read.put(w.gpa, c, {}); + lowest = @min(lowest, w.generationNow(c)); + if (w.flag(c) & explored_flag == 0) { + try w.set(c, explored_flag); + try w.genPush(&g, &g.explore, c); + } + (try w.indegreeOf(&g, c)).* = 1; + try w.genPush(&g, &g.indegree_queue, c); + } + try w.indegreesTo(&g, lowest, diag); + var seeds: std.ArrayList(Index) = .empty; + defer seeds.deinit(w.gpa); + for (starts.items) |c| if ((try w.indegreeOf(&g, c)).* == 1) try seeds.append(w.gpa, c); + if (w.options.order == .topo) { + // Newest first out of the stack, the first given first among + // commits of one date. + const Newest = struct { + fn lessThan(store: *const Commits, a: Index, b: Index) bool { + return store.time(a) > store.time(b); + } + }; + std.sort.insertion(Index, seeds.items, @as(*const Commits, w.store), Newest.lessThan); + std.mem.reverse(Index, seeds.items); + } + for (seeds.items) |c| try w.topoPush(&g, c); + while (true) { + if (w.options.max_count) |m| if (w.shown >= m) break; + const c = w.topoPop(&g) orelse break; + g.walked += 1; + try w.expandTopo(&g, c, diag); + if (w.excluded(c)) continue; + if (w.options.since) |s| if (w.store.time(c) < s) continue; + if (!try w.keep(c)) continue; + if (w.rewriting()) try w.rewriteParents(c, diag); + switch (w.admit()) { + .skip => continue, + .stop => break, + .show => {}, + } + try w.printed(c); + try w.set(c, shown_flag); + try w.output.append(w.gpa, .{ .commit = c, .side = w.sideOf(c) }); + } + w.topo_statistics = .{ .explored = g.explored, .counted = g.counted, .walked = g.walked }; + if (w.options.boundary) try w.addBoundary(); + if (w.options.reverse) std.mem.reverse(Entry, w.output.items); +} + /// The next commit the walk lists, or null after the last. pub fn next(w: *RevWalk, diag: ?*Diagnostics) Error!?Entry { switch (w.state) { .new => { const o = w.options; + if (w.usesGenerations()) { + try w.setUp(diag); + try w.generationWalk(diag); + w.state = .listed; + return w.next(diag); + } if (o.order != .default and w.store.graph.layers.len != 0) { - // With exclusions and paths, git's list was not matched. - const excluding = for (w.starts.items) |st| { - if (st.excluded) break true; - } else false; - if (o.first_parent or o.since != null or o.until != null or o.since_filter != null or (excluding and w.pruning())) + // Where git orders by generation number without walking + // by it, with these, its list was not matched. + if (o.first_parent or o.since != null or o.until != null or o.since_filter != null) return error.UnsupportedGraphOrdering; } if (w.rewriting() and !w.isLimited()) { diff --git a/tools/grebe_tests.zig b/tools/grebe_tests.zig index 7063b93..59f8729 100644 --- a/tools/grebe_tests.zig +++ b/tools/grebe_tests.zig @@ -23,14 +23,6 @@ fn same(repo: *Repo, args: []const []const u8) !void { try tool.expectSameAsGitHow(repo, args, .{}); } -/// `zig-git ` refuses by name what git would answer. -fn refused(repo: *Repo, args: []const []const u8) !void { - const ours = try tool.runOurs(repo, args, .{}); - defer ours.deinit(repo.gpa); - try testing.expectEqual(128, ours.code); - try testing.expect(std.mem.find(u8, ours.stderr, "not supported") != null); -} - fn write(repo: *Repo, path: []const u8, contents: []const u8) !void { if (std.fs.path.dirname(path)) |d| try repo.tmp.dir.createDirPath(repo.io, d); try repo.tmp.dir.writeFile(repo.io, .{ .sub_path = path, .data = contents }); @@ -106,7 +98,7 @@ fn makeHistory(repo: *Repo) !void { } /// What grebe runs, over one repository. -fn everything(repo: *Repo, graph: bool) !void { +fn everything(repo: *Repo) !void { const gpa = repo.gpa; const ids_out = try repo.output(&.{ "rev-list", "--all" }); defer gpa.free(ids_out); @@ -122,9 +114,10 @@ fn everything(repo: *Repo, graph: bool) !void { const range = try std.fmt.allocPrint(gpa, "{s}..{s}", .{ ids.items[ids.items.len - 1], ids.items[0] }); defer gpa.free(range); const revs = [_][]const []const u8{ - &.{}, &.{"--all"}, &.{ "--", "a.txt" }, &.{ "--all", "--", "d" }, - &.{"--date-order"}, &.{"--author-date-order"}, &.{"side"}, &.{"main~2..main"}, - &.{range}, &.{"side...other"}, &.{ "^side", "--all" }, &.{"--first-parent"}, + &.{}, &.{"--all"}, &.{ "--", "a.txt" }, &.{ "--all", "--", "d" }, + &.{"--date-order"}, &.{"--author-date-order"}, &.{"side"}, &.{"main~2..main"}, + &.{range}, &.{"side...other"}, &.{ "^side", "--all" }, &.{"--first-parent"}, + &.{ range, "--", "a.txt" }, &.{ "--first-parent", "main~3..main" }, }; for (revs) |r| { args.clearRetainingCapacity(); @@ -135,9 +128,7 @@ fn everything(repo: *Repo, graph: bool) !void { } else false; if (!ordered) try args.append(gpa, "--topo-order"); try args.appendSlice(gpa, r); - // An ordering with --first-parent is refused with a commit-graph. - const first_parent = r.len != 0 and std.mem.eql(u8, r[0], "--first-parent"); - if (graph and first_parent) try refused(repo, args.items) else try same(repo, args.items); + try same(repo, args.items); } const finds = [_][]const []const u8{ &.{ "--fixed-strings", "--grep=change" }, @@ -241,12 +232,12 @@ test "grebe's questions are answered as git answers them" { var repo = try Repo.init(testing.allocator, testing.io); defer repo.deinit(); try makeHistory(&repo); - try everything(&repo, false); + try everything(&repo); try repo.git(&.{ "commit-graph", "write", "--reachable", "--changed-paths" }); - try everything(&repo, true); + try everything(&repo); // Detached, and then on a branch not yet born. try repo.git(&.{ "checkout", "-q", "--detach", "HEAD~2" }); - try everything(&repo, true); + try everything(&repo); try repo.git(&.{ "symbolic-ref", "HEAD", "refs/heads/unborn" }); for ([_][]const []const u8{ &.{ "-c", "log.showSignature=false", "log", "--parents", "--no-color", "--encoding=UTF-8", "-z", "--date=raw", "--decorate=full", log_format, "--topo-order" }, diff --git a/tools/history.zig b/tools/history.zig index c869ee7..f85a4d6 100644 --- a/tools/history.zig +++ b/tools/history.zig @@ -105,7 +105,8 @@ pub const History = struct { error.InvalidParentPosition => return tool.fatalMessage(stderr, "invalid parent position {s}", .{diag.detail()}), error.MissingGenerationOverflow => return tool.fatalMessage(stderr, "commit-graph requires overflow generation data but has none", .{}), error.GenerationOverflowTooSmall => return tool.fatalMessage(stderr, "commit-graph overflow generation data is too small", .{}), - error.UnsupportedGraphOrdering => return tool.fatalMessage(stderr, "zig-git: an ordering with --first-parent, --since, --until, --since-as-filter, or exclusions and paths, is not supported in a repository with a commit-graph", .{}), + error.UnsupportedGraphOrdering => return tool.fatalMessage(stderr, "zig-git: an ordering with --first-parent, --since, --until or --since-as-filter, and with --children, --left-only, --right-only, --ancestry-path, --simplify-merges or --simplify-by-decoration, is not supported in a repository with a commit-graph", .{}), + error.UnsupportedAuthorDateOrdering => return tool.fatalMessage(stderr, "zig-git: --author-date-order is not supported here in a repository with a commit-graph, where git orders a commit whose author date it has not read", .{}), error.UnsupportedParentRewrite => return tool.fatalMessage(stderr, "zig-git rev-list: --parents with paths and --first-parent, or --full-history with --reverse or --skip, is not supported without exclusions or an ordering", .{}), error.NotATree, error.TruncatedTree, error.MalformedMode, error.EmptyName => return tool.fatalMessage(stderr, "zig-git: cannot read a tree: {t}", .{err}), else => return tool.fatalMessage(stderr, "zig-git: cannot read a commit: {t}", .{err}), diff --git a/tools/rev_list.zig b/tools/rev_list.zig index e63ddfd..5984780 100644 --- a/tools/rev_list.zig +++ b/tools/rev_list.zig @@ -586,7 +586,11 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder null; if (refusal) |r| return tool.fatalMessage(stderr, "zig-git rev-list: --simplify-merges with paths and {s} is not supported", .{r}); } - if (options.first_parent) { + // Walked by generation number, git simplifies as the library + // does, and none of these is refused. + options.simplify = simplify; + const by_generation = RevWalk.walksByGeneration(options, h.graph.layers.len != 0); + if (options.first_parent and !by_generation) { const refusal: ?[]const u8 = if (out.parents) "--parents" else if (out.children) @@ -601,9 +605,9 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder null; if (refusal) |r| return tool.fatalMessage(stderr, "zig-git rev-list: --first-parent with paths and {s} is not supported", .{r}); } - if (simplify.remove_empty and options.boundary) + if (simplify.remove_empty and options.boundary and !by_generation) return tool.fatalMessage(stderr, "zig-git rev-list: --remove-empty with paths and --boundary is not supported", .{}); - if (simplify.remove_empty and options.reverse and out.parents) + if (simplify.remove_empty and options.reverse and out.parents and !by_generation) return tool.fatalMessage(stderr, "zig-git rev-list: --remove-empty with paths, --reverse and --parents is not supported", .{}); simplify.bloom = h.changed_paths; // git's --remove-empty lists other commits when its changed-path diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 2228486..bee00aa 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -193,6 +193,8 @@ const outputs = [_][]const []const u8{ &.{ "--left-right", "--count" }, &.{ "--max-parents=1", "--parents" }, &.{ "--timestamp", "--left-right" }, + &.{ "--topo-order", "--first-parent", "--parents" }, + &.{ "--date-order", "--first-parent", "--boundary" }, }; /// Whether `zig-git` refuses the combination, as the module refuses it. @@ -211,11 +213,16 @@ fn isRefused(mode: []const []const u8, output: []const []const u8, range: []cons if (excluding or has(output, "--first-parent") or has(output, "--boundary") or has(output, "--max-parents=1")) return true; if (graph) return true; } - if (has(output, "--first-parent") and (parents or has(output, "--children") or has(output, "--boundary") or ordered or has(mode, "--remove-empty"))) return true; - if (has(mode, "--remove-empty") and (has(output, "--boundary") or (has(output, "--reverse") and parents))) return true; + // Walked by generation number, nothing below but the filters with + // --remove-empty is refused. + const by_generation = graph and ordered and !has(output, "--children") and !has(mode, "--simplify-merges"); + if (!by_generation) { + if (has(output, "--first-parent") and (parents or has(output, "--children") or has(output, "--boundary") or ordered or has(mode, "--remove-empty"))) return true; + if (has(mode, "--remove-empty") and (has(output, "--boundary") or (has(output, "--reverse") and parents))) return true; + } const limited = excluding or ordered or has(output, "--children"); if (has(mode, "--full-history") and !has(mode, "--sparse") and parents and !limited and (has(output, "--reverse") or has(output, "--skip=2"))) return true; - if (graph and ordered and excluding) return true; + if (graph and ordered and excluding and !by_generation) return true; if (bloom and has(mode, "--remove-empty")) return true; return false; } diff --git a/tools/walk_tests.zig b/tools/walk_tests.zig index 5ea6ac9..1de3f53 100644 --- a/tools/walk_tests.zig +++ b/tools/walk_tests.zig @@ -28,6 +28,23 @@ fn sameOutput(repo: *Repo, args: []const []const u8) !void { try tool.expectSameAsGitHow(repo, args, .{ .compare = .output }); } +/// The same as git, or, walking `--author-date-order` by generation +/// number, refused where git orders by an author date it has not read. +fn sameOrAuthorDateRefused(repo: *Repo, args: []const []const u8) !void { + const by_author_date = for (args) |a| { + if (std.mem.eql(u8, a, "--author-date-order")) break true; + } else false; + if (by_author_date) { + const ours = try tool.runOurs(repo, args, .{}); + defer ours.deinit(repo.gpa); + if (ours.code == 128 and std.mem.find(u8, ours.stderr, "--author-date-order is not supported") != null) { + try testing.expectEqualStrings("", ours.stdout); + return; + } + } + try same(repo, args); +} + /// `zig-git ` refuses by name what git would answer. fn refused(repo: *Repo, args: []const []const u8) !void { const ours = try tool.runOurs(repo, args, .{}); @@ -183,24 +200,29 @@ fn walkEverything(repo: *Repo, graph: bool) !void { try args.appendSlice(gpa, order); try args.appendSlice(gpa, extra); try args.appendSlice(gpa, range); - // With a commit-graph git orders these by generation number, - // which is matched for exclusions and refused for the rest. - const graph_order = graph and o != 0 and for (extra) |x| { + // With a commit-graph git walks these by generation number, as + // the library does, except where --children, --left-only or + // --right-only make it order the whole list instead: there, these + // are refused. + const dated = for (extra) |x| { if (std.mem.eql(u8, x, "--first-parent") or std.mem.startsWith(u8, x, "--max-age") or std.mem.startsWith(u8, x, "--min-age") or std.mem.startsWith(u8, x, "--since-as-filter")) break true; } else false; var has_children = false; var has_boundary = false; + var whole_list = false; for (extra) |x| { if (std.mem.eql(u8, x, "--children")) has_children = true; if (std.mem.eql(u8, x, "--boundary")) has_boundary = true; + if (std.mem.eql(u8, x, "--children") or std.mem.eql(u8, x, "--left-only") or std.mem.eql(u8, x, "--right-only")) whole_list = true; } + const graph_order = graph and o != 0 and dated and whole_list; const children_boundary_symmetric = has_children and has_boundary and std.mem.find(u8, range[0], "...") != null; if (graph_order or children_boundary_symmetric) { try refused(repo, args.items); continue; } - try same(repo, args.items); + if (graph) try sameOrAuthorDateRefused(repo, args.items) else try same(repo, args.items); }; } @@ -231,6 +253,47 @@ test "rev-list walks a history with a split commit-graph chain as git does" { try walkEverything(&repo, true); } +/// What the walk by generation number was found by: orderings with +/// --first-parent, the dates and exclusions, over clocks that run +/// backwards, octopus merges and commits past the end of the graph. +fn walkByGeneration(repo: *Repo) !void { + const gpa = repo.gpa; + var args: std.ArrayList([]const u8) = .empty; + defer args.deinit(gpa); + const limits = [_][]const []const u8{ + &.{"--first-parent"}, + &.{ "--first-parent", "--parents" }, + &.{ "--first-parent", "--boundary" }, + &.{ "--first-parent", "-n", "9" }, + &.{"--max-age=1790000300"}, + &.{ "--max-age=1790000300", "--first-parent" }, + &.{ "--min-age=1790000300", "--first-parent" }, + &.{ "--since-as-filter=@1790000300", "--first-parent" }, + &.{ "--first-parent", "--left-right" }, + }; + for (ranges[0..10]) |range| for (orders[1..]) |order| for (limits) |limit| { + args.clearRetainingCapacity(); + try args.append(gpa, "rev-list"); + try args.appendSlice(gpa, order); + try args.appendSlice(gpa, limit); + try args.appendSlice(gpa, range); + try sameOrAuthorDateRefused(repo, args.items); + }; +} + +test "rev-list walks by generation number as git does, with or without generation data" { + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try makeHistory(&repo, 8, 200); + try repo.git(&.{ "-c", "commitGraph.generationVersion=1", "commit-graph", "write", "--reachable" }); + try walkByGeneration(&repo); + try repo.git(&.{ "commit-graph", "write", "--reachable" }); + try walkByGeneration(&repo); + // Commits the graph does not list, whose generation is infinite. + for (0..15) |_| try repo.commit("past the graph"); + try walkByGeneration(&repo); +} + test "rev-list matches git in thousands of commits and many seeds" { var repo = try Repo.init(testing.allocator, testing.io); defer repo.deinit(); -- knotmirror 0.0.1 From e47de82ed456327351fc51a093f6ceea822334d1 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 2 Nov 2026 20:09:40 -0600 Subject: [PATCH 3/3] Exclude through the parents simplification left, as git holds them git replaces a simplified commit's parents with those simplification kept, so an exclusion reaching such a commit later passes only to them, and a commit the rest lead to can be listed though excluded commits reach it. This shows with paths whether or not the walk is by generation number. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- src/RevWalk.zig | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/RevWalk.zig b/src/RevWalk.zig index 3b5331d..b2f78cc 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -115,7 +115,9 @@ //! - A commit is simplified as it is processed, which is once, when it //! comes out of the queue or earlier when rewriting reaches it. A root //! is TREESAME when the pathspec matches nothing in its tree; an -//! excluded commit is never simplified. +//! excluded commit is never simplified. A commit simplified before it +//! is excluded passes the exclusion on only to the parents +//! simplification left it, as git has replaced the rest. //! - A merge's parents are **relevant** when they are not excluded, or are //! excluded starting points; a commit with one parent always counts it, //! and relevance is counted over every parent even with @@ -567,7 +569,8 @@ fn excludeParents(w: *RevWalk, c: Index) Allocator.Error!void { defer stack.deinit(w.gpa); try stack.append(w.gpa, c); while (stack.pop()) |x| { - for (w.store.parents(x)) |p| { + // The parents simplification left, as git has replaced them. + for (w.heldParents(x)) |p| { if (w.excluded(p)) continue; try w.set(p, excluded_flag); if (w.store.isLoaded(p)) try stack.append(w.gpa, p); @@ -575,6 +578,17 @@ fn excludeParents(w: *RevWalk, c: Index) Allocator.Error!void { } } +/// A commit's parents as git holds them: what simplification left once it +/// has been simplified (the parents past the first included with +/// `--first-parent` unless it is TREESAME to the first), all of them +/// otherwise. Excluding goes through these, so that a commit simplified +/// before it was excluded passes the exclusion only to the parents it +/// kept. +fn heldParents(w: *const RevWalk, c: Index) []const Index { + if (w.pruning()) return w.simplifiedParents(c); + return w.store.parents(c); +} + /// Takes a commit out of the queue's hands: its parents read, excluded if /// it is, given its sides, simplified, and queued. A commit is processed /// once, which may be before it comes out of the queue when its parents @@ -1692,14 +1706,6 @@ fn indegreeOf(w: *RevWalk, g: *GenWalk, c: Index) Allocator.Error!*i32 { return &g.indegree.items[c]; } -/// The parents a commit has now: all of its own when it is excluded or -/// the walk is not limited to paths, and what simplification left -/// otherwise, the parents past the first included with `--first-parent`. -fn currentParents(w: *const RevWalk, c: Index) []const Index { - if (!w.excluded(c) and w.pruning()) return w.simplifiedParents(c); - return w.store.parents(c); -} - /// Explores, as git's explore walk does, every queued commit of /// generation `depth` or more: reads it, excludes it if it is older than /// `--since`, processes it and queues every parent it has now. @@ -1713,8 +1719,8 @@ fn exploreTo(w: *RevWalk, g: *GenWalk, depth: u64, diag: ?*Diagnostics) Error!vo if (w.options.since) |s| if (w.store.time(c) < s) try w.set(c, excluded_flag); try w.process(c, diag); var i: usize = 0; - while (i < w.currentParents(c).len) : (i += 1) { - const p = w.currentParents(c)[i]; + while (i < w.heldParents(c).len) : (i += 1) { + const p = w.heldParents(c)[i]; if (w.flag(p) & explored_flag != 0) continue; try w.set(p, explored_flag); try w.genPush(g, &g.explore, p); @@ -1732,7 +1738,7 @@ fn indegreesTo(w: *RevWalk, g: *GenWalk, depth: u64, diag: ?*Diagnostics) Error! try w.exploreTo(g, w.generationNow(c), diag); var i: usize = 0; while (true) : (i += 1) { - const ps = w.currentParents(c); + const ps = w.heldParents(c); if (i >= ps.len or (w.options.first_parent and i > 0)) break; const p = ps[i]; try w.loadParent(c, p, diag); @@ -1772,8 +1778,8 @@ fn topoPop(w: *RevWalk, g: *GenWalk) ?Index { fn expandTopo(w: *RevWalk, g: *GenWalk, c: Index, diag: ?*Diagnostics) Error!void { try w.process(c, diag); var i: usize = 0; - while (i < w.currentParents(c).len) : (i += 1) { - const p = w.currentParents(c)[i]; + while (i < w.heldParents(c).len) : (i += 1) { + const p = w.heldParents(c)[i]; if (w.excluded(p)) continue; try w.loadParent(c, p, diag); const gp = w.generationNow(p);