Skip to content

Git designed for AI agents.

operations: UserLand resolves its branch by its exact name

Loris Salsia5fc06b44705
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.go
index e491da5..196bfa8 100644
--- a/internal/gitaly/service/operations/land.go
+++ b/internal/gitaly/service/operations/land.go
@@ -364,7 +364,7 @@ func (s *Server) updateLandedReference(
// attempt sees the current state of the branch.
var updateErr updateref.Error
if errors.As(err, &updateErr) {
- moved, resolveErr := landReferenceMoved(ctx, quarantineRepo, reference, oldOID)
+ moved, resolveErr := s.landReferenceMoved(ctx, quarantineRepo, reference, oldOID)
if resolveErr != nil {
return structerr.NewInternal("updating ref with hooks: %w", errors.Join(err, resolveErr))
}
@@ -383,24 +383,45 @@ func (s *Server) updateLandedReference(
// landReferenceMoved reports whether the reference no longer points to expectedOID. A missing
// reference has moved unless the zero OID was expected.
-func landReferenceMoved(ctx context.Context, repo *localrepo.Repo, reference git.ReferenceName, expectedOID git.ObjectID) (bool, error) {
+func (s *Server) landReferenceMoved(ctx context.Context, repo *localrepo.Repo, reference git.ReferenceName, expectedOID git.ObjectID) (bool, error) {
objectHash, err := repo.ObjectHash(ctx)
if err != nil {
return false, fmt.Errorf("detecting object hash: %w", err)
}
- current, err := repo.ResolveRevision(ctx, reference.Revision())
+ current, err := s.landBranchTip(ctx, repo, reference)
if err != nil {
- if errors.Is(err, git.ErrReferenceNotFound) {
- return expectedOID != objectHash.ZeroOID, nil
- }
-
return false, fmt.Errorf("resolving branch after failed update: %w", err)
}
+ if current == "" {
+ return expectedOID != objectHash.ZeroOID, nil
+ }
return current != expectedOID, nil
}
+// landBranchTip returns the commit of the branch reference, read by its exact name, or an empty ID
+// when the branch does not exist. git resolves refs/heads/<x> with its DWIM rules: when the branch
+// is missing, refs/refs/heads/<x>, refs/tags/refs/heads/<x>, refs/heads/refs/heads/<x> or
+// refs/remotes/refs/heads/<x> answers for it, and a land would decide, and answer already_landed,
+// against the commit such a reference (a tag an agent may create) chose. UserCommitFiles reads its
+// branches the same way (localrepo.ResolveBranchesExactly). A symbolic branch resolves to the commit
+// of its target.
+func (s *Server) landBranchTip(ctx context.Context, repo *localrepo.Repo, reference git.ReferenceName) (git.ObjectID, error) {
+ reader, release, err := s.catfileCache.ObjectInfoReader(ctx, repo)
+ if err != nil {
+ return "", fmt.Errorf("creating object info reader: %w", err)
+ }
+ defer release()
+
+ branches, err := repo.ResolveBranchesExactly(ctx, reader, reference)
+ if err != nil {
+ return "", err
+ }
+
+ return branches[reference], nil
+}
+
// landMergeConflictError returns the FailedPrecondition error for a change that does not apply
// cleanly onto the branch tip.
func landMergeConflictError(tip, commitID git.ObjectID, conflictingFiles [][]byte, cause error) error {
diff --git a/internal/gitaly/service/operations/land_attempt.go b/internal/gitaly/service/operations/land_attempt.go
index 9ce7cb8..9db0e42 100644
--- a/internal/gitaly/service/operations/land_attempt.go
+++ b/internal/gitaly/service/operations/land_attempt.go
@@ -130,16 +130,13 @@ func (s *Server) newLandingAttempt(ctx context.Context, repository *gitalypb.Rep
}, nil
}
-// Tip returns the tip of the reference. A missing reference yields an empty ID.
+// Tip returns the tip of the reference, read by its exact name (landBranchTip). A missing reference
+// yields an empty ID.
func (a *landingAttempt) Tip(ctx context.Context, reference git.ReferenceName) (git.ObjectID, error) {
defer a.observe(landBatchPhaseTip, time.Now())
- tip, err := a.repo.ResolveRevision(ctx, reference.Revision())
+ tip, err := a.server.landBranchTip(ctx, a.repo, reference)
if err != nil {
- if errors.Is(err, git.ErrReferenceNotFound) {
- return "", nil
- }
-
return "", structerr.NewInternal("resolving branch: %w", err)
}
diff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.go
index 65b9353..4d9228f 100644
--- a/internal/gitaly/service/operations/land_batch_server.go
+++ b/internal/gitaly/service/operations/land_batch_server.go
@@ -69,7 +69,8 @@ func (s *Server) landBatchable(ctx context.Context, in *gitalypb.UserLandRequest
}
reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))
- if _, err := s.localRepoFactory.Build(in.GetRepository()).ResolveRevision(ctx, reference.Revision()); err != nil {
+ tip, err := s.landBranchTip(ctx, s.localRepoFactory.Build(in.GetRepository()), reference)
+ if err != nil || tip == "" {
return false
}
diff --git a/internal/gitaly/service/operations/land_exact_test.go b/internal/gitaly/service/operations/land_exact_test.go
new file mode 100644
index 0000000..56c16d1
--- /dev/null
+++ b/internal/gitaly/service/operations/land_exact_test.go
@@ -0,0 +1,90 @@
+package operations
+
+import (
+ "testing"
+
+ "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/git/localrepo"
+ "github.com/feder-cr/git-ai/internal/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/stretchr/testify/require"
+ "google.golang.org/grpc/codes"
+)
+
+// TestUserLandResolvesBranchesExactly checks that UserLand reads its branch only by its exact
+// name, as TestUserCommitFilesResolvesBranchesExactly checks for UserCommitFiles: when
+// refs/heads/x is missing, git's DWIM rules would resolve x through refs/refs/heads/x,
+// refs/tags/refs/heads/x, refs/heads/refs/heads/x or refs/remotes/refs/heads/x. A land onto x must
+// behave as if x did not exist, unbatched and in standalone mode, where the branch also decides
+// whether the land enters a lane: it is not found, or creates x with create_branch, in one attempt.
+// It never answers already_landed from the decoy's commit, and never moves the decoy.
+func TestUserLandResolvesBranchesExactly(t *testing.T) {
+ t.Parallel()
+
+ for _, mode := range []struct {
+ desc string
+ graft config.Graft
+ }{
+ {desc: "unbatched", graft: config.Graft{}},
+ {desc: "batched", graft: batchingGraft()},
+ } {
+ t.Run(mode.desc, func(t *testing.T) {
+ t.Parallel()
+
+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
+ ctx, cfg, client := setupLandBatchService(t, ctx, mode.graft)
+
+ for _, decoy := range []git.ReferenceName{"refs/refs/heads/x", "refs/tags/refs/heads/x", "refs/heads/refs/heads/x", "refs/remotes/refs/heads/x"} {
+ for _, tc := range []struct {
+ desc string
+ // onDecoy lands a commit on top of the decoy's commit instead of the decoy's
+ // commit itself, which the decoy would answer as already landed.
+ onDecoy bool
+ create bool
+ }{
+ {desc: "the decoy's commit"},
+ {desc: "a commit on the decoy's commit", onDecoy: true},
+ {desc: "the decoy's commit, with create_branch", create: true},
+ {desc: "a commit on the decoy's commit, with create_branch", onDecoy: true, create: true},
+ } {
+ t.Run(string(decoy)+", "+tc.desc, func(t *testing.T) {
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ decoyCommit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("decoy"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "decoy", Mode: "100644", Content: "decoy\n"}))
+ gittest.WriteRef(t, cfg, repoPath, decoy, decoyCommit)
+
+ commit := decoyCommit
+ if tc.onDecoy {
+ commit = gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(decoyCommit), gittest.WithMessage("change"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "change", Mode: "100644", Content: "change\n"}))
+ }
+
+ request := landRequest(repo, "x", commit)
+ request.CreateBranch = tc.create
+ resp, err := client.UserLand(ctx, request)
+
+ if !tc.create {
+ testhelper.RequireGrpcCode(t, err, codes.NotFound)
+ require.Nil(t, resp)
+ _, err = localrepo.NewTestRepo(t, cfg, repo).GetReference(ctx, "refs/heads/x")
+ require.ErrorIs(t, err, git.ErrReferenceNotFound)
+ } else {
+ require.NoError(t, err)
+ require.False(t, resp.GetAlreadyLanded())
+ require.True(t, resp.GetBranchUpdate().GetBranchCreated())
+ require.Equal(t, gittest.DefaultObjectHash.ZeroOID.String(), resp.GetOldTip())
+ require.Equal(t, commit.String(), resp.GetLandedCommitId())
+ require.EqualValues(t, 1, resp.GetAttempts())
+ requireBranchAt(t, cfg, repoPath, "x", commit)
+ }
+
+ // The decoy is untouched.
+ require.Equal(t, decoyCommit, gittest.ResolveRevision(t, cfg, repoPath, decoy.String()))
+ })
+ }
+ }
+ })
+ }
+}
diff --git a/internal/gitaly/service/operations/land_preresolve.go b/internal/gitaly/service/operations/land_preresolve.go
index 57d8fb8..f979dbb 100644
--- a/internal/gitaly/service/operations/land_preresolve.go
+++ b/internal/gitaly/service/operations/land_preresolve.go
@@ -181,13 +181,13 @@ func (s *Server) landPreResolve(ctx context.Context, in *gitalypb.UserLandReques
}
defer cleanup()
- tip, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())
+ tip, err := s.landBranchTip(ctx, repo, git.NewReferenceNameFromBranchName(string(in.GetBranch())))
if err != nil {
- if errors.Is(err, git.ErrReferenceNotFound) {
- return nil
- }
return fmt.Errorf("reading the branch: %w", err)
}
+ if tip == "" {
+ return nil
+ }
commitID, err := repo.ResolveRevision(ctx, git.Revision(in.GetCommitId()+"^{commit}"))
if err != nil {
return nil // the lane refuses an invalid change
diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.go
index 9475696..5142a2f 100644
--- a/internal/gitaly/service/operations/land_reland.go
+++ b/internal/gitaly/service/operations/land_reland.go
@@ -203,14 +203,14 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa
// A resolution for a tip the branch has left, or for a change that already landed, needs no
// check: the lane refuses or answers it at once, as it reads the tip again under the lane.
- // The branch is read as the lane reads it (landingAttempt.Tip): a symbolic reference resolves
- // to its target's commit.
- branchTip, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())
+ // The branch is read as the lane reads it (landingAttempt.Tip): by its exact name, and a
+ // symbolic reference resolves to its target's commit.
+ branchTip, err := s.landBranchTip(ctx, repo, git.NewReferenceNameFromBranchName(string(in.GetBranch())))
switch {
- case errors.Is(err, git.ErrReferenceNotFound):
- return &landPrecheck{skipped: fmt.Sprintf("branch %q does not exist", string(in.GetBranch()))}, nil
case err != nil:
return &landPrecheck{tip: tip, commit: commitID, result: landCheckResult{failure: fmt.Sprintf("reading the branch: %v", err), failureErr: err}}, nil
+ case branchTip == "":
+ return &landPrecheck{skipped: fmt.Sprintf("branch %q does not exist", string(in.GetBranch()))}, nil
case branchTip != tip:
return &landPrecheck{skipped: fmt.Sprintf("the branch was at %s, not at the tip of the resolution", branchTip)}, nil
}
@@ -304,10 +304,8 @@ func (s *Server) landPrecheckVerify(caller, budgetCtx context.Context, budget ti
return structerr.NewInternal("building the tree to verify: %w", err)
}
current := func(ctx context.Context) (bool, error) {
- now, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())
- if errors.Is(err, git.ErrReferenceNotFound) {
- return false, nil
- }
+ // A missing branch reads as the empty ID, which is never the tip.
+ now, err := s.landBranchTip(ctx, repo, git.NewReferenceNameFromBranchName(string(in.GetBranch())))
return now == pc.tip, err
}
var verifyErr *landVerifyError
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index c9025c6..1eae939 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -277,10 +277,8 @@ func (s *Server) landResolveAt(
}
var verifyErr *landVerifyError
current := func(ctx context.Context) (bool, error) {
- now, err := repo.ResolveRevision(ctx, git.NewReferenceNameFromBranchName(string(in.GetBranch())).Revision())
- if errors.Is(err, git.ErrReferenceNotFound) {
- return false, nil
- }
+ // A missing branch reads as the empty ID, which is never the tip.
+ now, err := s.landBranchTip(ctx, repo, git.NewReferenceNameFromBranchName(string(in.GetBranch())))
return now == tip, err
}
switch err := s.landVerifyOutOfLane(ctx, in.GetRepository(), repo, tree, current); {
diff --git a/internal/gitaly/service/operations/land_resolve_test.go b/internal/gitaly/service/operations/land_resolve_test.go
index 773d78f..9ed69fd 100644
--- a/internal/gitaly/service/operations/land_resolve_test.go
+++ b/internal/gitaly/service/operations/land_resolve_test.go
@@ -270,6 +270,7 @@ func (fx resolveFixture) server(t *testing.T, rcfg config.GraftResolver) *Server
}
return &Server{
logger: testhelper.NewLogger(t), gitConfig: fx.cfg.Git, resolveCfg: rcfg, conflictResolver: client, verifyPool: pool,
+ catfileCache: cache,
localRepoFactory: localrepo.NewFactory(testhelper.NewLogger(t), config.NewLocator(fx.cfg), gittest.NewCommandFactory(t, fx.cfg), cache),
}
}