Git designed for AI agents.
PLAN11: the revert search is bounded and only shapes the answer
Federico Elia64714c6e81bb
diff --git a/doc/graft/LIMITATIONS.md b/doc/graft/LIMITATIONS.mdindex 49d08e4..8b0d18a 100644--- a/doc/graft/LIMITATIONS.md+++ b/doc/graft/LIMITATIONS.md@@ -185,8 +185,8 @@ Git hooks use private.- A change that landed is not landed again from the same `commit_id` on thesame branch while the commit it landed as is on the branch, also when alater commit reverted it: the land answers `already_landed` and names the- revert when its message says "This reverts commit <id>" (the land identity- of [PLAN11](PLAN11.md)). Landing it again needs a new commit: a revert of+ revert when its message says "This reverts commit <id>" and it is among the+ 1000 commits closest to the tip (the land identity of [PLAN11](PLAN11.md)). Landing it again needs a new commit: a revert ofthe revert, or the change committed again. A branch reset past the landlands the change again.- The land identity is recorded in the changeset log. Withdiff --git a/doc/graft/PLAN11.md b/doc/graft/PLAN11.mdindex 7e4416c..4c8d5ed 100644--- a/doc/graft/PLAN11.md+++ b/doc/graft/PLAN11.md@@ -143,13 +143,17 @@ does the same for each member before the batch plans its chain.### 5.1 The revert in the answerOnly when the answer is already `AlreadyLanded` from the index, the land looks for a-revert of the landed commit among the commits after it: one-`git log --format=%H -n1 --fixed-strings --grep="This reverts commit <landed>" <landed>..<tip>`,-the line that `git revert` ("This reverts commit <id>.") and graft-verify's revert-("This reverts commit <id>, which failed ...") write. The walk covers only the-commits landed after the change, it runs on the rare path of a retried or repeated land,-and its result only adds to the message: a revert written another way is not named, and-the answer is the same.+revert of the landed commit among the commits after it, with one bounded+`git log --max-count=1000 <landed>..<tip>`: the newest commit among the 1000 closest to+the tip whose message holds "This reverts commit <landed>", the line that `git revert`+("This reverts commit <id>.") and graft-verify's revert ("This reverts commit <id>,+which failed ...") write. The bound keeps a long history after the change from slowing+the answer; it runs only on the rare path of a retried or repeated land.++The revert only shapes the message, never the decision: the answer is `AlreadyLanded`+before it is looked for, and is the same whatever the search finds. A revert written+another way, or further than 1000 commits from the tip, is not named; a search that+fails is logged at Error and the answer names no revert.This changes one behaviour on purpose: a change that landed and was reverted cannot belanded again from the same commit. The land answers `AlreadyLanded`, because the changediff --git a/internal/gitaly/service/operations/land_identity.go b/internal/gitaly/service/operations/land_identity.goindex d5f529c..aecc04d 100644--- a/internal/gitaly/service/operations/land_identity.go+++ b/internal/gitaly/service/operations/land_identity.go@@ -89,43 +89,57 @@ func (a *landingAttempt) landedEarlier(ctx context.Context, reference git.Referereturn nil, nil}+ // The revert only explains the answer, so a failure to look for it does not change the+ // answer: it is logged, and the answer names no revert.revertedBy, err := a.revertOf(ctx, landed, tip)if err != nil {- return nil, err+ a.server.logger.WithError(err).WithField("landed", landed.String()).+ Error("graft: looking for the revert of an earlier land failed; the answer names no revert")}return &landIdentity{landed: landed, sequence: record.Sequence, revertedBy: revertedBy}, nil}+// revertSearchCommits bounds the commits that revertOf reads: the revert is looked for among the+// commits after the landed one that are closest to the tip, so that a long history after it does+// not slow the answer. Tests lower it.+var revertSearchCommits = 1000+// revertOf returns the latest commit after landed up to tip whose message holds "This reverts// commit <landed>", the line that git revert and graft-verify write, or an empty ID when there is-// none. A revert written another way is not found.+// none among the revertSearchCommits commits closest to tip. A revert written another way, or+// further from the tip, is not found. The revert only explains the answer: the land is already+// landed whatever revertOf finds.func (a *landingAttempt) revertOf(ctx context.Context, landed, tip git.ObjectID) (git.ObjectID, error) {var stdout, stderr bytes.Bufferif err := a.repo.ExecAndWait(ctx, gitcmd.Command{Name: "log",Flags: []gitcmd.Option{- gitcmd.Flag{Name: "--format=%H"},- gitcmd.Flag{Name: "--max-count=1"},- gitcmd.Flag{Name: "--fixed-strings"},- gitcmd.Flag{Name: "--grep=This reverts commit " + landed.String()},+ gitcmd.Flag{Name: "--format=%H%x00%B%x00"},+ gitcmd.Flag{Name: fmt.Sprintf("--max-count=%d", revertSearchCommits)},},Args: []string{landed.String() + ".." + tip.String()},}, gitcmd.WithStdout(&stdout), gitcmd.WithStderr(&stderr)); err != nil {- return "", structerr.NewInternal("looking for a revert of %s: %w, stderr: %q", landed, err, stderr.String())+ return "", fmt.Errorf("looking for a revert of %s: %w, stderr: %q", landed, err, stderr.String())}- line := strings.TrimSpace(stdout.String())- if line == "" {- return "", nil- }+ // The output is pairs of fields, the commit ID and its message, each ended by a NUL, newest+ // commit first. Messages cannot hold a NUL.+ line := "This reverts commit " + landed.String()+ fields := strings.Split(stdout.String(), "\x00")+ for i := 0; i+1 < len(fields); i += 2 {+ if !strings.Contains(fields[i+1], line) {+ continue+ }- revertedBy, err := a.objectHash.FromHex(line)- if err != nil {- return "", structerr.NewInternal("looking for a revert of %s: %w", landed, err)+ revertedBy, err := a.objectHash.FromHex(strings.TrimSpace(fields[i]))+ if err != nil {+ return "", fmt.Errorf("looking for a revert of %s: %w", landed, err)+ }+ return revertedBy, nil}- return revertedBy, nil+ return "", nil}// lookupLand returns the last land of original on the branch reference of repository that thediff --git a/internal/gitaly/service/operations/land_retry_test.go b/internal/gitaly/service/operations/land_retry_test.goindex 6970854..bc4e456 100644--- a/internal/gitaly/service/operations/land_retry_test.go+++ b/internal/gitaly/service/operations/land_retry_test.go@@ -292,6 +292,44 @@ func testUserLandRetryAfterALostResponse(t *testing.T, ctx context.Context) {})}+// TestUserLand_revertBeyondTheSearchIsNotNamed lands a change that a later commit reverted, with+// more commits after the revert than the search for it reads: the land still answers already+// landed, and names no revert. The search only explains the answer. The test lowers+// revertSearchCommits, so it does not run in parallel.+func TestUserLand_revertBeyondTheSearchIsNotNamed(t *testing.T) {+ ctx, cfg, client := setupOperationsService(t, testhelper.Context(t))+ ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.GPGSigning, true)++ original := revertSearchCommits+ revertSearchCommits = 2+ t.Cleanup(func() { revertSearchCommits = original })++ fx := newRetryFixture(t, ctx, cfg)+ request := landRequest(fx.repoProto, git.DefaultBranch, fx.change)+ first, err := client.UserLand(ctx, request)+ require.NoError(t, err)+ landed := git.ObjectID(first.GetLandedCommitId())++ tip := gittest.WriteCommit(t, cfg, fx.repoPath,+ gittest.WithBranch(git.DefaultBranch),+ gittest.WithParents(landed),+ gittest.WithTreeEntries(fx.withoutY...),+ gittest.WithMessage(fmt.Sprintf("Revert \"y\"\n\nThis reverts commit %s.\n", landed)),+ )+ for i := 0; i < 2; i++ {+ tip = gittest.WriteCommit(t, cfg, fx.repoPath,+ gittest.WithBranch(git.DefaultBranch),+ gittest.WithParents(tip),+ gittest.WithTreeEntries(append(fx.withoutY, gittest.TreeEntry{Mode: "100644", Path: "z", Content: fmt.Sprint(i)})...),+ )+ }++ second, err := client.UserLand(ctx, request)+ require.NoError(t, err)+ requireLandedEarlier(t, second, landEntry(t, fx.repoPath, landed), landed, "")+ requireBranchAt(t, cfg, fx.repoPath, git.DefaultBranch, tip)+}+// TestUserLand_retryOfAResolvedLand retries a land whose conflicts the resolver resolved into a// merge commit: commit_id is a parent of that commit, so the retry finds it on the branch, as for a// merge, and the resolver is not asked again.