Git designed for AI agents.
rebase: decide a landing from one range listing
Federico Elia4353a224e37d
diff --git a/internal/graft/rebase/plan.go b/internal/graft/rebase/plan.gonew file mode 100644index 0000000..94cc96b--- /dev/null+++ b/internal/graft/rebase/plan.go@@ -0,0 +1,144 @@+package rebase++import (+ "errors"+ "fmt"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/graft/ancestry"+)++// LandKind is how a commit lands onto the tip of a branch.+type LandKind int++const (+ // LandAlreadyLanded means that the commit is reachable from the tip, the tip included.+ LandAlreadyLanded LandKind = iota + 1+ // LandFastForward means that the tip is reachable from the commit, so the commit becomes the+ // new tip.+ LandFastForward+ // LandDiverged means that neither is reachable from the other: the change must be rebased or+ // merged onto the tip.+ LandDiverged+)++var (+ // ErrMergesInRange is returned by LandPlan.Replay when the commits that the change adds to+ // the tip contain a merge commit, which a rebase cannot replay.+ ErrMergesInRange = errors.New("commit range contains merge commits")+ // ErrUnrelated is returned by LandPlan.Replay when the commit and the tip have no common+ // ancestor.+ ErrUnrelated = errors.New("commit shares no history with the tip")+)++// LandPlan is the decision of how a commit lands onto a tip, made from the range tip..commit. It+// answers what landing used to ask git one question at a time: whether the commit is already+// landed, whether it fast-forwards the tip, whether its range has merge commits, whether it shares+// history with the tip, and which commits a rebase replays.+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 map[git.ObjectID][]git.ObjectID+}++// PlanLanding decides how commit lands onto tip from rng, the range tip..commit as+// ancestry.Checker.Range lists it:+//+// - The range is empty exactly when commit is reachable from tip: LandAlreadyLanded.+// - tip is a parent of a commit of the range exactly when tip is reachable from commit: on a path+// from commit down to tip, the commit right above tip is not reachable from tip, so it is in the+// range. That is LandFastForward, whatever merge commits the range has.+// - Otherwise LandDiverged.+//+// A non-empty range must contain commit and list every commit once; anything else is an error,+// because the range does not come from tip..commit.+func PlanLanding(tip, commit git.ObjectID, rng []ancestry.RangeCommit) (LandPlan, error) {+ if len(rng) == 0 {+ return LandPlan{kind: LandAlreadyLanded, commit: commit}, nil+ }++ parents := make(map[git.ObjectID][]git.ObjectID, len(rng))+ fastForward := false+ for _, c := range rng {+ if _, ok := parents[c.ID]; ok {+ return LandPlan{}, fmt.Errorf("range %s..%s lists commit %s twice", tip, commit, c.ID)+ }+ parents[c.ID] = c.Parents++ for _, parent := range c.Parents {+ if parent == tip {+ fastForward = true+ }+ }+ }++ if _, ok := parents[commit]; !ok {+ return LandPlan{}, fmt.Errorf("range %s..%s does not contain the commit", tip, commit)+ }++ if fastForward {+ return LandPlan{kind: LandFastForward, commit: commit}, nil+ }++ return LandPlan{kind: LandDiverged, commit: commit, parents: parents}, nil+}++// Kind returns how the commit lands.+func (p LandPlan) Kind() LandKind {+ return p.kind+}++// Replay returns the commits that a rebase of a LandDiverged plan replays onto the tip, oldest+// first. These are all the commits of the range: git rev-list --right-only --no-merges --topo-order+// --reverse tip...commit without --cherry-pick lists the same commits in the same order.+//+// It returns ErrMergesInRange when a commit of the range has more than one parent. Otherwise every+// commit of the range has at most one parent, and the range is the chain from the commit down its+// parents. The bottom of the chain either has a parent, which is outside of the range and therefore+// reachable from the tip, so the two share history, or it is a root: then no ancestor of the commit+// is reachable from the tip, and Replay returns ErrUnrelated.+func (p LandPlan) Replay() ([]git.ObjectID, error) {+ if p.kind != LandDiverged {+ return nil, fmt.Errorf("replay of a landing that is not diverged (kind %d)", p.kind)+ }++ for _, parents := range p.parents {+ if len(parents) > 1 {+ return nil, ErrMergesInRange+ }+ }++ chain := make([]git.ObjectID, 0, len(p.parents))+ for id := p.commit; ; {+ if len(chain) == len(p.parents) {+ // A history is acyclic, so a chain can only be longer than the range when the range+ // does not come from git.+ return nil, fmt.Errorf("range of %s has a cycle", p.commit)+ }+ chain = append(chain, id)++ parents := p.parents[id]+ if len(parents) == 0 {+ return nil, ErrUnrelated+ }++ if _, inRange := p.parents[parents[0]]; !inRange {+ break+ }+ id = parents[0]+ }++ // Every commit of the range is reachable from the commit through commits of the range, so+ // without merge commits the chain is the whole range.+ if len(chain) != len(p.parents) {+ return nil, fmt.Errorf("range of %s is not a chain: %d of %d commits reached", p.commit, len(chain), len(p.parents))+ }++ for i, j := 0, len(chain)-1; i < j; i, j = i+1, j-1 {+ chain[i], chain[j] = chain[j], chain[i]+ }++ return chain, nil+}diff --git a/internal/graft/rebase/plan_test.go b/internal/graft/rebase/plan_test.gonew file mode 100644index 0000000..502b662--- /dev/null+++ b/internal/graft/rebase/plan_test.go@@ -0,0 +1,326 @@+package rebase++import (+ "bytes"+ "fmt"+ "math/rand"+ "strings"+ "testing"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/catfile"+ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/gitaly/config"+ "github.com/feder-cr/git-ai/internal/graft/ancestry"+ "github.com/feder-cr/git-ai/internal/graft/derived"+ "github.com/stretchr/testify/require"+)++func TestPlanLanding(t *testing.T) {+ t.Parallel()++ // Synthetic IDs: the plan only compares them.+ id := func(name string) git.ObjectID { return git.ObjectID(name) }+ tip, commit := id("tip"), id("commit")+ rc := func(name string, parents ...string) ancestry.RangeCommit {+ ps := make([]git.ObjectID, 0, len(parents))+ for _, p := range parents {+ ps = append(ps, id(p))+ }+ return ancestry.RangeCommit{ID: id(name), Parents: ps}+ }++ for _, tc := range []struct {+ desc string+ rng []ancestry.RangeCommit+ wantKind LandKind+ wantTodo []git.ObjectID+ wantReplay error+ wantErr string+ }{+ {+ desc: "empty range is already landed",+ wantKind: LandAlreadyLanded,+ },+ {+ desc: "tip as parent is a fast-forward",+ rng: []ancestry.RangeCommit{rc("commit", "c1"), rc("c1", "tip")},+ wantKind: LandFastForward,+ },+ {+ desc: "fast-forward with a merge commit in the range",+ rng: []ancestry.RangeCommit{rc("commit", "m"), rc("m", "a", "b"), rc("a", "tip"), rc("b", "base")},+ wantKind: LandFastForward,+ },+ {+ desc: "diverged chain replays oldest first",+ rng: []ancestry.RangeCommit{rc("commit", "c2"), rc("c1", "base"), rc("c2", "c1")},+ wantKind: LandDiverged,+ wantTodo: []git.ObjectID{id("c1"), id("c2"), commit},+ },+ {+ desc: "merge commit in a diverged range",+ rng: []ancestry.RangeCommit{rc("commit", "a", "b"), rc("a", "base"), rc("b", "base")},+ wantKind: LandDiverged,+ wantReplay: ErrMergesInRange,+ },+ {+ desc: "merges are reported before unrelated history",+ rng: []ancestry.RangeCommit{rc("commit", "a", "b"), rc("a"), rc("b")},+ wantKind: LandDiverged,+ wantReplay: ErrMergesInRange,+ },+ {+ desc: "chain down to a root is unrelated",+ rng: []ancestry.RangeCommit{rc("commit", "c1"), rc("c1")},+ wantKind: LandDiverged,+ wantReplay: ErrUnrelated,+ },+ {+ desc: "single root commit is unrelated",+ rng: []ancestry.RangeCommit{rc("commit")},+ wantKind: LandDiverged,+ wantReplay: ErrUnrelated,+ },+ {+ desc: "range without the commit",+ rng: []ancestry.RangeCommit{rc("other", "base")},+ wantErr: "does not contain the commit",+ },+ {+ desc: "commit listed twice",+ rng: []ancestry.RangeCommit{rc("commit", "base"), rc("commit", "base")},+ wantErr: "twice",+ },+ } {+ t.Run(tc.desc, func(t *testing.T) {+ t.Parallel()++ plan, err := PlanLanding(tip, commit, tc.rng)+ if tc.wantErr != "" {+ require.ErrorContains(t, err, tc.wantErr)+ return+ }+ require.NoError(t, err)+ require.Equal(t, tc.wantKind, plan.Kind())++ if tc.wantKind != LandDiverged {+ _, err := plan.Replay()+ require.Error(t, err, "only a diverged plan replays")+ return+ }++ todo, err := plan.Replay()+ if tc.wantReplay != nil {+ require.ErrorIs(t, err, tc.wantReplay)+ return+ }+ require.NoError(t, err)+ require.Equal(t, tc.wantTodo, todo)+ })+ }+}++func TestPlanLandingRejectsRangesThatAreNotHistories(t *testing.T) {+ t.Parallel()++ commit := git.ObjectID("commit")+ plan, err := PlanLanding("tip", commit, []ancestry.RangeCommit{+ {ID: commit, Parents: []git.ObjectID{"a"}},+ {ID: "a", Parents: []git.ObjectID{commit}},+ })+ require.NoError(t, err)++ _, err = plan.Replay()+ require.ErrorContains(t, err, "cycle")++ plan, err = PlanLanding("tip", commit, []ancestry.RangeCommit{+ {ID: commit, Parents: []git.ObjectID{"base"}},+ {ID: "stray", Parents: []git.ObjectID{"base"}},+ })+ require.NoError(t, err)++ _, err = plan.Replay()+ require.ErrorContains(t, err, "not a chain")+}++// forestHistory returns the parents of n commits as indexes lower than the commit's own: a forest+// with several roots, long linear stretches, merges of earlier commits and a criss-cross.+func forestHistory(rnd *rand.Rand, n int) [][]int {+ parents := make([][]int, n)+ parents[1] = []int{0}+ parents[2] = []int{0}+ parents[3] = []int{1, 2}+ parents[4] = []int{2, 1}++ for i := 5; i < n; i++ {+ switch r := rnd.Intn(20); {+ case r == 0:+ // A new root: its descendants share no history with the other trees.+ case r < 6:+ first := rnd.Intn(i)+ second := rnd.Intn(i)+ if second == first {+ parents[i] = []int{first}+ } else {+ parents[i] = []int{first, second}+ }+ default:+ if rnd.Intn(3) == 0 {+ parents[i] = []int{rnd.Intn(i)}+ } else {+ parents[i] = []int{i - 1}+ }+ }+ }++ return parents+}++// writeForest creates the commits with git fast-import. Commit i has the parents parents[i] and is+// reachable as refs/heads/c<i>.+func writeForest(t *testing.T, cfg config.Cfg, repoPath string, parents [][]int) []git.ObjectID {+ t.Helper()++ var stream bytes.Buffer+ for i, list := range parents {+ name := fmt.Sprintf("c%d", i)+ fmt.Fprintf(&stream, "commit refs/heads/%s\n", name)+ fmt.Fprintf(&stream, "mark :%d\n", i+1)+ fmt.Fprintf(&stream, "committer Tester <[email protected]> %d +0000\n", 1600000000+i)+ fmt.Fprintf(&stream, "data %d\n%s\n", len(name), name)+ for j, parent := range list {+ if j == 0 {+ fmt.Fprintf(&stream, "from :%d\n", parent+1)+ } else {+ fmt.Fprintf(&stream, "merge :%d\n", parent+1)+ }+ }+ stream.WriteString("\n")+ }++ gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: &stream}, "-C", repoPath, "fast-import", "--quiet")++ ids := make([]git.ObjectID, 0, len(parents))+ for i := range parents {+ ids = append(ids, gittest.ResolveRevision(t, cfg, repoPath, fmt.Sprintf("refs/heads/c%d", i)))+ }+ return ids+}++// phase4aDecision is how landing decided before the plan, one git command per question, in the+// order of the batch stepper and the engine: already landed, fast-forward, merges in the range,+// common ancestor, and the todo list of the rebase.+type phase4aDecision struct {+ kind LandKind+ replayErr error+ todo []git.ObjectID+}++func decideAsPhase4a(t *testing.T, cfg config.Cfg, repoPath string, tip, commit git.ObjectID) phase4aDecision {+ t.Helper()++ exitCode := func(args ...string) int {+ cmd := gittest.NewCommand(t, cfg, append([]string{"-C", repoPath}, args...)...)+ if err := cmd.Run(); err != nil {+ return cmd.ProcessState.ExitCode()+ }+ return 0+ }++ if exitCode("merge-base", "--is-ancestor", commit.String(), tip.String()) == 0 {+ return phase4aDecision{kind: LandAlreadyLanded}+ }+ if exitCode("merge-base", "--is-ancestor", tip.String(), commit.String()) == 0 {+ return phase4aDecision{kind: LandFastForward}+ }++ if out := strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--merges", tip.String()+".."+commit.String()))); out != "" {+ return phase4aDecision{kind: LandDiverged, replayErr: ErrMergesInRange}+ }++ switch code := exitCode("merge-base", "--all", tip.String(), commit.String()); code {+ case 0:+ case 1:+ return phase4aDecision{kind: LandDiverged, replayErr: ErrUnrelated}+ default:+ t.Fatalf("merge-base --all exited with %d", code)+ }++ var todo []git.ObjectID+ for _, line := range strings.Fields(string(gittest.Exec(t, cfg, "-C", repoPath,+ "rev-list", "--right-only", "--no-merges", "--topo-order", "--reverse", tip.String()+"..."+commit.String()))) {+ todo = append(todo, git.ObjectID(line))+ }++ return phase4aDecision{kind: LandDiverged, todo: todo}+}++// TestPlanLandingMatchesPhase4aDecisions compares, on seeded random forests, the plan made from one+// range listing with the decisions landing made before it with one git command per question, for+// the git and the graph checker.+func TestPlanLandingMatchesPhase4aDecisions(t *testing.T) {+ t.Parallel()++ for _, seed := range []int64{1, 2, 3} {+ t.Run(fmt.Sprintf("seed %d", seed), func(t *testing.T) {+ t.Parallel()++ f := newFixture(t)+ rnd := rand.New(rand.NewSource(seed))+ ids := writeForest(t, f.cfg, f.repoPath, forestHistory(rnd, 160))++ catfileCache := catfile.NewCache(f.cfg)+ t.Cleanup(catfileCache.Stop)+ reader, cancel, err := catfileCache.ObjectReader(f.ctx, f.repo)+ require.NoError(t, err)+ t.Cleanup(cancel)++ graphChecker, err := ancestry.Graph(f.ctx, f.repo, reader, derived.NewStore(f.repoPath))+ require.NoError(t, err)++ checkers := map[string]ancestry.Checker{"git": ancestry.Git(f.repo), "graph": graphChecker}++ type pair struct{ tip, commit git.ObjectID }+ pairs := []pair{{ids[3], ids[4]}, {ids[4], ids[3]}, {ids[3], ids[3]}, {ids[0], ids[len(ids)-1]}}+ for range 200 {+ pairs = append(pairs, pair{ids[rnd.Intn(len(ids))], ids[rnd.Intn(len(ids))]})+ }++ seen := map[string]int{}+ for _, p := range pairs {+ want := decideAsPhase4a(t, f.cfg, f.repoPath, p.tip, p.commit)++ for name, checker := range checkers {+ rng, err := checker.Range(f.ctx, p.tip, p.commit)+ require.NoError(t, err)++ plan, err := PlanLanding(p.tip, p.commit, rng)+ require.NoError(t, err)+ require.Equal(t, want.kind, plan.Kind(), "%s: kind of %s onto %s", name, p.commit, p.tip)++ if want.kind != LandDiverged {+ continue+ }++ todo, err := plan.Replay()+ if want.replayErr != nil {+ require.ErrorIs(t, err, want.replayErr, "%s: replay of %s onto %s", name, p.commit, p.tip)+ continue+ }+ require.NoError(t, err)+ require.Equal(t, want.todo, todo, "%s: todo of %s onto %s", name, p.commit, p.tip)+ }++ label := fmt.Sprint(want.kind)+ if want.replayErr != nil {+ label += want.replayErr.Error()+ }+ seen[label]+++ }++ // The forests must exercise every decision.+ require.Len(t, seen, 5, "decisions seen: %v", seen)+ })+ }+}