diff --git a/README.md b/README.md index bb101de..a8259be 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,10 @@ do: that is also a starting point; the commits off the path counted by the sort and `--children` before they are left out), `--since`, `--until`, `--since-as-filter`, `--boundary`, the sides of a symmetric difference - and children — every + (its merge bases queued before either side, which orders commits of + one date, and a boundary commit on the left only where the left side + reached it or named it, as `--count --left-right` counts them) 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, git's walk of `--topo-order`, @@ -214,7 +217,9 @@ do: - 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, reading the commits git reads to find - them, which a symmetric difference's exclusions then reach through. + them (with a commit-graph, stopping once no commit queued could lead to + another base, a base queued a second time not counted), 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 @@ -1103,11 +1108,6 @@ deliberate or not yet matched: - 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); -- `--children` with `--boundary` for a symmetric difference is refused, - whose boundary commits' children depend on which commits git's - merge-base search happened to read, and so is `rev-list --count` with - `--left-right` and `--boundary` there, which counts each boundary - commit on the side that reached it, by the same search; - given a commit-graph whose parent positions are corrupt in a commit that is only an ancestor of the merge bases, git may find the bases without reading it, where `zig-git merge-base` reads it and stops with diff --git a/src/RevWalk.zig b/src/RevWalk.zig index 9aca436..8a333ee 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -906,7 +906,11 @@ fn process(w: *RevWalk, c: Index, diag: ?*Diagnostics) Error!void { if (is_excluded) try w.set(p, excluded_flag); try w.loadParent(c, p, diag); if (is_excluded) try w.excludeParents(p); - if (sides != 0) try w.set(p, sides); + // An excluded commit passes on its exclusion and not its side, + // so that a boundary commit is on the left only when a commit of + // the left side reached it, or it was named there, as git counts + // them. + if (sides != 0 and !is_excluded) try w.set(p, sides); try w.giveSource(p, w.sourceOf(c)); if (w.flag(p) & seen_flag == 0) { try w.set(p, seen_flag); diff --git a/src/merge_base.zig b/src/merge_base.zig index cef809e..59fb6b6 100644 --- a/src/merge_base.zig +++ b/src/merge_base.zig @@ -30,11 +30,12 @@ //! 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 -//! 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. +//! sides and not stale (a candidate queued again not counted), 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; @@ -135,6 +136,10 @@ const Painter = struct { fn mayFindMore(p: *Painter) bool { var highest: [2]?u64 = .{ null, null }; for (p.queue.items) |q| { + // A candidate queued a second time before it came out is not + // counted, as without a commit-graph: git was seen to read no + // further for it. + if (p.flag(q.commit) & result != 0) continue; 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; diff --git a/tools/rev_list.zig b/tools/rev_list.zig index 50de3f6..adf90ca 100644 --- a/tools/rev_list.zig +++ b/tools/rev_list.zig @@ -639,22 +639,6 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder held.clearRetainingCapacity(); } simplify.rewrite_parents = out.parents or out.children; - // Which boundary commits' children git lists in a symmetric difference - // depends on which commits finding its merge bases read, which is not - // matched. - if (stopped == null and out.children and options.boundary) for (words.items) |w| switch (w) { - .revision => |r| if (std.mem.find(u8, r.text, "...") != null) - return tool.fatalMessage(stderr, "zig-git {s}: --children with --boundary and a symmetric difference is not supported", .{command_name}), - else => {}, - }; - // Counting each side, git counts a boundary commit on the side that - // reached it, which in a symmetric difference depends on how much of - // each side its search for the merge bases read: not matched. - if (stopped == null and out.count and out.left_right and options.boundary) for (words.items) |w| switch (w) { - .revision => |r| if (std.mem.find(u8, r.text, "...") != null) - return tool.fatalMessage(stderr, "zig-git {s}: --count with --left-right, --boundary and a symmetric difference is not supported", .{command_name}), - else => {}, - }; var path_phase = false; var path_count: usize = 0; var revision_count: usize = 0; @@ -751,9 +735,13 @@ fn runPrefixed(ctx: Context, args: []const []const u8, stdout: *Io.Writer, stder if (dashdash == null) try notAlsoPath(ctx, repo, text, stderr); revision_count += 1; // git queues the excluded end of `A..B` and `A^-` - // first, which decides the order of commits with one date. + // first, and the merge bases of `A...B` before either + // side, which decides the order of commits with one date. if (git.revision.parseSet(text)) |set| switch (set) { .range, .parent_range => std.mem.reverse(history.Part, parts.items), + .symmetric => if (parts.items.len > 2) { + std.mem.rotate(history.Part, parts.items, 2); + }, else => {}, }; for (parts.items) |p| { diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index 6f8dc4a..1429fd1 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -33,8 +33,6 @@ fn sameIn(repo: *Repo, cwd: []const u8, args: []const []const u8) !void { const conditional_refusals = [_][]const u8{ "--first-parent with paths in a symmetric difference is not supported", "is not supported where it reaches a commit before the walk has learned whether an exclusion reaches it", - // Not conditional, but met by the generated ranges. - "--children with --boundary and a symmetric difference is not supported", }; /// `zig-git ` writes what git writes, or refuses with one of diff --git a/tools/walk_tests.zig b/tools/walk_tests.zig index 7101533..46e924b 100644 --- a/tools/walk_tests.zig +++ b/tools/walk_tests.zig @@ -162,6 +162,7 @@ const extras = [_][]const []const u8{ &.{"--timestamp"}, &.{"--count"}, &.{ "--count", "--left-right" }, + &.{ "--count", "--left-right", "--boundary" }, &.{"--left-right"}, &.{ "--left-right", "--boundary" }, &.{"--left-only"}, @@ -201,16 +202,6 @@ fn walkEverything(repo: *Repo, graph: bool) !void { try args.appendSlice(gpa, order); try args.appendSlice(gpa, extra); try args.appendSlice(gpa, range); - var has_children = false; - var has_boundary = 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 (has_children and has_boundary and std.mem.find(u8, range[0], "...") != null) { - try refused(repo, args.items); - continue; - } if (graph) try sameOrAuthorDateRefused(repo, args.items) else try same(repo, args.items); }; } @@ -567,6 +558,84 @@ test "a symmetric difference's search for merge bases stops as git's does" { }) |a| try same(&repo, a); } +test "a boundary commit is counted on the left only when the left side reached it" { + // c2, the left side, is its own merge base. Excluded, it passes its + // exclusion to c1 and not its side: git counts c1, which c4 on the + // right reached, on the right, and c2 alone on the left. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try simplify_tests.importDated(&repo, &.{ + .{ .files = "a=0", .time = 10 }, + .{ .files = "a=1", .parents = &.{0}, .time = 20 }, + .{ .files = "a=2", .parents = &.{1}, .time = 30 }, + .{ .files = "a=3", .parents = &.{2}, .time = 40 }, + .{ .files = "a=4", .parents = &.{ 3, 1 }, .time = 50 }, + }, true); + for ([_][]const []const u8{ + &.{ "rev-list", "--count", "--left-right", "--boundary", "c2...c4" }, + &.{ "rev-list", "--count", "--left-right", "--boundary", "c4...c2" }, + &.{ "rev-list", "--count", "--left-right", "--boundary", "--topo-order", "c2...c4" }, + &.{ "rev-list", "--count", "--left-right", "--boundary", "c2...c4", "--", "a" }, + &.{ "rev-list", "--left-only", "--boundary", "c2...c4" }, + &.{ "rev-list", "--right-only", "--boundary", "c2...c4" }, + &.{ "rev-list", "--children", "--boundary", "c2...c4" }, + }) |a| try same(&repo, a); +} + +test "with a commit-graph, a candidate queued again does not keep the search for merge bases going" { + // c3, the right side, is its own merge base, queued again from c5 + // before it comes out, behind c4 of the same date and generation. + // Painting c3 from both sides, git stops without taking c4, so that + // c1, c4's parent, is never read and the exclusion of c3 stops at + // c2: c6 is simplified knowing nothing of c0's exclusion, and c7's + // parents are rewritten through it as they are. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try simplify_tests.importDated(&repo, &.{ + .{ .files = "a=1", .time = 1372 }, + .{ .files = "a=1", .parents = &.{0}, .time = 1459 }, + .{ .files = "a=1", .parents = &.{1}, .time = 1532 }, + .{ .files = "a=1", .parents = &.{2}, .time = 1656 }, + .{ .files = "a=1", .parents = &.{1}, .time = 1656 }, + .{ .files = "a=1", .parents = &.{3}, .time = 1726 }, + .{ .files = "a=1", .parents = &.{ 0, 2, 5, 4 }, .time = 2661 }, + .{ .files = "a=2", .parents = &.{ 4, 6, 0, 2, 5 }, .time = 2906 }, + .{ .files = "a=2", .parents = &.{ 5, 4, 0, 2, 6, 7 }, .time = 2971 }, + }, true); + try repo.git(&.{ "commit-graph", "write", "--reachable" }); + for ([_][]const []const u8{ + &.{ "rev-list", "--parents", "--boundary", "c8...c3", "--", "a" }, + &.{ "rev-list", "--children", "--boundary", "c8...c3", "--", "a" }, + &.{ "rev-list", "--parents", "c8...c3", "--", "a" }, + &.{ "merge-base", "--all", "c8", "c3" }, + }) |a| try same(&repo, a); +} + +test "a symmetric difference queues its merge bases before either side" { + // c3, the right side, is its own merge base, of c4's date. Queued + // before c4, it is processed first, reading c2, so that its exclusion + // reaches c0 through c1 before c4 is simplified, and c4, TREESAME + // only to the excluded c0, is listed. Without a commit-graph the + // search for the merge bases reads that far itself. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try simplify_tests.importDated(&repo, &.{ + .{ .files = "a=1", .time = 574 }, + .{ .files = "a=2", .parents = &.{0}, .time = 606 }, + .{ .files = "a=2", .parents = &.{1}, .time = 376 }, + .{ .files = "a=2", .parents = &.{2}, .time = 353 }, + .{ .files = "a=1", .parents = &.{ 0, 2, 3 }, .time = 353 }, + }, true); + try repo.git(&.{ "commit-graph", "write", "--reachable" }); + for ([_][]const []const u8{ + &.{ "rev-list", "c4...c3", "--", "a" }, + &.{ "rev-list", "--left-right", "--boundary", "--children", "c4...c3", "--", "a" }, + &.{ "rev-list", "c3...c4", "--", "a" }, + &.{ "rev-list", "c4", "^c3", "--", "a" }, + &.{ "rev-list", "^c3", "c4", "--", "a" }, + }) |a| try same(&repo, a); +} + test "an ordering that needs the whole list stops as the walk by date does, with a commit-graph" { // c11 excludes c8 only through c9 and c10, dated before everything: // the walk stops, after the excluded c0 to c7, before it reaches them,