diff --git a/README.md b/README.md index 51fafe6..bb101de 100644 --- a/README.md +++ b/README.md @@ -297,11 +297,19 @@ do: instead, as below, and one in the list it loses, the root too), while a commit with one parent keeps it; a TREESAME merge that would lose every parent it is TREESAME to keeps the first of them, however - it lost it; git holds, once it has simplified the merges, the parents + it lost it, and a merge that would lose every parent keeps the first + it is TREESAME to, TREESAME or not (so that a merge TREESAME to a root + `--remove-empty` made, which its changed-path Bloom filter takes for + TREESAME to the empty tree though it holds what the pathspec matches, + is replaced by it, and a pull `--show-pulls` keeps so holds it but + shows no parent, and is shown as nothing in its children's parents); + git holds, once it has simplified the merges, the parents left and, when none is left, the roots TREESAME to the empty tree they lost (a merge, only those it is TREESAME to), or, when those left are none it is TREESAME to, the first parent it is TREESAME to (shown as well when it - is no root, or is one excluded only after it was found TREESAME), with the later parents unreplaced under + is no root, or is one excluded only after it was found TREESAME, and + diffed against under `--full-diff`, a root `--remove-empty` made as + much as a true one), with the later parents unreplaced under `--first-parent`, and drops the roots and replaces the later parents only as it shows a commit — so the parent counts count what it holds, and a boundary commit shows it; under `--simplify-by-decoration`, a @@ -319,7 +327,8 @@ do: rewritten parents for having more than one relevant parent counts them among the parents it holds once the merges are simplified; ancestry for dropping parents goes - through no parent of a commit `--remove-empty` made a root, and, with + through no parent of a commit `--remove-empty` made a root, excluded + or not and whenever it made it one, and, with exclusions, `--since` or `--ancestry-path`, through the parents simplification left each commit rather than its real ones (so that under `--sparse` a merge whose excluded parent is reached only through @@ -1044,14 +1053,6 @@ deliberate or not yet matched: not to take the nearest, nor one kind over another; with candidates of one kind it matches git's choice and depth; `--dirty`, `--broken`, `--debug` and describing a blob are refused; -- `--simplify-merges` and `--simplify-by-decoration` refuse, where git - was seen to decide by rules not matched, apparently by when it learned - of an exclusion or which commit `--remove-empty` made a root first: - `--remove-empty` where a merge loses a parent - `--remove-empty` made a root and either keeps no parent or a parent - count or `--full-diff` would see the loss; and, with an excluded - commit, where whether one of a merge's parents is an ancestor of - another depends on a commit `--remove-empty` made a root; - ownership and filesystem boundaries are found with `statx`, so only on Linux; elsewhere a caller supplies its own answers or discovery says it cannot check; diff --git a/src/RevWalk.zig b/src/RevWalk.zig index bae5ba0..9aca436 100644 --- a/src/RevWalk.zig +++ b/src/RevWalk.zig @@ -218,7 +218,13 @@ //! duplicates gone, TREESAME roots dropped and parents that real //! ancestry says are ancestors of others dropped, it has one and is //! TREESAME to it. When the commit is TREESAME and that drops every -//! parent it is TREESAME to, the first of them stays. A merge that +//! parent it is TREESAME to, the first of them stays, as it does, +//! TREESAME or not, when that drops every parent: so that a merge +//! TREESAME to a root `--remove-empty` made that its changed-path +//! Bloom filter takes for TREESAME to the empty tree, though it holds +//! what the pathspec matches, is replaced by it, and a pull +//! `--show-pulls` keeps so holds it but shows no parent, and is shown +//! as nothing in its children's parents. A merge that //! lost a parent as an ancestor of another is TREESAME, before the //! roots go, when it is TREESAME to every parent left on the path. A //! root excluded only after it was found TREESAME, or one off the @@ -243,12 +249,14 @@ //! one relevant parent counts them among the parents it is left. //! With exclusions or `--since`, ancestry is reckoned through the parents //! simplification has left, and never through the parents of a commit -//! `--remove-empty` made a root. With `--first-parent` only the first +//! `--remove-empty` made a root, even one it made a root only after it +//! was simplified, or one excluded. With `--first-parent` only the first //! parent counts in this; the others are shown replaced, less listed //! TREESAME roots and duplicates. git holds the parents left (or, when //! none is, the roots TREESAME to the empty tree they lost), and, //! decorated roots included, when those are none it is -//! TREESAME to, the first parent it is TREESAME to after all, and the +//! TREESAME to, the first parent it is TREESAME to after all (a root +//! `--remove-empty` made as much as any), and the //! later parents unreplaced, and drops the roots and replaces the later //! parents only as it shows a commit: what it holds is what the parent //! counts count and a boundary commit shows (so that such a parent lost @@ -579,6 +587,9 @@ const SimState = struct { rewritten: bool = false, /// Kept whole by `--simplify-merges`, rather than replaced. remains: bool = true, + /// Kept by `--simplify-merges` holding, as the one parent it was left, + /// a root TREESAME to the empty tree that it does not show. + holds_hidden: bool = false, relevant_count: u32 = 0, parents: Span = .{}, relevant: Span = .{}, @@ -676,20 +687,16 @@ pub const Entry = struct { /// on what this walk does not follow: `UnsupportedFirstParentExclusion`, /// `--first-parent` in a symmetric difference where a merge's first /// parent was excluded after it was simplified (git's search for the -/// merge bases may have read enough to know sooner). Under -/// `--simplify-merges` (or `--simplify-by-decoration`), git was seen to -/// decide by rules not matched: `UnsupportedRemoveEmptyMerge`, a merge -/// losing a parent `--remove-empty` made a root where that shows; and -/// `UnsupportedRemoveEmptyAncestry`, with exclusions, where whether a merge's parent is an ancestor of another depends on a -/// commit `--remove-empty` made a root; `UnsupportedRewriteUnexplored`, -/// rewriting in a walk by generation number that processes a commit the -/// explore walk has not reached while an excluded commit still to be -/// explored reaches one of its parents (or the commit itself, rewriting -/// going on past it), or goes through a merge whose go-through depends -/// on such an exclusion or that had an irrelevant parent when it was -/// simplified and has been left one relevant parent of several since, -/// where git was seen to answer as if it knew of the exclusion or not. -pub const Error = Commits.Error || TreeDiff.Error || error{ UnsupportedAuthorDateOrdering, UnsupportedFirstParentExclusion, UnsupportedRemoveEmptyMerge, UnsupportedRemoveEmptyAncestry, UnsupportedRewriteUnexplored, FilterFailed }; +/// merge bases may have read enough to know sooner); and +/// `UnsupportedRewriteUnexplored`, rewriting in a walk by generation +/// number that processes a commit the explore walk has not reached while +/// an excluded commit still to be explored reaches one of its parents (or +/// the commit itself, rewriting going on past it), or goes through a +/// merge whose go-through depends on such an exclusion or that had an +/// irrelevant parent when it was simplified and has been left one +/// relevant parent of several since, where git was seen to answer as if +/// it knew of the exclusion or not. +pub const Error = Commits.Error || TreeDiff.Error || error{ UnsupportedAuthorDateOrdering, UnsupportedFirstParentExclusion, UnsupportedRewriteUnexplored, FilterFailed }; const Queued = struct { time: i64, sequence: u64, commit: Index }; @@ -1802,16 +1809,8 @@ fn simplifyOneMerge( defer kept_anc.deinit(w.gpa); for (list.items) |e| { const is_ancestor = for (list.items) |o| { - if (o.commit != e.commit and try w.reaches(o.commit, e.commit, in_list, reach, true)) break true; + if (o.commit != e.commit and try w.reaches(o.commit, e.commit, in_list, reach)) break true; } else false; - // With exclusions, git was seen to reckon ancestry through a - // commit --remove-empty made a root by rules not matched. - if (!is_ancestor and w.options.simplify.?.remove_empty and w.excluding()) { - for (list.items) |o| { - if (o.commit != e.commit and try w.reaches(o.commit, e.commit, in_list, reach, false)) - return error.UnsupportedRemoveEmptyAncestry; - } - } if (!is_ancestor) try kept_anc.append(w.gpa, e); } try protect(w.gpa, list.items, &kept_anc, true); @@ -1850,7 +1849,17 @@ fn simplifyOneMerge( var before_protect: std.ArrayList(Parent) = .empty; defer before_protect.deinit(w.gpa); try before_protect.appendSlice(w.gpa, step2.items); - try protect(w.gpa, list.items, &step2, ts_after_anc); + // Left none, a merge keeps the first parent it is TREESAME to all + // the same, TREESAME or not, as git was seen to keep it. That matters + // where such a parent is a root --remove-empty made that holds what + // the pathspec matches but changed none of it, which its Bloom filter + // takes for TREESAME to the empty tree: the merge, TREESAME to it, is + // replaced by it. Kept all the same (a pull), it holds that root but, + // as git drops it as it shows the merge, shows none (`left_hidden`). + const left_none = step2.items.len == 0 and !ts_after_anc; + try protect(w.gpa, list.items, &step2, ts_after_anc or left_none); + const left_root: ?Index = if (left_none and step2.items.len == 1 and !w.options.simplify.?.sparse and + in_list.contains(step2.items[0].commit) and w.isTreesameRoot(step2.items[0].commit)) step2.items[0].commit else null; // The parent that put back, which a merge `--show-pulls` keeps for // show without it is still diffed against under `--full-diff`. const put_back: ?Index = for (step2.items) |e| { @@ -2063,12 +2072,16 @@ fn simplifyOneMerge( if (e.same) break e.commit; } else null; } + // The root a merge left none holds but does not show, as above; one + // listed anyway already shows the parents it had before that. + const left_hidden: ?Index = if (through_shown) null else left_root; if (kept or st.pull or anyway) { st.replacement = c; st.remains = true; var commits: std.ArrayList(Index) = .empty; defer commits.deinit(w.gpa); - for (step2.items) |e| try commits.append(w.gpa, e.commit); + for (step2.items) |e| if (left_hidden != e.commit) try commits.append(w.gpa, e.commit); + const shown_len = commits.items.len; try commits.appendSlice(w.gpa, later.items); const held_sp = try w.storeSpan(commits.items); // A pull shown as the parent another parent is shows it once (with @@ -2098,38 +2111,32 @@ fn simplifyOneMerge( var held: std.ArrayList(Index) = .empty; defer held.deinit(w.gpa); for (list.items) |e| { - const shown = for (step2.items) |k| { + // The root a merge left none holds is diffed against and held + // as one it lost. + const hidden = left_hidden == e.commit; + const shown = !hidden and for (step2.items) |k| { if (k.commit == e.commit) break true; } else false; - const dropped_root = for (step1.items) |k| { + const dropped_root = hidden or for (step1.items) |k| { if (k.commit == e.commit) break false; } else true; const is_ancestor = false; - // A root --remove-empty made (rather than a true one) is - // counted and diffed against only when the commit shows no - // parent; a merge losing one, git was seen to count and diff - // by rules not matched, where those matter. - const made_root = w.store.parents(e.commit).len != 0 and !w.peekState(e.commit).as_root; - if (dropped_root and made_root and list.items.len > 1) { - const o = w.options; - if (step2.items.len == 0 or o.min_parents != 0 or o.max_parents != null or w.options.simplify.?.full_diff) - return error.UnsupportedRemoveEmptyMerge; - } // With --sparse, git was seen to diff against the parents shown // alone, when there are any. // Left others, none it is TREESAME to, it is diffed against - // the one git holds in their place, however that was lost. - const lost = if (step2.items.len == 0) + // the one git holds in their place, however that was lost, + // a root --remove-empty made as much as a true one. + const lost = if (shown_len == 0) dropped_root and !is_ancestor and (list.items.len == 1 or e.same) else - !made_root and !w.options.simplify.?.sparse and (restored == e.commit or (by_held and held_first.?.commit == e.commit) or (through_shown and put_back == e.commit)); + !w.options.simplify.?.sparse and (restored == e.commit or (by_held and held_first.?.commit == e.commit) or (through_shown and put_back == e.commit)); if (shown or lost) try commits.append(w.gpa, e.commit); // What git holds loses the TREESAME roots, unless nothing else // is left: a merge taken for a root holds none; a merge left // none holds only those it is TREESAME to; one left others, // decorated roots included, only the first parent it is // TREESAME to, when it shows none. - const held_root = dropped_root and !is_ancestor and step2.items.len == 0 and (list.items.len == 1 or e.same); + const held_root = dropped_root and !is_ancestor and shown_len == 0 and (list.items.len == 1 or e.same); if (!as_root and (shown or held_root or restored == e.commit or anyway_restored == e.commit)) try held.append(w.gpa, e.commit); } // Under --full-diff, with --first-parent, the later parents are @@ -2148,6 +2155,7 @@ fn simplifyOneMerge( st2.rewritten = true; st2.anyway_only = through_shown; st2.merged_treesame = recomputed; + st2.holds_hidden = left_hidden != null; } else { st.replacement = step2.items[0].commit; st.remains = false; @@ -2185,7 +2193,11 @@ fn shownFor(w: *const RevWalk, p: Index) ?Index { x = st.anyway_through; } const st = w.peekState(x); - if (!st.pull_only or !st.replaced or !st.remains or !st.rewritten or st.rewritten_parents.len != 1 or st.boundary_parents.len != 1) return x; + if (!st.pull_only or !st.replaced or !st.remains or !st.rewritten) return x; + // Holding as its one parent a root TREESAME to the empty tree it does + // not show, it is shown as that is: as nothing. + if (st.holds_hidden) return null; + if (st.rewritten_parents.len != 1 or st.boundary_parents.len != 1) return x; return w.span(st.rewritten_parents)[0]; } @@ -2278,7 +2290,11 @@ fn levelOf(w: *RevWalk, r: *Reach, c: Index) Error!u32 { return r.levels.items[c]; } -/// Whether `ancestor` is reachable from `from` through real parents. +/// Whether `ancestor` is reachable from `from` through real parents, +/// none of them those of a commit `--remove-empty` made a root, whenever +/// it made it one: so that, with exclusions, an excluded commit it made +/// a root relates none of a merge's parents to another, as git was seen +/// to reckon it. /// /// With `--ancestry-path`, with exclusions, or with `--since` (which /// excludes the commits older than it) it is ancestry as simplification @@ -2298,8 +2314,6 @@ fn reaches( ancestor: Index, in_list: *const std.AutoHashMapUnmanaged(Index, void), r: *Reach, - /// Whether a commit `--remove-empty` made a root has no parents. - cleared_roots: bool, ) Error!bool { const simplified = w.off_path.count() != 0 or w.excluding() or w.options.since != null; const floor = try w.levelOf(r, ancestor); @@ -2314,8 +2328,10 @@ fn reaches( var one: [1]Index = undefined; while (r.stack.pop()) |x| { w.work.ancestry_steps += 1; - // A commit --remove-empty made a root has no parents for git. - const onward: []const Index = if ((cleared_roots and w.peekState(x).cleared) or w.peekState(x).as_root) &.{} else if (simplified and in_list.contains(x)) blk: { + // A commit --remove-empty made a root has no parents for git, + // even one made a root only once it was simplified, its parents + // queued already. + const onward: []const Index = if (w.peekState(x).cleared or w.peekState(x).as_root) &.{} else if (simplified and in_list.contains(x)) blk: { const st = w.peekState(x); if (!st.replaced) break :blk w.store.parents(x); if (st.remains) break :blk w.span(st.merged_parents); diff --git a/tools/history.zig b/tools/history.zig index 33f5f1a..7c011e5 100644 --- a/tools/history.zig +++ b/tools/history.zig @@ -109,9 +109,7 @@ pub const History = struct { error.GenerationOverflowTooSmall => return tool.fatalMessage(stderr, "commit-graph overflow generation data is too small", .{}), 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.UnsupportedFirstParentExclusion => return tool.fatalMessage(stderr, "zig-git {s}: --first-parent with paths in a symmetric difference is not supported where a merge's first parent is excluded later than git's merge-base search may have found it", .{h.command}), - error.UnsupportedRemoveEmptyAncestry => return tool.fatalMessage(stderr, "zig-git {s}: --remove-empty with --simplify-merges or --simplify-by-decoration and an excluded commit is not supported where a merge's parents are related through a commit --remove-empty made a root", .{h.command}), error.UnsupportedRewriteUnexplored => return tool.fatalMessage(stderr, "zig-git {s}: rewriting parents with paths and an ordering in a repository with a commit-graph is not supported where it reaches a commit before the walk has learned whether an exclusion reaches it", .{h.command}), - error.UnsupportedRemoveEmptyMerge => return tool.fatalMessage(stderr, "zig-git {s}: --remove-empty with --simplify-merges or --simplify-by-decoration is not supported where a merge loses a parent --remove-empty made a root", .{h.command}), error.NotATree, error.TruncatedTree, error.MalformedMode, error.EmptyName => return tool.fatalMessage(stderr, "zig-git: cannot read a tree: {f}", .{tool.errorWords(err)}), else => return tool.fatalMessage(stderr, "zig-git: cannot read a commit: {f}", .{tool.errorWords(err)}), } diff --git a/tools/simplify_tests.zig b/tools/simplify_tests.zig index e9ae5d7..6f8dc4a 100644 --- a/tools/simplify_tests.zig +++ b/tools/simplify_tests.zig @@ -32,8 +32,6 @@ fn sameIn(repo: *Repo, cwd: []const u8, args: []const []const u8) !void { /// answer depends on what the walk does not follow. const conditional_refusals = [_][]const u8{ "--first-parent with paths in a symmetric difference is not supported", - "--remove-empty with --simplify-merges or --simplify-by-decoration is not supported where a merge loses a parent --remove-empty made a root", - "is not supported where a merge's parents are related through a commit --remove-empty made a root", "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", @@ -3237,3 +3235,79 @@ test "lw4: a commit named through a tag is read, and takes the tag's exclusion, } } } + +test "re: --full-diff diffs a merge against a root --remove-empty made that it holds as the first parent it is TREESAME to" { + // c2 adds f to c1, which holds none: --remove-empty makes c1 a root. + // c3, which holds no f, is TREESAME to c1 and not to c2; it keeps c2 + // and loses c1, a root TREESAME to the empty tree, but holds it still + // as the first parent it is TREESAME to: it counts it, and + // --full-diff diffs c3 against it as against c2, as git does for a + // true root it holds so. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "f=1", .time = 100 }, + .{ .files = "x=1", .parents = &.{0}, .time = 200 }, + .{ .files = "x=1 f=2", .parents = &.{1}, .time = 300 }, + .{ .files = "x=1 y=1", .parents = &.{ 2, 1 }, .time = 400 }, + }, true); + for ([_][]const []const u8{ + &.{ "log", "--format=%s %p", "--raw", "--full-diff", "-m", "--simplify-merges", "--remove-empty", "main", "--", "f" }, + &.{ "log", "--format=%s %p", "--raw", "--full-diff", "--cc", "--simplify-merges", "--remove-empty", "main", "--", "f" }, + &.{ "log", "--format=%s %p", "--stat", "--full-diff", "--simplify-merges", "--remove-empty", "main", "--", "f" }, + &.{ "rev-list", "--parents", "--min-parents=2", "--simplify-merges", "--remove-empty", "main", "--", "f" }, + &.{ "rev-list", "--parents", "--max-parents=1", "--simplify-merges", "--remove-empty", "main", "--", "f" }, + &.{ "rev-list", "--parents", "--boundary", "--simplify-merges", "--remove-empty", "main", "^c2", "--", "f" }, + }) |a| try same(&repo, a); +} + +test "re: --simplify-merges keeps the first parent a merge left no parent is TREESAME to" { + // c2 only adds g to c1, whose Bloom filter rules f and g out: + // --remove-empty makes c1 a root, which it takes for TREESAME to the + // empty tree though it holds f. c4 differs from c3, a root holding + // neither, and is TREESAME to c1: losing both as roots TREESAME to + // the empty tree, it keeps c1 all the same, not being TREESAME, and + // is replaced by it, so that c5 loses it as an ancestor of c2 and is + // replaced by c2. With c1 not its only parent kept, a merge counts + // c1 among its parents but does not show it. + for ([_][]const u8{ "x=6", "f=2" }) |other| { + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "f=1", .time = 100 }, + .{ .files = "f=1 y=1", .parents = &.{0}, .time = 200 }, + .{ .files = "f=1 y=1 g=1", .parents = &.{1}, .time = 300 }, + .{ .files = other, .time = 400 }, + .{ .files = "x=6 f=1", .parents = &.{ 3, 1 }, .time = 500 }, + .{ .files = "f=1 y=1 g=1", .parents = &.{ 2, 4 }, .time = 600 }, + }, true); + try writeFilters(&repo); + for ([_][]const []const u8{ + &.{ "rev-list", "--parents", "--simplify-merges", "--remove-empty", "main", "--", "f", "g" }, + &.{ "rev-list", "--parents", "--simplify-merges", "--remove-empty", "--min-parents=2", "main", "--", "f", "g" }, + &.{ "rev-list", "--parents", "--simplify-merges", "--remove-empty", "--boundary", "main", "^c3", "--", "f", "g" }, + &.{ "rev-list", "--parents", "--simplify-merges", "--remove-empty", "--full-history", "main", "--", "f", "g" }, + &.{ "log", "--format=%s %p", "--raw", "--full-diff", "-m", "--simplify-merges", "--remove-empty", "main", "--", "f", "g" }, + &.{ "log", "--format=%s %p", "--name-status", "--simplify-merges", "--show-pulls", "--remove-empty", "main", "--", "f", "g" }, + }) |a| try same(&repo, a); + } +} + +test "re: with exclusions, ancestry goes through no parent of a commit --remove-empty made a root" { + // c2 adds s to c1 and c0, which hold none: --remove-empty makes both + // roots, though c0 is c1's parent. With both excluded, git does not + // reckon c0 an ancestor of c1, and c2 keeps both. + var repo = try Repo.init(testing.allocator, testing.io); + defer repo.deinit(); + try importDated(&repo, &.{ + .{ .files = "x=1", .time = 100 }, + .{ .files = "x=2", .parents = &.{0}, .time = 200 }, + .{ .files = "x=2 s=1", .parents = &.{ 1, 0 }, .time = 300 }, + }, true); + for ([_][]const []const u8{ + &.{ "rev-list", "--parents", "--simplify-merges", "--remove-empty", "main", "^c1", "^c0", "--", "s" }, + &.{ "rev-list", "--parents", "--boundary", "--simplify-merges", "--remove-empty", "main", "^c1", "^c0", "--", "s" }, + &.{ "rev-list", "--parents", "--min-parents=2", "--simplify-merges", "--remove-empty", "main", "^c1", "^c0", "--", "s" }, + &.{ "log", "--format=%s %p", "--raw", "--full-diff", "-m", "--simplify-merges", "--remove-empty", "main", "^c1", "^c0", "--", "s" }, + }) |a| try same(&repo, a); +}