Skip to content

Git designed for AI agents.

PLAN11: land identity, so that a retried land never lands twice

Federico Eliadb38f9dd41bb
diff --git a/internal/git/gitcmd/hooks_payload.go b/internal/git/gitcmd/hooks_payload.go
index e414986..361e126 100644
--- a/internal/git/gitcmd/hooks_payload.go
+++ b/internal/git/gitcmd/hooks_payload.go
@@ -163,6 +163,12 @@ type ChangelogMeta struct {
// MembersTruncated is true when Members lacks information: the resolved paths
// were dropped or the list was cut to fit maxChangelogMembersSize.
MembersTruncated bool `json:"members_truncated,omitempty"`
+ // LandReference and Lands are the identities of the changes that the updates land on
+ // that branch (see changelog.WithLands and doc/graft/PLAN11.md). They are never cut:
+ // WithLands bounds them by changelog.MaxLandsSize and refuses a land whose identities
+ // do not fit. Only the server builds them from the context, like Members.
+ LandReference string `json:"land_reference,omitempty"`
+ Lands []changelog.Land `json:"lands,omitempty"`
}
// IsZero reports whether m holds no metadata. ChangelogMeta holds slices and
@@ -175,7 +181,9 @@ func (m ChangelogMeta) IsZero() bool {
m.Invocation == "" &&
m.MVCCCommit == "" &&
len(m.Members) == 0 &&
- !m.MembersTruncated
+ !m.MembersTruncated &&
+ m.LandReference == "" &&
+ len(m.Lands) == 0
}
// NewChangelogInvocation returns a new random value for ChangelogMeta.Invocation.
@@ -253,6 +261,7 @@ func NewHooksPayload(
id, hasIdentity := identity.FromContext(ctx)
members, membersTruncated := capChangelogMembers(changelog.BatchFromContext(ctx))
+ landReference, lands := changelog.LandsFromContext(ctx)
return HooksPayload{
Repo: &gitalypb.Repository{
@@ -281,6 +290,8 @@ func NewHooksPayload(
Rewritten: changelog.IsRewritten(ctx),
Members: members,
MembersTruncated: membersTruncated,
+ LandReference: landReference,
+ Lands: lands,
MVCCCommit: mvccCommitHandle(ctx, cfg),
},
}
diff --git a/internal/gitaly/config/config_graft.go b/internal/gitaly/config/config_graft.go
index db91fc0..5145509 100644
--- a/internal/gitaly/config/config_graft.go
+++ b/internal/gitaly/config/config_graft.go
@@ -249,6 +249,10 @@ const (
DefaultGraftLandBatchWindow = 10 * time.Millisecond
// DefaultGraftLandBatchMaxSize is the default GraftLandBatch.MaxSize.
DefaultGraftLandBatchMaxSize = 50
+ // MaxGraftLandBatchMaxSize is the largest GraftLandBatch.MaxSize. The update of a batch
+ // records the identity of every member (doc/graft/PLAN11.md) in the hooks payload, which
+ // bounds them (changelog.MaxLandsPerUpdate).
+ MaxGraftLandBatchMaxSize = 100
)
// WindowOrDefault parses Window, applying the default.
@@ -278,6 +282,9 @@ func (b GraftLandBatch) validate() error {
if b.MaxSize < 0 {
return fmt.Errorf("graft land batch: max_size must not be negative")
}
+ if b.MaxSize > MaxGraftLandBatchMaxSize {
+ return fmt.Errorf("graft land batch: max_size must not exceed %d", MaxGraftLandBatchMaxSize)
+ }
if _, err := b.TargetLatencyOrDefault(); err != nil {
return err
}
diff --git a/internal/gitaly/hook/storage_voter.go b/internal/gitaly/hook/storage_voter.go
index 4d56528..1c2b9dd 100644
--- a/internal/gitaly/hook/storage_voter.go
+++ b/internal/gitaly/hook/storage_voter.go
@@ -102,6 +102,8 @@ func changelogMetaOf(payload gitcmd.HooksPayload, graftMeta gitcmd.ChangelogMeta
Rewritten: graftMeta.Rewritten,
Members: graftMeta.Members,
MembersTruncated: graftMeta.MembersTruncated,
+ LandReference: graftMeta.LandReference,
+ Lands: graftMeta.Lands,
}
if meta.Operation == "" {
meta.Operation = changelog.UnknownOperation
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.go
index 1641f40..54f62df 100644
--- a/internal/gitaly/service/operations/land.go
+++ b/internal/gitaly/service/operations/land.go
@@ -191,7 +191,11 @@ func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest,
oldTip := tip
switch {
case step.alreadyLanded:
- return landResponse(tip, tip, false, true, false), nil
+ resp := landResponse(tip, tip, false, true, false)
+ if step.earlier != nil {
+ step.earlier.setOn(resp)
+ }
+ return resp, nil
case step.created:
oldTip = attempt.objectHash.ZeroOID
case step.tip == tip:
@@ -203,7 +207,8 @@ func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest,
return nil, err
}
- if err := s.updateLandedReference(ctx, in, attempt.quarantineDir, attempt.repo, reference, oldTip, step.tip, step.rewritten); err != nil {
+ lands := []changelog.Land{{Original: step.commit.String(), Landed: step.tip.String()}}
+ if err := s.updateLandedReference(ctx, in, attempt.quarantineDir, attempt.repo, reference, oldTip, step.tip, step.rewritten, lands); err != nil {
return nil, err
}
@@ -215,9 +220,10 @@ func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest,
return resp, nil
}
-// updateLandedReference performs the compare-and-swap of the branch. Hook rejections are returned
-// as PermissionDenied errors, lost races as landConflict and invalid requests or conflicting
-// references as InvalidArgument or FailedPrecondition. Everything else is Internal.
+// updateLandedReference performs the compare-and-swap of the branch, recording lands, the
+// identities of the changes it lands (doc/graft/PLAN11.md), in its changelog entry. Hook rejections
+// are returned as PermissionDenied errors, lost races as landConflict and invalid requests or
+// conflicting references as InvalidArgument or FailedPrecondition. Everything else is Internal.
func (s *Server) updateLandedReference(
ctx context.Context,
in *gitalypb.UserLandRequest,
@@ -226,11 +232,17 @@ func (s *Server) updateLandedReference(
reference git.ReferenceName,
oldOID, newOID git.ObjectID,
rewritten bool,
+ lands []changelog.Land,
) error {
if rewritten {
ctx = changelog.WithRewritten(ctx)
}
+ ctx, err := landWithRecord(ctx, reference, lands)
+ if err != nil {
+ return err
+ }
+
// On MVCC repositories a concurrent writer is detected when the snapshot is committed rather
// than by the compare-and-swap above. The commit then returns the registered error.
if cache := mvcc.CacheFromContext(ctx); cache != nil {
@@ -241,7 +253,7 @@ func (s *Server) updateLandedReference(
)
}
- err := s.updateReferenceWithHooks(ctx, in.GetRepository(), in.GetUser(), quarantineDir, reference, newOID, oldOID)
+ err = s.updateReferenceWithHooks(ctx, in.GetRepository(), in.GetUser(), quarantineDir, reference, newOID, oldOID)
if err == nil {
return nil
}
diff --git a/internal/gitaly/service/operations/land_attempt.go b/internal/gitaly/service/operations/land_attempt.go
index f1af65c..481febf 100644
--- a/internal/gitaly/service/operations/land_attempt.go
+++ b/internal/gitaly/service/operations/land_attempt.go
@@ -197,6 +197,16 @@ func (a *landingAttempt) decide(ctx context.Context, tip git.ObjectID, req *land
return landStep{commit: commitID, tip: commitID}, nil
}
+ // commit_id is not on the branch, but an earlier land may have put it there as a rewritten
+ // commit, whose response was lost. Its record answers, so the change is not landed twice.
+ earlier, err := a.landedEarlier(ctx, req.reference(), tip, commitID)
+ if err != nil {
+ return landStep{}, err
+ }
+ if earlier != nil {
+ return landStep{commit: commitID, tip: tip, alreadyLanded: true, earlier: earlier}, nil
+ }
+
newTip, err := a.computeTip(ctx, in, plan, tip, commitID, req.committer)
var resolution *landResolution
if err != nil {
diff --git a/internal/gitaly/service/operations/land_batch.go b/internal/gitaly/service/operations/land_batch.go
index a8a0483..4897636 100644
--- a/internal/gitaly/service/operations/land_batch.go
+++ b/internal/gitaly/service/operations/land_batch.go
@@ -64,8 +64,11 @@ type landStep struct {
tip git.ObjectID
rewritten bool
alreadyLanded bool
- created bool
- resolution *landResolution
+ // earlier is the earlier land of the request that is still on the branch, when it is what
+ // tells that the request is already landed (see landingAttempt.landedEarlier).
+ earlier *landIdentity
+ created bool
+ resolution *landResolution
// tipIndependent is set with an error that does not depend on the running tip: the commit ID is
// unparsable or does not resolve to a commit. Such an error is final wherever the request is.
tipIndependent bool
@@ -106,8 +109,11 @@ type landBatchOutcome struct {
LandedCommit git.ObjectID
Rewritten bool
AlreadyLanded bool
- Created bool
- Resolution *landResolution
+ // Earlier is the earlier land that tells that the request is already landed, when the record
+ // of the land tells it rather than the reachability of the commit.
+ Earlier *landIdentity
+ Created bool
+ Resolution *landResolution
// BatchSize is the number of requests of the batch, the same for every response of the batch.
BatchSize int
// BatchPosition is the zero-based index of the request in the batch, in submission
@@ -335,6 +341,7 @@ func (e landBatchExecutor) attempt(ctx context.Context, reqs []*landBatchRequest
case step.alreadyLanded:
outs[pos].LandedCommit = cur
outs[pos].AlreadyLanded = true
+ outs[pos].Earlier = step.earlier
// The answer depends on the tip only while no member precedes the request. A member
// that does not reach the branch would take the answer away.
diff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.go
index 7435585..378e5a2 100644
--- a/internal/gitaly/service/operations/land_batch_server.go
+++ b/internal/gitaly/service/operations/land_batch_server.go
@@ -204,6 +204,9 @@ func landBatchResult(outcome landBatchOutcome) landlane.Result[*gitalypb.UserLan
resp.Attempts = int32(outcome.Attempts)
resp.BatchSize = int32(outcome.BatchSize)
resp.BatchPosition = int32(outcome.BatchPosition + 1)
+ if outcome.Earlier != nil {
+ outcome.Earlier.setOn(resp)
+ }
if outcome.Resolution != nil {
resp.ResolvedPaths = outcome.Resolution.resolvedPaths
resp.Resolver = outcome.Resolution.resolver
@@ -310,7 +313,7 @@ func (st *serverLandBatchStepper) Update(ctx context.Context, reference git.Refe
User: landBatchIdentity,
}
- return st.updateKind(st.server.updateLandedReference(ctx, in, st.quarantineDir, st.repo, reference, oldOID, newTip, rewritten))
+ return st.updateKind(st.server.updateLandedReference(ctx, in, st.quarantineDir, st.repo, reference, oldOID, newTip, rewritten, landsOf(members)))
}
// UpdateSolo publishes a batch of one member as a land: the update runs with the identity of the member
@@ -331,7 +334,17 @@ func (st *serverLandBatchStepper) UpdateSolo(ctx context.Context, req *landBatch
ctx = requestIdentityContext(ctx, req)
ctx = changelog.WithBatch(changelog.WithOperation(ctx, changelogOperationLand), []changelog.Member{member})
- return st.updateKind(st.server.updateLandedReference(ctx, req.in, st.quarantineDir, st.repo, reference, oldOID, newTip, rewritten))
+ return st.updateKind(st.server.updateLandedReference(ctx, req.in, st.quarantineDir, st.repo, reference, oldOID, newTip, rewritten, landsOf([]changelog.Member{member})))
+}
+
+// landsOf returns the identities of the changes that members land (doc/graft/PLAN11.md): each
+// member's original commit and the commit of the chain that holds it.
+func landsOf(members []changelog.Member) []changelog.Land {
+ lands := make([]changelog.Land, len(members))
+ for i, member := range members {
+ lands[i] = changelog.Land{Original: member.OriginalCommit, Landed: member.Commit}
+ }
+ return lands
}
// changelogOperationLand is the operation of an unbatched land in the changelog. The lanes run outside
diff --git a/internal/gitaly/service/operations/land_identity.go b/internal/gitaly/service/operations/land_identity.go
new file mode 100644
index 0000000..d5f529c
--- /dev/null
+++ b/internal/gitaly/service/operations/land_identity.go
@@ -0,0 +1,168 @@
+package operations
+
+import (
+ "bytes"
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+
+ "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/localrepo"
+ "github.com/feder-cr/git-ai/internal/graft/changelog"
+ "github.com/feder-cr/git-ai/internal/graft/corestore"
+ "github.com/feder-cr/git-ai/internal/structerr"
+ "github.com/feder-cr/git-ai/proto/go/gitalypb"
+)
+
+// landIdentity is the earlier land of a change that is still on the branch (doc/graft/PLAN11.md,
+// section 5): the record of the changelog that landed the change, and the commit that reverted it
+// later, if one is found.
+type landIdentity struct {
+ // landed is the commit the earlier land put on the branch, which holds the change.
+ landed git.ObjectID
+ // sequence is the changelog sequence of the update of the earlier land.
+ sequence uint64
+ // revertedBy is the commit after landed on the branch that says it reverts landed, empty when
+ // none is found.
+ revertedBy git.ObjectID
+}
+
+// detail explains the answer, see UserLandResponse.already_landed_detail.
+func (id *landIdentity) detail() string {
+ detail := fmt.Sprintf("landed as %s at changelog %d", id.landed, id.sequence)
+ if id.revertedBy != "" {
+ detail += fmt.Sprintf("; it was reverted later by %s", id.revertedBy)
+ }
+ return detail
+}
+
+// setOn reports the earlier land on the response of an already landed request.
+func (id *landIdentity) setOn(resp *gitalypb.UserLandResponse) {
+ resp.LandedCommitId = id.landed.String()
+ resp.LandedSequence = id.sequence
+ resp.RevertedByCommitId = id.revertedBy.String()
+ resp.AlreadyLandedDetail = id.detail()
+}
+
+// landWithRecord returns the binding that records the identities of lands on the branch reference
+// in the changelog of the update performed with ctx (changelog.WithLands). The land must not run
+// when it fails: it would land without its identity.
+func landWithRecord(ctx context.Context, reference git.ReferenceName, lands []changelog.Land) (context.Context, error) {
+ ctx, err := changelog.WithLands(ctx, reference.String(), lands)
+ if err != nil {
+ return ctx, structerr.NewInternal("recording the land identities: %w", err)
+ }
+ return ctx, nil
+}
+
+// landedEarlier returns the earlier land of commitID on the branch reference when it is still on
+// the branch, that is when the commit it landed as is reachable from tip; nil otherwise. A change
+// whose commit is not reachable from tip may have landed as a rewritten commit, and the record of
+// that land is the only way to tell (doc/graft/PLAN11.md, section 5). Only then is the commit that
+// reverted it looked for, to explain the answer.
+func (a *landingAttempt) landedEarlier(ctx context.Context, reference git.ReferenceName, tip, commitID git.ObjectID) (*landIdentity, error) {
+ record, found, err := a.server.lookupLand(ctx, a.repository, reference, commitID)
+ if err != nil {
+ return nil, err
+ }
+ if !found {
+ return nil, nil
+ }
+
+ landed, err := a.objectHash.FromHex(record.Landed)
+ if err != nil {
+ return nil, structerr.NewInternal("land identity of %s: %w", commitID, err)
+ }
+
+ onBranch, err := a.repo.IsAncestor(ctx, landed.Revision(), tip.Revision())
+ if err != nil {
+ // The branch was reset past the earlier land and its commit was pruned since: the change
+ // is no longer on the branch.
+ if errors.As(err, new(localrepo.InvalidCommitError)) {
+ return nil, nil
+ }
+ return nil, structerr.NewInternal("checking the earlier land of %s: %w", commitID, err)
+ }
+ if !onBranch {
+ return nil, nil
+ }
+
+ revertedBy, err := a.revertOf(ctx, landed, tip)
+ if err != nil {
+ return nil, err
+ }
+
+ return &landIdentity{landed: landed, sequence: record.Sequence, revertedBy: revertedBy}, nil
+}
+
+// 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.
+func (a *landingAttempt) revertOf(ctx context.Context, landed, tip git.ObjectID) (git.ObjectID, error) {
+ var stdout, stderr bytes.Buffer
+ if 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()},
+ },
+ 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())
+ }
+
+ line := strings.TrimSpace(stdout.String())
+ if line == "" {
+ return "", nil
+ }
+
+ revertedBy, err := a.objectHash.FromHex(line)
+ if err != nil {
+ return "", structerr.NewInternal("looking for a revert of %s: %w", landed, err)
+ }
+
+ return revertedBy, nil
+}
+
+// lookupLand returns the last land of original on the branch reference of repository that the
+// changelog recorded, from where the changelog lives: the storage core's database in its
+// authoritative mode, the file log of the repository otherwise. Without a changelog nothing is
+// recorded, and nothing is found.
+func (s *Server) lookupLand(ctx context.Context, repository *gitalypb.Repository, reference git.ReferenceName, original git.ObjectID) (changelog.LandRecord, bool, error) {
+ if s.graftStorage.Authoritative() {
+ repo, err := s.graftStorage.Meta.GetRepository(ctx, corestore.KeyOf(repository))
+ if err != nil {
+ // A repository that the database does not track has no recorded land. Whether it
+ // can be written to is the write path's decision, as for any land.
+ if errors.Is(err, corestore.ErrRepositoryNotFound) {
+ return changelog.LandRecord{}, false, nil
+ }
+ return changelog.LandRecord{}, false, structerr.NewUnavailable("storage core: %w", err)
+ }
+
+ record, found, err := s.graftStorage.Meta.LookupLand(ctx, repo.ID, reference.String(), original.String())
+ if err != nil {
+ return changelog.LandRecord{}, false, structerr.NewUnavailable("storage core: %w", err)
+ }
+ return record, found, nil
+ }
+
+ if s.graft.Changelog.Disabled {
+ return changelog.LandRecord{}, false, nil
+ }
+
+ repoPath, err := s.locator.GetRepoPath(ctx, repository)
+ if err != nil {
+ return changelog.LandRecord{}, false, err
+ }
+
+ record, found, err := changelog.LookupLand(repoPath, reference.String(), original.String())
+ if err != nil {
+ return changelog.LandRecord{}, false, structerr.NewInternal("looking up the land identity: %w", err)
+ }
+ return record, found, nil
+}
diff --git a/internal/gitaly/service/operations/land_identity_test.go b/internal/gitaly/service/operations/land_identity_test.go
new file mode 100644
index 0000000..152b4ea
--- /dev/null
+++ b/internal/gitaly/service/operations/land_identity_test.go
@@ -0,0 +1,105 @@
+package operations
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/internal/graft/changelog"
+)
+
+// TestLandBatch_retryAfterALostResponse lands two changes by rebase in one batch, and then lands
+// them again, as clients do whose responses were lost: the batch records the identity of every
+// member (doc/graft/PLAN11.md), and each retried member answers already landed as the commit its
+// first land put on the branch.
+func TestLandBatch_retryAfterALostResponse(t *testing.T) {
+ t.Parallel()
+
+ runLandBatchTest(t, testLandBatchRetryAfterALostResponse)
+}
+
+func testLandBatchRetryAfterALostResponse(t *testing.T, ctx context.Context) {
+ t.Parallel()
+
+ ctx, cfg, client := setupAgentLandService(t, ctx, agentBatchingGraft(nil))
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
+
+ baseEntry := gittest.TreeEntry{Mode: "100644", Path: "base", Content: "base"}
+ base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(baseEntry))
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithParents(base),
+ gittest.WithTreeEntries(baseEntry, gittest.TreeEntry{Mode: "100644", Path: "moved", Content: "moved"}))
+ change := func(name string) git.ObjectID {
+ return gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base),
+ gittest.WithTreeEntries(baseEntry, gittest.TreeEntry{Mode: "100644", Path: name, Content: name}))
+ }
+ a, b := change("a"), change("b")
+
+ first, errs := landInOrder(ctx, client, repoProto,
+ agentLanding{agent: "agent-a", commit: a},
+ agentLanding{agent: "agent-b", commit: b},
+ )
+ for i := range errs {
+ require.NoError(t, errs[i])
+ require.Equal(t, int32(2), first[i].GetBatchSize(), "both changes land in one batch")
+ require.True(t, first[i].GetRewritten())
+ }
+ landedA, landedB := git.ObjectID(first[0].GetLandedCommitId()), git.ObjectID(first[1].GetLandedCommitId())
+
+ // The batch lands its members in the order they reached the lane, which the test does not
+ // fix: the tip is the commit of the last of them.
+ tip := gittest.ResolveRevision(t, cfg, repoPath, git.DefaultRef.String())
+ require.Contains(t, []git.ObjectID{landedA, landedB}, tip)
+
+ entry := landEntry(t, repoPath, tip)
+ require.Equal(t, changelog.OperationLandBatch, entry.Operation)
+ require.ElementsMatch(t, []changelog.Land{
+ {Original: a.String(), Landed: landedA.String()},
+ {Original: b.String(), Landed: landedB.String()},
+ }, entry.Lands)
+ require.Equal(t, tip.String(), entry.Lands[len(entry.Lands)-1].Landed, "the lands are in the order of the batch")
+
+ second, errs := landInOrder(ctx, client, repoProto,
+ agentLanding{agent: "agent-a", commit: a},
+ agentLanding{agent: "agent-b", commit: b},
+ )
+ for i, landed := range []git.ObjectID{landedA, landedB} {
+ require.NoError(t, errs[i])
+ requireLandedEarlier(t, second[i], entry, landed, "")
+ require.Equal(t, tip.String(), second[i].GetBranchUpdate().GetCommitId())
+ }
+ requireBranchAt(t, cfg, repoPath, git.DefaultBranch, tip)
+}
+
+// TestLandBatch_retryOfABatchOfOne retries a change that landed alone in its lane, which publishes
+// it as a land of its own (UpdateSolo): its identity is recorded, and the retry answers from it.
+func TestLandBatch_retryOfABatchOfOne(t *testing.T) {
+ t.Parallel()
+
+ runLandBatchTest(t, testLandBatchRetryOfABatchOfOne)
+}
+
+func testLandBatchRetryOfABatchOfOne(t *testing.T, ctx context.Context) {
+ t.Parallel()
+
+ ctx, cfg, client := setupAgentLandService(t, ctx, batchingGraft())
+ fx := newRetryFixture(t, ctx, cfg)
+
+ request := landRequest(fx.repoProto, git.DefaultBranch, fx.change)
+ first, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ require.Equal(t, int32(1), first.GetBatchSize())
+ require.True(t, first.GetRewritten())
+ landed := git.ObjectID(first.GetLandedCommitId())
+
+ entry := landEntry(t, fx.repoPath, landed)
+ require.Equal(t, changelogOperationLand, entry.Operation)
+ require.Equal(t, []changelog.Land{{Original: fx.change.String(), Landed: landed.String()}}, entry.Lands)
+
+ second, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ requireLandedEarlier(t, second, entry, landed, "")
+ requireBranchAt(t, cfg, fx.repoPath, git.DefaultBranch, landed)
+}
diff --git a/internal/gitaly/service/operations/land_retry_test.go b/internal/gitaly/service/operations/land_retry_test.go
index e661290..6970854 100644
--- a/internal/gitaly/service/operations/land_retry_test.go
+++ b/internal/gitaly/service/operations/land_retry_test.go
@@ -2,6 +2,7 @@ package operations
import (
"context"
+ "fmt"
"testing"
"github.com/stretchr/testify/require"
@@ -9,15 +10,88 @@ import (
"github.com/feder-cr/git-ai/internal/featureflag"
"github.com/feder-cr/git-ai/internal/git"
"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/changelog"
"github.com/feder-cr/git-ai/internal/testhelper"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
+// retryFixture is a repository whose branch main moved on from base with a commit that adds x,
+// and a change from base that adds y, which lands on main by a rebase or a merge.
+type retryFixture struct {
+ repoProto *gitalypb.Repository
+ repoPath string
+ base git.ObjectID
+ tip git.ObjectID
+ change git.ObjectID
+ // withoutY is the tree of main before the change.
+ withoutY []gittest.TreeEntry
+}
+
+func newRetryFixture(t *testing.T, ctx context.Context, cfg config.Cfg) retryFixture {
+ t.Helper()
+
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ withoutY := []gittest.TreeEntry{
+ {Mode: "100644", Path: "a", Content: "a"},
+ {Mode: "100644", Path: "x", Content: "x"},
+ }
+ base := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"}),
+ )
+ tip := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithParents(base), gittest.WithTreeEntries(withoutY...))
+ change := gittest.WriteCommit(t, cfg, repoPath,
+ gittest.WithParents(base),
+ gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
+ gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"},
+ ),
+ )
+
+ return retryFixture{repoProto: repoProto, repoPath: repoPath, base: base, tip: tip, change: change, withoutY: withoutY}
+}
+
+// landEntry returns the changelog entry of the update of main to landed.
+func landEntry(t *testing.T, repoPath string, landed git.ObjectID) changelog.Entry {
+ t.Helper()
+
+ entries, err := changelog.List(repoPath, 0, 1000)
+ require.NoError(t, err)
+
+ var found []changelog.Entry
+ for _, entry := range entries {
+ if entry.Reference == git.DefaultRef.String() && entry.NewOID == landed.String() {
+ found = append(found, entry)
+ }
+ }
+ require.Len(t, found, 1, "the changelog records the update of main to %s once", landed)
+ return found[0]
+}
+
+// requireLandedEarlier checks the answer of a land that the record of the earlier land at entry
+// answered: already landed as the commit of that land, with its sequence and the revert found.
+func requireLandedEarlier(t *testing.T, resp *gitalypb.UserLandResponse, entry changelog.Entry, landed, revertedBy git.ObjectID) {
+ t.Helper()
+
+ require.True(t, resp.GetAlreadyLanded())
+ require.False(t, resp.GetRewritten())
+ require.Equal(t, landed.String(), resp.GetLandedCommitId())
+ require.Equal(t, entry.Sequence, resp.GetLandedSequence())
+ require.Equal(t, revertedBy.String(), resp.GetRevertedByCommitId())
+
+ detail := fmt.Sprintf("landed as %s at changelog %d", landed, entry.Sequence)
+ if revertedBy != "" {
+ detail += fmt.Sprintf("; it was reverted later by %s", revertedBy)
+ }
+ require.Equal(t, detail, resp.GetAlreadyLandedDetail())
+}
+
// TestUserLand_retryAfterALostResponse lands a change twice, as a client does that retries
// Unavailable after the response of a land that succeeded was lost (sdk.UnavailableRetry): the
-// change is never landed twice. With a merge, the change itself is on the branch, so the second
-// land reports it already landed. With a rebase, the branch holds a rebased copy: the second land
-// replays the change, which comes out empty, and leaves the branch unchanged.
+// change is never landed twice (doc/graft/PLAN11.md). With a merge or a fast-forward, the change
+// itself is on the branch, so the second land finds it reachable. With a rebase, the branch holds a
+// rebased copy, and the record of the first land answers.
func TestUserLand_retryAfterALostResponse(t *testing.T) {
t.Parallel()
@@ -32,91 +106,218 @@ func testUserLandRetryAfterALostResponse(t *testing.T, ctx context.Context) {
for _, tc := range []struct {
desc string
strategy gitalypb.UserLandRequest_Strategy
- // alreadyLanded is what the second land reports.
- alreadyLanded bool
+ // fastForward lands the change onto base, which it fast-forwards.
+ fastForward bool
+ // commits is the number of commits of the change; the request lands the last.
+ commits int
+ // fromRecord is whether the record of the first land answers the second.
+ fromRecord bool
}{
- {desc: "rebase", strategy: gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED},
- {desc: "merge", strategy: gitalypb.UserLandRequest_STRATEGY_MERGE, alreadyLanded: true},
+ {desc: "rebase", strategy: gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED, commits: 1, fromRecord: true},
+ {desc: "rebase of several commits", strategy: gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED, commits: 3, fromRecord: true},
+ {desc: "merge", strategy: gitalypb.UserLandRequest_STRATEGY_MERGE, commits: 1},
+ {desc: "fast-forward", strategy: gitalypb.UserLandRequest_STRATEGY_FF_ONLY, fastForward: true, commits: 1},
} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
- repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
- base := gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithBranch(git.DefaultBranch),
- gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"}),
- )
- gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithBranch(git.DefaultBranch),
- gittest.WithParents(base),
- gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
- gittest.TreeEntry{Mode: "100644", Path: "x", Content: "x"},
- ),
- )
- change := gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithParents(base),
- gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
- gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"},
- ),
- )
-
- request := landRequest(repoProto, git.DefaultBranch, change)
+ fx := newRetryFixture(t, ctx, cfg)
+ if tc.fastForward {
+ gittest.Exec(t, cfg, "-C", fx.repoPath, "update-ref", git.DefaultRef.String(), fx.base.String())
+ }
+
+ change := fx.change
+ for i := 1; i < tc.commits; i++ {
+ change = gittest.WriteCommit(t, cfg, fx.repoPath,
+ gittest.WithParents(change),
+ gittest.WithTreeEntries(
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
+ gittest.TreeEntry{Mode: "100644", Path: "y", Content: fmt.Sprintf("y%d", i)},
+ ),
+ )
+ }
+
+ request := landRequest(fx.repoProto, git.DefaultBranch, change)
request.Strategy = tc.strategy
first, err := client.UserLand(ctx, request)
require.NoError(t, err)
- require.True(t, first.GetRewritten())
+ require.Equal(t, !tc.fastForward, first.GetRewritten())
landed := git.ObjectID(first.GetLandedCommitId())
- commits := gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--count", landed.String())
+ commits := gittest.Exec(t, cfg, "-C", fx.repoPath, "rev-list", "--count", landed.String())
+
+ // Every land records its identity: the commit of the request and the commit it landed as.
+ entry := landEntry(t, fx.repoPath, landed)
+ require.Equal(t, []changelog.Land{{Original: change.String(), Landed: landed.String()}}, entry.Lands)
// The retry of the same request.
second, err := client.UserLand(ctx, request)
require.NoError(t, err)
- require.Equal(t, tc.alreadyLanded, second.GetAlreadyLanded())
+ require.True(t, second.GetAlreadyLanded())
require.False(t, second.GetRewritten())
- require.Equal(t, landed.String(), second.GetLandedCommitId(), "the branch is left at the first land")
- requireBranchAt(t, cfg, repoPath, git.DefaultBranch, landed)
- require.Equal(t, commits, gittest.Exec(t, cfg, "-C", repoPath, "rev-list", "--count", git.DefaultBranch))
+ require.Equal(t, landed.String(), second.GetLandedCommitId(), "the answer names the commit of the first land")
+ require.Equal(t, landed.String(), second.GetBranchUpdate().GetCommitId())
+ if tc.fromRecord {
+ requireLandedEarlier(t, second, entry, landed, "")
+ } else {
+ require.Zero(t, second.GetLandedSequence())
+ require.Empty(t, second.GetAlreadyLandedDetail())
+ }
+
+ requireBranchAt(t, cfg, fx.repoPath, git.DefaultBranch, landed)
+ require.Equal(t, commits, gittest.Exec(t, cfg, "-C", fx.repoPath, "rev-list", "--count", git.DefaultBranch))
+ landEntry(t, fx.repoPath, landed)
})
}
- t.Run("rebase, after a revert of the change", func(t *testing.T) {
+ t.Run("after a revert of the change", func(t *testing.T) {
t.Parallel()
- // A change that a later commit reverted is replayed again, as git's cherry-pick would.
- repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
- withoutY := []gittest.TreeEntry{
- {Mode: "100644", Path: "a", Content: "a"},
- {Mode: "100644", Path: "x", Content: "x"},
+ for _, revert := range []struct {
+ desc string
+ // message is the message of the revert of landed, empty for a revert that does not say
+ // what it reverts.
+ message func(landed git.ObjectID) string
+ named bool
+ }{
+ {
+ desc: "git revert",
+ message: func(landed git.ObjectID) string {
+ return fmt.Sprintf("Revert \"y\"\n\nThis reverts commit %s.\n", landed)
+ },
+ named: true,
+ },
+ {
+ desc: "graft-verify",
+ message: func(landed git.ObjectID) string {
+ return fmt.Sprintf("graft-verify: revert %s\n\nThis reverts commit %s, which failed the verification of main.\n", landed.String()[:12], landed)
+ },
+ named: true,
+ },
+ {
+ desc: "a revert that does not say so",
+ message: func(git.ObjectID) string { return "drop y\n" },
+ },
+ } {
+ t.Run(revert.desc, func(t *testing.T) {
+ t.Parallel()
+
+ // The change landed and a later commit reverted it. The retry of its land, or
+ // a new land of the same commit, does not land it again: it answers already
+ // landed, the revert stays, and nothing is replayed.
+ 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())
+
+ reverted := gittest.WriteCommit(t, cfg, fx.repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithParents(landed),
+ gittest.WithTreeEntries(fx.withoutY...),
+ gittest.WithMessage(revert.message(landed)),
+ )
+ later := gittest.WriteCommit(t, cfg, fx.repoPath,
+ gittest.WithBranch(git.DefaultBranch),
+ gittest.WithParents(reverted),
+ gittest.WithTreeEntries(append(fx.withoutY, gittest.TreeEntry{Mode: "100644", Path: "z", Content: "z"})...),
+ )
+
+ second, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+
+ var revertedBy git.ObjectID
+ if revert.named {
+ revertedBy = reverted
+ }
+ requireLandedEarlier(t, second, landEntry(t, fx.repoPath, landed), landed, revertedBy)
+ require.Equal(t, later.String(), second.GetBranchUpdate().GetCommitId())
+ require.Equal(t, later.String(), second.GetOldTip())
+ requireBranchAt(t, cfg, fx.repoPath, git.DefaultBranch, later)
+ })
}
- base := gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithBranch(git.DefaultBranch),
- gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"}),
- )
- gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithParents(base), gittest.WithTreeEntries(withoutY...))
- change := gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithParents(base),
- gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a"},
- gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"},
- ),
- )
+ })
- request := landRequest(repoProto, git.DefaultBranch, change)
+ t.Run("after the branch was reset past the land", func(t *testing.T) {
+ t.Parallel()
+
+ // The change landed, and the branch was reset to the tip before it: the change is no
+ // longer on the branch, and the land lands it again, as a new land.
+ fx := newRetryFixture(t, ctx, cfg)
+ request := landRequest(fx.repoProto, git.DefaultBranch, fx.change)
first, err := client.UserLand(ctx, request)
require.NoError(t, err)
- reverted := gittest.WriteCommit(t, cfg, repoPath,
- gittest.WithBranch(git.DefaultBranch),
- gittest.WithParents(git.ObjectID(first.GetLandedCommitId())),
- gittest.WithTreeEntries(withoutY...),
+
+ moved := gittest.WriteCommit(t, cfg, fx.repoPath,
+ gittest.WithParents(fx.tip),
+ gittest.WithTreeEntries(append(fx.withoutY, gittest.TreeEntry{Mode: "100644", Path: "w", Content: "w"})...),
)
+ gittest.Exec(t, cfg, "-C", fx.repoPath, "update-ref", git.DefaultRef.String(), moved.String(), first.GetLandedCommitId())
second, err := client.UserLand(ctx, request)
require.NoError(t, err)
+ require.False(t, second.GetAlreadyLanded())
require.True(t, second.GetRewritten())
- require.Equal(t, reverted.String(), second.GetOldTip())
- gittest.RequireTree(t, cfg, repoPath, second.GetLandedCommitId(), append(withoutY, gittest.TreeEntry{Mode: "100644", Path: "y", Content: "y"}))
+ require.Zero(t, second.GetLandedSequence())
+ require.Equal(t, moved.String(), second.GetOldTip())
+ relanded := git.ObjectID(second.GetLandedCommitId())
+ gittest.RequireTree(t, cfg, fx.repoPath, relanded.String(), []gittest.TreeEntry{
+ {Mode: "100644", Path: "a", Content: "a"},
+ {Mode: "100644", Path: "w", Content: "w"},
+ {Mode: "100644", Path: "x", Content: "x"},
+ {Mode: "100644", Path: "y", Content: "y"},
+ })
+
+ // The new land replaced the record: a retry of it answers with the new land.
+ third, err := client.UserLand(ctx, request)
+ require.NoError(t, err)
+ requireLandedEarlier(t, third, landEntry(t, fx.repoPath, relanded), relanded, "")
})
+
+ t.Run("on another branch", func(t *testing.T) {
+ t.Parallel()
+
+ // The identity of a land is the branch and the commit: the change landed on main is a new
+ // land on another branch.
+ fx := newRetryFixture(t, ctx, cfg)
+ gittest.Exec(t, cfg, "-C", fx.repoPath, "branch", "other", fx.tip.String())
+
+ _, err := client.UserLand(ctx, landRequest(fx.repoProto, git.DefaultBranch, fx.change))
+ require.NoError(t, err)
+
+ other, err := client.UserLand(ctx, landRequest(fx.repoProto, "other", fx.change))
+ require.NoError(t, err)
+ require.False(t, other.GetAlreadyLanded())
+ require.True(t, other.GetRewritten())
+ })
+}
+
+// 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.
+func TestUserLand_retryOfAResolvedLand(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "")
+ writeAnswer(t, dir, validAnswer())
+ fx := newResolveFixture(t, enabledResolver(argv))
+ request := fx.request("Land agent work")
+
+ first, err := fx.client.UserLand(fx.ctx, request)
+ require.NoError(t, err)
+ require.True(t, first.GetRewritten())
+ require.NotEmpty(t, first.GetResolvedPaths())
+ require.Equal(t, 1, resolverCalls(t, dir))
+ landed := git.ObjectID(first.GetLandedCommitId())
+
+ require.Equal(t, []changelog.Land{{Original: fx.commit.String(), Landed: landed.String()}}, landEntry(t, fx.repoPath, landed).Lands)
+
+ second, err := fx.client.UserLand(fx.ctx, request)
+ require.NoError(t, err)
+ require.True(t, second.GetAlreadyLanded())
+ require.Equal(t, landed.String(), second.GetLandedCommitId())
+ require.Zero(t, second.GetLandedSequence())
+ require.Empty(t, second.GetResolvedPaths())
+ require.Equal(t, 1, resolverCalls(t, dir), "the retry does not resolve again")
+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, landed)
}
diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.go
index 2230dfc..84901c3 100644
--- a/internal/gitaly/service/operations/server.go
+++ b/internal/gitaly/service/operations/server.go
@@ -46,6 +46,9 @@ type Server struct {
// storageSync catches up the local repository from the storage core before a land is retried,
// see doc/graft/STORAGE.md. It does nothing unless the storage core is authoritative.
storageSync corestore.Freshener
+ // graftStorage is the storage core. In its authoritative mode the land identities are looked
+ // up in its database (doc/graft/PLAN11.md). Nil without a storage core.
+ graftStorage *corestore.Core
// testHook, when set by a test, is called at named points of a request.
testHook func(point string)
}
@@ -81,6 +84,7 @@ func NewServer(deps *service.Dependencies) *Server {
conflictResolver: conflictResolver,
shutdownCtx: deps.GetShutdownContext(),
storageSync: deps.GetGraftStorageSyncer(),
+ graftStorage: deps.GetGraftStorage(),
}
// The lanes may run the batches with the server, so they are taken after it is created.
diff --git a/internal/graft/changelog/lands.go b/internal/graft/changelog/lands.go
new file mode 100644
index 0000000..973b52c
--- /dev/null
+++ b/internal/graft/changelog/lands.go
@@ -0,0 +1,137 @@
+package changelog
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "strings"
+)
+
+// Land is the identity of one change that a land put on a branch (doc/graft/PLAN11.md): the commit
+// the request submitted and the commit on the branch that holds the change. Landed is the rebased
+// copy of Original, the merge commit of a merged or resolved land, or Original itself for a
+// fast-forward or a created branch.
+type Land struct {
+ // Original is the commit the land request submitted, its commit_id peeled to a commit.
+ Original string `json:"original"`
+ // Landed is the commit on the branch that holds the change.
+ Landed string `json:"landed"`
+}
+
+// LandRecord is what the index of a branch knows about the land of one original commit: the
+// changelog entry that recorded it and the commit it landed as. A later land of the same original
+// commit replaces it (see LookupLand).
+type LandRecord struct {
+ // Sequence is the sequence of the changelog entry that recorded the land.
+ Sequence uint64
+ // Landed is the commit on the branch that holds the change, see Land.Landed.
+ Landed string
+}
+
+const (
+ // MaxLandsSize bounds the encoded size of the identities of one update (the reference and its
+ // lands), which travel in the hooks payload, in the environment of every git process of the
+ // update, next to the members (see gitcmd.ChangelogMeta). Unlike the members, the identities are
+ // never cut: an update whose identities do not fit fails before it runs (WithLands). With the
+ // limits of the server it cannot happen: a batch has at most MaxLandsPerUpdate members, a land
+ // of a SHA-256 repository encodes in 156 bytes, and a branch name of 4096 bytes, which no files
+ // reference backend exceeds, encodes in at most 6 bytes per byte: 40 KiB hold both with room.
+ MaxLandsSize = 40 << 10
+ // MaxLandsPerUpdate is the most lands that one update records: the largest batch the
+ // configuration allows (config.MaxGraftLandBatchMaxSize).
+ MaxLandsPerUpdate = 100
+)
+
+// encodedLands is the encoded form of the identities of one update whose size MaxLandsSize bounds.
+type encodedLands struct {
+ Reference string `json:"land_reference"`
+ Lands []Land `json:"lands"`
+}
+
+// ValidateLands checks the identities that an update of reference records: reference is a branch,
+// every land names two full object IDs, there are at most MaxLandsPerUpdate of them and their
+// encoding fits MaxLandsSize.
+func ValidateLands(reference string, lands []Land) error {
+ if !strings.HasPrefix(reference, "refs/heads/") {
+ return fmt.Errorf("land identities of %q: not a branch", reference)
+ }
+ if len(lands) == 0 {
+ return fmt.Errorf("land identities of %q: no lands", reference)
+ }
+ if len(lands) > MaxLandsPerUpdate {
+ return fmt.Errorf("land identities of %q: %d lands, at most %d per update", reference, len(lands), MaxLandsPerUpdate)
+ }
+
+ for i, land := range lands {
+ if !isObjectID(land.Original) || !isObjectID(land.Landed) {
+ return fmt.Errorf("land identities of %q: land %d: %q -> %q is not a pair of object IDs", reference, i, land.Original, land.Landed)
+ }
+ }
+
+ encoded, err := json.Marshal(encodedLands{Reference: reference, Lands: lands})
+ if err != nil {
+ return fmt.Errorf("land identities of %q: %w", reference, err)
+ }
+ if len(encoded) > MaxLandsSize {
+ return fmt.Errorf("land identities of %q: %d bytes, at most %d", reference, len(encoded), MaxLandsSize)
+ }
+
+ return nil
+}
+
+// isObjectID reports whether s is a full SHA-1 or SHA-256 object ID in lower-case hex, as git
+// prints them.
+func isObjectID(s string) bool {
+ if len(s) != 40 && len(s) != 64 {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
+ return false
+ }
+ }
+
+ return true
+}
+
+type landsKey struct{}
+
+// landsValue is what WithLands stores in a context.
+type landsValue struct {
+ reference string
+ lands []Land
+}
+
+// WithLands records the identities of the lands that the update of reference performed with ctx
+// lands (doc/graft/PLAN11.md). The update's changelog entry for reference then carries them as
+// Entry.Lands, and the index of the land identities learns them. It fails, without changing ctx,
+// when they do not pass ValidateLands: the land must then not run, as it would land without its
+// identity.
+func WithLands(ctx context.Context, reference string, lands []Land) (context.Context, error) {
+ if err := ValidateLands(reference, lands); err != nil {
+ return ctx, err
+ }
+
+ return context.WithValue(ctx, landsKey{}, landsValue{reference: reference, lands: lands}), nil
+}
+
+// LandsFromContext returns the reference and the lands set by WithLands, or an empty reference and
+// no lands when there are none.
+func LandsFromContext(ctx context.Context) (string, []Land) {
+ value, _ := ctx.Value(landsKey{}).(landsValue)
+ return value.reference, value.lands
+}
+
+// LandsOf returns the lands that the changelog entry of an update of reference carries under meta:
+// a copy of the lands of meta when they belong to reference, none otherwise (see Entry.Lands).
+// Every writer of the changelog uses it (Append and the storage core's database), so that every
+// home of the changelog records the same lands. Only the server sets them, through WithLands,
+// which validated them (see gitcmd.ChangelogMeta).
+func LandsOf(meta Meta, reference string) []Land {
+ if len(meta.Lands) == 0 || meta.LandReference != reference {
+ return nil
+ }
+
+ return append([]Land(nil), meta.Lands...)
+}
diff --git a/internal/graft/changelog/lands_index.go b/internal/graft/changelog/lands_index.go
new file mode 100644
index 0000000..c853e8b
--- /dev/null
+++ b/internal/graft/changelog/lands_index.go
@@ -0,0 +1,416 @@
+package changelog
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "hash/crc32"
+ "io"
+ "os"
+ "path/filepath"
+ "sync"
+)
+
+const (
+ // landsFileName is the name of the index of the land identities inside dirName.
+ landsFileName = "changelog.lands"
+ // maxIdleLandsIndexes bounds the number of indexes that are kept in memory while no lookup
+ // uses them. An index holds every land of its repository, so fewer of them are kept than of
+ // the states of the logs (maxIdleStates). A dropped index is loaded from its file again.
+ maxIdleLandsIndexes = 64
+)
+
+// The index of the land identities of a repository (doc/graft/PLAN11.md, 4.2) maps a branch and an
+// original commit to the last changelog entry that landed that commit on that branch. It is derived
+// from the changelog: the log entries carry the lands (Entry.Lands), and the index is the result of
+// reading them, kept in memory and in the file graft/changelog.lands so that a new process does not
+// read the whole log again.
+//
+// The file is a sequence of blocks of JSON lines. A block holds the records of the lands of some
+// log entries, in log order, and ends with a mark: the sequence of the last log entry the block
+// covers, the reference and new value of that entry, and the CRC-32 of the record lines of the
+// block. The index is complete up to the sequence of the last mark.
+//
+// The file is never trusted beyond what it can prove. Reading it stops at the first line that is
+// not a record or a mark, at a mark whose checksum does not match its records, and at a mark that
+// does not follow the one before; the file is cut there, and the lookup reads the log from the last
+// good mark on, as it does after the entries that other appends added since. The mark proves that
+// it describes this log: the lookup reads the log entry at the mark's sequence first, and when that
+// entry is not the one the mark names (the log was replaced, for example by a repository created
+// again at the same path) the index is rebuilt from the whole log. The file is therefore written
+// without fsync: what a crash leaves of it is either a good prefix or is cut off.
+
+// landsIndexKey identifies a land in an index.
+type landsIndexKey struct {
+ reference string
+ original string
+}
+
+// landsMark identifies the log entry up to which an index is complete.
+type landsMark struct {
+ sequence uint64
+ reference string
+ newOID string
+}
+
+// matches reports whether entry is the log entry that the mark names.
+func (m landsMark) matches(entry Entry) bool {
+ return entry.Sequence == m.sequence && entry.Reference == m.reference && entry.NewOID == m.newOID
+}
+
+// landsIndex is the in-process view of the index of one repository.
+type landsIndex struct {
+ // mu serializes the lookups of this process in the repository and guards the fields below.
+ mu sync.Mutex
+ // users counts the callers that hold the index. It is guarded by landsIndexesMu.
+ users int
+ // loaded is false until the index has been read from its file.
+ loaded bool
+ // mark is the log entry up to which records is complete; its sequence is zero when records
+ // covers no entry.
+ mark landsMark
+ // records maps the lands of the log entries up to mark to their last record.
+ records map[landsIndexKey]LandRecord
+ // fileSize is the size of the index file when this process last read or wrote it. A file of
+ // another size was written by another process and is read again.
+ fileSize int64
+}
+
+var (
+ landsIndexesMu sync.Mutex
+ // landsIndexes holds the indexes by the path of their file. An index is kept after its last
+ // user releases it, up to maxIdleLandsIndexes idle ones.
+ landsIndexes = map[string]*landsIndex{}
+)
+
+// acquireLandsIndex returns the index whose file is path, and the function that releases it.
+func acquireLandsIndex(path string) (*landsIndex, func()) {
+ landsIndexesMu.Lock()
+ index, ok := landsIndexes[path]
+ if !ok {
+ index = &landsIndex{}
+ landsIndexes[path] = index
+ }
+ index.users++
+ landsIndexesMu.Unlock()
+
+ return index, func() {
+ landsIndexesMu.Lock()
+ defer landsIndexesMu.Unlock()
+
+ index.users--
+ evictIdleLandsIndexes()
+ }
+}
+
+// evictIdleLandsIndexes drops idle indexes until at most maxIdleLandsIndexes remain idle. The caller
+// must hold landsIndexesMu.
+func evictIdleLandsIndexes() {
+ if len(landsIndexes) <= maxIdleLandsIndexes {
+ return
+ }
+
+ idle := 0
+ for _, index := range landsIndexes {
+ if index.users == 0 {
+ idle++
+ }
+ }
+
+ for path, index := range landsIndexes {
+ if idle <= maxIdleLandsIndexes {
+ return
+ }
+ if index.users == 0 {
+ delete(landsIndexes, path)
+ idle--
+ }
+ }
+}
+
+// landsPath returns the path of the index of the land identities of the repository whose git
+// directory is repoPath.
+func landsPath(repoPath string) string {
+ return filepath.Join(repoPath, dirName, landsFileName)
+}
+
+// LookupLand returns the last land of original on reference that the changelog of the repository
+// whose git directory is repoPath recorded (doc/graft/PLAN11.md): the sequence of its entry and the
+// commit it landed as. found is false when the changelog recorded no land of original on reference.
+// The lookup reads the durable entries of the log (see AppendGroup) that the index does not cover
+// yet, and adds them to the index first.
+func LookupLand(repoPath, reference, original string) (record LandRecord, found bool, err error) {
+ index, release := acquireLandsIndex(landsPath(repoPath))
+ defer release()
+
+ index.mu.Lock()
+ defer index.mu.Unlock()
+
+ if err := index.catchUp(repoPath); err != nil {
+ // What the failed catch-up did to the index is not known, so the next lookup reads it again.
+ index.loaded = false
+ return LandRecord{}, false, err
+ }
+
+ record, found = index.records[landsIndexKey{reference: reference, original: original}]
+ return record, found, nil
+}
+
+// catchUp brings the index up to the last durable entry of the log. The caller must hold i.mu.
+func (i *landsIndex) catchUp(repoPath string) error {
+ last, err := LastSequence(repoPath)
+ if err != nil {
+ return err
+ }
+ if i.loaded && last == i.mark.sequence {
+ return nil
+ }
+
+ path := landsPath(repoPath)
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ return fmt.Errorf("creating changelog directory: %w", err)
+ }
+
+ unlock, err := lockFile(path + lockSuffix)
+ if err != nil {
+ return err
+ }
+ defer unlock()
+
+ f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
+ if err != nil {
+ return fmt.Errorf("opening land index: %w", err)
+ }
+ defer f.Close()
+
+ info, err := f.Stat()
+ if err != nil {
+ return fmt.Errorf("stat land index: %w", err)
+ }
+ if !i.loaded || info.Size() != i.fileSize {
+ if err := i.load(f); err != nil {
+ return err
+ }
+ }
+
+ block, err := i.scanLog(repoPath)
+ if errors.Is(err, errLandsIndexStale) {
+ // The log is not the one the index was built from: build it again from the whole log.
+ if err := truncateFile(f, 0); err != nil {
+ return fmt.Errorf("truncating land index: %w", err)
+ }
+ i.reset(0)
+ block, err = i.scanLog(repoPath)
+ }
+ if err != nil {
+ return err
+ }
+
+ if block.mark.sequence == i.mark.sequence {
+ i.loaded = true
+ return nil
+ }
+
+ encoded, err := block.encode()
+ if err != nil {
+ return err
+ }
+ if _, err := f.WriteAt(encoded, i.fileSize); err != nil {
+ return fmt.Errorf("writing land index: %w", err)
+ }
+
+ for _, record := range block.records {
+ i.records[landsIndexKey{reference: record.reference, original: record.original}] = LandRecord{Sequence: record.sequence, Landed: record.landed}
+ }
+ i.mark = block.mark
+ i.fileSize += int64(len(encoded))
+ i.loaded = true
+
+ return nil
+}
+
+// errLandsIndexStale is returned by scanLog when the log entry at the mark of the index is not the
+// one the mark names.
+var errLandsIndexStale = errors.New("the land index does not describe the log")
+
+// landsBlockRecord is a record of a block, see landsBlock.
+type landsBlockRecord struct {
+ sequence uint64
+ reference string
+ original string
+ landed string
+}
+
+// landsBlock is the records of the lands of the log entries after the mark of an index, and the
+// mark of the last of those entries.
+type landsBlock struct {
+ records []landsBlockRecord
+ mark landsMark
+}
+
+// scanLog reads the log entries after the mark of the index and returns their lands as a block. It
+// first checks that the log entry at the mark is the one the mark names, and returns
+// errLandsIndexStale when it is not. The caller must hold i.mu.
+func (i *landsIndex) scanLog(repoPath string) (landsBlock, error) {
+ block := landsBlock{mark: i.mark}
+
+ after := i.mark.sequence
+ verified := after == 0
+ if !verified {
+ after--
+ }
+
+ if err := Scan(repoPath, after, func(entry Entry) (bool, error) {
+ if !verified {
+ if !i.mark.matches(entry) {
+ return false, errLandsIndexStale
+ }
+ verified = true
+ return true, nil
+ }
+
+ for _, land := range entry.Lands {
+ block.records = append(block.records, landsBlockRecord{
+ sequence: entry.Sequence,
+ reference: entry.Reference,
+ original: land.Original,
+ landed: land.Landed,
+ })
+ }
+ block.mark = landsMark{sequence: entry.Sequence, reference: entry.Reference, newOID: entry.NewOID}
+
+ return true, nil
+ }); err != nil {
+ return landsBlock{}, err
+ }
+
+ // The log ends before the mark: it was cut or replaced.
+ if !verified {
+ return landsBlock{}, errLandsIndexStale
+ }
+
+ return block, nil
+}
+
+// landsLine is one line of the index file: a record when Mark is zero, a mark otherwise. Reference
+// names may hold bytes that are not valid UTF-8, so they are encoded as bytes (base64 in JSON).
+type landsLine struct {
+ Sequence uint64 `json:"seq,omitempty"`
+ Reference []byte `json:"ref"`
+ Original string `json:"original,omitempty"`
+ Landed string `json:"landed,omitempty"`
+ // Mark is the sequence of the log entry that the mark names; Reference and NewOID are its
+ // reference and new value, CRC the CRC-32 (IEEE) of the record lines of the block.
+ Mark uint64 `json:"mark,omitempty"`
+ NewOID string `json:"new_oid,omitempty"`
+ CRC uint32 `json:"crc,omitempty"`
+}
+
+// encode returns the lines of the block, its records and then its mark.
+func (b landsBlock) encode() ([]byte, error) {
+ var buf bytes.Buffer
+ for _, record := range b.records {
+ line, err := json.Marshal(landsLine{
+ Sequence: record.sequence,
+ Reference: []byte(record.reference),
+ Original: record.original,
+ Landed: record.landed,
+ })
+ if err != nil {
+ return nil, fmt.Errorf("encoding land index record: %w", err)
+ }
+ buf.Write(line)
+ buf.WriteByte('\n')
+ }
+
+ mark, err := json.Marshal(landsLine{
+ Reference: []byte(b.mark.reference),
+ Mark: b.mark.sequence,
+ NewOID: b.mark.newOID,
+ CRC: crc32.ChecksumIEEE(buf.Bytes()),
+ })
+ if err != nil {
+ return nil, fmt.Errorf("encoding land index mark: %w", err)
+ }
+ buf.Write(mark)
+ buf.WriteByte('\n')
+
+ return buf.Bytes(), nil
+}
+
+// reset empties the in-memory index of a file of the given size.
+func (i *landsIndex) reset(fileSize int64) {
+ i.mark = landsMark{}
+ i.records = map[landsIndexKey]LandRecord{}
+ i.fileSize = fileSize
+ i.loaded = false
+}
+
+// load reads the index from its file f up to its last good mark, and cuts the file there. The
+// caller must hold i.mu and the lock file of the index.
+func (i *landsIndex) load(f *os.File) error {
+ i.reset(0)
+
+ var (
+ pending []landsBlockRecord
+ crc = crc32.NewIEEE()
+ offset int64
+ )
+
+ r := bufio.NewReader(f)
+ if _, err := f.Seek(0, io.SeekStart); err != nil {
+ return fmt.Errorf("reading land index: %w", err)
+ }
+
+ for {
+ line, err := r.ReadBytes('\n')
+ if err != nil && !errors.Is(err, io.EOF) {
+ return fmt.Errorf("reading land index: %w", err)
+ }
+ // A line without its newline is a torn tail.
+ if len(line) == 0 || line[len(line)-1] != '\n' {
+ break
+ }
+ offset += int64(len(line))
+
+ var decoded landsLine
+ if json.Unmarshal(line, &decoded) != nil {
+ break
+ }
+
+ if decoded.Mark == 0 {
+ if decoded.Sequence <= i.mark.sequence || (len(pending) > 0 && decoded.Sequence < pending[len(pending)-1].sequence) {
+ break
+ }
+ pending = append(pending, landsBlockRecord{
+ sequence: decoded.Sequence,
+ reference: string(decoded.Reference),
+ original: decoded.Original,
+ landed: decoded.Landed,
+ })
+ crc.Write(line)
+ continue
+ }
+
+ if decoded.Mark <= i.mark.sequence || decoded.CRC != crc.Sum32() ||
+ (len(pending) > 0 && pending[len(pending)-1].sequence > decoded.Mark) {
+ break
+ }
+
+ for _, record := range pending {
+ i.records[landsIndexKey{reference: record.reference, original: record.original}] = LandRecord{Sequence: record.sequence, Landed: record.landed}
+ }
+ i.mark = landsMark{sequence: decoded.Mark, reference: string(decoded.Reference), newOID: decoded.NewOID}
+ i.fileSize = offset
+ pending = nil
+ crc.Reset()
+ }
+
+ if err := truncateFile(f, i.fileSize); err != nil {
+ return fmt.Errorf("truncating land index: %w", err)
+ }
+
+ return nil
+}
diff --git a/internal/graft/changelog/lands_test.go b/internal/graft/changelog/lands_test.go
new file mode 100644
index 0000000..445997a
--- /dev/null
+++ b/internal/graft/changelog/lands_test.go
@@ -0,0 +1,515 @@
+package changelog
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// landOID returns a SHA-1 object ID for n.
+func landOID(n int) string { return fmt.Sprintf("%040x", n) }
+
+// sha256OID returns a SHA-256 object ID for n.
+func sha256OID(n int) string { return fmt.Sprintf("%064x", n) }
+
+// appendLand records an update of reference to landed that lands original as landed.
+func appendLand(t *testing.T, repoPath, reference, original, landed string) uint64 {
+ t.Helper()
+
+ entries, err := Append(repoPath, Meta{Operation: "land", Actor: "agent", LandReference: reference, Lands: []Land{{Original: original, Landed: landed}}},
+ []Update{{Reference: reference, OldOID: landOID(0), NewOID: landed}})
+ require.NoError(t, err)
+ require.Len(t, entries, 1)
+ return entries[0].Sequence
+}
+
+// appendPlain records an update of reference that lands nothing.
+func appendPlain(t *testing.T, repoPath, reference, newOID string) uint64 {
+ t.Helper()
+
+ entries, err := Append(repoPath, Meta{Operation: "update_ref", Actor: "user"},
+ []Update{{Reference: reference, OldOID: landOID(0), NewOID: newOID}})
+ require.NoError(t, err)
+ return entries[0].Sequence
+}
+
+// lookup looks up a land and fails the test on an error.
+func lookup(t *testing.T, repoPath, reference, original string) (LandRecord, bool) {
+ t.Helper()
+
+ record, found, err := LookupLand(repoPath, reference, original)
+ require.NoError(t, err)
+ return record, found
+}
+
+// forgetLandsIndex drops the in-memory index of the repository, as a new process starts without it.
+func forgetLandsIndex(t *testing.T, repoPath string) {
+ t.Helper()
+
+ landsIndexesMu.Lock()
+ defer landsIndexesMu.Unlock()
+ delete(landsIndexes, landsPath(repoPath))
+}
+
+// forgetLog drops the in-memory state of the log of the repository.
+func forgetLog(t *testing.T, repoPath string) {
+ t.Helper()
+
+ statesMu.Lock()
+ defer statesMu.Unlock()
+ delete(states, Path(repoPath))
+}
+
+func TestValidateLands(t *testing.T) {
+ t.Parallel()
+
+ land := Land{Original: landOID(1), Landed: landOID(2)}
+
+ t.Run("a land on a branch", func(t *testing.T) {
+ require.NoError(t, ValidateLands("refs/heads/main", []Land{land}))
+ })
+
+ t.Run("the largest batch with the longest branch name fits", func(t *testing.T) {
+ // Every byte of the name is escaped by JSON into 6 bytes, the worst case.
+ reference := "refs/heads/" + strings.Repeat("<", 4096-len("refs/heads/"))
+ lands := make([]Land, MaxLandsPerUpdate)
+ for i := range lands {
+ lands[i] = Land{Original: sha256OID(2 * i), Landed: sha256OID(2*i + 1)}
+ }
+
+ require.NoError(t, ValidateLands(reference, lands))
+ })
+
+ t.Run("more lands than a batch has", func(t *testing.T) {
+ lands := make([]Land, MaxLandsPerUpdate+1)
+ for i := range lands {
+ lands[i] = land
+ }
+
+ require.ErrorContains(t, ValidateLands("refs/heads/main", lands), "at most 100 per update")
+ })
+
+ t.Run("identities beyond the size bound", func(t *testing.T) {
+ reference := "refs/heads/" + strings.Repeat("<", MaxLandsSize/6)
+
+ require.ErrorContains(t, ValidateLands(reference, []Land{land}), fmt.Sprintf("at most %d", MaxLandsSize))
+ })
+
+ t.Run("not a branch", func(t *testing.T) {
+ require.ErrorContains(t, ValidateLands("refs/tags/v1", []Land{land}), "not a branch")
+ })
+
+ t.Run("no lands", func(t *testing.T) {
+ require.ErrorContains(t, ValidateLands("refs/heads/main", nil), "no lands")
+ })
+
+ for _, bad := range []Land{
+ {Original: "", Landed: landOID(2)},
+ {Original: landOID(1), Landed: "abc"},
+ {Original: strings.ToUpper(landOID(0xabc)), Landed: landOID(2)},
+ {Original: landOID(1), Landed: landOID(2) + "0"},
+ } {
+ t.Run(fmt.Sprintf("malformed %q -> %q", bad.Original, bad.Landed), func(t *testing.T) {
+ require.ErrorContains(t, ValidateLands("refs/heads/main", []Land{bad}), "is not a pair of object IDs")
+ })
+ }
+}
+
+func TestWithLands(t *testing.T) {
+ t.Parallel()
+
+ ctx := context.Background()
+
+ reference, lands := LandsFromContext(ctx)
+ require.Empty(t, reference)
+ require.Empty(t, lands)
+
+ recorded := []Land{{Original: landOID(1), Landed: landOID(2)}}
+ withLands, err := WithLands(ctx, "refs/heads/main", recorded)
+ require.NoError(t, err)
+
+ reference, lands = LandsFromContext(withLands)
+ require.Equal(t, "refs/heads/main", reference)
+ require.Equal(t, recorded, lands)
+
+ unchanged, err := WithLands(ctx, "refs/tags/v1", recorded)
+ require.Error(t, err)
+ require.Equal(t, ctx, unchanged)
+}
+
+func TestAppendRecordsLandsOnTheirBranchOnly(t *testing.T) {
+ t.Parallel()
+
+ repoPath := t.TempDir()
+ lands := []Land{{Original: landOID(1), Landed: landOID(2)}, {Original: landOID(3), Landed: landOID(4)}}
+
+ entries, err := Append(repoPath, Meta{Operation: "land", LandReference: "refs/heads/main", Lands: lands}, []Update{
+ {Reference: "refs/heads/other", OldOID: landOID(0), NewOID: landOID(5)},
+ {Reference: "refs/heads/main", OldOID: landOID(0), NewOID: landOID(4)},
+ })
+ require.NoError(t, err)
+ require.Len(t, entries, 2)
+ require.Empty(t, entries[0].Lands)
+ require.Equal(t, lands, entries[1].Lands)
+
+ // The lands are part of the entry in the log, and an entry without them has no field.
+ scanned, err := List(repoPath, 0, 10)
+ require.NoError(t, err)
+ require.Equal(t, entries, scanned)
+
+ content, err := os.ReadFile(Path(repoPath))
+ require.NoError(t, err)
+ lines := bytes.Split(bytes.TrimSuffix(content, []byte("\n")), []byte("\n"))
+ require.Len(t, lines, 2)
+ require.NotContains(t, string(lines[0]), `"lands"`)
+ require.Contains(t, string(lines[1]), `"lands":[{"original":"`+landOID(1)+`","landed":"`+landOID(2)+`"}`)
+}
+
+func TestLookupLand(t *testing.T) {
+ t.Parallel()
+
+ repoPath := t.TempDir()
+
+ t.Run("without a log", func(t *testing.T) {
+ _, found := lookup(t, repoPath, "refs/heads/main", landOID(1))
+ require.False(t, found)
+ })
+
+ first := appendLand(t, repoPath, "refs/heads/main", landOID(1), landOID(2))
+ appendPlain(t, repoPath, "refs/heads/main", landOID(9))
+ other := appendLand(t, repoPath, "refs/heads/other", landOID(1), landOID(3))
+
+ t.Run("a land on its branch", func(t *testing.T) {
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: first, Landed: landOID(2)}, record)
+ })
+
+ t.Run("the same commit on another branch is another land", func(t *testing.T) {
+ record, found := lookup(t, repoPath, "refs/heads/other", landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: other, Landed: landOID(3)}, record)
+ })
+
+ t.Run("a commit that did not land", func(t *testing.T) {
+ _, found := lookup(t, repoPath, "refs/heads/main", landOID(2))
+ require.False(t, found)
+ })
+
+ t.Run("a later land of the commit replaces the record", func(t *testing.T) {
+ again := appendLand(t, repoPath, "refs/heads/main", landOID(1), landOID(7))
+
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: again, Landed: landOID(7)}, record)
+ })
+
+ t.Run("a batch records every member", func(t *testing.T) {
+ lands := []Land{{Original: landOID(11), Landed: landOID(12)}, {Original: landOID(13), Landed: landOID(14)}}
+ entries, err := Append(repoPath, Meta{Operation: OperationLandBatch, LandReference: "refs/heads/main", Lands: lands},
+ []Update{{Reference: "refs/heads/main", OldOID: landOID(7), NewOID: landOID(14)}})
+ require.NoError(t, err)
+
+ for _, land := range lands {
+ record, found := lookup(t, repoPath, "refs/heads/main", land.Original)
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: entries[0].Sequence, Landed: land.Landed}, record)
+ }
+ })
+}
+
+func TestLookupLandOnANonUTF8Branch(t *testing.T) {
+ t.Parallel()
+
+ repoPath := t.TempDir()
+ reference := "refs/heads/\xff\xfe"
+ seq := appendLand(t, repoPath, reference, landOID(1), landOID(2))
+
+ record, found := lookup(t, repoPath, reference, landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: seq, Landed: landOID(2)}, record)
+
+ // The file keeps the name exactly: a new process finds the land in the file, with a log whose
+ // entry no longer has it.
+ rewriteLog(t, repoPath, func(entries []Entry) []Entry {
+ entries[0].Lands = nil
+ return entries
+ })
+ forgetLandsIndex(t, repoPath)
+
+ record, found = lookup(t, repoPath, reference, landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: seq, Landed: landOID(2)}, record)
+}
+
+// landsIndexLines returns the decoded lines of the index file of the repository.
+func landsIndexLines(t *testing.T, repoPath string) []landsLine {
+ t.Helper()
+
+ content, err := os.ReadFile(landsPath(repoPath))
+ require.NoError(t, err)
+
+ var lines []landsLine
+ for _, raw := range bytes.SplitAfter(content, []byte("\n")) {
+ if len(raw) == 0 {
+ continue
+ }
+ var line landsLine
+ require.NoError(t, json.Unmarshal(raw, &line), "line %q", raw)
+ lines = append(lines, line)
+ }
+ return lines
+}
+
+func TestLandsIndexFile(t *testing.T) {
+ t.Parallel()
+
+ t.Run("blocks of records and marks", func(t *testing.T) {
+ repoPath := t.TempDir()
+ first := appendLand(t, repoPath, "refs/heads/main", landOID(1), landOID(2))
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+
+ plain := appendPlain(t, repoPath, "refs/heads/main", landOID(3))
+ second := appendLand(t, repoPath, "refs/heads/main", landOID(4), landOID(5))
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+
+ lines := landsIndexLines(t, repoPath)
+ require.Len(t, lines, 4)
+ require.Equal(t, landsLine{Sequence: first, Reference: []byte("refs/heads/main"), Original: landOID(1), Landed: landOID(2)}, lines[0])
+ require.Equal(t, first, lines[1].Mark)
+ require.Equal(t, landOID(2), lines[1].NewOID)
+ require.Equal(t, landsLine{Sequence: second, Reference: []byte("refs/heads/main"), Original: landOID(4), Landed: landOID(5)}, lines[2])
+ require.Equal(t, second, lines[3].Mark)
+ require.Greater(t, second, plain)
+ })
+
+ t.Run("a lookup without new entries writes nothing", func(t *testing.T) {
+ repoPath := t.TempDir()
+ appendLand(t, repoPath, "refs/heads/main", landOID(1), landOID(2))
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+
+ before, err := os.ReadFile(landsPath(repoPath))
+ require.NoError(t, err)
+
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+ forgetLandsIndex(t, repoPath)
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+
+ after, err := os.ReadFile(landsPath(repoPath))
+ require.NoError(t, err)
+ require.Equal(t, before, after)
+ })
+
+ t.Run("a new process reads the file, not the log", func(t *testing.T) {
+ repoPath := t.TempDir()
+ seq := appendLand(t, repoPath, "refs/heads/main", landOID(1), landOID(2))
+ lookup(t, repoPath, "refs/heads/main", landOID(1))
+
+ // The entry is cut from the log while keeping its sequence: only the file knows the land.
+ rewriteLog(t, repoPath, func(entries []Entry) []Entry {
+ entries[0].Lands = nil
+ return entries
+ })
+
+ forgetLandsIndex(t, repoPath)
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(1))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: seq, Landed: landOID(2)}, record)
+ })
+}
+
+// rewriteLog replaces the entries of the log of the repository with what edit makes of them, and
+// drops the in-memory state of the log.
+func rewriteLog(t *testing.T, repoPath string, edit func([]Entry) []Entry) {
+ t.Helper()
+
+ entries, err := List(repoPath, 0, 1<<20)
+ require.NoError(t, err)
+
+ var buf bytes.Buffer
+ for _, entry := range edit(entries) {
+ line, err := json.Marshal(entry)
+ require.NoError(t, err)
+ buf.Write(line)
+ buf.WriteByte('\n')
+ }
+
+ require.NoError(t, os.WriteFile(Path(repoPath), buf.Bytes(), 0o644))
+ forgetLog(t, repoPath)
+}
+
+func TestLandsIndexRecovers(t *testing.T) {
+ t.Parallel()
+
+ // setUp records two lands, each read into the index in a block of its own, and a third land
+ // that the index does not cover yet: the log append of the third is durable, and the process
+ // stopped before a lookup brought the index up to it.
+ setUp := func(t *testing.T) (string, []uint64) {
+ repoPath := t.TempDir()
+ var seqs []uint64
+ for i := 1; i <= 3; i++ {
+ seqs = append(seqs, appendLand(t, repoPath, "refs/heads/main", landOID(10*i), landOID(10*i+1)))
+ if i < 3 {
+ lookup(t, repoPath, "refs/heads/main", landOID(10))
+ }
+ }
+ forgetLandsIndex(t, repoPath)
+ return repoPath, seqs
+ }
+
+ // requireAll checks that every land of setUp is found with its record.
+ requireAll := func(t *testing.T, repoPath string, seqs []uint64) {
+ t.Helper()
+ for i, seq := range seqs {
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(10*(i+1)))
+ require.True(t, found, "land %d", i+1)
+ require.Equal(t, LandRecord{Sequence: seq, Landed: landOID(10*(i+1) + 1)}, record)
+ }
+ require.Equal(t, seqs[len(seqs)-1], landsIndexLines(t, repoPath)[len(landsIndexLines(t, repoPath))-1].Mark)
+ }
+
+ t.Run("an index behind the log catches up", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("a torn tail is cut off", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ appendToFile(t, landsPath(repoPath), `{"seq":3,"ref":"cmVmcy9oZWFkcy9tYWlu","original":"`)
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("records without their mark are cut off", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ bogus, err := json.Marshal(landsLine{Sequence: seqs[2], Reference: []byte("refs/heads/main"), Original: landOID(30), Landed: landOID(99)})
+ require.NoError(t, err)
+ appendToFile(t, landsPath(repoPath), string(bogus)+"\n")
+
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("a block whose checksum does not match is cut off with what follows", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+
+ // The landed commit of the first record is changed: its line still decodes, but its mark
+ // does not match it any more.
+ content, err := os.ReadFile(landsPath(repoPath))
+ require.NoError(t, err)
+ corrupted := bytes.Replace(content, []byte(landOID(11)), []byte(landOID(66)), 1)
+ require.NotEqual(t, content, corrupted)
+ require.NoError(t, os.WriteFile(landsPath(repoPath), corrupted, 0o644))
+
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("a line that is not JSON is cut off with what follows", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+
+ content, err := os.ReadFile(landsPath(repoPath))
+ require.NoError(t, err)
+ require.NoError(t, os.WriteFile(landsPath(repoPath), append([]byte("\x00\x00\x00\n"), content...), 0o644))
+
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("a missing file is rebuilt from the log", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ require.NoError(t, os.Remove(landsPath(repoPath)))
+ requireAll(t, repoPath, seqs)
+ })
+
+ t.Run("an index of another log is rebuilt", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+
+ // The repository is created again at the same path: its log has other entries at the same
+ // sequences, and the commit of the first land landed nowhere.
+ rewriteLog(t, repoPath, func(entries []Entry) []Entry {
+ for i := range entries {
+ entries[i].NewOID = landOID(500 + i)
+ entries[i].Lands = nil
+ }
+ entries[2].Lands = []Land{{Original: landOID(30), Landed: landOID(502)}}
+ return entries
+ })
+
+ _, found := lookup(t, repoPath, "refs/heads/main", landOID(10))
+ require.False(t, found)
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(30))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: seqs[2], Landed: landOID(502)}, record)
+ })
+
+ t.Run("an index past the end of the log is rebuilt", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ lookup(t, repoPath, "refs/heads/main", landOID(10))
+
+ // The log is replaced by a shorter one, which ends before the mark of the index.
+ rewriteLog(t, repoPath, func(entries []Entry) []Entry { return entries[:1] })
+ forgetLandsIndex(t, repoPath)
+
+ _, found := lookup(t, repoPath, "refs/heads/main", landOID(30))
+ require.False(t, found)
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(10))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: seqs[0], Landed: landOID(11)}, record)
+ })
+
+ t.Run("the index of another process is read again", func(t *testing.T) {
+ repoPath, seqs := setUp(t)
+ requireAll(t, repoPath, seqs)
+
+ // Another process brings the index up to a new land; this process sees the file grow.
+ fourth := appendLand(t, repoPath, "refs/heads/main", landOID(40), landOID(41))
+ index, release := acquireLandsIndex(landsPath(repoPath))
+ index.mu.Lock()
+ stale := index.fileSize
+ index.mu.Unlock()
+ release()
+
+ forgetLandsIndex(t, repoPath)
+ lookup(t, repoPath, "refs/heads/main", landOID(40))
+ landsIndexesMu.Lock()
+ landsIndexes[landsPath(repoPath)] = index
+ landsIndexesMu.Unlock()
+ require.Less(t, stale, fileSize(t, landsPath(repoPath)))
+
+ record, found := lookup(t, repoPath, "refs/heads/main", landOID(40))
+ require.True(t, found)
+ require.Equal(t, LandRecord{Sequence: fourth, Landed: landOID(41)}, record)
+
+ // The block of the other process is not written twice.
+ marks := 0
+ for _, line := range landsIndexLines(t, repoPath) {
+ if line.Mark == fourth {
+ marks++
+ }
+ }
+ require.Equal(t, 1, marks)
+ })
+}
+
+// appendToFile appends s to the file at path.
+func appendToFile(t *testing.T, path, s string) {
+ t.Helper()
+
+ f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o644)
+ require.NoError(t, err)
+ _, err = f.WriteString(s)
+ require.NoError(t, err)
+ require.NoError(t, f.Close())
+}
+
+// fileSize returns the size of the file at path.
+func fileSize(t *testing.T, path string) int64 {
+ t.Helper()
+
+ info, err := os.Stat(path)
+ require.NoError(t, err)
+ return info.Size()
+}
diff --git a/internal/graft/changelog/log.go b/internal/graft/changelog/log.go
index cdc8fd2..c101eb8 100644
--- a/internal/graft/changelog/log.go
+++ b/internal/graft/changelog/log.go
@@ -92,6 +92,11 @@ type Entry struct {
// folded into this update, because the list was cut to fit the size limit that
// the hooks payload imposes (see gitcmd.ChangelogMeta).
MembersTruncated bool `json:"members_truncated,omitempty"`
+ // Lands are the identities of the changes that the update landed on the branch Reference, in
+ // the order they were landed: every member of a batch, or the one change of a land. They are
+ // recorded on every land path and are never cut (see Land, WithLands and doc/graft/PLAN11.md).
+ // They are empty for updates that are not lands.
+ Lands []Land `json:"lands,omitempty"`
}
// entryFields is Entry without its JSON methods.
@@ -167,6 +172,11 @@ type Meta struct {
// MembersTruncated is true when Members is not the complete list of the changes
// folded into the updates, see Entry.MembersTruncated.
MembersTruncated bool
+ // LandReference is the branch that Lands landed on. Only the entry of the update of that
+ // branch carries them (see LandsOf).
+ LandReference string
+ // Lands are the identities of the changes that the updates landed, see Entry.Lands.
+ Lands []Land
}
// Path returns the path of the changeset log of the repository whose git
@@ -466,6 +476,7 @@ func (s *logState) appendLocked(repoPath, path string, records []Record, entries
Rewritten: meta.Rewritten,
Members: normalizeMembers(meta.Members),
MembersTruncated: meta.MembersTruncated,
+ Lands: LandsOf(meta, update.Reference),
}
line, err := json.Marshal(entry)
diff --git a/internal/graft/corestore/metastore.go b/internal/graft/corestore/metastore.go
index 73aa78e..8d0b158 100644
--- a/internal/graft/corestore/metastore.go
+++ b/internal/graft/corestore/metastore.go
@@ -156,6 +156,12 @@ type MetaStore interface {
// ScanChangelog returns up to limit changelog entries with a sequence
// greater than after, in ascending order. limit <= 0 means no limit.
ScanChangelog(ctx context.Context, repoID int64, after uint64, limit int) ([]changelog.Entry, error)
+ // LookupLand returns the last land of the original commit on the branch
+ // reference that the changelog recorded (changelog.Entry.Lands,
+ // doc/graft/PLAN11.md): the sequence of its entry and the commit it landed
+ // as. found is false when the changelog recorded no such land. The record is
+ // written with its entry, in the same transaction.
+ LookupLand(ctx context.Context, repoID int64, reference, original string) (record changelog.LandRecord, found bool, err error)
// ImportHistory brings history written outside the database (shadow mode
// writes the file changelog and file reviews) into a tracked repository, in
// one transaction (doc/graft/PLAN6.md, LOCK AND HISTORY RULE). The changelog
diff --git a/internal/graft/corestore/sqlstore/changeset.go b/internal/graft/corestore/sqlstore/changeset.go
index d13fc52..c83cdd9 100644
--- a/internal/graft/corestore/sqlstore/changeset.go
+++ b/internal/graft/corestore/sqlstore/changeset.go
@@ -255,6 +255,7 @@ func buildChangelogEntries(repo *repoState, meta *changelog.Meta, now time.Time,
Rewritten: meta.Rewritten,
Members: slices.Clone(meta.Members),
MembersTruncated: meta.MembersTruncated,
+ Lands: changelog.LandsOf(*meta, u.Name),
})
}
diff --git a/internal/graft/corestore/sqlstore/landidentity_test.go b/internal/graft/corestore/sqlstore/landidentity_test.go
new file mode 100644
index 0000000..16172eb
--- /dev/null
+++ b/internal/graft/corestore/sqlstore/landidentity_test.go
@@ -0,0 +1,186 @@
+package sqlstore_test
+
+import (
+ "context"
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/graft/changelog"
+ "github.com/feder-cr/git-ai/internal/graft/corestore"
+ "github.com/feder-cr/git-ai/internal/graft/corestore/sqlstore"
+)
+
+// landMeta is the changelog metadata of a land of original on reference as landed.
+func landMeta(reference string, lands ...changelog.Land) *changelog.Meta {
+ return &changelog.Meta{Operation: "land", Actor: "agent", LandReference: reference, Lands: lands}
+}
+
+// requireLand checks the land record of original on reference.
+func requireLand(t *testing.T, ctx context.Context, s *sqlstore.Store, repoID int64, reference string, original corestore.Target, want changelog.LandRecord) {
+ t.Helper()
+
+ record, found, err := s.LookupLand(ctx, repoID, reference, string(original))
+ require.NoError(t, err)
+ require.True(t, found, "land of %s on %s", original, reference)
+ require.Equal(t, want, record)
+}
+
+// requireNoLand checks that no land of original on reference is recorded.
+func requireNoLand(t *testing.T, ctx context.Context, s *sqlstore.Store, repoID int64, reference string, original corestore.Target) {
+ t.Helper()
+
+ _, found, err := s.LookupLand(ctx, repoID, reference, string(original))
+ require.NoError(t, err)
+ require.False(t, found, "land of %s on %s", original, reference)
+}
+
+// TestLandIdentities verifies the land identities of the database (doc/graft/PLAN11.md, 4.1): the
+// changeset of a land records its lands with its changelog entry, a later land of the same commit
+// replaces the record, and the identity is per repository and branch.
+func TestLandIdentities(t *testing.T) {
+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {
+ repo := register(t, ctx, s, testKey("lands"))
+ other := register(t, ctx, s, testKey("lands-other"))
+
+ requireNoLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10))
+
+ // A batch of two lands on main, with an update of another branch in the same changeset,
+ // which records no land.
+ result, err := s.CommitChangeset(ctx, corestore.ChangesetInput{
+ RepoID: repo.ID,
+ Updates: []corestore.RefUpdate{
+ create("refs/heads/side", oid(9)),
+ create("refs/heads/main", oid(12)),
+ },
+ Changelog: landMeta("refs/heads/main",
+ changelog.Land{Original: string(oid(10)), Landed: string(oid(11))},
+ changelog.Land{Original: string(oid(20)), Landed: string(oid(12))},
+ ),
+ })
+ require.NoError(t, err)
+ require.Len(t, result.Changelog, 2)
+ require.Empty(t, result.Changelog[0].Lands)
+ require.Equal(t, []changelog.Land{
+ {Original: string(oid(10)), Landed: string(oid(11))},
+ {Original: string(oid(20)), Landed: string(oid(12))},
+ }, result.Changelog[1].Lands)
+ batchSeq := result.Changelog[1].Sequence
+
+ entries, err := s.ScanChangelog(ctx, repo.ID, 0, 0)
+ require.NoError(t, err)
+ require.Equal(t, result.Changelog, entries, "the entries keep their lands")
+
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10), changelog.LandRecord{Sequence: batchSeq, Landed: string(oid(11))})
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(20), changelog.LandRecord{Sequence: batchSeq, Landed: string(oid(12))})
+ requireNoLand(t, ctx, s, repo.ID, "refs/heads/side", oid(10))
+ requireNoLand(t, ctx, s, other.ID, "refs/heads/main", oid(10))
+
+ // A later land of the same commit replaces its record.
+ result, err = s.CommitChangeset(ctx, corestore.ChangesetInput{
+ RepoID: repo.ID,
+ Updates: []corestore.RefUpdate{update("refs/heads/main", oid(12), oid(13))},
+ Changelog: landMeta("refs/heads/main", changelog.Land{Original: string(oid(10)), Landed: string(oid(13))}),
+ })
+ require.NoError(t, err)
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10), changelog.LandRecord{Sequence: result.Changelog[0].Sequence, Landed: string(oid(13))})
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(20), changelog.LandRecord{Sequence: batchSeq, Landed: string(oid(12))})
+
+ // A branch whose name is not valid UTF-8.
+ result, err = s.CommitChangeset(ctx, corestore.ChangesetInput{
+ RepoID: repo.ID,
+ Updates: []corestore.RefUpdate{create("refs/heads/\xff", oid(31))},
+ Changelog: landMeta("refs/heads/\xff", changelog.Land{Original: string(oid(30)), Landed: string(oid(31))}),
+ })
+ require.NoError(t, err)
+ requireLand(t, ctx, s, repo.ID, "refs/heads/\xff", oid(30), changelog.LandRecord{Sequence: result.Changelog[0].Sequence, Landed: string(oid(31))})
+ requireNoLand(t, ctx, s, repo.ID, "refs/heads/�", oid(30))
+ })
+}
+
+// TestLandIdentitiesAreWrittenWithTheChangeset verifies that the land record and the changeset are
+// one transaction: when the record cannot be written, the changeset is not committed either.
+func TestLandIdentitiesAreWrittenWithTheChangeset(t *testing.T) {
+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {
+ repo := register(t, ctx, s, testKey("lands-atomic"))
+ _, err := commit(ctx, s, repo.ID, create("refs/heads/main", oid(1)))
+ require.NoError(t, err)
+
+ execSQL(t, ctx, s, "DROP TABLE graft_land_identities")
+
+ _, err = s.CommitChangeset(ctx, corestore.ChangesetInput{
+ RepoID: repo.ID,
+ Updates: []corestore.RefUpdate{update("refs/heads/main", oid(1), oid(3))},
+ Changelog: landMeta("refs/heads/main", changelog.Land{Original: string(oid(2)), Landed: string(oid(3))}),
+ })
+ require.ErrorContains(t, err, "insert land identity of changelog entry 1")
+
+ requireRefs(t, ctx, s, repo.ID, map[string]corestore.Target{"refs/heads/main": oid(1)})
+ entries, err := s.ScanChangelog(ctx, repo.ID, 0, 0)
+ require.NoError(t, err)
+ require.Empty(t, entries)
+ after, err := s.GetRepository(ctx, testKey("lands-atomic"))
+ require.NoError(t, err)
+ require.Equal(t, repo.HeadSeq+1, after.HeadSeq, "only the changeset without a land was committed")
+ })
+}
+
+// TestLandIdentitiesOfImportedHistory verifies that history written outside the database keeps
+// its land identities when it is brought in: the initial history of a registered repository and
+// the history that shadow mode imports.
+func TestLandIdentitiesOfImportedHistory(t *testing.T) {
+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {
+ landed := func(seq uint64, original, landed corestore.Target) changelog.Entry {
+ return changelog.Entry{
+ Sequence: seq,
+ Reference: "refs/heads/main",
+ OldOID: string(oid(0)),
+ NewOID: string(landed),
+ Operation: "land",
+ Lands: []changelog.Land{{Original: string(original), Landed: string(landed)}},
+ }
+ }
+
+ repo, _, err := s.RegisterRepository(ctx, testKey("lands-import"), "sha1", corestore.InitialState{
+ Changelog: []changelog.Entry{landed(1, oid(10), oid(11)), landed(2, oid(20), oid(21))},
+ })
+ require.NoError(t, err)
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10), changelog.LandRecord{Sequence: 1, Landed: string(oid(11))})
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(20), changelog.LandRecord{Sequence: 2, Landed: string(oid(21))})
+
+ imported, _, err := s.ImportHistory(ctx, repo.ID, []changelog.Entry{landed(3, oid(10), oid(31)), landed(4, oid(40), oid(41))}, nil)
+ require.NoError(t, err)
+ require.Equal(t, 2, imported)
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(10), changelog.LandRecord{Sequence: 3, Landed: string(oid(31))})
+ requireLand(t, ctx, s, repo.ID, "refs/heads/main", oid(40), changelog.LandRecord{Sequence: 4, Landed: string(oid(41))})
+ })
+}
+
+// TestLandIdentitiesGoWithTheRepository verifies that the land identities of a removed repository
+// are removed with it, and that a repository registered again under its key has none.
+func TestLandIdentitiesGoWithTheRepository(t *testing.T) {
+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {
+ key := testKey("lands-removed")
+ first := register(t, ctx, s, key)
+ _, err := s.CommitChangeset(ctx, corestore.ChangesetInput{
+ RepoID: first.ID,
+ Updates: []corestore.RefUpdate{create("refs/heads/main", oid(11))},
+ Changelog: landMeta("refs/heads/main", changelog.Land{Original: string(oid(10)), Landed: string(oid(11))}),
+ })
+ require.NoError(t, err)
+
+ deleted, err := s.DeleteRepository(ctx, key)
+ require.NoError(t, err)
+ require.True(t, deleted)
+ requireNoLand(t, ctx, s, first.ID, "refs/heads/main", oid(10))
+
+ second := register(t, ctx, s, key)
+ requireNoLand(t, ctx, s, second.ID, "refs/heads/main", oid(10))
+
+ var rows int
+ require.NoError(t, s.DB().QueryRowContext(ctx,
+ fmt.Sprintf("SELECT COUNT(*) FROM graft_land_identities WHERE repo_id = %d", first.ID)).Scan(&rows))
+ require.Zero(t, rows, "the rows of the removed repository are gone")
+ })
+}
diff --git a/internal/graft/corestore/sqlstore/migrations.go b/internal/graft/corestore/sqlstore/migrations.go
index 4a1481e..c300642 100644
--- a/internal/graft/corestore/sqlstore/migrations.go
+++ b/internal/graft/corestore/sqlstore/migrations.go
@@ -176,6 +176,24 @@ var sqliteMigrations = []*migrate.Migration{
`DROP TRIGGER graft_repositories_tombstone`,
},
},
+ {
+ // Land identity (doc/graft/PLAN11.md, 4.1): the last changelog entry that landed an
+ // original commit on a branch, written with the entry in the same transaction.
+ Id: "20261020000000_graft_land_identities",
+ Up: []string{
+ `CREATE TABLE graft_land_identities (
+ repo_id INTEGER NOT NULL REFERENCES graft_repositories (repo_id) ON DELETE CASCADE,
+ branch BLOB NOT NULL,
+ original_commit TEXT NOT NULL,
+ log_seq INTEGER NOT NULL,
+ landed_commit TEXT NOT NULL,
+ PRIMARY KEY (repo_id, branch, original_commit)
+ )`,
+ },
+ Down: []string{
+ `DROP TABLE graft_land_identities`,
+ },
+ },
}
// notOwnedPackKeyMessage is the error of the trigger that refuses a changeset
@@ -355,4 +373,21 @@ var postgresMigrations = []*migrate.Migration{
`DROP FUNCTION graft_repositories_tombstone()`,
},
},
+ {
+ // See the SQLite migration of the same ID.
+ Id: "20261020000000_graft_land_identities",
+ Up: []string{
+ `CREATE TABLE graft_land_identities (
+ repo_id BIGINT NOT NULL REFERENCES graft_repositories (repo_id) ON DELETE CASCADE,
+ branch BYTEA NOT NULL,
+ original_commit TEXT NOT NULL,
+ log_seq BIGINT NOT NULL,
+ landed_commit TEXT NOT NULL,
+ PRIMARY KEY (repo_id, branch, original_commit)
+ )`,
+ },
+ Down: []string{
+ `DROP TABLE graft_land_identities`,
+ },
+ },
}
diff --git a/internal/graft/corestore/sqlstore/repository.go b/internal/graft/corestore/sqlstore/repository.go
index ed65581..2a1ebbc 100644
--- a/internal/graft/corestore/sqlstore/repository.go
+++ b/internal/graft/corestore/sqlstore/repository.go
@@ -557,7 +557,10 @@ func (s *Store) ReportApplied(ctx context.Context, repoID int64, nodeID string,
}
// insertChangelog appends entries to the changelog of the repository. The
-// sequences of the entries are their own.
+// sequences of the entries are their own. The lands of an entry are recorded in
+// the land identities of the repository with it (doc/graft/PLAN11.md, 4.1), so
+// that the changesets, the imported history and the initial history of a
+// repository all keep them.
func insertChangelog(ctx context.Context, s *Store, q querier, repoID int64, entries []changelog.Entry) error {
for _, entry := range entries {
data, err := json.Marshal(entry)
@@ -569,6 +572,49 @@ func insertChangelog(ctx context.Context, s *Store, q querier, repoID int64, ent
repoID, int64(entry.Sequence), string(data)); err != nil {
return fmt.Errorf("insert changelog entry %d: %w", entry.Sequence, err)
}
+
+ if err := insertLandIdentities(ctx, s, q, repoID, entry); err != nil {
+ return err
+ }
}
return nil
}
+
+// insertLandIdentities records the lands of entry as the last lands of their
+// original commits on the branch of entry. Every writer of the changelog appends
+// its entries after the last one (the sequences of an import must be greater),
+// so the entry is the latest land of its commits and replaces their records.
+func insertLandIdentities(ctx context.Context, s *Store, q querier, repoID int64, entry changelog.Entry) error {
+ for _, land := range entry.Lands {
+ if _, err := q.ExecContext(ctx, s.bind(`
+ INSERT INTO graft_land_identities (repo_id, branch, original_commit, log_seq, landed_commit)
+ VALUES (?, ?, ?, ?, ?)
+ ON CONFLICT (repo_id, branch, original_commit) DO UPDATE SET
+ log_seq = excluded.log_seq,
+ landed_commit = excluded.landed_commit
+ `), repoID, bytesArg([]byte(entry.Reference)), land.Original, int64(entry.Sequence), land.Landed); err != nil {
+ return fmt.Errorf("insert land identity of changelog entry %d: %w", entry.Sequence, err)
+ }
+ }
+ return nil
+}
+
+// LookupLand implements corestore.MetaStore.
+func (s *Store) LookupLand(ctx context.Context, repoID int64, reference, original string) (changelog.LandRecord, bool, error) {
+ var (
+ seq int64
+ landed string
+ )
+ err := s.db.QueryRowContext(ctx, s.bind(`
+ SELECT log_seq, landed_commit FROM graft_land_identities
+ WHERE repo_id = ? AND branch = ? AND original_commit = ?
+ `), repoID, bytesArg([]byte(reference)), original).Scan(&seq, &landed)
+ if errors.Is(err, sql.ErrNoRows) {
+ return changelog.LandRecord{}, false, nil
+ }
+ if err != nil {
+ return changelog.LandRecord{}, false, fmt.Errorf("look up land identity: %w", err)
+ }
+
+ return changelog.LandRecord{Sequence: uint64(seq), Landed: landed}, true, nil
+}
diff --git a/internal/graft/e2e/storage_landidentity_test.go b/internal/graft/e2e/storage_landidentity_test.go
new file mode 100644
index 0000000..222f75f
--- /dev/null
+++ b/internal/graft/e2e/storage_landidentity_test.go
@@ -0,0 +1,93 @@
+package e2e
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/graft/corestore"
+ "github.com/feder-cr/git-ai/internal/graft/corestore/sqlstore"
+ "github.com/feder-cr/git-ai/sdk"
+)
+
+// TestStorageLandIdentity lands a change by rebase on one node of the storage core in its
+// authoritative mode and lands it again on another node, as a client does whose response was lost
+// and whose retry reached another node: the land identity in the database answers on every node
+// (doc/graft/PLAN11.md, 4.1). A revert of the change on the branch does not let it land again.
+func TestStorageLandIdentity(t *testing.T) {
+ t.Parallel()
+
+ for _, batched := range []bool{false, true} {
+ t.Run(fmt.Sprintf("batched=%t", batched), func(t *testing.T) {
+ t.Parallel()
+
+ forEachStorageDialect(t, func(t *testing.T, dialect sqlstore.Dialect) {
+ graft, tokens := workerGraft(2)
+ if !batched {
+ graft = unbatched(graft)
+ }
+ cluster := newStorageCluster(t, dialect, corestore.ModeAuthoritative, graft)
+ a := cluster.addNode(t)
+ b := cluster.addNode(t)
+
+ rel := a.createRepository(t).GetRelativePath()
+ onA := a.agent(t, rel, tokens[workerName(0)])
+ onB := b.agent(t, rel, tokens[workerName(1)])
+
+ base, err := commitFile(a.ctx, onA, workerName(0), "main", "", "README.md", "land identity\n")
+ require.NoError(t, err)
+ _, err = commitFile(a.ctx, onA, workerName(0), "main", base, "moved.txt", "main moved on\n")
+ require.NoError(t, err)
+ change, err := commitFile(a.ctx, onA, workerName(1), "work/change", base, "change.txt", "the change\n")
+ require.NoError(t, err)
+
+ first, err := landOn(a.ctx, onA, "main", change, false)
+ require.NoError(t, err)
+ require.True(t, first.Rewritten)
+
+ again, err := landOn(b.ctx, onB, "main", change, false)
+ require.NoError(t, err)
+ require.True(t, again.AlreadyLanded, "node-b answers from the record of node-a's land")
+ require.Equal(t, first.LandedCommitID, again.LandedCommitID)
+ require.Empty(t, again.RevertedByCommitID)
+
+ // The record names the changelog entry of the first land.
+ var landedAt uint64
+ for _, c := range allChangesets(a.ctx, t, onA) {
+ if c.GetNewOid() == first.LandedCommitID {
+ landedAt = c.GetSequence()
+ }
+ }
+ require.NotZero(t, landedAt)
+ require.Equal(t, landedAt, again.LandedSequence)
+ require.Equal(t, fmt.Sprintf("landed as %s at changelog %d", first.LandedCommitID, landedAt), again.AlreadyLandedDetail)
+
+ // A revert of the change on main, as graft-verify writes it.
+ revert, err := onA.CommitFiles(a.ctx, sdk.CommitOptions{
+ Branch: "main",
+ StartRev: first.LandedCommitID,
+ Message: fmt.Sprintf("graft-verify: revert\n\nThis reverts commit %s, which failed the verification of main.\n", first.LandedCommitID),
+ Author: sdk.Signature{Name: workerName(0), Email: workerName(0) + "@example.com"},
+ User: sdk.User{ID: workerName(0), Name: workerName(0), Email: workerName(0) + "@example.com"},
+ Actions: []sdk.Action{sdk.Delete{Path: "change.txt"}},
+ })
+ require.NoError(t, err)
+
+ afterRevert, err := landOn(b.ctx, onB, "main", change, false)
+ require.NoError(t, err)
+ require.True(t, afterRevert.AlreadyLanded)
+ require.Equal(t, first.LandedCommitID, afterRevert.LandedCommitID)
+ require.Equal(t, revert.CommitID, afterRevert.RevertedByCommitID)
+ require.Equal(t, fmt.Sprintf("landed as %s at changelog %d; it was reverted later by %s", first.LandedCommitID, landedAt, revert.CommitID), afterRevert.AlreadyLandedDetail)
+
+ tip, err := onB.ResolveRevision(b.ctx, "main")
+ require.NoError(t, err)
+ require.Equal(t, revert.CommitID, tip, "the revert stays")
+
+ a.requireConsistent(t, rel)
+ b.requireConsistent(t, rel)
+ })
+ })
+ }
+}
diff --git a/proto/go/gitalypb/operations.pb.go b/proto/go/gitalypb/operations.pb.go
index c76fea8..226b633 100644
--- a/proto/go/gitalypb/operations.pb.go
+++ b/proto/go/gitalypb/operations.pb.go
@@ -4654,14 +4654,21 @@ type UserLandResponse struct {
// now at the branch tip.
BranchUpdate *OperationBranchUpdate `protobuf:"bytes,1,opt,name=branch_update,json=branchUpdate,proto3" json:"branch_update,omitempty"`
// landed_commit_id is the commit now at the branch tip: commit_id itself
- // when fast-forwarded, otherwise the rebased or merge commit.
+ // when fast-forwarded, otherwise the rebased or merge commit. When
+ // already_landed comes from the record of an earlier land (landed_sequence
+ // is set), it is the commit that land put on the branch, which holds the
+ // change.
LandedCommitId string `protobuf:"bytes,2,opt,name=landed_commit_id,json=landedCommitId,proto3" json:"landed_commit_id,omitempty"`
// rewritten is true if the server rebased or merged.
Rewritten bool `protobuf:"varint,3,opt,name=rewritten,proto3" json:"rewritten,omitempty"`
// attempts is the number of compare-and-swap attempts used.
Attempts int32 `protobuf:"varint,4,opt,name=attempts,proto3" json:"attempts,omitempty"`
- // already_landed is true if commit_id was already reachable from the tip
- // and the branch was left unchanged.
+ // already_landed is true if the change is already on the branch and the
+ // branch was left unchanged: commit_id is reachable from the tip, or an
+ // earlier land of commit_id on this branch put a commit on it that is still
+ // reachable from the tip (landed_sequence is then set). Such a change is not
+ // landed again, also when a later commit reverted it: landing it again needs
+ // a new commit.
AlreadyLanded bool `protobuf:"varint,5,opt,name=already_landed,json=alreadyLanded,proto3" json:"already_landed,omitempty"`
// old_tip is the branch tip that was replaced (zero OID if created).
OldTip string `protobuf:"bytes,6,opt,name=old_tip,json=oldTip,proto3" json:"old_tip,omitempty"`
@@ -4677,8 +4684,23 @@ type UserLandResponse struct {
BatchSize int32 `protobuf:"varint,9,opt,name=batch_size,json=batchSize,proto3" json:"batch_size,omitempty"`
// batch_position is the 1-based position of this request in its batch.
BatchPosition int32 `protobuf:"varint,10,opt,name=batch_position,json=batchPosition,proto3" json:"batch_position,omitempty"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
+ // landed_sequence is set when already_landed comes from the record of an
+ // earlier land of commit_id on the branch: the changelog sequence of the
+ // update that landed it as landed_commit_id. Zero otherwise.
+ LandedSequence uint64 `protobuf:"varint,11,opt,name=landed_sequence,json=landedSequence,proto3" json:"landed_sequence,omitempty"`
+ // reverted_by_commit_id is the commit on the branch, after
+ // landed_commit_id, whose message says that it reverts landed_commit_id
+ // ("This reverts commit <landed_commit_id>", as git revert and graft-verify
+ // write it). It is looked for only when landed_sequence is set, and is
+ // empty when no such commit is found.
+ RevertedByCommitId string `protobuf:"bytes,12,opt,name=reverted_by_commit_id,json=revertedByCommitId,proto3" json:"reverted_by_commit_id,omitempty"`
+ // already_landed_detail explains an already_landed answer that
+ // landed_sequence is set for: "landed as <landed_commit_id> at changelog
+ // <landed_sequence>", followed by "; it was reverted later by
+ // <reverted_by_commit_id>" when that is set. Empty otherwise.
+ AlreadyLandedDetail string `protobuf:"bytes,13,opt,name=already_landed_detail,json=alreadyLandedDetail,proto3" json:"already_landed_detail,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
}
func (x *UserLandResponse) Reset() {
@@ -4781,6 +4803,27 @@ func (x *UserLandResponse) GetBatchPosition() int32 {
return 0
}
+func (x *UserLandResponse) GetLandedSequence() uint64 {
+ if x != nil {
+ return x.LandedSequence
+ }
+ return 0
+}
+
+func (x *UserLandResponse) GetRevertedByCommitId() string {
+ if x != nil {
+ return x.RevertedByCommitId
+ }
+ return ""
+}
+
+func (x *UserLandResponse) GetAlreadyLandedDetail() string {
+ if x != nil {
+ return x.AlreadyLandedDetail
+ }
+ return ""
+}
+
// UserLandError is an error returned by the UserLand RPC in some specific
// well defined error cases.
type UserLandError struct {
@@ -5515,7 +5558,7 @@ const file_operations_proto_rawDesc = "" +
"\bStrategy\x12\x1f\n" +
"\x1bSTRATEGY_REBASE_UNSPECIFIED\x10\x00\x12\x12\n" +
"\x0eSTRATEGY_MERGE\x10\x01\x12\x14\n" +
- "\x10STRATEGY_FF_ONLY\x10\x02\"\x83\x03\n" +
+ "\x10STRATEGY_FF_ONLY\x10\x02\"\x93\x04\n" +
"\x10UserLandResponse\x12B\n" +
"\rbranch_update\x18\x01 \x01(\v2\x1d.gitaly.OperationBranchUpdateR\fbranchUpdate\x12(\n" +
"\x10landed_commit_id\x18\x02 \x01(\tR\x0elandedCommitId\x12\x1c\n" +
@@ -5528,7 +5571,10 @@ const file_operations_proto_rawDesc = "" +
"\n" +
"batch_size\x18\t \x01(\x05R\tbatchSize\x12%\n" +
"\x0ebatch_position\x18\n" +
- " \x01(\x05R\rbatchPosition\"\xe2\x02\n" +
+ " \x01(\x05R\rbatchPosition\x12'\n" +
+ "\x0flanded_sequence\x18\v \x01(\x04R\x0elandedSequence\x121\n" +
+ "\x15reverted_by_commit_id\x18\f \x01(\tR\x12revertedByCommitId\x122\n" +
+ "\x15already_landed_detail\x18\r \x01(\tR\x13alreadyLandedDetail\"\xe2\x02\n" +
"\rUserLandError\x12C\n" +
"\x0emerge_conflict\x18\x01 \x01(\v2\x1a.gitaly.MergeConflictErrorH\x00R\rmergeConflict\x12=\n" +
"\fnot_ancestor\x18\x02 \x01(\v2\x18.gitaly.NotAncestorErrorH\x00R\vnotAncestor\x12I\n" +
diff --git a/proto/operations.proto b/proto/operations.proto
index d5a31f2..bf0401a 100644
--- a/proto/operations.proto
+++ b/proto/operations.proto
@@ -1278,14 +1278,21 @@ message UserLandResponse {
// now at the branch tip.
OperationBranchUpdate branch_update = 1;
// landed_commit_id is the commit now at the branch tip: commit_id itself
- // when fast-forwarded, otherwise the rebased or merge commit.
+ // when fast-forwarded, otherwise the rebased or merge commit. When
+ // already_landed comes from the record of an earlier land (landed_sequence
+ // is set), it is the commit that land put on the branch, which holds the
+ // change.
string landed_commit_id = 2;
// rewritten is true if the server rebased or merged.
bool rewritten = 3;
// attempts is the number of compare-and-swap attempts used.
int32 attempts = 4;
- // already_landed is true if commit_id was already reachable from the tip
- // and the branch was left unchanged.
+ // already_landed is true if the change is already on the branch and the
+ // branch was left unchanged: commit_id is reachable from the tip, or an
+ // earlier land of commit_id on this branch put a commit on it that is still
+ // reachable from the tip (landed_sequence is then set). Such a change is not
+ // landed again, also when a later commit reverted it: landing it again needs
+ // a new commit.
bool already_landed = 5;
// old_tip is the branch tip that was replaced (zero OID if created).
string old_tip = 6;
@@ -1301,6 +1308,21 @@ message UserLandResponse {
int32 batch_size = 9;
// batch_position is the 1-based position of this request in its batch.
int32 batch_position = 10;
+ // landed_sequence is set when already_landed comes from the record of an
+ // earlier land of commit_id on the branch: the changelog sequence of the
+ // update that landed it as landed_commit_id. Zero otherwise.
+ uint64 landed_sequence = 11;
+ // reverted_by_commit_id is the commit on the branch, after
+ // landed_commit_id, whose message says that it reverts landed_commit_id
+ // ("This reverts commit <landed_commit_id>", as git revert and graft-verify
+ // write it). It is looked for only when landed_sequence is set, and is
+ // empty when no such commit is found.
+ string reverted_by_commit_id = 12;
+ // already_landed_detail explains an already_landed answer that
+ // landed_sequence is set for: "landed as <landed_commit_id> at changelog
+ // <landed_sequence>", followed by "; it was reverted later by
+ // <reverted_by_commit_id>" when that is set. Empty otherwise.
+ string already_landed_detail = 13;
}
// UserLandError is an error returned by the UserLand RPC in some specific
diff --git a/sdk/land.go b/sdk/land.go
index f14d6dd..a044e2d 100644
--- a/sdk/land.go
+++ b/sdk/land.go
@@ -59,8 +59,7 @@ type LandOptions struct {
// merge conflict as without this option.
ResolveConflicts bool
// RetryUnavailable is how Land retries a call that the server answers
- // with Unavailable (see UnavailableRetry). It is on by default for
- // LandMerge and LandFastForwardOnly, and off for LandRebase.
+ // with Unavailable (see UnavailableRetry). It is on by default.
RetryUnavailable UnavailableRetry
}
@@ -72,25 +71,23 @@ type LandOptions struct {
// backoff with full jitter: before retry n it waits a random time between zero
// and min(Cap, Base x 2^(n-1)). A context that ends stops the waiting.
//
-// The response of a call can be lost after the server landed the change. With
-// LandMerge and LandFastForwardOnly, Commit is then reachable from the tip, and
-// the retry is reported as AlreadyLanded: these lands are retried by default.
-// With LandRebase, ResolveConflicts included (a land without conflicts is a
-// plain rebase), the branch holds a rebased copy, not Commit itself, and the
-// server cannot tell yet that the change landed: the retry replays the change
-// onto the tip. Every commit of it comes out empty and is dropped, unless a
-// later commit reverted the change in between, for example graft-verify
-// reverting the culprit: the retry would then land the change again. Rebase
-// lands are therefore not retried unless RebaseLands is set by a caller that
-// accepts this. A retried land with ResolveConflicts may run the conflict
-// resolver again, a model included, for the same conflict; LandResult reports
-// the retries (UnavailableRetries), so that this cost is visible.
+// The response of a call can be lost after the server landed the change. The
+// retry then answers AlreadyLanded and lands nothing (doc/graft/PLAN11.md): with
+// LandMerge and LandFastForwardOnly, Commit is reachable from the tip; with
+// LandRebase, ResolveConflicts included, the branch holds a rebased copy of
+// Commit, and the server answers from its record of the first land, with
+// LandedCommitID the copy and LandedSequence its changelog sequence. This holds
+// also when a later commit reverted the change in between, for example
+// graft-verify reverting the culprit: the change is not landed again, and
+// RevertedByCommitID names the revert. A retry whose first call never reached
+// the update lands the change; with ResolveConflicts it may run the conflict
+// resolver again, a model included, for the same conflict, and LandResult
+// reports the retries (UnavailableRetries), so that this cost is visible.
type UnavailableRetry struct {
// Disabled turns the retries off: Land returns Unavailable at once.
Disabled bool
- // RebaseLands also retries LandRebase lands, which a retry after a lost
- // response may land again when the change was reverted in between.
- RebaseLands bool
+ // SkipRebaseLands turns the retries off for LandRebase lands only.
+ SkipRebaseLands bool
// MaxRetries bounds the retries after the first call. Zero means 5.
MaxRetries int
// Base is the backoff of the first retry. Zero means 100ms.
@@ -149,7 +146,7 @@ func (r UnavailableRetry) backoff(n int) time.Duration {
// maxRetries returns the retries of a land with the given strategy.
func (r UnavailableRetry) maxRetries(strategy LandStrategy) int {
switch {
- case r.Disabled, strategy == LandRebase && !r.RebaseLands:
+ case r.Disabled, strategy == LandRebase && r.SkipRebaseLands:
return 0
case r.MaxRetries > 0:
return r.MaxRetries
@@ -168,9 +165,25 @@ type LandResult struct {
OldTip string
// Rewritten is true if the server rebased or merged the change.
Rewritten bool
- // AlreadyLanded is true if Commit was already reachable from the branch
- // tip. The branch was left unchanged.
+ // AlreadyLanded is true if the change was already on the branch and the
+ // branch was left unchanged: Commit is reachable from the tip, or an
+ // earlier land of Commit on the branch put LandedCommitID on it, which is
+ // still reachable from the tip (LandedSequence is then set). A change that
+ // a later commit reverted is not landed again: landing it again needs a
+ // new commit.
AlreadyLanded bool
+ // LandedSequence is the changelog sequence of the earlier land when the
+ // server answered AlreadyLanded from its record (see AlreadyLanded); zero
+ // otherwise. LandedCommitID is then the commit of that land.
+ LandedSequence uint64
+ // RevertedByCommitID is the commit after LandedCommitID on the branch
+ // whose message says that it reverts it ("This reverts commit <id>"), when
+ // LandedSequence is set and such a commit exists.
+ RevertedByCommitID string
+ // AlreadyLandedDetail explains an AlreadyLanded answer that LandedSequence
+ // is set for, for example "landed as <oid> at changelog 42; it was
+ // reverted later by <oid>".
+ AlreadyLandedDetail string
// BranchCreated is true if the branch was created by this call.
BranchCreated bool
// Attempts is the number of compare-and-swap attempts the server used.
@@ -284,6 +297,10 @@ func (r *Repo) Land(ctx context.Context, opts LandOptions) (*LandResult, error)
Resolver: resp.GetResolver(),
Batch: batchOf(resp),
+ LandedSequence: resp.GetLandedSequence(),
+ RevertedByCommitID: resp.GetRevertedByCommitId(),
+ AlreadyLandedDetail: resp.GetAlreadyLandedDetail(),
+
UnavailableRetries: retries,
LastUnavailable: lastUnavailable,
}, nil
diff --git a/sdk/land_identity_integration_test.go b/sdk/land_identity_integration_test.go
new file mode 100644
index 0000000..ab15d3d
--- /dev/null
+++ b/sdk/land_identity_integration_test.go
@@ -0,0 +1,118 @@
+package sdk_test
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/sdk"
+)
+
+// lostLandResponses loses the response of the first UserLand call after the server answered it, the
+// way a connection reset or a node that stops after the update does: the client gets Unavailable
+// for a land that happened. afterLand runs once the first call landed, before the client sees the
+// loss.
+type lostLandResponses struct {
+ mu sync.Mutex
+ calls int
+ landed string
+ afterLand func(landed string)
+}
+
+func (l *lostLandResponses) intercept(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
+ if err := invoker(ctx, method, req, reply, cc, opts...); err != nil || method != "/gitaly.OperationService/UserLand" {
+ return err
+ }
+
+ l.mu.Lock()
+ defer l.mu.Unlock()
+
+ l.calls++
+ if l.calls > 1 {
+ return nil
+ }
+
+ l.landed = reply.(interface{ GetLandedCommitId() string }).GetLandedCommitId()
+ if l.afterLand != nil {
+ l.afterLand(l.landed)
+ }
+ return status.Error(codes.Unavailable, "connection reset after the land")
+}
+
+// TestIntegration_LandRetryAfterALostResponse lands a change by rebase through the SDK, whose first
+// answer is lost after the server landed it: the SDK retries the Unavailable, and the server answers
+// from its record of the first land (doc/graft/PLAN11.md). The change is on the branch once.
+func TestIntegration_LandRetryAfterALostResponse(t *testing.T) {
+ t.Parallel()
+
+ user := sdk.User{ID: "user-1", Name: "Ada", Email: "[email protected]"}
+ retry := sdk.UnavailableRetry{Base: time.Millisecond, Cap: 2 * time.Millisecond}
+
+ for _, tc := range []struct {
+ desc string
+ // revert reverts the first land before the retry reaches the server.
+ revert bool
+ }{
+ {desc: "the retry answers with the rebased copy"},
+ {desc: "a revert before the retry stays", revert: true},
+ } {
+ t.Run(tc.desc, func(t *testing.T) {
+ t.Parallel()
+
+ lost := &lostLandResponses{}
+ env := startIntegration(t, nil, sdk.WithGRPCOptions(grpc.WithUnaryInterceptor(lost.intercept)))
+ base := env.writeCommit(t, "main", map[string]string{"base.txt": "base"})
+ tip := env.writeCommit(t, "main", map[string]string{"base.txt": "base", "x": "x"}, base)
+ change := env.writeCommit(t, "", map[string]string{"base.txt": "base", "y": "y"}, base)
+
+ var reverted string
+ if tc.revert {
+ lost.afterLand = func(landed string) {
+ reverted = gittest.WriteCommit(t, env.cfg, env.repoPath,
+ gittest.WithTree(env.writeTree(t, map[string]string{"base.txt": "base", "x": "x"})),
+ gittest.WithParents(git.ObjectID(landed)),
+ gittest.WithMessage(fmt.Sprintf("Revert \"y\"\n\nThis reverts commit %s.\n", landed)),
+ ).String()
+ env.git(t, "update-ref", "refs/heads/main", reverted, landed)
+ }
+ }
+
+ result, err := env.repo.Land(env.ctx, sdk.LandOptions{
+ Branch: "main",
+ Commit: change.String(),
+ User: user,
+ Strategy: sdk.LandRebase,
+ RetryUnavailable: retry,
+ })
+ require.NoError(t, err)
+ require.Equal(t, 1, result.UnavailableRetries)
+ require.Equal(t, "connection reset after the land", result.LastUnavailable)
+
+ require.NotEqual(t, change.String(), lost.landed, "the first land rebased the change")
+ require.True(t, result.AlreadyLanded)
+ require.False(t, result.Rewritten)
+ require.Equal(t, lost.landed, result.LandedCommitID)
+ require.NotZero(t, result.LandedSequence)
+ require.Equal(t, reverted, result.RevertedByCommitID)
+
+ detail := fmt.Sprintf("landed as %s at changelog %d", lost.landed, result.LandedSequence)
+ head := lost.landed
+ if tc.revert {
+ detail += fmt.Sprintf("; it was reverted later by %s", reverted)
+ head = reverted
+ }
+ require.Equal(t, detail, result.AlreadyLandedDetail)
+ require.Equal(t, head, env.mustResolve(t, "main"))
+ require.Equal(t, tip.String(), env.git(t, "rev-parse", lost.landed+"^"), "the change landed once, on the tip")
+ })
+ }
+}
diff --git a/sdk/land_retry_test.go b/sdk/land_retry_test.go
index 6b2ba5f..3be407f 100644
--- a/sdk/land_retry_test.go
+++ b/sdk/land_retry_test.go
@@ -24,7 +24,7 @@ func mergeLand() sdk.LandOptions {
return opts
}
-func TestRepo_Land_RebaseLandsAreNotRetriedByDefault(t *testing.T) {
+func TestRepo_Land_RebaseLandsAreRetried(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
@@ -33,9 +33,9 @@ func TestRepo_Land_RebaseLandsAreNotRetriedByDefault(t *testing.T) {
retry sdk.UnavailableRetry
expectedRequests int
}{
- {desc: "rebase", retry: fastRetry, expectedRequests: 1},
- {desc: "rebase with conflict resolution", resolve: true, retry: fastRetry, expectedRequests: 1},
- {desc: "rebase, opted in", retry: sdk.UnavailableRetry{RebaseLands: true, Base: time.Millisecond, Cap: 2 * time.Millisecond}, expectedRequests: 6},
+ {desc: "rebase", retry: fastRetry, expectedRequests: 6},
+ {desc: "rebase with conflict resolution", resolve: true, retry: fastRetry, expectedRequests: 6},
+ {desc: "rebase, opted out", retry: sdk.UnavailableRetry{SkipRebaseLands: true, Base: time.Millisecond, Cap: 2 * time.Millisecond}, expectedRequests: 1},
} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
@@ -224,3 +224,35 @@ func TestRepo_Land_RetriedResolveLandReportsItsOutcome(t *testing.T) {
require.True(t, req.GetResolveConflicts(), "every call asks for the resolution")
}
}
+
+func TestRepo_Land_RetriedRebaseReportsTheEarlierLand(t *testing.T) {
+ t.Parallel()
+
+ // The first call rebased the change but its answer was lost to an Unavailable: the retry
+ // answers from the record of that land, with the commit it landed as and the revert after it.
+ fakes := newFakeServices()
+ fakes.operations.landErrs = []error{status.Error(codes.Unavailable, "connection reset")}
+ fakes.operations.landResponse = &gitalypb.UserLandResponse{
+ BranchUpdate: &gitalypb.OperationBranchUpdate{CommitId: "tip-sha"},
+ LandedCommitId: "copy-sha",
+ AlreadyLanded: true,
+ OldTip: "tip-sha",
+ LandedSequence: 42,
+ RevertedByCommitId: "revert-sha",
+ AlreadyLandedDetail: "landed as copy-sha at changelog 42; it was reverted later by revert-sha",
+ }
+ repo := dialFake(t, fakes.register).Repo("repo")
+
+ opts := validLand()
+ opts.Strategy = sdk.LandRebase
+ opts.RetryUnavailable = fastRetry
+ result, err := repo.Land(t.Context(), opts)
+ require.NoError(t, err)
+ require.True(t, result.AlreadyLanded)
+ require.Equal(t, "copy-sha", result.LandedCommitID)
+ require.Equal(t, uint64(42), result.LandedSequence)
+ require.Equal(t, "revert-sha", result.RevertedByCommitID)
+ require.Equal(t, "landed as copy-sha at changelog 42; it was reverted later by revert-sha", result.AlreadyLandedDetail)
+ require.Equal(t, 1, result.UnavailableRetries)
+ require.Len(t, fakes.operations.landRequests, 2)
+}