From 675909e6d07b2e76be0ebc859be8dc5c123beebb Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 18:05:42 -0600 Subject: [PATCH 1/3] revisions: take a message search's ties and reads as git does Among commits of one committer date, `:/` and `^{/}` now answer the starting points first, the one named last first, and then the commits queued while searching, first queued first; and the search reads the parents of the commit it answers, as git does, so that an exclusion of that commit reaches through them before the walk starts. Both were found by experiment, in histories with shared dates and in `--first-parent --sparse` walks whose output shows which commits git had read. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 12 +++--- src/Resolver.zig | 46 ++++++++++++-------- tools/simplify_tests.zig | 90 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a64b1ad..4ad5f7c 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,11 @@ do: then resolved — object IDs and their abbreviations, ref names by git's ordered rules with its warnings for names that are more than one thing, `git describe` output, `@`, `^`, `~`, every `^{...}`, - `:/` and `^{/}` with POSIX extended regular expressions, + `:/` and `^{/}` with POSIX extended regular expressions + (searched newest first, among commits of one date the starting points + first, the one named last first, then the commits queued, first + queued first; the parents of the commit found read, as git reads + them, so that excluding it reaches past them), `:` with `./` and `../`, `:[:]` in the index, reflog entries by count and by date (the current time passed in), `@{-}`, and `@{upstream}` and `@{push}` from branches, remotes and @@ -679,7 +683,8 @@ 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, nor resolving the revisions — `HEAD~5` reads `HEAD` to `HEAD~4`, and a -message search the commits it looks through — read) +message search the commits it looks through and the parents of the one +it finds — 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. @@ -954,9 +959,6 @@ deliberate or not yet matched: `remotes/` or `branches/`, which git still reads; - `@{-}` reads only `HEAD`'s `checkout: moving from` entries, and `@{push}` takes no account of `remote..mirror`; -- among commits of one committer date, a message search answers the one - put in its queue last, as git was seen to; git's order may differ in - histories where that rule was not tested; - `zig-git cat-file` finds nothing by a range, as git does, rather than refusing it; - `zig-git rev-parse --disambiguate` with a single digit is refused: git diff --git a/src/Resolver.zig b/src/Resolver.zig index e481054..2100fe4 100644 --- a/src/Resolver.zig +++ b/src/Resolver.zig @@ -27,8 +27,9 @@ //! at another stage, or under the caller's directory instead. //! - **Message searches** walk from their starting points newest commit //! first, by committer date, and answer the first commit whose message -//! matches; among commits of the same date the one put in the queue -//! last comes out first. +//! matches, having read its parents as git has. Among commits of the same date the starting points come +//! out first, the one named last first, and then the commits queued +//! while walking, in the order they were queued. //! //! Warnings, and the errors git reports without stopping, are kept in //! `messages` for the caller to show; an error that stops resolution has @@ -1074,11 +1075,13 @@ fn searchAll(r: *Resolver, s: revision.Search, diag: ?*Diagnostics) Error!?Id { const Queued = struct { id: Id, date: i64, - order: u64, + /// Negative for a starting point, counting down as they are named; + /// positive for a parent, counting up as they are queued. + order: i64, fn before(a: Queued, b: Queued) bool { if (a.date != b.date) return a.date > b.date; - return a.order > b.order; + return a.order < b.order; } }; @@ -1097,15 +1100,17 @@ fn search(r: *Resolver, starts: []const Id, s: revision.Search, diag: ?*Diagnost defer heap.deinit(r.gpa); var seen: std.AutoHashMapUnmanaged([object.max_byte_length]u8, void) = .empty; defer seen.deinit(r.gpa); - var order: u64 = 0; + var order: i64 = 0; // Every starting point goes in, the same commit twice if two refs // name it, so that the one named last comes out first; a commit is // looked at once, and a parent goes in only if nothing has put it in - // already. + // already, behind everything of its date already queued. for (starts) |id| { const c = try r.peelToCommitQuietly(id) orelse continue; - try r.enqueue(&heap, &seen, c, &order, true); + order -= 1; + try r.enqueue(&heap, &seen, c, order, true); } + order = 0; var looked: std.AutoHashMapUnmanaged([object.max_byte_length]u8, void) = .empty; defer looked.deinit(r.gpa); while (heap.items.len > 0) { @@ -1114,14 +1119,22 @@ fn search(r: *Resolver, starts: []const Id, s: revision.Search, diag: ?*Diagnost var o = try r.readObject(top.id) orelse continue; defer o.deinit(r.gpa); const message = if (std.mem.find(u8, o.data, "\n\n")) |b| o.data[b + 2 ..] else ""; - if (try pattern.matches(r.gpa, message) != s.negate) return top.id; + // The parents are queued, and so read, before the message is + // looked at: git has read the parents of the commit it answers, + // which an exclusion of it then reaches through. if ((try r.grafts(diag)).lookup(top.id)) |g| { - for (g.parents) |p| try r.enqueue(&heap, &seen, p, &order, false); - continue; - } - const c = Commit.parse(top.id.object_format, o.data) catch continue; - var it = c.parents(); - while (it.next()) |p| try r.enqueue(&heap, &seen, p, &order, false); + for (g.parents) |p| { + order += 1; + try r.enqueue(&heap, &seen, p, order, false); + } + } else if (Commit.parse(top.id.object_format, o.data)) |c| { + var it = c.parents(); + while (it.next()) |p| { + order += 1; + try r.enqueue(&heap, &seen, p, order, false); + } + } else |_| {} + if (try pattern.matches(r.gpa, message) != s.negate) return top.id; } return null; } @@ -1133,7 +1146,7 @@ fn peelToCommitQuietly(r: *Resolver, id: Id) Error!?Id { return r.commitOf(id); } -fn enqueue(r: *Resolver, heap: *std.ArrayList(Queued), seen: *std.AutoHashMapUnmanaged([object.max_byte_length]u8, void), id: Id, order: *u64, again: bool) Error!void { +fn enqueue(r: *Resolver, heap: *std.ArrayList(Queued), seen: *std.AutoHashMapUnmanaged([object.max_byte_length]u8, void), id: Id, order: i64, again: bool) Error!void { const gop = try seen.getOrPut(r.gpa, id.bytes); if (gop.found_existing and !again) return; var o = try r.readObject(id) orelse return; @@ -1142,8 +1155,7 @@ fn enqueue(r: *Resolver, heap: *std.ArrayList(Queued), seen: *std.AutoHashMapUnm 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.* }); + try heap.append(r.gpa, .{ .id = id, .date = when, .order = order }); var i = heap.items.len - 1; while (i > 0) { const parent = (i - 1) / 2; diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 8671476..9db82c0 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -424,6 +424,48 @@ fn importCommits(repo: *Repo, commits: []const Made) !void { try repo.git(&.{ "symbolic-ref", "HEAD", "refs/heads/main" }); } +/// A commit for `importDated`: its files as `importCommits` takes them, +/// its parents by position, its commit date as seconds after +/// 1 790 000 000, its message (`c` by default) and the branches made +/// to point at it. +pub const Dated = struct { + files: []const u8 = "", + parents: []const usize = &.{}, + time: u64, + message: ?[]const u8 = null, + branches: []const []const u8 = &.{}, +}; + +/// Makes `commits` with `git fast-import`, dated as they say, the last on +/// `main`; with `tags`, commit `i` is tagged `c`. +pub fn importDated(repo: *Repo, commits: []const Dated, tags: bool) !void { + const gpa = repo.gpa; + var stream: Io.Writer.Allocating = .init(gpa); + defer stream.deinit(); + const w = &stream.writer; + for (commits, 1..) |c, mark| { + var message_buffer: [24]u8 = undefined; + const message = c.message orelse try std.fmt.bufPrint(&message_buffer, "c{d}", .{mark - 1}); + if (c.parents.len == 0 and mark != 1) try w.writeAll("reset refs/heads/main\n\n"); + try w.print("commit refs/heads/main\nmark :{d}\ncommitter C {d} +0000\ndata {d}\n{s}\n", .{ mark, 1_790_000_000 + c.time, message.len + 1, message }); + for (c.parents, 0..) |p, i| try w.print("{s} :{d}\n", .{ if (i == 0) "from" else "merge", p + 1 }); + try w.writeAll("deleteall\n"); + var files = std.mem.tokenizeScalar(u8, c.files, ' '); + while (files.next()) |f| { + const eq = std.mem.findScalar(u8, f, '=').?; + try w.print("M 100644 inline {s}\ndata {d}\n{s}\n", .{ f[0..eq], f.len - eq, f[eq + 1 ..] }); + } + try w.writeAll("\n"); + if (tags) try w.print("reset refs/tags/c{d}\nfrom :{d}\n\n", .{ mark - 1, mark }); + for (c.branches) |b| try w.print("reset refs/heads/{s}\nfrom :{d}\n\n", .{ b, mark }); + } + const imported = try repo.runIn(".", &.{ "fast-import", "--quiet" }, stream.written()); + defer imported.deinit(gpa); + if (imported.code != 0) std.debug.print("{s}", .{imported.stderr}); + try testing.expectEqual(0, imported.code); + try repo.git(&.{ "symbolic-ref", "HEAD", "refs/heads/main" }); +} + test "history with paths under --first-parent, as git does" { // A later parent the walk never queued stops rewriting, and git counts // it as processed: reached later, it is listed and nothing past it @@ -1179,3 +1221,51 @@ test "--simplify-by-decoration with paths does work in proportion to the history try same(&repo, &.{ "rev-list", "--simplify-by-decoration", "--parents", "release", "--", "." }); try same(&repo, &.{ "rev-list", "--simplify-by-decoration", "release", "--", "t1" }); } + +test "message searches take commits of one date as git does, and read the parents of the one they answer" { + // Among commits of one date, the starting points come out first, the + // one named last first, then the parents queued, first queued first. + { + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 100, .message = "m x", .branches = &.{"a"} }, + .{ .files = "a=1", .time = 100, .message = "m y" }, + .{ .files = "a=1", .parents = &.{1}, .time = 200, .message = "z", .branches = &.{"b"} }, + .{ .files = "a=1", .time = 100, .message = "m 1", .branches = &.{"y"} }, + .{ .files = "a=1", .time = 100, .message = "m 2" }, + .{ .files = "a=1", .parents = &.{ 3, 4 }, .time = 300, .message = "p" }, + }, false); + for ([_][]const u8{ ":/m", "main^{/m}", "b^{/m}", "a^{/m}", ":/m 1", ":/z", ":/!-z" }) |rev| { + try same(&repo, &.{ "rev-parse", rev }); + } + try repo.git(&.{ "update-ref", "refs/heads/a", "y" }); + try same(&repo, &.{ "rev-parse", ":/m" }); + } + // git has read the parents of the commit a search answers, so that + // excluding it excludes their parents before the walk reaches the + // merge whose first parent is one of them. + { + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 10 }, + .{ .files = "a=1", .parents = &.{0}, .time = 20 }, + .{ .files = "a=1", .parents = &.{1}, .time = 30, .message = "find" }, + .{ .files = "a=1", .parents = &.{2}, .time = 40 }, + .{ .files = "a=1", .parents = &.{ 0, 3 }, .time = 100 }, + }, false); + for ([_][]const u8{ "^main^{/find}", "^:/find", "^main^2~1" }) |exclude| for ([_][]const []const u8{ + &.{ "--first-parent", "--sparse" }, + &.{"--first-parent"}, + &.{ "--first-parent", "--full-history" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "rev-list", "--boundary", "--parents" }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "main", exclude, "--", "d" }); + try same(&repo, a.items); + }; + } +} -- knotmirror 0.0.1 From 21a5f47565696f1d2da10f824dde349c315793da Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 18:05:51 -0600 Subject: [PATCH 2/3] merge-base: stop painting where git stops, past a repeated candidate A commit painted from both sides can be queued a second time before it comes out, as a symmetric difference's side that is its own merge base is. git does not count that second entry as one still to paint: it stops once only stale commits and such entries are left, having read fewer commits, so that the exclusion of the merge base reaches less far and `rev-list A...B` lists commits dated before it arrives. Found by removing commit objects from a copy of the repository and seeing which ones `git merge-base` still needed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 3 ++- src/merge_base.zig | 16 +++++++++++----- tools/walk_tests.zig | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4ad5f7c..69b7f8e 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,8 @@ do: 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. + `--fork-point` from the reflog, reading the commits git reads to find + them, which a symmetric difference's exclusions then reach through. - pathspecs as gitglossary(7) describes them and `git ls-files` and `git log` match them: literal paths and directories, wildcards matched against whole paths, the `top`, `literal`, `glob`, `icase` and diff --git a/src/merge_base.zig b/src/merge_base.zig index 96dfdcb..cef809e 100644 --- a/src/merge_base.zig +++ b/src/merge_base.zig @@ -12,7 +12,9 @@ //! out among equals; with a commit-graph, by generation number first, as //! git does, which changes which commits are read but not the answer), each marked with the side it came from; a commit //! painted from both sides is a candidate, and what it reaches is painted -//! stale; the walk stops when only stale commits are queued. The +//! stale; the walk stops when only stale commits are queued (or a +//! candidate queued a second time before it came out, which git does +//! not count, as what `rev-list A...B` then lists showed). The //! candidates no stale paint reached, less any that another reaches, are //! the merge bases, newest first by commit date and, among equals, in the //! order they were found. The order, in criss-cross histories and with @@ -23,9 +25,10 @@ //! 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. -//! 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 +//! Without a commit-graph it reads each commit as it queues it, for its +//! date (git was seen to need a commit it queued and never took out), +//! and stops as above. With +//! one, git reads each commit as it queues it too, 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 @@ -117,8 +120,11 @@ const Painter = struct { return top; } + /// Whether a commit queued is neither stale nor already a candidate + /// (queued a second time before it came out, as git does not count + /// it). fn anyFresh(p: *Painter) bool { - for (p.queue.items) |q| if (p.flag(q.commit) & stale == 0) return true; + for (p.queue.items) |q| if (p.flag(q.commit) & (stale | result) == 0) return true; return false; } diff --git a/tools/walk_tests.zig b/tools/walk_tests.zig index 7201d34..16357a0 100644 --- a/tools/walk_tests.zig +++ b/tools/walk_tests.zig @@ -16,6 +16,7 @@ const Io = std.Io; const git = @import("git"); const tool = @import("zig_git.zig"); +const simplify_tests = @import("simplify_tests.zig"); const Repo = tool.Repo; const testing = std.testing; @@ -547,3 +548,33 @@ fn countOf(bytes: []const u8, h: usize) usize { } return 0; } + +test "a symmetric difference's search for merge bases stops as git's does" { + // c8, the right side, is its own merge base, queued a second time from + // c10 before it comes out: git stops painting once only that and + // stale commits are left, having read c7 and no further, so that the + // exclusion of c8 does not reach c0 through c6 to c1, and the walk + // lists c0 before the exclusion arrives. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try simplify_tests.importDated(&repo, &.{ + .{ .files = "a=1", .time = 90 }, + .{ .files = "a=44", .parents = &.{0}, .time = 44 }, + .{ .files = "a=45", .parents = &.{1}, .time = 45 }, + .{ .files = "a=46", .parents = &.{2}, .time = 46 }, + .{ .files = "a=47", .parents = &.{3}, .time = 47 }, + .{ .files = "a=48", .parents = &.{4}, .time = 48 }, + .{ .files = "a=49", .parents = &.{5}, .time = 49 }, + .{ .files = "a=50", .parents = &.{6}, .time = 50 }, + .{ .files = "a=2", .parents = &.{7}, .time = 10, .branches = &.{"l"} }, + .{ .files = "a=3", .parents = &.{0}, .time = 15 }, + .{ .files = "a=4", .parents = &.{ 9, 8 }, .time = 20 }, + }, true); + for ([_][]const []const u8{ + &.{ "rev-list", "--first-parent", "main...l" }, + &.{ "rev-list", "main...l" }, + &.{ "rev-list", "--left-right", "--boundary", "main...l" }, + &.{ "rev-list", "--parents", "l...main", "--", "a" }, + &.{ "merge-base", "--all", "main", "l" }, + }) |a| try same(&repo, a); +} -- knotmirror 0.0.1 From fa6711bd591103474d0ab3fcdc2cf3e2a3414b9c Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 10 Nov 2026 18:05:59 -0600 Subject: [PATCH 3/3] log, rev-list: walk --first-parent merges, rewriting and the boundary as git does What git was seen to do, each found by experiment and given a test: - Under --first-parent, whether a merge whose first parent is excluded has any relevant parent is asked of its first two parents only: it is TREESAME, compared with nothing, when the second is relevant, and is compared with the first alone otherwise. With rewritten parents such a merge is listed when more than one parent is relevant, as under --full-history; and with --show-pulls whenever its first parent differs, though rewriting still goes through it. The refusal of --show-pulls past an excluded first parent is kept only where the exclusion arrived after the merge was simplified, and now also applies to the walk by generation number, which listed such merges wrongly. - Rewriting stops at an excluded commit before processing it, so that its exclusion is not passed on from there; and it walks through a merge --skip passed when exactly one of the parents it was rewritten to is relevant. - --remove-empty leaves a commit --skip passed no parents once it makes it a root. - git diffs the commits it shows before it gathers the boundary, and the parents those diffs read are known to the boundary's sort (none for a combined diff); under --simplify-by-decoration a boundary commit is diffed against the parents it shows. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- README.md | 23 ++++- src/RevWalk.zig | 160 ++++++++++++++++++++++++------ tools/log_diff.zig | 18 ++++ tools/rev_list.zig | 28 +++++- tools/simplify_tests.zig | 205 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 401 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 69b7f8e..d5bc932 100644 --- a/README.md +++ b/README.md @@ -222,13 +222,21 @@ do: `--simplify-merges`, `--show-pulls` and `--remove-empty` (whose roots pass on no exclusion, and which leaves a commit listed before it made it a root the parents it was listed with, unless `--reverse` shows it - only after), merges' + only after, and one `--skip` passed none), merges' parents counted only when they are relevant, parents rewritten for `--parents` and `--children` with git's reading ahead, `--first-parent` with each of them (a later parent the walk never queued is where rewriting stops, and git counts it as processed without simplifying it, so that should the walk reach it later it is listed and nothing - past it is), merges shown against their parents (`-m`, `--dd`, + past it is; a merge whose first parent is excluded is TREESAME, + compared with nothing, when its second parent is relevant, and is + compared with its first parent alone otherwise, whatever its later + parents; with parents rewritten, such a merge is listed when more than + one of its parents is relevant, and under `--show-pulls` whenever its + first parent differs), rewriting stopping at an excluded commit + before processing it and going through a merge `--skip` passed when + one of the parents it was rewritten to is relevant, merges shown + against their parents (`-m`, `--dd`, `--diff-merges`), under which git walks the history as `--full-history` walks it once any such option is given, even one a later option took back, and the commit-graph's changed-path Bloom filters asked before @@ -688,7 +696,12 @@ message search the commits it looks through and the parents of the one it finds — 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. +ordered by date, and its parents are not known to the boundary's order, +unless a listed commit was diffed against it (the one parent of a +commit with one, the first parent of a merge shown against it, every +parent of one shown against each; git was seen to read none for a +combined diff). Under `--simplify-by-decoration` a boundary commit is +diffed against the parents it shows. `-S`, `-G`, `--find-object` and `--diff-filter` choose commits by the same diffs. @@ -1006,7 +1019,9 @@ deliberate or not yet matched: later one is not or, with parents rewritten, is one the merge is TREESAME to, whose listing git was seen to decide by whether parents are rewritten and which commits rewriting passed through (a merge none - of whose parents is relevant pulls from none when they are not); + of whose parents is relevant pulls from none when they are not) — + under `--first-parent`, only where the first parent was excluded after + the merge was simplified, or a later parent was; - `--follow` is refused across a merge (at the first one the walk reaches, after the commits before it are written) and with `--reverse`; diff --git a/src/RevWalk.zig b/src/RevWalk.zig index 66c7ede..f7939f0 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -44,7 +44,8 @@ //! that were not listed — excluded, filtered out or past `--max-count` //! — found in the order listed and taken last first, then sorted as the //! list is (depth first unless the order is by date), knowing only the -//! parents of commits that have been read. +//! parents of commits that have been read, the parents the diffs of +//! listed commits were made against among them (`shown_reads`). //! - **`--children`** names, for each commit, the commits in the sorted //! list, the later-excluded ones included, that name it as a parent, //! the last in the list first. @@ -136,7 +137,8 @@ //! 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 and keeps the -//! others unless the merge is TREESAME to it. By default the walk +//! others unless the merge is TREESAME to it, and asks whether the +//! merge has any relevant parent of its first two only. 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, @@ -144,16 +146,19 @@ //! it has them all, counting only the parents still relevant and keeping //! 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, counted as it is listed. `--sparse` compares no +//! rewritten parents when it has more than one relevant parent, counted +//! as it is listed, under `--full-history`, or under `--first-parent` +//! when it did not follow one parent it is TREESAME to. `--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, 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 +//! one is excluded (where it stops before processing it), not +//! TREESAME, a pull, or a merge — through which it goes on when the +//! merge has exactly one relevant parent, counted over the parents it +//! was rewritten to when it was skipped — and a root that is TREESAME +//! is dropped. git lets go of a commit's parents once it //! has printed it, so that walking through it later finds none, except //! with `--reverse`, which prints only at the end; one whose parents //! were rewritten already (printed or skipped) is walked through as @@ -162,13 +167,16 @@ //! simplifying it, so that //! should the walk reach it later it is listed and nothing past it is. //! - **`--show-pulls`** lists a merge TREESAME to a later relevant parent -//! and not to a relevant first one. +//! and not to a relevant first one; with `--first-parent` and rewritten +//! parents, git also lists one whose first parent is irrelevant and +//! differs, which rewriting then goes through as TREESAME. //! - **`--remove-empty`**: a commit that differs from a parent holding //! nothing the pathspec matches, while it holds something (and, under //! `--simplify-by-decoration`, is not decorated), makes that parent a //! root; an excluded one then passes the exclusion on, and queues, no //! parents. One already listed keeps the parents it was listed with, -//! unless `--reverse` shows it only after the walk. +//! unless `--reverse` shows it only after the walk; one skipped keeps +//! none. //! - **`--simplify-merges`** sorts as `--topo-order` does, replacing each //! commit by its only parent when, with its parents replaced in turn, //! duplicates gone, TREESAME roots dropped (unless the commit is @@ -356,6 +364,12 @@ pub const Options = struct { /// after `--skip` has counted it and before `--max-count` does, and /// asked of `--boundary`'s commits too. diff_filter: ?Filter = null, + /// What git reads of each commit it shows (or the diff filter + /// looks at) before it gathers the boundary: the parents its diff + /// is against, whose own parents and dates the boundary's sort then + /// knows. Asked of each such commit just before the boundary is + /// sorted; `keep`'s answer is ignored. + shown_reads: ?Filter = null, /// Whether the list is limited to an ancestry path. pub fn onAncestryPath(o: Options) bool { @@ -444,6 +458,13 @@ const SimState = struct { simplified: bool = false, treesame: bool = false, pull: bool = false, + /// TREESAME to the one relevant parent the walk follows, its only + /// parent now. + followed_one: bool = false, + /// Listed though TREESAME, and not a pull: under `--show-pulls`, + /// `--first-parent` and rewritten parents, a merge whose first parent + /// is irrelevant and differs. + listed_anyway: bool = false, cleared: bool = false, freed: bool = false, rewritten: bool = false, @@ -472,6 +493,11 @@ const SimState = struct { boundary_parents: Span = .{}, /// Kept by `--simplify-merges` only because `--show-pulls` lists it. pull_only: bool = false, + /// With `--first-parent`, whether the first parent was irrelevant + /// when the commit was simplified, and how many later parents were + /// relevant then: what `--show-pulls` was decided with. + first_irrelevant: bool = false, + later_relevant: u32 = 0, /// 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, @@ -952,6 +978,7 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { defer chosen.deinit(w.gpa); var treesame = false; var pull = false; + var listed_anyway = false; var relevant_list: SmallList(Index, 8) = .{}; defer relevant_list.deinit(w.gpa); var same_bits: SmallList(Index, 8) = .{}; @@ -979,7 +1006,12 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { break :decide; } for (real) |p| if (single or w.relevant(p)) try relevant_list.append(w.gpa, p); - const has_relevant = relevant_list.items().len != 0; + // With --first-parent, git asks whether the merge has relevant + // parents of its first two only, as it was seen to. + const has_relevant = if (w.options.first_parent and real.len > 2) + single or w.relevant(real[0]) or w.relevant(real[1]) + else + relevant_list.items().len != 0; var all_relevant_same = true; var all_same = true; var first_same: ?bool = null; @@ -1014,7 +1046,9 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { const ps = try w.simState(p); ps.cleared = true; if (ps.simplified and (!listed or w.options.reverse)) ps.parents = .{}; - if (listed and w.options.reverse) ps.rewritten_parents = .{}; + // One skipped, its parents rewritten but not shown, keeps + // none either. + if (!listed or w.options.reverse) ps.rewritten_parents = .{}; } // An irrelevant parent is never marked TREESAME, so a merge // TREESAME to one is not counted as pulling from it. @@ -1026,6 +1060,12 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { compared = true; try chosen.appendSlice(w.gpa, parents.items()); if (s.show_pulls and parents.items().len > 1 and first_same == false and later_same) pull = true; + // With --first-parent and parents rewritten, git was seen to list + // a merge whose first parent is irrelevant and differs, whatever + // its later parents, without taking it for a pull: rewriting + // goes through it. + if (s.show_pulls and w.options.first_parent and w.rewriting() and !s.simplify_merges and real.len > 1 and + same_bits.items().len != 0 and parents.items()[0] == real[0] and !w.relevant(real[0]) and same_bits.items()[0] == 0) listed_anyway = true; // A merge whose compared parents are all irrelevant, while it has // relevant ones, is TREESAME to all it compared: none. treesame = if (has_relevant) all_relevant_same else all_same; @@ -1045,6 +1085,12 @@ fn simplifyCommit(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { st.simplified = true; st.treesame = treesame; st.pull = pull; + st.followed_one = followed_one; + st.listed_anyway = listed_anyway; + if (w.options.first_parent and real.len > 1 and !excluded_commit) { + st.first_irrelevant = !w.relevant(real[0]); + st.later_relevant = laterRelevant(w, real); + } st.parents = parents_span; st.relevant = relevant_span; st.relevant_count = relevant_span.len; @@ -1100,17 +1146,7 @@ fn reconsider(w: *RevWalk, list: []const Index) Error!void { // to depend on more than this walk follows: whether parents are // rewritten, and which commits rewriting passes through. const sim = w.options.simplify.?; - if (sim.show_pulls and (sim.full_history or w.options.first_parent) and real.len > 1 and !w.relevant(real[0])) { - for (real[1..]) |p| if (w.relevant(p)) return error.UnsupportedPullPastExclusion; - // With every parent excluded, git was seen to keep a merge - // TREESAME to a later one when parents are rewritten. - if (w.rewriting() and !sim.simplify_merges) { - for (real[1..]) |p| { - const at = std.mem.findScalar(Index, w.span(st.compared), p) orelse return error.UnsupportedPullPastExclusion; - if (w.span(st.same)[at] != 0) return error.UnsupportedPullPastExclusion; - } - } - } + try w.checkPullPastExclusion(c); // Under --simplify-merges, whether a merge TREESAME to an excluded // parent pulls was seen to depend on when git learned of the // exclusion. @@ -1129,6 +1165,41 @@ fn reconsider(w: *RevWalk, list: []const Index) Error!void { } } +/// `UnsupportedPullPastExclusion`: under `--show-pulls` with +/// `--full-history` or `--first-parent`, a merge whose first parent is +/// excluded and a later one is not or, with parents rewritten, is one it +/// is TREESAME to, where git was seen to decide by more than this walk +/// follows — unless, with `--first-parent` (and not `--simplify-merges`), +/// the first parent was irrelevant already when the merge was +/// simplified, and the later ones are as they were then: then the rule +/// found for that applies. +fn checkPullPastExclusion(w: *const RevWalk, c: Index) Error!void { + const sim = w.options.simplify.?; + const st = w.peekState(c); + if (!st.simplified or st.compared.len == 0) return; + const real = w.store.parents(c); + if (!sim.show_pulls or !(sim.full_history or w.options.first_parent) or real.len < 2 or w.relevant(real[0])) return; + if (w.options.first_parent and !sim.simplify_merges and st.first_irrelevant and st.later_relevant == laterRelevant(w, real)) return; + for (real[1..]) |p| if (w.relevant(p)) return error.UnsupportedPullPastExclusion; + // With every parent excluded, git was seen to keep a merge TREESAME + // to a later one when parents are rewritten. + if (w.rewriting() and !sim.simplify_merges) { + for (real[1..]) |p| { + const at = std.mem.findScalar(Index, w.span(st.compared), p) orelse return error.UnsupportedPullPastExclusion; + if (w.span(st.same)[at] != 0) return error.UnsupportedPullPastExclusion; + } + } +} + +/// How many of a merge's parents after the first are relevant. +fn laterRelevant(w: *const RevWalk, real: []const Index) u32 { + var n: u32 = 0; + for (real[1..]) |p| { + if (w.relevant(p)) n += 1; + } + return n; +} + /// How many of a commit's parents are relevant now; a single parent /// always is. On an ancestry path, git was seen to count the parents /// `--simplify-merges` left a commit, and none it took out of the list @@ -1154,12 +1225,14 @@ fn shownBySimplification(w: *const RevWalk, c: Index) bool { 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 (!st.treesame or st.pull or st.listed_anyway) return true; if (s.sparse) return true; // Counted as the commit is listed: in a walk by generation number, // the exclusions known by then. A commit --remove-empty made a root // has none. - if (w.rewriting() and s.full_history and !st.cleared and w.relevantNow(c) > 1) return true; + // With --first-parent, git counts them too unless the commit followed + // one parent. + if (w.rewriting() and (s.full_history or (w.options.first_parent and !st.followed_one)) and !st.cleared and w.relevantNow(c) > 1) return true; return false; } @@ -1192,6 +1265,13 @@ fn rewriteParents(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { try out.append(w.gpa, p); break; } + // An excluded commit is where rewriting stops, before it + // is processed: git does not pass its exclusion on from + // here, as it was seen not to. + if (w.excluded(p)) { + try out.append(w.gpa, p); + break; + } try w.loadParent(c, p, diag); try w.process(p, diag); const ps = w.peekState(p); @@ -1204,9 +1284,22 @@ fn rewriteParents(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { var q = if (ps.freed) &[_]Index{} else if (ps.rewritten) w.span(ps.rewritten_parents) else w.simplifiedParents(p); if (w.options.first_parent and q.len > 1) q = q[0..1]; if (q.len > 1) { - const rel = w.span(ps.relevant); - if (rel.len == 1) { - p = rel[0]; + // Through a merge whose parents were rewritten already + // (skipped), relevance is counted over those. + var one: ?Index = null; + var count: usize = 0; + if (ps.rewritten and !ps.freed) { + for (q) |x| if (w.relevant(x)) { + count += 1; + one = x; + }; + } else { + const rel = w.span(ps.relevant); + count = rel.len; + if (count == 1) one = rel[0]; + } + if (count == 1) { + p = one.?; continue; } try out.append(w.gpa, p); @@ -1270,7 +1363,8 @@ pub fn diffParentsOf(w: *const RevWalk, e: Entry, full_diff: bool) []const Index if (s.simplify_merges) { if (!st.rewritten) return w.simplifiedParents(e.commit); if (full_diff) return w.span(st.root_parents); - const shown = w.span(st.rewritten_parents); + // A boundary commit is diffed against the parents it shows. + const shown = if (e.boundary and st.replaced and st.remains) w.span(st.boundary_parents) else w.span(st.rewritten_parents); if (shown.len == 0) return w.span(st.root_parents); return shown; } @@ -2163,6 +2257,13 @@ fn nextStreamed(w: *RevWalk, diag: ?*Diagnostics) Error!?Entry { } fn addBoundary(w: *RevWalk) Error!void { + if (w.options.shown_reads) |r| { + if (w.options.diff_filter == null) { + for (w.output.items) |e| _ = try r.keep(r.context, e.commit); + } else for (w.passed.items) |c| { + _ = try r.keep(r.context, c); + } + } var found: std.ArrayList(Index) = .empty; defer found.deinit(w.gpa); var marked: std.AutoHashMapUnmanaged(Index, void) = .empty; @@ -2477,6 +2578,9 @@ fn generationWalk(w: *RevWalk, diag: ?*Diagnostics) Error!void { try w.expandTopo(&g, c, diag); if (w.excluded(c)) continue; if (w.options.since) |s| if (w.store.time(c) < s) continue; + // The exclusions are known by now, as they are when a walk by date + // has them all. + if (w.pruning()) try w.checkPullPastExclusion(c); if (!try w.keep(c)) continue; if (w.rewriting()) try w.rewriteParents(c, diag); switch (try w.admit(c)) { diff --git a/tools/log_diff.zig b/tools/log_diff.zig index e1de13a..d7e4100 100644 --- a/tools/log_diff.zig +++ b/tools/log_diff.zig @@ -500,6 +500,24 @@ pub const Writer = struct { return w.writeTrees(stdout, old, try treeOfCommitParentsGiven(w, id), framing, gpa); } + /// How many of a commit's `parents`, from the first, git reads in + /// writing its diff, as it was seen to: the one parent of a commit + /// with one, and of a merge the first when it is shown against it, + /// every one when shown against each, and none when it is shown + /// combined (a later parent compared only there was seen to stay + /// unread). + pub fn parentsRead(w: *const Writer, parents: usize) usize { + if (parents == 0) return 0; + if (w.merges_only and parents < 2) return 0; + if (parents == 1) return 1; + if (w.skip_merges) return 0; + return switch (w.merges) { + .first_parent => 1, + .separate => parents, + else => 0, + }; + } + /// Whether merge commit `id` is written once for each of its /// `parents`, each time with `(from )` and the diff against /// that parent (`-m`, `--diff-merges=separate`). diff --git a/tools/rev_list.zig b/tools/rev_list.zig index d060369..1b6908c 100644 --- a/tools/rev_list.zig +++ b/tools/rev_list.zig @@ -982,6 +982,10 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder if (follower) |*f| filter.follower = f; 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 }; + if (diffs) |dw| { + filter.shown_diffs = dw; + options.shown_reads = .{ .context = &filter, .keep = Filter.readShown }; + } // What git read resolving the revisions it has read for the walk too. for (revs.resolver.parsed.items) |id| { @@ -1166,6 +1170,8 @@ const Filter = struct { diffs: ?*log_diff.Writer = null, /// Set under `--follow`. follower: ?*log_diff.Follower = null, + /// Set when commits are shown with diffs, whatever chooses them. + shown_diffs: ?*log_diff.Writer = null, /// The walk, which says which parents each commit is diffed /// against (`RevWalk.diffParentsOf`). walk: ?*RevWalk = null, @@ -1212,7 +1218,7 @@ const Filter = struct { var parents: std.ArrayList(ObjectId) = .empty; defer parents.deinit(f.ctx.gpa); // As the diff written after it is (`boundaryRoot`). - const diff_parents = if (f.boundaryRoot(f.walk.?.filtering_boundary, commit)) &.{} else f.walk.?.diffParentsOf(.{ .commit = commit }, f.full_diff); + const diff_parents = if (f.boundaryRoot(f.walk.?.filtering_boundary, commit)) &.{} else f.walk.?.diffParentsOf(.{ .commit = commit, .boundary = f.walk.?.filtering_boundary }, f.full_diff); for (diff_parents) |p| parents.append(f.ctx.gpa, f.h.store.id(p)) catch |err| { f.failure = err; f.failed_id = id; @@ -1234,6 +1240,26 @@ const Filter = struct { }; } + /// Reads the parents git reads diffing a commit it shows, before it + /// gathers the boundary (`RevWalk.Options.shown_reads`). + fn readShown(context: *anyopaque, commit: git.Commits.Index) error{FilterFailed}!bool { + const f: *Filter = @ptrCast(@alignCast(context)); + const parents = f.walk.?.diffParentsOf(.{ .commit = commit }, f.full_diff); + const n = f.shown_diffs.?.parentsRead(parents.len); + var i: usize = 0; + while (i < n) : (i += 1) { + // Reading a parent may move the parent lists. + const p = f.walk.?.diffParentsOf(.{ .commit = commit }, f.full_diff)[i]; + var diag: Diagnostics = .{}; + f.h.store.load(p, &diag) catch |err| { + f.failure = err; + f.failed_id = f.h.store.id(p); + return error.FilterFailed; + }; + } + return true; + } + fn keepMessage(f: *Filter, commit: git.Commits.Index) error{FilterFailed}!bool { const id = f.h.store.id(commit); var diag: Diagnostics = .{}; diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 9db82c0..298373c 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -1269,3 +1269,208 @@ test "message searches take commits of one date as git does, and read the parent }; } } + +test "--first-parent decides a merge whose first parent is excluded by its first two parents" { + // A merge whose first parent is excluded is TREESAME, compared with + // nothing, when its second parent is relevant; when that one is + // excluded too, git compares it with its first parent alone, however + // many relevant parents it has after them. Under --parents it is + // listed with more than one relevant parent, and, with --show-pulls, + // whenever its first parent differs. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + const merges = [_][]const usize{ + &.{ 1, 3, 5 }, &.{ 1, 3, 7 }, &.{ 1, 2, 5 }, &.{ 1, 5, 3 }, &.{ 3, 5, 6 }, + &.{ 3, 7, 5 }, &.{ 1, 5 }, &.{ 1, 7 }, &.{ 1, 3 }, &.{ 3, 1, 5 }, + &.{ 3, 5 }, &.{ 1, 5, 6 }, &.{ 7, 3 }, &.{ 1, 3, 5, 6 }, &.{ 1, 7, 5 }, + }; + var commits: std.ArrayList(Dated) = .empty; + defer commits.deinit(testing.allocator); + try commits.appendSlice(testing.allocator, &.{ + .{ .files = "f=1", .time = 10 }, + // Excluded (by the branches below), differing from the merges. + .{ .files = "f=2", .parents = &.{0}, .time = 20 }, + .{ .files = "f=4", .parents = &.{0}, .time = 21 }, + // Excluded, the same as the merges. + .{ .files = "f=3", .parents = &.{0}, .time = 22 }, + .{ .files = "f=3", .parents = &.{1}, .time = 23 }, + // Relevant, the same as the merges, and differing. + .{ .files = "f=3", .parents = &.{0}, .time = 24 }, + .{ .files = "f=3", .parents = &.{0}, .time = 25 }, + .{ .files = "f=5", .parents = &.{0}, .time = 26 }, + .{ .files = "f=2", .parents = &.{1}, .time = 40, .branches = &.{"x1"} }, + .{ .files = "f=4", .parents = &.{2}, .time = 40, .branches = &.{"x2"} }, + .{ .files = "f=3", .parents = &.{3}, .time = 40, .branches = &.{"x3"} }, + .{ .files = "f=3", .parents = &.{4}, .time = 40, .branches = &.{"x4"} }, + }); + var names: [merges.len][8]u8 = undefined; + var name_slices: [merges.len][1][]const u8 = undefined; + for (merges, 0..) |ps, i| { + const name = try std.fmt.bufPrint(&names[i], "m{d}", .{i}); + name_slices[i] = .{name}; + try commits.append(testing.allocator, .{ .files = "f=3", .parents = ps, .time = 100, .branches = &name_slices[i] }); + } + try importDated(&repo, commits.items, false); + for (name_slices) |n| for ([_][]const []const u8{ + &.{}, + &.{"--parents"}, + &.{"--full-history"}, + &.{ "--full-history", "--parents" }, + &.{ "--boundary", "--parents" }, + &.{"--show-pulls"}, + &.{ "--show-pulls", "--parents" }, + &.{ "--show-pulls", "--full-history", "--parents" }, + &.{ "--show-pulls", "--topo-order", "--parents" }, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "rev-list", "--first-parent" }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ n[0], "^x1", "^x2", "^x3", "^x4", "--", "f" }); + try same(&repo, a.items); + }; +} + +test "rewriting walks through a skipped merge left one relevant parent by its rewritten ones" { + // The merge c5 is skipped, its parents rewritten to c1 and the + // excluded c2; walking through it from c6, git counts relevance over + // those, and goes on to c1. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 10 }, + .{ .files = "a=2", .parents = &.{0}, .time = 50 }, + .{ .files = "a=2", .parents = &.{0}, .time = 40 }, + .{ .files = "a=2", .parents = &.{2}, .time = 60 }, + .{ .files = "a=5", .parents = &.{2}, .time = 70, .branches = &.{"x"} }, + .{ .files = "a=2", .parents = &.{ 1, 3 }, .time = 90 }, + .{ .files = "a=3", .parents = &.{5}, .time = 80, .branches = &.{"side"} }, + .{ .files = "a=4", .parents = &.{5}, .time = 100 }, + }, true); + for ([_][]const u8{ "--skip=0", "--skip=1", "--skip=2", "--skip=3" }) |skip| { + try same(&repo, &.{ "rev-list", "--parents", "--full-history", skip, "main", "side", "^x", "--", "a" }); + try same(&repo, &.{ "rev-list", "--parents", "--full-history", "--boundary", skip, "main", "side", "^x", "--", "a" }); + } +} + +test "the boundary is sorted knowing the parents the diffs of listed commits read" { + // c5's parents are rewritten to c4 and c2, later parents the + // --first-parent walk never read; diffing c5 against the first of + // them reads it, as git does before it sorts the boundary, which then + // knows c4's parent c2. A combined diff was seen to read neither. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 1000 }, + .{ .files = "a=1", .parents = &.{0}, .time = 1050 }, + .{ .files = "a=1 d/x=2", .parents = &.{ 1, 0 }, .time = 1020 }, + .{ .time = 1000 }, + .{ .files = "a=0 d/x=2", .parents = &.{2}, .time = 1040 }, + .{ .files = "a=1 b=2", .parents = &.{ 0, 4, 2 }, .time = 1049 }, + }, true); + for ([_][]const []const u8{ + &.{ "-m", "--raw" }, + &.{ "-m", "-p" }, + &.{"--diff-merges=first-parent"}, + &.{ "-s", "--diff-merges=first-parent" }, + &.{ "--diff-merges=separate", "--name-only" }, + &.{"-m"}, + &.{ "-s", "-m" }, + &.{ "-c", "--raw" }, + &.{"--cc"}, + &.{"--raw"}, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "log", "--format=%m %h %p" }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "--first-parent", "--boundary", "--parents", "main", "--", "d" }); + try same(&repo, a.items); + } +} + +test "--remove-empty leaves a commit --skip passed no parents once it makes it a root" { + // c1 holds nothing the path matches; it is skipped, its parents + // rewritten, before rewriting c3's parents reaches c2, which makes it + // a root. As a boundary commit it then shows none. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 10 }, + .{ .files = "z=1", .parents = &.{0}, .time = 100, .branches = &.{"s"} }, + .{ .files = "a=3", .parents = &.{ 0, 1 }, .time = 50 }, + .{ .files = "a=2", .parents = &.{2}, .time = 60 }, + .{ .files = "a=4", .parents = &.{3}, .time = 120 }, + }, true); + for ([_][]const u8{ "--skip=1", "--skip=2", "--skip=3" }) |skip| { + for ([_][]const []const u8{ &.{}, &.{"--full-history"}, &.{"--reverse"} }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.appendSlice(testing.allocator, &.{ "rev-list", "--boundary", "--parents", "--remove-empty", skip }); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "main", "s", "--", "a" }); + try same(&repo, a.items); + } + try same(&repo, &.{ "log", "--format=%m %h %p", "--boundary", "--parents", "--remove-empty", skip, "main", "s", "--", "a" }); + } +} + +test "rewriting stops at an excluded commit without passing its exclusion on" { + // c4 is excluded and old enough that the walk stops (after c12 to c8 + // come out excluded) before it comes out. Rewriting c5's parents stops + // at it, as git does, without reading c3 and excluding c2 and c1 + // through it: c2's parents are rewritten through c1 to c0. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "a=1", .time = 100 }, + .{ .files = "a=1", .parents = &.{0}, .time = 110 }, + .{ .files = "a=2", .parents = &.{1}, .time = 120 }, + .{ .files = "a=2", .parents = &.{2}, .time = 130 }, + .{ .files = "a=2", .parents = &.{3}, .time = 5, .branches = &.{"x"} }, + .{ .files = "a=3", .parents = &.{4}, .time = 200, .branches = &.{"y"} }, + .{ .files = "a=9", .time = 50 }, + .{ .files = "a=9", .parents = &.{6}, .time = 51 }, + .{ .files = "a=9", .parents = &.{7}, .time = 52 }, + .{ .files = "a=9", .parents = &.{8}, .time = 53 }, + .{ .files = "a=9", .parents = &.{9}, .time = 54 }, + .{ .files = "a=9", .parents = &.{10}, .time = 55 }, + .{ .files = "a=9", .parents = &.{11}, .time = 56, .branches = &.{"z"} }, + .{ .files = "a=4", .parents = &.{2}, .time = 150 }, + }, true); + for ([_][]const []const u8{ + &.{"--parents"}, + &.{ "--parents", "--full-history" }, + &.{ "--boundary", "--parents" }, + &.{ "--parents", "--sparse" }, + &.{"--children"}, + }) |opts| { + var a: std.ArrayList([]const u8) = .empty; + defer a.deinit(testing.allocator); + try a.append(testing.allocator, "rev-list"); + try a.appendSlice(testing.allocator, opts); + try a.appendSlice(testing.allocator, &.{ "y", "main", "^x", "^z", "--", "a" }); + try same(&repo, a.items); + } +} + +test "--simplify-by-decoration diffs a boundary commit against the parents it shows" { + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "d/x=0", .time = 1040 }, + .{ .files = "d/x=0", .parents = &.{0}, .time = 1010 }, + .{ .files = "a=1", .time = 1020 }, + .{ .files = "b=2 d/x=0", .parents = &.{ 0, 2 }, .time = 1000 }, + .{ .files = "a=2 b=2 d/x=0", .parents = &.{ 2, 3 }, .time = 1040 }, + .{ .parents = &.{2}, .time = 1045 }, + .{ .files = "a=2 b=2", .parents = &.{4}, .time = 1060 }, + .{ .files = "d/x=0", .parents = &.{0}, .time = 1070 }, + .{ .files = "a=1 d/x=0", .parents = &.{1}, .time = 1050 }, + .{ .files = "a=0 b=2 d/x=0", .parents = &.{3}, .time = 1089 }, + .{ .files = "a=2 b=2 d/x=1", .parents = &.{ 6, 0, 8 }, .time = 1099 }, + }, true); + for ([_][]const u8{ "--raw", "--name-status", "-p" }) |format| for ([_][]const u8{ "--max-parents=1", "--no-merges", "--min-parents=0" }) |count| { + try same(&repo, &.{ "log", "--format=%m %h %p", format, "--simplify-by-decoration", "--decorate-refs=refs/tags/c3", "--first-parent", "--parents", count, "--boundary", "main", "--", "d" }); + }; +} -- knotmirror 0.0.1