Skip to content

Git designed for AI agents.

localrepo: exact rebase todo list under skewed committer dates

Federico Elia878d5e125dd1
diff --git a/internal/git/localrepo/rebase.go b/internal/git/localrepo/rebase.go
index 29f69e5..627c3e0 100644
--- a/internal/git/localrepo/rebase.go
+++ b/internal/git/localrepo/rebase.go
@@ -11,6 +11,7 @@ import (
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/gitcmd"
+ "github.com/feder-cr/git-ai/internal/git/revrange"
"github.com/feder-cr/git-ai/internal/helper/text"
"github.com/feder-cr/git-ai/internal/structerr"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
@@ -94,10 +95,32 @@ func (repo *Repo) Rebase(ctx context.Context, upstream, branch string, options .
return newOID, nil
}
-func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, upstreamOID, branchOID git.ObjectID) (git.ObjectID, error) {
- objectHash, err := repo.ObjectHash(ctx)
+// rebaseTodo returns the commits of branch that are not in upstream, oldest first, without merge
+// commits and, unless reapplyCherryPicks is set, without the commits that are patch-equivalent to a
+// commit of upstream that is not in branch: the pick-only todo list of git-rebase(1).
+//
+// git rev-list upstream...branch alone may list commits of the history that both share when
+// committer dates are skewed (see revrange): a commit that upstream already has would be replayed
+// again, and with --cherry-pick a commit of the branch could be dropped as equivalent to a commit
+// 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.
+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)
if err != nil {
- return "", structerr.NewInternal("getting object hash %w", err)
+ return nil, structerr.NewInternal("listing the commits of the branch: %w", err)
+ }
+ exclude := revrange.Boundary(branchSide)
+
+ // 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)
+ if err != nil {
+ return nil, structerr.NewInternal("listing the commits of upstream: %w", err)
+ }
+ exclude = append(exclude, revrange.Boundary(upstreamSide)...)
}
// Flags of git-rev-list to get the pick-only todo_list for a rebase.
@@ -105,7 +128,7 @@ func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, up
// merge commits, which is also what git2go does.
// The flags are inferred from https://github.com/git/git/blob/v2.41.0/sequencer.c#L5704-L5714
var flags []gitcmd.Option
- if !cfg.reapplyCherryPicks {
+ if !reapplyCherryPicks {
flags = append(flags, gitcmd.Flag{Name: "--cherry-pick"})
}
flags = append(flags,
@@ -113,8 +136,16 @@ func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, up
gitcmd.Flag{Name: "--no-merges"},
gitcmd.Flag{Name: "--topo-order"},
gitcmd.Flag{Name: "--reverse"},
+ // The excluded boundaries are read from the standard input, whose size is not limited
+ // like the command line.
+ gitcmd.Flag{Name: "--stdin"},
)
+ var excluded strings.Builder
+ for _, id := range exclude {
+ excluded.WriteString("^" + id.String() + "\n")
+ }
+
var stderr bytes.Buffer
cmd, err := repo.Exec(ctx, gitcmd.Command{
Name: "rev-list",
@@ -125,9 +156,9 @@ func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, up
// the provided --right-only flag, the result should be only commits which
// exist on the branch that is to be rebased.
Args: []string{fmt.Sprintf("%s...%s", upstreamOID, branchOID)},
- }, gitcmd.WithStderr(&stderr), gitcmd.WithSetupStdout())
+ }, gitcmd.WithStdin(strings.NewReader(excluded.String())), gitcmd.WithStderr(&stderr), gitcmd.WithSetupStdout())
if err != nil {
- return "", structerr.NewInternal("start git rev-list: %w", err)
+ return nil, structerr.NewInternal("start git rev-list: %w", err)
}
var todoList []string
@@ -136,10 +167,24 @@ func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, up
todoList = append(todoList, strings.TrimSpace(scanner.Text()))
}
if err := scanner.Err(); err != nil {
- return "", fmt.Errorf("scanning rev-list output: %w", err)
+ return nil, fmt.Errorf("scanning rev-list output: %w", err)
}
if err := cmd.Wait(); err != nil {
- return "", structerr.NewInternal("git rev-list: %w", err).WithMetadata("stderr", stderr.String())
+ return nil, structerr.NewInternal("git rev-list: %w", err).WithMetadata("stderr", stderr.String())
+ }
+
+ return todoList, nil
+}
+
+func (repo *Repo) rebaseUsingMergeTree(ctx context.Context, cfg rebaseConfig, upstreamOID, branchOID git.ObjectID) (git.ObjectID, error) {
+ objectHash, err := repo.ObjectHash(ctx)
+ if err != nil {
+ return "", structerr.NewInternal("getting object hash %w", err)
+ }
+
+ todoList, err := repo.rebaseTodo(ctx, upstreamOID, branchOID, cfg.reapplyCherryPicks)
+ if err != nil {
+ return "", err
}
upstreamCommit, err := repo.ReadCommit(ctx, git.Revision(upstreamOID))
diff --git a/internal/git/localrepo/rebase_skew_test.go b/internal/git/localrepo/rebase_skew_test.go
new file mode 100644
index 0000000..a139da1
--- /dev/null
+++ b/internal/git/localrepo/rebase_skew_test.go
@@ -0,0 +1,155 @@
+package localrepo
+
+import (
+ "bytes"
+ "fmt"
+ "math/rand"
+ "strings"
+ "testing"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"
+ "github.com/stretchr/testify/require"
+)
+
+// TestRebaseTodoUnderSkewedDates checks the todo list of Rebase on a random history in which runs
+// of commits are dated a year before the first commit. git rev-list upstream...branch lists commits
+// of the shared history for some pairs of such a history; the todo list must still hold exactly
+// the commits of the branch that upstream does not reach, without merge commits, parents first.
+// Every commit changes a file of its own, so no commit is patch-equivalent to another and
+// --cherry-pick drops nothing.
+func TestRebaseTodoUnderSkewedDates(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)
+
+ rnd := rand.New(rand.NewSource(7))
+ const n = 400
+ parents := make([][]int, n)
+ dates := make([]int64, n)
+ for i := 0; i < n; i++ {
+ dates[i] = 1700000000 + int64(i)*60
+ if i == 0 {
+ continue
+ }
+ first := i - 1
+ if rnd.Intn(4) == 0 {
+ first = rnd.Intn(i)
+ }
+ parents[i] = []int{first}
+ if rnd.Intn(6) == 0 {
+ if second := rnd.Intn(i); second != first {
+ parents[i] = append(parents[i], second)
+ }
+ }
+ }
+ for run := 0; run < 25; run++ {
+ start := 1 + rnd.Intn(n-20)
+ for k := 0; k < 6+rnd.Intn(12); k++ {
+ dates[start+k] = 1600000000 + int64(k)
+ }
+ }
+
+ var stream bytes.Buffer
+ for i, list := range parents {
+ name := fmt.Sprintf("c%d", i)
+ fmt.Fprintf(&stream, "commit refs/heads/%s\nmark :%d\n", name, i+1)
+ fmt.Fprintf(&stream, "committer Tester <[email protected]> %d +0000\n", dates[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)
+ }
+ }
+ fmt.Fprintf(&stream, "M 100644 inline %s\ndata %d\n%s\n\n", name, len(name)+1, name+"\n")
+ }
+ gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: &stream}, "-C", repoPath, "fast-import", "--quiet")
+
+ ids := make([]git.ObjectID, n)
+ index := map[string]int{}
+ for i := range ids {
+ ids[i] = gittest.ResolveRevision(t, cfg, repoPath, fmt.Sprintf("refs/heads/c%d", i))
+ index[ids[i].String()] = i
+ }
+
+ ancestors := func(x int) map[int]bool {
+ seen := map[int]bool{x: true}
+ stack := []int{x}
+ for len(stack) > 0 {
+ c := stack[len(stack)-1]
+ stack = stack[:len(stack)-1]
+ for _, p := range parents[c] {
+ if !seen[p] {
+ seen[p] = true
+ stack = append(stack, p)
+ }
+ }
+ }
+ return seen
+ }
+
+ wrongBefore := 0
+ for k := 0; k < 300; k++ {
+ upstream, branch := rnd.Intn(n), rnd.Intn(n)
+ upstreamAncestors := ancestors(upstream)
+ want := map[int]bool{}
+ for x := range ancestors(branch) {
+ if !upstreamAncestors[x] && len(parents[x]) <= 1 {
+ want[x] = true
+ }
+ }
+
+ // The listing that Rebase used before, to show that the history exercises the skew.
+ listed := map[int]bool{}
+ for _, line := range strings.Fields(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-list",
+ "--right-only", "--no-merges", ids[upstream].String()+"..."+ids[branch].String()))) {
+ listed[index[line]] = true
+ }
+ if !equalSets(want, listed) {
+ wrongBefore++
+ }
+
+ for _, reapply := range []bool{true, false} {
+ todo, err := repo.rebaseTodo(ctx, ids[upstream], ids[branch], reapply)
+ require.NoError(t, err)
+
+ got := map[int]bool{}
+ position := map[int]int{}
+ for i, id := range todo {
+ got[index[id]] = true
+ position[index[id]] = i
+ }
+ require.True(t, equalSets(want, got), "todo of c%d onto c%d (reapply %v): got %v, want %v", branch, upstream, reapply, got, want)
+
+ // Parents come before their children.
+ for x := range got {
+ for _, p := range parents[x] {
+ if got[p] {
+ require.Less(t, position[p], position[x], "c%d before its parent c%d", x, p)
+ }
+ }
+ }
+ }
+ }
+ require.Positive(t, wrongBefore, "git rev-list got no pair of this history wrong")
+ t.Logf("git rev-list upstream...branch listed a wrong todo for %d of 300 pairs", wrongBefore)
+}
+
+func equalSets(a, b map[int]bool) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for x := range a {
+ if !b[x] {
+ return false
+ }
+ }
+ return true
+}