Git designed for AI agents.
revrange: a fixed number of processes per range, no peeling
Federico Elia6aecff10bb4e
diff --git a/doc/graft/LAND-BATCHING.md b/doc/graft/LAND-BATCHING.mdindex f0c363b..ab13e09 100644--- a/doc/graft/LAND-BATCHING.md+++ b/doc/graft/LAND-BATCHING.md@@ -101,19 +101,21 @@ short git process, and one more when it diverges from the tip:otherwise it diverges. Both facts are exact even when committer dates areskewed. A skewed history can make `git rev-list` list commits that the tipalready has (its walk stops early when the commits it still has to visit are- older than those it listed), never fewer. So a diverging change has its- listing made exact first (`revrange.Refine`): one- `git merge-base --is-ancestor` per bottom of the listing (a listed commit- none of whose parents is listed), usually one. If a bottom is reachable from- the tip, the listing held extra commits: it is listed again below the exact- merge bases (`git merge-base --all`), and the bottoms that the tip reaches- are removed one check at a time (counted by- `graft_revrange_skewed_listings_total` and- `graft_revrange_skew_checks_total`). For the rebase strategy, a merge commit- in the exact range refuses the change (`InvalidArgument`, use- `STRATEGY_MERGE`), a range that ends at a root commit means unrelated- history (`InvalidArgument`), and otherwise its commits, oldest first, are- the commits to replay.+ older than those it listed), never fewer. So for a diverging change one+ `git merge-base --independent tip <bottoms>` tells whether the listing is+ exact: it drops exactly the bottoms of the listing (listed commits none of+ whose parents is listed) that the tip reaches, and the listing has extra+ commits exactly when one of them is dropped. Without skew that is the only+ extra process. With skew (counted by `graft_revrange_skewed_listings_total`)+ `git merge-base --all` decides: the change is already landed when its only+ merge base is the commit itself; otherwise one listing below the merge+ bases either is a chain of commits with one parent each down to the only+ merge base, which is then the exact range, or the range has a merge commit.+ A land therefore costs at most four git processes, whatever the size of the+ history. For the rebase strategy, a merge commit in the range refuses the+ change (`InvalidArgument`, use `STRATEGY_MERGE`), a range that ends at a root+ commit means unrelated history (`InvalidArgument`), and otherwise its+ commits, oldest first, are the commits to replay.3. The commits are replayed by the `rebase.Engine` of the attempt, which keepsone `git merge-tree --stdin` process and one `git hash-object --stdin-paths`process running for the whole attempt. The replay has the rules, errors anddiff --git a/internal/git/localrepo/rebase.go b/internal/git/localrepo/rebase.goindex 627c3e0..28dbdd5 100644--- a/internal/git/localrepo/rebase.go+++ b/internal/git/localrepo/rebase.go@@ -105,10 +105,20 @@ func (repo *Repo) Rebase(ctx context.Context, upstream, branch string, options .// that is not on the side of upstream. The listing therefore also excludes the boundaries of both// sides, computed exactly: every path from either tip into the shared history runs through one of// them, so the walk lists exactly the commits of each side.+//+// Both sides come from one listing (revrange.Sides), and one question for the bottoms of each side+// tells whether it is exact (revrange.Refine): without skew that is two or three processes more+// than the listing alone, whatever the size of the history. A skewed side costs one more process,+// which walks the history of the other tip.func (repo *Repo) rebaseTodo(ctx context.Context, upstreamOID, branchOID git.ObjectID, reapplyCherryPicks bool) ([]string, error) {walker := revrange.Git(repo)- branchSide, err := revrange.Exact(ctx, walker, upstreamOID, branchOID)+ upstreamSide, branchSide, err := revrange.Sides(ctx, repo, upstreamOID, branchOID)+ if err != nil {+ return nil, structerr.NewInternal("listing the commits of both sides: %w", err)+ }++ branchSide, err = revrange.Refine(ctx, walker, upstreamOID, branchSide)if err != nil {return nil, structerr.NewInternal("listing the commits of the branch: %w", err)}@@ -116,7 +126,7 @@ func (repo *Repo) rebaseTodo(ctx context.Context, upstreamOID, branchOID git.Obj// The side of upstream only matters to --cherry-pick, which compares the branch with it.if !reapplyCherryPicks {- upstreamSide, err := revrange.Exact(ctx, walker, branchOID, upstreamOID)+ upstreamSide, err = revrange.Refine(ctx, walker, branchOID, upstreamSide)if err != nil {return nil, structerr.NewInternal("listing the commits of upstream: %w", err)}diff --git a/internal/git/localrepo/rebase_skew_test.go b/internal/git/localrepo/rebase_skew_test.goindex a139da1..726b96d 100644--- a/internal/git/localrepo/rebase_skew_test.go+++ b/internal/git/localrepo/rebase_skew_test.go@@ -9,6 +9,8 @@ import ("github.com/feder-cr/git-ai/internal/git""github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/git/revrange"+ "github.com/feder-cr/git-ai/internal/log""github.com/feder-cr/git-ai/internal/testhelper""github.com/feder-cr/git-ai/internal/testhelper/testcfg""github.com/stretchr/testify/require"@@ -153,3 +155,186 @@ func equalSets(a, b map[int]bool) bool {}return true}++// TestRebaseTodoKeepsAReappliedCommitUnderSkewedDates rebases a branch whose commit X adds a file+// again, after the shared history added it (m10) and deleted it (m20), onto an upstream that merged+// the shared history in through a path git rev-list walks before the merge base marks it. The main+// line m0..m59 is followed by ten commits dated a year before it, then M; the branch is X and T on+// M; upstream is H, which merges M into F, a fork of m50. git rev-list --left-right H...T then+// lists m0..m50 on the side of upstream, although the branch has them, and with --cherry-pick it+// drops X as equivalent to m10. X is not equivalent to any commit that only upstream has, so the+// todo list keeps it; without the exact boundary of the side of upstream it would not.+func TestRebaseTodoKeepsAReappliedCommitUnderSkewedDates(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ cfg := testcfg.Build(t)+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})+ repo := NewTestRepo(t, cfg, repoProto)++ type spec struct {+ name string+ parents []int+ date int64+ ops []string+ }+ base := int64(1767225600)+ var specs []spec+ add := func(s spec) int {+ specs = append(specs, s)+ return len(specs) - 1+ }++ prev := -1+ for i := 0; i < 60; i++ {+ ops := []string{fmt.Sprintf("M 100644 inline m%d\ndata 3\nm%d\n", i, i%10)}+ switch i {+ case 10:+ ops = []string{"M 100644 inline f\ndata 3\nre\n"}+ case 20:+ ops = []string{"D f\n"}+ }+ var parents []int+ if prev >= 0 {+ parents = []int{prev}+ }+ prev = add(spec{name: fmt.Sprintf("m%d", i), parents: parents, date: base + int64(i)*60, ops: ops})+ }+ m50 := 50+ for j := 0; j < 10; j++ {+ prev = add(spec{name: fmt.Sprintf("old%d", j), parents: []int{prev}, date: 1735689600 + int64(j),+ ops: []string{fmt.Sprintf("M 100644 inline old%d\ndata 2\no\n", j)}})+ }+ now := base + 60*60 + 1000+ m := add(spec{name: "M", parents: []int{prev}, date: now, ops: []string{"M 100644 inline M\ndata 2\nM\n"}})+ x := add(spec{name: "X", parents: []int{m}, date: now + 30, ops: []string{"M 100644 inline f\ndata 3\nre\n"}})+ tip := add(spec{name: "T", parents: []int{x}, date: now + 60, ops: []string{"M 100644 inline T\ndata 2\nT\n"}})+ f := add(spec{name: "F", parents: []int{m50}, date: now + 120, ops: []string{"M 100644 inline F\ndata 2\nF\n"}})+ h := add(spec{name: "H", parents: []int{f, m}, date: now + 180})++ var stream bytes.Buffer+ for i, s := range specs {+ fmt.Fprintf(&stream, "commit refs/heads/%s\nmark :%d\n", s.name, i+1)+ fmt.Fprintf(&stream, "committer Tester <[email protected]> %d +0000\n", s.date)+ fmt.Fprintf(&stream, "data %d\n%s\n", len(s.name), s.name)+ for j, parent := range s.parents {+ if j == 0 {+ fmt.Fprintf(&stream, "from :%d\n", parent+1)+ } else {+ fmt.Fprintf(&stream, "merge :%d\n", parent+1)+ }+ }+ for _, op := range s.ops {+ stream.WriteString(op)+ }+ stream.WriteString("\n")+ }+ gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: &stream}, "-C", repoPath, "fast-import", "--quiet")++ resolve := func(i int) git.ObjectID {+ return gittest.ResolveRevision(t, cfg, repoPath, "refs/heads/"+specs[i].name)+ }+ upstream, branch, xID, tipID := resolve(h), resolve(tip), resolve(x), resolve(tip)++ // The skew this test is about: git lists m10 on the side of upstream and drops X.+ listed := strings.Fields(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--left-only",+ upstream.String()+"..."+branch.String())))+ require.Contains(t, listed, resolve(10).String(), "git rev-list no longer shows the skew this test is about")+ dropped := strings.Fields(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--cherry-pick", "--right-only",+ "--no-merges", "--topo-order", "--reverse", upstream.String()+"..."+branch.String())))+ require.Equal(t, []string{tipID.String()}, dropped, "git rev-list --cherry-pick no longer drops X")++ for _, reapply := range []bool{false, true} {+ todo, err := repo.rebaseTodo(ctx, upstream, branch, reapply)+ require.NoError(t, err)+ require.Equal(t, []string{xID.String(), tipID.String()}, todo, "reapply %v", reapply)+ }+}++// TestRebaseTodoCostsAFixedNumberOfProcesses lists the todo of a branch and an upstream that both+// merge a dozen commits of the shared history, so that each side joins it at a dozen bottoms. With+// growing dates the listings are exact, and the todo list costs the listing of both sides, one+// question for the bottoms of each side that matters, and the final listing: four processes with+// --cherry-pick, three without, however many bottoms the sides have.+func TestRebaseTodoCostsAFixedNumberOfProcesses(t *testing.T) {+ t.Parallel()++ ctx := log.InitContextCustomFields(testhelper.Context(t))+ cfg := testcfg.Build(t)+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})+ repo := NewTestRepo(t, cfg, repoProto)++ var stream bytes.Buffer+ mark := 0+ commit := func(name string, date int64, parents ...int) int {+ mark+++ fmt.Fprintf(&stream, "commit refs/heads/%s\nmark :%d\ncommitter Tester <[email protected]> %d +0000\n", name, mark, date)+ fmt.Fprintf(&stream, "data %d\n%s\n", len(name), name)+ for j, parent := range parents {+ if j == 0 {+ fmt.Fprintf(&stream, "from :%d\n", parent)+ } else {+ fmt.Fprintf(&stream, "merge :%d\n", parent)+ }+ }+ fmt.Fprintf(&stream, "M 100644 inline %s\ndata %d\n%s\n\n", name, len(name)+1, name+"\n")+ return mark+ }++ date := int64(1767225600)+ var main []int+ for i := 0; i < 40; i++ {+ var parents []int+ if i > 0 {+ parents = []int{main[i-1]}+ }+ date += 60+ main = append(main, commit(fmt.Sprintf("m%d", i), date, parents...))+ }++ // Side lines fork from the shared history and are merged into both sides, a dozen times.+ upstream, branch := main[27], main[27]+ for i := 0; i < 12; i++ {+ date += 60+ side := commit(fmt.Sprintf("s%d", i), date, main[2*i])+ date += 60+ upstream = commit(fmt.Sprintf("u%d", i), date, upstream, side)+ date += 60+ branch = commit(fmt.Sprintf("b%d", i), date, branch, main[2*i+1])+ }+ date += 60+ commit("upstream", date, upstream)+ date += 60+ commit("branch", date, branch)+ gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: &stream}, "-C", repoPath, "fast-import", "--quiet")++ upstreamID := gittest.ResolveRevision(t, cfg, repoPath, "refs/heads/upstream")+ branchID := gittest.ResolveRevision(t, cfg, repoPath, "refs/heads/branch")++ leftSide, rightSide, err := revrange.Sides(ctx, repo, upstreamID, branchID)+ require.NoError(t, err)+ leftBottoms, err := revrange.Bottoms(leftSide)+ require.NoError(t, err)+ rightBottoms, err := revrange.Bottoms(rightSide)+ require.NoError(t, err)+ require.GreaterOrEqual(t, len(leftBottoms)+len(rightBottoms), 12, "the sides must join the shared history at many bottoms")++ processes := func() int {+ count, _ := log.CustomFieldsFromContext(ctx).Fields()["command.count"].(int)+ return count+ }++ for _, tc := range []struct {+ reapply bool+ wantProcesses int+ }{+ {reapply: false, wantProcesses: 4},+ {reapply: true, wantProcesses: 3},+ } {+ before := processes()+ todo, err := repo.rebaseTodo(ctx, upstreamID, branchID, tc.reapply)+ require.NoError(t, err)+ require.NotEmpty(t, todo)+ require.Equal(t, tc.wantProcesses, processes()-before, "reapply %v with %d and %d bottoms", tc.reapply, len(leftBottoms), len(rightBottoms))+ }+}diff --git a/internal/git/revrange/revrange.go b/internal/git/revrange/revrange.goindex 477af06..626df85 100644--- a/internal/git/revrange/revrange.go+++ b/internal/git/revrange/revrange.go@@ -10,23 +10,27 @@// listing never misses a commit of the range, because only the ancestors of base are ever marked// as excluded, but it may hold extra commits, and every one of them is an ancestor of base.//-// Exact builds the exact range from such a listing with exact primitives (git merge-base-// --is-ancestor and git merge-base --all, which walk until the answer is certain). It relies on-// two facts about the extra commits X of a listing L of head excluding base:+// Refine makes such a listing exact with a fixed number of git processes, whatever the size of the+// history. It relies on three facts about a listing L of head that excludes base (or commits that+// base reaches), whose extra commits are X://// - X is closed downwards in L: the parents of an extra commit are ancestors of base too, so// those of them that are in L are extra as well.// - Hence X is not empty exactly when some bottom of L, a commit of L none of whose parents is in// L, is an ancestor of base: going down from an extra commit through parents that are in L// ends at a bottom, which is extra.+// - git merge-base --independent base b1 ... bn drops exactly the bottoms that base reaches. It+// drops a commit that base or another of the commits reaches. If a bottom bi is reached by+// another bottom bj, the path from bj to bi runs through a parent of bj that L does not list,+// which is therefore an ancestor of base, so base reaches bi too.//-// Without skew a listing has one bottom per point where the range joins the history of base,-// usually one, and checking the bottoms costs one git process each. Under skew Exact lists again-// below the exact merge bases, which bound the walk on every path that runs through them, and then-// removes the remaining extra commits bottom by bottom with one check each.+// So one process tells whether the listing is exact. When it is not, one git rev-list base, which+// excludes nothing and is therefore exact, lists every ancestor of base, and the commits of the+// listing among them are removed: work linear in the history of base, in one process.package revrangeimport (+ "bufio""bytes""context""errors"@@ -45,15 +49,21 @@ var (skewedListingsTotal = promauto.NewCounter(prometheus.CounterOpts{Name: "graft_revrange_skewed_listings_total",Help: "Listings of a revision range that held commits outside of the range, which happens when " +- "committer dates are skewed, and that were made exact.",+ "committer dates are skewed.",})- skewChecksTotal = promauto.NewCounter(prometheus.CounterOpts{- Name: "graft_revrange_skew_checks_total",- Help: "Ancestry checks spent removing the commits outside of the range from skewed listings.",+ skewExtraCommitsTotal = promauto.NewCounter(prometheus.CounterOpts{+ Name: "graft_revrange_skew_extra_commits_total",+ Help: "Commits outside of the range that skewed listings held and that were removed.",}))+// CountSkewedListing counts a listing that held commits outside of its range, for callers that+// handle such a listing themselves (see rebase.Plan).+func CountSkewedListing() {+ skewedListingsTotal.Inc()+}+// Commit is a commit of a range with all of its parents in order, as git rev-list --parents prints// it.type Commit struct {@@ -61,19 +71,23 @@ type Commit struct {Parents []git.ObjectID}-// Walker answers the questions that Exact builds a range from.+// Walker answers the questions that Refine builds a range from.type Walker interface {// List returns every commit that is reachable from head and from none of the commits of// exclude, each with all of its parents, in no particular order. The listing may also hold// commits that are reachable from a commit of exclude (see the package documentation), never- // fewer commits. Only Exact and Refine make it exact.+ // fewer commits. Only Refine makes it exact.List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]Commit, error)- // IsAncestor reports whether ancestor is reachable from descendant, the same commit included.- // The answer is exact.- IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error)// MergeBases returns the best common ancestors of a and b, as git merge-base --all prints them,// and none when a and b share no history. The answer is exact.MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error)+ // ReachedBottoms returns the commits of bottoms that base reaches. bottoms must be the bottoms+ // of a listing that excludes base or commits that base reaches (see Bottoms), which lets one+ // question answer for all of them. The answer is exact.+ ReachedBottoms(ctx context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error)+ // Reachable returns the commits of commits that base reaches, for any commits. It may walk the+ // whole history of base. The answer is exact.+ Reachable(ctx context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error)}// Exact returns the commits reachable from head and not from base, exactly, in no particular order.@@ -84,95 +98,78 @@ func Exact(ctx context.Context, w Walker, base, head git.ObjectID) ([]Commit, erreturn nil, err}- return Refine(ctx, w, base, head, listed)+ return Refine(ctx, w, base, listed)}-// Refine returns the exact range of the commits reachable from head and not from base, given-// listed, which w.List returned for head excluding base.-//-// When the listing is exact, which it is unless committer dates are skewed, Refine checks each of-// its bottoms once. Otherwise it lists again below the merge bases of base and head and removes the-// commits of that listing that base reaches, checking one bottom at a time: at most one check for-// each such commit plus one for each bottom of the range.-func Refine(ctx context.Context, w Walker, base, head git.ObjectID, listed []Commit) ([]Commit, error) {+// Refine returns the exact range of a listing: the commits of listed that base does not reach.+// listed must be a listing of a commit that excludes base or commits that base reaches (Walker.List,+// or one side of a symmetric difference with base on the other side). Without skew that costs one+// question for all the bottoms of the listing; otherwise one more, which may walk the history of+// base.+func Refine(ctx context.Context, w Walker, base git.ObjectID, listed []Commit) ([]Commit, error) {if len(listed) == 0 {return nil, nil}- rng, err := newRangeSet(listed)+ bottoms, err := Bottoms(listed)if err != nil {- return nil, fmt.Errorf("listing of %s excluding %s: %w", head, base, err)+ return nil, err}- extra := false- for _, bottom := range rng.bottoms() {- reachable, err := w.IsAncestor(ctx, bottom, base)- if err != nil {- return nil, err- }- if reachable {- extra = true- break- }+ reached, err := w.ReachedBottoms(ctx, base, bottoms)+ if err != nil {+ return nil, err}- if !extra {+ if len(reached) == 0 {return listed, nil}skewedListingsTotal.Inc()- // Every common ancestor of base and head is an ancestor of one of their merge bases, so the- // range is also what head reaches without reaching a merge base, and a walk that excludes the- // merge bases stops at them.- bases, err := w.MergeBases(ctx, base, head)- if err != nil {- return nil, err- }- if len(bases) == 0 {- // A bottom of the listing is an ancestor of base, and every commit of the listing is an- // ancestor of head, so base and head share history.- return nil, fmt.Errorf("%s and %s have no merge base although they share history", base, head)+ ids := make([]git.ObjectID, 0, len(listed))+ for _, c := range listed {+ ids = append(ids, c.ID)}-- relisted, err := w.List(ctx, head, bases)+ extra, err := w.Reachable(ctx, base, ids)if err != nil {return nil, err}+ skewExtraCommitsTotal.Add(float64(len(extra)))- rng, err = newRangeSet(relisted)- if err != nil {- return nil, fmt.Errorf("listing of %s excluding the merge bases: %w", head, err)+ rng := make([]Commit, 0, len(listed)-len(extra))+ for _, c := range listed {+ if !extra[c.ID] {+ rng = append(rng, c)+ }}+ return rng, nil+}- // A bottom that base does not reach is in the range for good: the commits of the range are- // exactly those that base does not reach, whatever else the walk removes.- inRange := make(map[git.ObjectID]bool)- checks := 0- for removed := true; removed; {- removed = false- for _, bottom := range rng.bottoms() {- if inRange[bottom] {- continue- }-- checks++- reachable, err := w.IsAncestor(ctx, bottom, base)- if err != nil {- skewChecksTotal.Add(float64(checks))- return nil, err- }+// Bottoms returns the commits of listed none of whose parents is in listed, in listing order. It+// returns an error when a commit is listed twice.+func Bottoms(listed []Commit) ([]git.ObjectID, error) {+ in := make(map[git.ObjectID]bool, len(listed))+ for _, c := range listed {+ if in[c.ID] {+ return nil, fmt.Errorf("commit %s listed twice", c.ID)+ }+ in[c.ID] = true+ }- if reachable {- rng.remove(bottom)- removed = true- } else {- inRange[bottom] = true+ var bottoms []git.ObjectID+ for _, c := range listed {+ bottom := true+ for _, parent := range c.Parents {+ if in[parent] {+ bottom = false+ break}}+ if bottom {+ bottoms = append(bottoms, c.ID)+ }}- skewChecksTotal.Add(float64(checks))-- return rng.commits(), nil+ return bottoms, nil}// Boundary returns the parents of the commits of rng that are not themselves in rng, sorted. For@@ -200,62 +197,28 @@ func Boundary(rng []Commit) []git.ObjectID {return boundary}-// rangeSet is a listing of commits that Refine removes commits from.-type rangeSet struct {- // order is the order of the listing.- order []git.ObjectID- // byID holds the commits that are still in the set.- byID map[git.ObjectID]Commit-}--func newRangeSet(listed []Commit) (*rangeSet, error) {- s := &rangeSet{byID: make(map[git.ObjectID]Commit, len(listed))}- for _, c := range listed {- if _, ok := s.byID[c.ID]; ok {- return nil, fmt.Errorf("commit %s listed twice", c.ID)- }- s.byID[c.ID] = c- s.order = append(s.order, c.ID)- }- return s, nil-}--// bottoms returns the commits of the set none of whose parents is in the set, in listing order.-func (s *rangeSet) bottoms() []git.ObjectID {- var bottoms []git.ObjectID- for _, id := range s.order {- c, ok := s.byID[id]- if !ok {- continue- }-- bottom := true- for _, parent := range c.Parents {- if _, ok := s.byID[parent]; ok {- bottom = false- break+// Sides lists the symmetric difference left...right with one git rev-list --left-right --parents:+// the commits that git lists on the side of left and those it lists on the side of right. Each side+// is a listing that excludes commits the other tip reaches (the merge bases), so Refine with the+// other tip as base makes it exact.+func Sides(ctx context.Context, repo gitcmd.RepositoryExecutor, left, right git.ObjectID) (leftSide, rightSide []Commit, _ error) {+ w := gitWalker{repo: repo}+ err := w.revListLines(ctx,+ []gitcmd.Option{gitcmd.Flag{Name: "--left-right"}, gitcmd.Flag{Name: "--parents"}},+ []string{left.String() + "..." + right.String()},+ func(line string, ids []git.ObjectID) error {+ c := Commit{ID: ids[0], Parents: ids[1:]}+ switch line[0] {+ case '<':+ leftSide = append(leftSide, c)+ case '>':+ rightSide = append(rightSide, c)+ default:+ return fmt.Errorf("rev-list --left-right printed %q", line)}- }- if bottom {- bottoms = append(bottoms, id)- }- }- return bottoms-}--func (s *rangeSet) remove(id git.ObjectID) {- delete(s.byID, id)-}--// commits returns the commits of the set in listing order.-func (s *rangeSet) commits() []Commit {- commits := make([]Commit, 0, len(s.byID))- for _, id := range s.order {- if c, ok := s.byID[id]; ok {- commits = append(commits, c)- }- }- return commits+ return nil+ })+ return leftSide, rightSide, err}// Git returns a Walker that runs git in repo for each question.@@ -269,97 +232,162 @@ type gitWalker struct {// List runs git rev-list --parents.func (w gitWalker) List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]Commit, error) {- objectHash, err := w.repo.ObjectHash(ctx)- if err != nil {- return nil, fmt.Errorf("detecting object hash: %w", err)- }-args := make([]string, 0, len(exclude)+1)args = append(args, head.String())for _, id := range exclude {args = append(args, "^"+id.String())}- var stdout, stderr bytes.Buffer- if err := w.repo.ExecAndWait(ctx,- gitcmd.Command{- Name: "rev-list",- Flags: []gitcmd.Option{gitcmd.Flag{Name: "--parents"}},- Args: args,- },- gitcmd.WithStdout(&stdout),- gitcmd.WithStderr(&stderr),- ); err != nil {- return nil, fmt.Errorf("rev-list: %w, stderr: %q", err, stderr.String())+ var commits []Commit+ err := w.revList(ctx, []gitcmd.Option{gitcmd.Flag{Name: "--parents"}}, args, func(fields []git.ObjectID) {+ commits = append(commits, Commit{ID: fields[0], Parents: fields[1:]})+ })+ return commits, err+}++// MergeBases runs git merge-base --all, which exits with 1 and prints nothing when a and b share no+// history.+func (w gitWalker) MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {+ stdout, empty, err := w.mergeBase(ctx, "--all", []string{a.String(), b.String()})+ if err != nil || empty {+ return nil, err}+ return stdout, nil+}- var commits []Commit- for _, line := range strings.Split(stdout.String(), "\n") {- fields := strings.Fields(line)- if len(fields) == 0 {- continue- }+// ReachedBottoms runs git merge-base --independent base bottoms...: it prints the commits that no+// other of its arguments reaches, so the bottoms it does not print are those that base reaches (see+// the package documentation).+func (w gitWalker) ReachedBottoms(ctx context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error) {+ if len(bottoms) == 0 {+ return nil, nil+ }- ids, err := parseIDs(objectHash, fields)- if err != nil {- return nil, fmt.Errorf("parsing rev-list output %q: %w", line, err)+ args := make([]string, 0, len(bottoms)+1)+ args = append(args, base.String())+ for _, id := range bottoms {+ args = append(args, id.String())+ }++ independent, _, err := w.mergeBase(ctx, "--independent", args)+ if err != nil {+ return nil, err+ }++ printed := make(map[git.ObjectID]bool, len(independent))+ for _, id := range independent {+ printed[id] = true+ }++ reached := map[git.ObjectID]bool{}+ for _, id := range bottoms {+ if !printed[id] {+ reached[id] = true}- commits = append(commits, Commit{ID: ids[0], Parents: ids[1:]})+ }+ return reached, nil+}++// Reachable runs git rev-list base, which excludes nothing and therefore lists every ancestor of+// base, and keeps the commits of commits among them.+func (w gitWalker) Reachable(ctx context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error) {+ wanted := make(map[git.ObjectID]bool, len(commits))+ for _, id := range commits {+ wanted[id] = true}- return commits, nil+ reached := map[git.ObjectID]bool{}+ err := w.revList(ctx, nil, []string{base.String()}, func(fields []git.ObjectID) {+ if wanted[fields[0]] {+ reached[fields[0]] = true+ }+ })+ return reached, err}-// IsAncestor runs git merge-base --is-ancestor, which exits with 1 when ancestor is not reachable-// from descendant.-func (w gitWalker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {+// revList runs git rev-list with flags and args, and calls fn with the object IDs of each line of+// its output, as it reads them.+func (w gitWalker) revList(ctx context.Context, flags []gitcmd.Option, args []string, fn func([]git.ObjectID)) error {+ return w.revListLines(ctx, flags, args, func(_ string, ids []git.ObjectID) error {+ fn(ids)+ return nil+ })+}++// revListLines runs git rev-list with flags and args, and calls fn with each line of its output and+// the object IDs of the line, without the side mark of --left-right, as it reads them.+func (w gitWalker) revListLines(ctx context.Context, flags []gitcmd.Option, args []string, fn func(string, []git.ObjectID) error) error {+ objectHash, err := w.repo.ObjectHash(ctx)+ if err != nil {+ return fmt.Errorf("detecting object hash: %w", err)+ }+var stderr bytes.Buffer- if err := w.repo.ExecAndWait(ctx,- gitcmd.Command{- Name: "merge-base",- Flags: []gitcmd.Option{gitcmd.Flag{Name: "--is-ancestor"}},- Args: []string{ancestor.String(), descendant.String()},- },- gitcmd.WithStderr(&stderr),- ); err != nil {- if status, ok := command.ExitStatus(err); ok && status == 1 && stderr.Len() == 0 {- return false, nil+ cmd, err := w.repo.Exec(ctx, gitcmd.Command{Name: "rev-list", Flags: flags, Args: args},+ gitcmd.WithStderr(&stderr), gitcmd.WithSetupStdout())+ if err != nil {+ return fmt.Errorf("rev-list: %w", err)+ }++ scanner := bufio.NewScanner(cmd)+ scanner.Buffer(make([]byte, 64*1024), 16*1024*1024)+ var parseErr error+ for scanner.Scan() {+ line := scanner.Text()+ fields := strings.Fields(strings.TrimLeft(line, "<>"))+ if len(fields) == 0 || parseErr != nil {+ continue+ }+ ids, err := parseIDs(objectHash, fields)+ if err != nil {+ parseErr = fmt.Errorf("parsing rev-list output %q: %w", line, err)+ continue+ }+ if err := fn(line, ids); err != nil {+ parseErr = err}- return false, fmt.Errorf("merge-base --is-ancestor: %w, stderr: %q", err, stderr.String())}+ scanErr := scanner.Err()- return true, nil+ if err := cmd.Wait(); err != nil {+ return fmt.Errorf("rev-list: %w, stderr: %q", err, stderr.String())+ }+ if scanErr != nil {+ return fmt.Errorf("reading rev-list output: %w", scanErr)+ }+ return parseErr}-// MergeBases runs git merge-base --all, which exits with 1 and prints nothing when a and b share no-// history.-func (w gitWalker) MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {+// mergeBase runs git merge-base with the mode flag and args and returns the commits it prints.+// empty reports that it exited with 1 without printing anything, which --all does when the commits+// share no history.+func (w gitWalker) mergeBase(ctx context.Context, flag string, args []string) (_ []git.ObjectID, empty bool, _ error) {objectHash, err := w.repo.ObjectHash(ctx)if err != nil {- return nil, fmt.Errorf("detecting object hash: %w", err)+ return nil, false, fmt.Errorf("detecting object hash: %w", err)}var stdout, stderr bytes.Bufferif err := w.repo.ExecAndWait(ctx,gitcmd.Command{Name: "merge-base",- Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}},- Args: []string{a.String(), b.String()},+ Flags: []gitcmd.Option{gitcmd.Flag{Name: flag}},+ Args: args,},gitcmd.WithStdout(&stdout),gitcmd.WithStderr(&stderr),); err != nil {if status, ok := command.ExitStatus(err); ok && status == 1 && stdout.Len() == 0 && stderr.Len() == 0 {- return nil, nil+ return nil, true, nil}- return nil, fmt.Errorf("merge-base --all: %w, stderr: %q", err, stderr.String())+ return nil, false, fmt.Errorf("merge-base %s: %w, stderr: %q", flag, err, stderr.String())}- bases, err := parseIDs(objectHash, strings.Fields(stdout.String()))+ ids, err := parseIDs(objectHash, strings.Fields(stdout.String()))if err != nil {- return nil, fmt.Errorf("parsing merge-base output: %w", err)+ return nil, false, fmt.Errorf("parsing merge-base output: %w", err)}- return bases, nil+ return ids, false, nil}func parseIDs(objectHash git.ObjectHash, fields []string) ([]git.ObjectID, error) {diff --git a/internal/git/revrange/revrange_test.go b/internal/git/revrange/revrange_test.goindex ae3cc31..717c041 100644--- a/internal/git/revrange/revrange_test.go+++ b/internal/git/revrange/revrange_test.go@@ -19,9 +19,7 @@ type modelWalker struct {// extra is the probability that List adds a commit that an excluded commit reaches.extra float64- lists, isAncestors, mergeBases int- // checksAfterMergeBases counts the IsAncestor calls made after the first MergeBases call.- checksAfterMergeBases int+ lists, reachedBottoms, reachables, mergeBases int// lastExtra is the number of extra commits of the last List.lastExtra int}@@ -64,12 +62,25 @@ func (m *modelWalker) List(_ context.Context, head git.ObjectID, exclude []git.Oreturn commits, nil}-func (m *modelWalker) IsAncestor(_ context.Context, ancestor, descendant git.ObjectID) (bool, error) {- m.isAncestors++- if m.mergeBases > 0 {- m.checksAfterMergeBases+++func (m *modelWalker) ReachedBottoms(_ context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error) {+ m.reachedBottoms+++ return m.reachable(base, bottoms), nil+}++func (m *modelWalker) Reachable(_ context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error) {+ m.reachables+++ return m.reachable(base, commits), nil+}++func (m *modelWalker) reachable(base git.ObjectID, commits []git.ObjectID) map[git.ObjectID]bool {+ ancestors := m.ancestors(base)+ reached := map[git.ObjectID]bool{}+ for _, id := range commits {+ if ancestors[id] {+ reached[id] = true+ }}- return m.ancestors(descendant)[ancestor], nil+ return reached}func (m *modelWalker) MergeBases(_ context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {@@ -140,27 +151,10 @@ func rangeOf(commits []Commit) map[git.ObjectID][]git.ObjectID {return set}-func bottomsOf(set map[git.ObjectID][]git.ObjectID) int {- bottoms := 0- for _, parents := range set {- bottom := true- for _, p := range parents {- if _, ok := set[p]; ok {- bottom = false- }- }- if bottom {- bottoms++- }- }- return bottoms-}-// TestExactOnRandomHistories checks Exact against the exact range on random histories, with// listings that hold no extra commit, some, and every commit that the base reaches. It also checks-// the costs that the package documents: without extra commits one check per bottom of the listing-// and nothing else; with them, at most one check per extra commit of the second listing and per-// bottom of the range.+// the costs that the package documents, which do not depend on the size of the history: without+// extra commits one listing and one question for all its bottoms; with them, one question more.func TestExactOnRandomHistories(t *testing.T) {t.Parallel()@@ -190,19 +184,20 @@ func TestExactOnRandomHistories(t *testing.T) {require.NoError(t, err)require.Equal(t, want, rangeOf(got), "range %s..%s", base, head)- if m.mergeBases == 0 {- // The listing was exact: one check per bottom at most, one listing.- require.Equal(t, 1, m.lists)- require.Equal(t, bottomsOf(want), m.isAncestors)+ require.Equal(t, 1, m.lists)+ require.Zero(t, m.mergeBases)+ if len(want) == 0 && m.lastExtra == 0 {+ require.Zero(t, m.reachedBottoms+m.reachables, "an empty listing needs no question")+ continue+ }+ require.Equal(t, 1, m.reachedBottoms)+ if m.lastExtra == 0 {+ require.Zero(t, m.reachables, "an exact listing needs no walk")continue}skewed++- require.Equal(t, 2, m.lists)- require.Equal(t, 1, m.mergeBases)- require.LessOrEqual(t, m.checksAfterMergeBases, m.lastExtra+bottomsOf(want),- "range %s..%s: %d checks for %d extra commits and %d bottoms", base, head,- m.checksAfterMergeBases, m.lastExtra, bottomsOf(want))+ require.Equal(t, 1, m.reachables)}if extra == 0 {@@ -220,10 +215,10 @@ func TestRefineOfAnEmptyListing(t *testing.T) {t.Parallel()m := &modelWalker{rnd: rand.New(rand.NewSource(1))}- got, err := Refine(context.Background(), m, "a", "b", nil)+ got, err := Refine(context.Background(), m, "a", nil)require.NoError(t, err)require.Empty(t, got)- require.Zero(t, m.isAncestors+m.lists+m.mergeBases)+ require.Zero(t, m.reachedBottoms+m.reachables+m.lists+m.mergeBases)}// TestRefineRejectsListingsThatAreNotHistories reports a listing that lists a commit twice.@@ -231,7 +226,7 @@ func TestRefineRejectsListingsThatAreNotHistories(t *testing.T) {t.Parallel()m := &modelWalker{rnd: rand.New(rand.NewSource(1))}- _, err := Refine(context.Background(), m, "a", "b", []Commit{{ID: "b"}, {ID: "b"}})+ _, err := Refine(context.Background(), m, "a", []Commit{{ID: "b"}, {ID: "b"}})require.ErrorContains(t, err, "listed twice")}diff --git a/internal/gitaly/service/operations/land_batch_pack_test.go b/internal/gitaly/service/operations/land_batch_pack_test.goindex 65a4be4..be7e0d4 100644--- a/internal/gitaly/service/operations/land_batch_pack_test.go+++ b/internal/gitaly/service/operations/land_batch_pack_test.go@@ -312,10 +312,10 @@ func TestLandBatchStepperProcessesPerMember(t *testing.T) {// A request already landed and a fast-forward of the starting tip each run one short git// process: the range listing (git rev-list), from which both facts are exact. A rebase onto the- // running tip runs one more, the check of the one bottom of its listing (git merge-base- // --is-ancestor), which makes the range it replays exact under skewed committer dates (see- // revrange). The command factory reads the object format for attr.tree from the repository's- // files, without a process.+ // running tip runs one more, the check of the bottoms of its listing (git merge-base+ // --independent), which tells that the range it replays is exact under skewed committer dates+ // (see rebase.Plan). The command factory reads the object format for attr.tree from the+ // repository's files, without a process.const rangeListingProcesses = 1const exactRangeProcesses = rangeListingProcesses + 1diff --git a/internal/graft/ancestry/ancestry.go b/internal/graft/ancestry/ancestry.goindex 8837916..c00b76f 100644--- a/internal/graft/ancestry/ancestry.go+++ b/internal/graft/ancestry/ancestry.go@@ -152,7 +152,12 @@ const (// and no excluded commit does. The walk ends when no commit that only head reaches is left to take,// as the ancestors of the others are not in the range.func (c graphChecker) List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]RangeCommit, error) {- w := graphWalk{checker: c, flags: map[git.ObjectID]uint8{}}+ w := graphWalk{+ checker: c,+ flags: map[git.ObjectID]uint8{},+ queued: map[git.ObjectID]bool{},+ taken: map[git.ObjectID]bool{},+ }if err := w.add(ctx, head, fromHead); err != nil {return nil, err@@ -170,6 +175,8 @@ func (c graphChecker) List(ctx context.Context, head git.ObjectID, exclude []gitif flags == fromHead {w.headOnly--}+ delete(w.queued, next.id)+ w.taken[next.id] = trueparents, err := c.source.Parents(ctx, next.id)if err != nil {@@ -194,9 +201,24 @@ func (c graphChecker) List(ctx context.Context, head git.ObjectID, exclude []gitreturn commits, nil}-// IsAncestor answers from the graph index.-func (c graphChecker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {- return c.index.IsAncestor(ctx, ancestor, descendant)+// ReachedBottoms answers from the graph index, one bottom at a time, in this process.+func (c graphChecker) ReachedBottoms(ctx context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error) {+ return c.Reachable(ctx, base, bottoms)+}++// Reachable answers from the graph index, one commit at a time, in this process.+func (c graphChecker) Reachable(ctx context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error) {+ reached := map[git.ObjectID]bool{}+ for _, id := range commits {+ ok, err := c.index.IsAncestor(ctx, id, base)+ if err != nil {+ return nil, err+ }+ if ok {+ reached[id] = true+ }+ }+ return reached, nil}// MergeBases answers from the graph index.@@ -213,6 +235,8 @@ type graphWalk struct {queue generationQueue// queued holds the commits of queue.queued map[git.ObjectID]bool+ // taken holds the commits that the walk took from queue.+ taken map[git.ObjectID]bool// headOnly counts the commits of queue whose flags are fromHead alone.headOnly int}@@ -221,11 +245,11 @@ type graphWalk struct {func (w *graphWalk) add(ctx context.Context, id git.ObjectID, flags uint8) error {old, reached := w.flags[id]if reached {- if !w.queued[id] {+ if w.taken[id] {// A commit is taken after all of its descendants, so a commit that was taken gets no- // flags from a descendant afterwards.+ // new flags from a descendant afterwards, unless the generation numbers are wrong.if old|flags != old {- return fmt.Errorf("graph walk reached commit %s again after taking it", id)+ return fmt.Errorf("graph walk reached commit %s with new flags after taking it: the generation numbers do not order the history", id)}return nil}@@ -242,9 +266,6 @@ func (w *graphWalk) add(ctx context.Context, id git.ObjectID, flags uint8) errorreturn err}- if w.queued == nil {- w.queued = map[git.ObjectID]bool{}- }w.flags[id] = flagsw.queued[id] = trueif flags == fromHead {diff --git a/internal/graft/rebase/plan.go b/internal/graft/rebase/plan.goindex fc17893..6fcccab 100644--- a/internal/graft/rebase/plan.go+++ b/internal/graft/rebase/plan.go@@ -41,11 +41,16 @@ type LandPlan struct {kind LandKind// commit is the commit that lands.commit git.ObjectID- // parents are the parents of every commit of the range tip..commit.+ // parents are the parents of every commit of the range tip..commit. It is empty when+ // mergesInRange is set.parents map[git.ObjectID][]git.ObjectID+ // mergesInRange is set when the range is known to hold a merge commit without being listed+ // exactly (see Plan).+ mergesInRange bool}-// Plan decides how commit lands onto tip, with the answers of checker.+// Plan decides how commit lands onto tip, with the answers of checker, in at most four+// questions whatever the size of the history.//// The listing of the commits that commit adds to tip (checker.List) may hold commits that tip// reaches when committer dates are skewed, never fewer than the range (see revrange). Two facts@@ -56,9 +61,19 @@ type LandPlan struct {// is reachable from commit, so tip is then reachable from commit; and on a path from commit down// to tip, the commit right above tip is in the range, so it is listed. That is a fast-forward.//-// Whether the change has merge commits, whether it shares history with tip and which commits a-// rebase replays need the exact range, so a diverged change has its listing made exact by-// revrange.Refine first, which checks the bottoms of the listing once without skew.+// Otherwise the change diverges, and one question for all the bottoms of the listing+// (checker.ReachedBottoms) tells whether the listing is exact. Without skew it is, and the plan is+// made from it. With skew:+//+// - The merge bases of tip and commit are commit alone exactly when tip reaches commit: the+// change is already landed.+// - Otherwise only a rebase needs more than the kind, and a rebase needs a range without merge+// commits. Such a range is a chain from commit down to the one commit p where it leaves the+// range; p is a common ancestor, and every common ancestor is reachable from commit through+// the chain, so through p: p is the only merge base. A listing of commit that excludes p then+// lists the chain and nothing else, because every path from commit runs through p. So the+// listing below the merge bases is either such a chain, and exact, or the range has a merge+// commit, and a rebase refuses the change (ErrMergesInRange) without the exact range.func Plan(ctx context.Context, checker ancestry.Checker, tip, commit git.ObjectID) (LandPlan, error) {listed, err := checker.List(ctx, commit, []git.ObjectID{tip})if err != nil {@@ -70,14 +85,67 @@ func Plan(ctx context.Context, checker ancestry.Checker, tip, commit git.ObjectIreturn plan, err}- rng, err := revrange.Refine(ctx, checker, tip, commit, listed)+ bottoms, err := revrange.Bottoms(listed)+ if err != nil {+ return LandPlan{}, fmt.Errorf("listing the commits of the change: %w", err)+ }+ reached, err := checker.ReachedBottoms(ctx, tip, bottoms)+ if err != nil {+ return LandPlan{}, fmt.Errorf("checking the listing of the change: %w", err)+ }+ if len(reached) == 0 {+ return plan, nil+ }++ revrange.CountSkewedListing()++ bases, err := checker.MergeBases(ctx, tip, commit)if err != nil {- return LandPlan{}, fmt.Errorf("making the range of the change exact: %w", err)+ return LandPlan{}, fmt.Errorf("finding the merge bases of the change: %w", err)+ }+ switch {+ case len(bases) == 0:+ // A bottom of the listing is reachable from tip, and every listed commit from commit.+ return LandPlan{}, fmt.Errorf("%s and %s have no merge base although they share history", tip, commit)+ case len(bases) == 1 && bases[0] == commit:+ return LandPlan{kind: LandAlreadyLanded, commit: commit}, nil+ case len(bases) > 1:+ // A range without merge commits leaves through one commit, the only merge base.+ return LandPlan{kind: LandDiverged, commit: commit, mergesInRange: true}, nil}- // The exact range is empty when every listed commit was reachable from tip: the commit is- // already landed. It cannot be a fast-forward, which the listing would have shown.- return planLanding(tip, commit, rng)+ relisted, err := checker.List(ctx, commit, bases)+ if err != nil {+ return LandPlan{}, fmt.Errorf("listing the commits of the change: %w", err)+ }+ if !isChainDownTo(relisted, commit, bases[0]) {+ return LandPlan{kind: LandDiverged, commit: commit, mergesInRange: true}, nil+ }++ return planLanding(tip, commit, relisted)+}++// isChainDownTo reports whether listed is a chain of commits with one parent each, from commit+// down to a commit whose parent is base, and nothing else.+func isChainDownTo(listed []ancestry.RangeCommit, commit, base git.ObjectID) bool {+ parents := make(map[git.ObjectID][]git.ObjectID, len(listed))+ for _, c := range listed {+ parents[c.ID] = c.Parents+ }++ visited := 0+ for id := commit; ; {+ ps, ok := parents[id]+ if !ok || len(ps) != 1 || visited == len(listed) {+ return false+ }+ visited++++ if ps[0] == base {+ return visited == len(listed)+ }+ id = ps[0]+ }}// planLanding decides how commit lands onto tip from rng, the exact range tip..commit:@@ -141,6 +209,10 @@ func (p LandPlan) Replay() ([]git.ObjectID, error) {return nil, fmt.Errorf("replay of a landing that is not diverged (kind %d)", p.kind)}+ if p.mergesInRange {+ return nil, ErrMergesInRange+ }+for _, parents := range p.parents {if len(parents) > 1 {return nil, ErrMergesInRangediff --git a/internal/graft/rebase/plan_skew_test.go b/internal/graft/rebase/plan_skew_test.goindex 69ce368..38a6711 100644--- a/internal/graft/rebase/plan_skew_test.go+++ b/internal/graft/rebase/plan_skew_test.go@@ -17,7 +17,12 @@ import (// countingChecker counts the questions that a plan asks, each a git process for the git checker.type countingChecker struct {ancestry.Checker- lists, isAncestors, mergeBases int+ lists, reachedBottoms, reachables, mergeBases int+}++// processes is the number of git processes that the questions cost with the git checker.+func (c *countingChecker) processes() int {+ return c.lists + c.reachedBottoms + c.reachables + c.mergeBases}func (c *countingChecker) List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]ancestry.RangeCommit, error) {@@ -25,9 +30,14 @@ func (c *countingChecker) List(ctx context.Context, head git.ObjectID, exclude [return c.Checker.List(ctx, head, exclude)}-func (c *countingChecker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {- c.isAncestors++- return c.Checker.IsAncestor(ctx, ancestor, descendant)+func (c *countingChecker) ReachedBottoms(ctx context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error) {+ c.reachedBottoms+++ return c.Checker.ReachedBottoms(ctx, base, bottoms)+}++func (c *countingChecker) Reachable(ctx context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error) {+ c.reachables+++ return c.Checker.Reachable(ctx, base, commits)}func (c *countingChecker) MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {@@ -96,9 +106,8 @@ func TestPlanOfTheReviewChain(t *testing.T) {// TestPlanOnALongSkewedHistory measures what a skewed listing costs on a long history: a main line// of 2000 commits, the commit A of a change on it, and a tip of ten commits on A dated a year-// before the main line. git rev-list tip..A then lists A and the whole main line. The plan asks-// one listing, one check of its only bottom, the merge bases and one listing below them, and-// nothing that grows with the history.+// before the main line. git rev-list tip..A then lists A and the whole main line. The plan asks a+// fixed number of questions, whatever the length of the history.func TestPlanOnALongSkewedHistory(t *testing.T) {t.Parallel()@@ -127,12 +136,15 @@ func TestPlanOnALongSkewedHistory(t *testing.T) {require.Greater(t, len(listed), mainLine, "git rev-list must list the main line for this test to measure anything")for _, tc := range []struct {- desc string- commit git.ObjectID- wantKind LandKind+ desc string+ commit git.ObjectID+ wantKind LandKind+ wantProcesses int}{- {desc: "already landed", commit: change, wantKind: LandAlreadyLanded},- {desc: "diverged", commit: onA, wantKind: LandDiverged},+ // The listing, the bottoms, the merge bases (only commit: already landed).+ {desc: "already landed", commit: change, wantKind: LandAlreadyLanded, wantProcesses: 3},+ // The same and the listing below the merge base, which is the chain of the change.+ {desc: "diverged", commit: onA, wantKind: LandDiverged, wantProcesses: 4},} {checker := &countingChecker{Checker: ancestry.Git(f.repo)}start := time.Now()@@ -140,11 +152,67 @@ func TestPlanOnALongSkewedHistory(t *testing.T) {elapsed := time.Since(start)require.NoError(t, err)require.Equal(t, tc.wantKind, plan.Kind(), tc.desc)+ require.Equal(t, tc.wantProcesses, checker.processes(), tc.desc)+ require.Zero(t, checker.reachables, "%s: a land never walks the history of the tip", tc.desc)+ t.Logf("%s: %d listed by git rev-list, %d processes, %v", tc.desc, len(listed), checker.processes(), elapsed)+ }+}++// TestPlanOfAMergeOfTheTipIntoTheChange is the shape that the review of the skew fix reported: a+// main line of 3000 commits, ten commits on it dated a year before the main line, then M and the+// tip T. A change F forks from main line commit 2500, and H merges M into it. The merge base of T+// and H is M, but the path H -> F -> m2500 reaches the shared history without running through M,+// so the listing below M still holds m0..m2500. Removing them one check at a time took 2503+// checks and 43 s. The plan asks a fixed number of questions instead: the range has a merge+// commit, which a rebase refuses.+func TestPlanOfAMergeOfTheTipIntoTheChange(t *testing.T) {+ t.Parallel()- require.Equal(t, 2, checker.lists, tc.desc)- require.Equal(t, 1, checker.mergeBases, tc.desc)- require.LessOrEqual(t, checker.isAncestors, 2, tc.desc)- t.Logf("%s: %d listed by git rev-list, %d listings, %d ancestry checks, %d merge-base, %v",- tc.desc, len(listed), checker.lists, checker.isAncestors, checker.mergeBases, elapsed)+ f := newFixture(t)+ const n, k = 3000, 2500+ base := int64(1767225600)+ var parents []int+ var dates []int64+ for i := 0; i < n; i++ {+ parents = append(parents, i-1)+ dates = append(dates, base+int64(i)*60)}+ for j := 0; j < 10; j++ {+ parents = append(parents, len(parents)-1)+ dates = append(dates, 1735689600+int64(j))+ }+ now := base + int64(n)*60 + 1000+ m := len(parents)+ parents = append(parents, m-1)+ dates = append(dates, now)+ tipIndex := len(parents)+ parents = append(parents, m)+ dates = append(dates, now+60)+ fIndex := len(parents)+ parents = append(parents, k)+ dates = append(dates, now+120)+ ids := writeChain(t, f, parents, dates)++ // H merges M into F.+ h := git.ObjectID(strings.TrimSpace(string(gittest.ExecOpts(t, f.cfg, gittest.ExecConfig{+ Env: []string{"GIT_COMMITTER_DATE=" + fmt.Sprintf("%d +0000", now+180), "GIT_AUTHOR_DATE=" + fmt.Sprintf("%d +0000", now+180)},+ }, "-C", f.repoPath, "commit-tree", ids[fIndex].String()+"^{tree}", "-p", ids[fIndex].String(), "-p", ids[m].String(), "-m", "merge main"))))+ tip := ids[tipIndex]++ relisted := strings.Fields(string(gittest.Exec(t, f.cfg, "-C", f.repoPath, "rev-list", h.String(), "^"+ids[m].String())))+ require.Greater(t, len(relisted), k, "the listing below the merge base must hold the main line for this test to measure anything")++ checker := &countingChecker{Checker: ancestry.Git(f.repo)}+ start := time.Now()+ plan, err := Plan(f.ctx, checker, tip, h)+ elapsed := time.Since(start)+ require.NoError(t, err)+ require.Equal(t, LandDiverged, plan.Kind())+ _, err = plan.Replay()+ require.ErrorIs(t, err, ErrMergesInRange)++ require.LessOrEqual(t, checker.processes(), 4)+ require.Zero(t, checker.reachables)+ require.Less(t, elapsed, 10*time.Second)+ t.Logf("%d listed below the merge base by git rev-list; %d processes, %v", len(relisted), checker.processes(), elapsed)}diff --git a/internal/graft/rebase/plan_test.go b/internal/graft/rebase/plan_test.goindex f8d7f9c..58bf19f 100644--- a/internal/graft/rebase/plan_test.go+++ b/internal/graft/rebase/plan_test.go@@ -399,29 +399,58 @@ func TestPlanMatchesTheExactHistory(t *testing.T) {}}-// overListingChecker answers from a history held in memory. Its List adds to the exact range-// every commit that an excluded commit reaches and head reaches too, the most that git rev-list can-// list under committer-date skew.+// overListingChecker answers from a history held in memory. Its List lists every commit that head+// reaches without going through an excluded commit, the commits that the excluded commits reach+// included: the most that git rev-list can list under committer-date skew, since git marks the+// excluded commits as excluded from the start and its walk from head stops at them.type overListingChecker struct {parents [][]intids []git.ObjectIDindex map[git.ObjectID]int}-func (c overListingChecker) List(_ context.Context, head git.ObjectID, _ []git.ObjectID) ([]ancestry.RangeCommit, error) {+func (c overListingChecker) List(_ context.Context, head git.ObjectID, exclude []git.ObjectID) ([]ancestry.RangeCommit, error) {+ excluded := map[int]bool{}+ for _, id := range exclude {+ excluded[c.index[id]] = true+ }+ if excluded[c.index[head]] {+ return nil, nil+ }++ seen := map[int]bool{c.index[head]: true}+ stack := []int{c.index[head]}var commits []ancestry.RangeCommit- for x := range ancestorsOf(c.parents, c.index[head]) {+ for len(stack) > 0 {+ x := stack[len(stack)-1]+ stack = stack[:len(stack)-1]+var parents []git.ObjectIDfor _, p := range c.parents[x] {parents = append(parents, c.ids[p])+ if !seen[p] && !excluded[p] {+ seen[p] = true+ stack = append(stack, p)+ }}commits = append(commits, ancestry.RangeCommit{ID: c.ids[x], Parents: parents})}return commits, nil}-func (c overListingChecker) IsAncestor(_ context.Context, ancestor, descendant git.ObjectID) (bool, error) {- return ancestorsOf(c.parents, c.index[descendant])[c.index[ancestor]], nil+func (c overListingChecker) ReachedBottoms(ctx context.Context, base git.ObjectID, bottoms []git.ObjectID) (map[git.ObjectID]bool, error) {+ return c.Reachable(ctx, base, bottoms)+}++func (c overListingChecker) Reachable(_ context.Context, base git.ObjectID, commits []git.ObjectID) (map[git.ObjectID]bool, error) {+ ancestors := ancestorsOf(c.parents, c.index[base])+ reached := map[git.ObjectID]bool{}+ for _, id := range commits {+ if ancestors[c.index[id]] {+ reached[id] = true+ }+ }+ return reached, nil}func (c overListingChecker) MergeBases(_ context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {