Git designed for AI agents.
land: one landing attempt for batched and unbatched lands
Federico Elia4aca49fec354
diff --git a/internal/gitaly/service/operations/land.go b/internal/gitaly/service/operations/land.goindex 0b98390..4e61087 100644--- a/internal/gitaly/service/operations/land.go+++ b/internal/gitaly/service/operations/land.go@@ -1,7 +1,6 @@package operationsimport (- "bytes""context""errors""fmt"@@ -9,9 +8,7 @@ import ("strings""time"- "github.com/feder-cr/git-ai/internal/command""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/git/mvcc""github.com/feder-cr/git-ai/internal/git/quarantine"@@ -171,259 +168,53 @@ func (s *Server) landAncestry(ctx context.Context, quarantineRepo *localrepo.Rep// landAttempt performs a single compare-and-swap attempt of UserLand. It returns a landConflict// if the branch was moved by a concurrent writer, in which case the caller retries.func (s *Server) landAttempt(ctx context.Context, in *gitalypb.UserLandRequest, committer git.Signature) (*gitalypb.UserLandResponse, error) {- quarantineDir, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, in.GetRepository())+ attempt, cleanup, err := s.newLandingAttempt(ctx, in.GetRepository(), ignoreLandPhase)if err != nil {return nil, err}defer cleanup()- checker, releaseChecker, err := s.landAncestry(ctx, quarantineRepo)+ reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))+ tip, err := attempt.Tip(ctx, reference)if err != nil {return nil, err}- defer releaseChecker()-- objectHash, err := quarantineRepo.ObjectHash(ctx)- if err != nil {- return nil, structerr.NewInternal("detecting object hash: %w", err)- }-- commitID, err := objectHash.FromHex(in.GetCommitId())- if err != nil {- return nil, structerr.NewInvalidArgument("cannot parse commit ID: %w", err).- WithMetadata("commit_id", in.GetCommitId())- }- // The ID may refer to a tag object or another non-commit object that peels to a commit. A- // branch can only point to a commit, so the peeled commit is the one that lands.- commitID, err = quarantineRepo.ResolveRevision(ctx, git.Revision(commitID.String()+"^{commit}"))+ // The request is decided as a member of a batch is (landingAttempt.decide). The policy is not+ // evaluated here: the update runs with the identity of the request, so the hooks judge it.+ req := &landBatchRequest{ctx: ctx, in: in, committer: committer, actor: landBatchActor(ctx, in)}+ step, err := attempt.decide(ctx, tip, req)if err != nil {- return nil, structerr.NewInvalidArgument("cannot resolve commit ID: %w", err).- WithMetadata("commit_id", in.GetCommitId())- }-- reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))-- tip, err := quarantineRepo.ResolveRevision(ctx, reference.Revision())- if err != nil {- if !errors.Is(err, git.ErrReferenceNotFound) {- return nil, structerr.NewInternal("resolving branch: %w", err)- }-- if !in.GetCreateBranch() {- return nil, structerr.NewNotFound("branch %q does not exist", string(in.GetBranch()))- }-- // The branch does not exist yet and may be created at the commit. The- // compare-and-swap expects the zero OID, so a concurrent creation is detected.- if err := s.updateLandedReference(ctx, in, quarantineDir, quarantineRepo, reference, objectHash.ZeroOID, commitID, false); err != nil {- return nil, err- }-- return landResponse(objectHash.ZeroOID, commitID, false, false, true), nil+ return nil, err}- // The commit is already reachable from the tip, so there is nothing to do. This also covers- // commit_id being equal to the tip.- landed, err := checker.IsAncestor(ctx, commitID, tip)- if err != nil {- return nil, structerr.NewInternal("checking whether commit is already landed: %w", err)- }- if landed {+ oldTip := tip+ switch {+ case step.alreadyLanded:return landResponse(tip, tip, false, true, false), nil+ case step.created:+ oldTip = attempt.objectHash.ZeroOID+ case step.tip == tip:+ // The change became empty on top of the tip: the landing leaves the branch unchanged.+ return landResponse(tip, tip, false, false, false), nil}- newTip := commitID- rewritten := false- var resolution *landResolution-- fastForward, err := checker.IsAncestor(ctx, tip, commitID)- if err != nil {- return nil, structerr.NewInternal("checking for ancestry: %w", err)- }-- if !fastForward {- newTip, err = s.computeLandedTip(ctx, in, quarantineRepo, tip, commitID, committer)- if err != nil {- if !in.GetResolveConflicts() || !isLandMergeConflict(err) {- return nil, err- }-- // The conflict is resolved into a merge commit. When that fails, the client gets- // the merge conflict it would have got without resolution.- res, resolveErr := s.resolveLandConflicts(ctx, in, quarantineRepo, tip, commitID, committer)- if resolveErr != nil {- s.logger.WithError(resolveErr).WithField("branch", string(in.GetBranch())).- Info("graft: conflict resolution of land failed")- return nil, err- }-- resolution = &res- newTip = res.commit- }- rewritten = true-- // Rebasing drops the commits that become empty on top of the tip, so every commit- // of the change was dropped when the tip already holds all of its changes, for- // example because they were applied upstream as cherry-picks. commit_id itself is- // not reachable from the tip, so the change is not reported as already landed: the- // response reports a landing that left the branch unchanged.- if resolution == nil && newTip == tip {- return landResponse(tip, tip, false, false, false), nil- }+ if err := attempt.publish(ctx); err != nil {+ return nil, err}- if err := s.updateLandedReference(ctx, in, quarantineDir, quarantineRepo, reference, tip, newTip, rewritten); err != nil {+ if err := s.updateLandedReference(ctx, in, attempt.quarantineDir, attempt.repo, reference, oldTip, step.tip, step.rewritten); err != nil {return nil, err}- resp := landResponse(tip, newTip, rewritten, false, false)- if resolution != nil {- resp.ResolvedPaths = resolution.resolvedPaths- resp.Resolver = resolution.resolver+ resp := landResponse(oldTip, step.tip, step.rewritten, false, step.created)+ if step.resolution != nil {+ resp.ResolvedPaths = step.resolution.resolvedPaths+ resp.Resolver = step.resolution.resolver}return resp, nil}-// computeLandedTip computes the new branch tip for a commit that is neither reachable from the-// tip nor a fast-forward of it. It rebases or merges the change onto the tip, depending on the-// strategy. FF_ONLY fails with a NotAncestor error.-func (s *Server) computeLandedTip(- ctx context.Context,- in *gitalypb.UserLandRequest,- quarantineRepo *localrepo.Repo,- tip, commitID git.ObjectID,- committer git.Signature,-) (git.ObjectID, error) {- switch in.GetStrategy() {- case gitalypb.UserLandRequest_STRATEGY_FF_ONLY:- return "", structerr.NewFailedPrecondition("branch %q cannot be fast-forwarded to commit", string(in.GetBranch())).- WithDetail(&gitalypb.UserLandError{- Error: &gitalypb.UserLandError_NotAncestor{- NotAncestor: &gitalypb.NotAncestorError{- ParentRevision: []byte(tip.String()),- ChildRevision: []byte(commitID.String()),- },- },- })-- case gitalypb.UserLandRequest_STRATEGY_MERGE:- mergeMessage := string(in.GetMergeMessage())- if mergeMessage == "" {- mergeMessage = fmt.Sprintf("Merge commit '%s' into %s", commitID, string(in.GetBranch()))- }-- mergeCommitID, err := s.merge(ctx, quarantineRepo,- committer,- committer,- mergeMessage,- tip.String(),- commitID.String(),- false,- false,- )- if err != nil {- var conflictErr *localrepo.MergeTreeConflictError- if errors.As(err, &conflictErr) {- return "", landMergeConflictError(tip, commitID, conflictingFileNames(conflictErr), conflictErr)- }-- return "", structerr.NewInternal("merging commit: %w", err)- }-- return git.ObjectID(mergeCommitID), nil-- case gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED:- if err := rejectMergeCommitsInRange(ctx, quarantineRepo, tip, commitID); err != nil {- return "", err- }-- // Rebasing needs a merge base. Unrelated histories can only be landed with STRATEGY_MERGE.- related, err := hasMergeBase(ctx, quarantineRepo, tip, commitID)- if err != nil {- return "", structerr.NewInternal("checking for a common ancestor: %w", err)- }- if !related {- return "", structerr.NewInvalidArgument("commit shares no history with branch %q: use STRATEGY_MERGE to land unrelated history", string(in.GetBranch()))- }-- // Commits that are patch-equivalent to a commit of the tip's history are reapplied- // instead of being dropped. The tip may have reverted such a commit since, and- // dropping it would lose its changes while the landing succeeds. A reapplied commit- // whose changes the tip still holds becomes empty and is dropped by the rebase. One- // whose changes the tip modified further conflicts, which is reported rather than- // guessed at.- rebasedCommitID, err := quarantineRepo.Rebase(- ctx,- tip.String(),- commitID.String(),- localrepo.RebaseWithCommitter(committer),- localrepo.RebaseWithReappliedCherryPicks(),- )- if err != nil {- // The conflict error is passed as the cause itself, not the structerr-wrapped- // error from Rebase, because structerr takes the code of the innermost error.- var conflictErr *localrepo.RebaseConflictError- if errors.As(err, &conflictErr) {- return "", landMergeConflictError(tip, commitID, conflictingFileNames(conflictErr.ConflictError), conflictErr)- }-- return "", structerr.NewInternal("rebasing commit: %w", err)- }-- return rebasedCommitID, nil-- default:- return "", structerr.NewInvalidArgument("unknown strategy: %d", in.GetStrategy())- }-}--// rejectMergeCommitsInRange fails when the commits reachable from commit but not from tip contain-// a merge commit. Rebasing drops merge commits, so such a change can only be landed with-// STRATEGY_MERGE.-func rejectMergeCommitsInRange(ctx context.Context, repo *localrepo.Repo, tip, commitID git.ObjectID) error {- var stdout, stderr bytes.Buffer- if err := repo.ExecAndWait(ctx,- gitcmd.Command{- Name: "rev-list",- Flags: []gitcmd.Option{gitcmd.Flag{Name: "--merges"}},- Args: []string{fmt.Sprintf("%s..%s", tip, commitID)},- },- gitcmd.WithStdout(&stdout),- gitcmd.WithStderr(&stderr),- ); err != nil {- return structerr.NewInternal("listing merge commits: %w", err).WithMetadata("stderr", stderr.String())- }-- if strings.TrimSpace(stdout.String()) != "" {- return structerr.NewInvalidArgument("commit range contains merge commits, which cannot be rebased: use STRATEGY_MERGE")- }-- return nil-}--// hasMergeBase reports whether a and b have a common ancestor. Unrelated histories have no merge-// base, which git merge-base reports with exit status 1.-func hasMergeBase(ctx context.Context, repo *localrepo.Repo, a, b git.ObjectID) (bool, error) {- var stdout, stderr bytes.Buffer- if err := repo.ExecAndWait(ctx,- gitcmd.Command{- Name: "merge-base",- Args: []string{a.String(), b.String()},- },- gitcmd.WithStdout(&stdout),- gitcmd.WithStderr(&stderr),- ); err != nil {- if status, ok := command.ExitStatus(err); ok && status == 1 {- return false, nil- }-- return false, fmt.Errorf("merge-base: %w, stderr: %q", err, stderr.String())- }-- return true, 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.diff --git a/internal/gitaly/service/operations/land_attempt.go b/internal/gitaly/service/operations/land_attempt.gonew file mode 100644index 0000000..7b637e2--- /dev/null+++ b/internal/gitaly/service/operations/land_attempt.go@@ -0,0 +1,355 @@+package operations++import (+ "context"+ "errors"+ "fmt"+ "path/filepath"+ "time"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/catfile"+ "github.com/feder-cr/git-ai/internal/git/localrepo"+ "github.com/feder-cr/git-ai/internal/git/quarantine"+ "github.com/feder-cr/git-ai/internal/graft/ancestry"+ "github.com/feder-cr/git-ai/internal/graft/rebase"+ "github.com/feder-cr/git-ai/internal/structerr"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// landPhaseObserver observes the time a phase of a landing attempt took, from start until now.+// Batch attempts observe graft_land_batch_phase_seconds (observeLandBatchPhase); an unbatched land+// observes nothing.+type landPhaseObserver func(phase string, start time.Time)++// ignoreLandPhase is the landPhaseObserver of an unbatched land.+func ignoreLandPhase(string, time.Time) {}++// landingAttempt is one attempt to land changes onto a branch, in a quarantine of its own. The+// unbatched land and every attempt of a batch use it, so both decide how a change lands, rebase it+// and publish its objects the same way (doc/graft/PLAN8.md): one listing of the commits a change+// adds to the tip decides, one rebase engine replays, and the objects the attempt wrote are made+// durable once, as one pack, before the reference moves.+type landingAttempt struct {+ server *Server+ repository *gitalypb.Repository+ quarantineDir *quarantine.Dir+ repo *localrepo.Repo+ objectHash git.ObjectHash+ // info peels the commit IDs of the requests. It serves the whole attempt.+ info catfile.ObjectInfoReader+ // rebaser replays the rebased changes of the attempt. It serves the whole attempt.+ rebaser batchRebaser+ // ancestry lists the commits that a change adds to the tip, with the graph index when it is+ // enabled.+ ancestry ancestry.Checker+ // observe observes the phases of the attempt.+ observe landPhaseObserver+}++// batchRebaser replays the commits of the changes of an attempt. It is rebase.Engine.+type batchRebaser interface {+ Replay(ctx context.Context, upstream git.ObjectID, todo []git.ObjectID, committer *git.Signature) (git.ObjectID, error)+ Close() error+}++// newBatchRebaser starts the rebaser of an attempt on the quarantine of the attempt. The objects it+// writes are not fsynced one by one: the publish of the attempt packs them into one fsynced pack+// before the reference points to them (see publish). It is a variable so that tests can observe the+// replays.+var newBatchRebaser = func(repo *localrepo.Repo) batchRebaser {+ return rebase.New(repo, rebase.WithDeferredDurability())+}++// newLandingAttempt starts an attempt on repository: it opens the quarantine of the attempt and the+// processes that serve it. The returned function ends the attempt.+func (s *Server) newLandingAttempt(ctx context.Context, repository *gitalypb.Repository, observe landPhaseObserver) (*landingAttempt, func(), error) {+ defer observe(landBatchPhaseQuarantine, time.Now())++ quarantineDir, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, repository)+ if err != nil {+ return nil, nil, err+ }++ objectHash, err := quarantineRepo.ObjectHash(ctx)+ if err != nil {+ cleanup()+ return nil, nil, structerr.NewInternal("detecting object hash: %w", err)+ }++ checker, releaseChecker, err := s.landAncestry(ctx, quarantineRepo)+ if err != nil {+ cleanup()+ return nil, nil, err+ }++ // The commit IDs of the requests are peeled by the object info process of the quarantine,+ // which the attempt starts once.+ info, releaseInfo, err := s.catfileCache.ObjectInfoReader(ctx, quarantineRepo)+ if err != nil {+ releaseChecker()+ cleanup()+ return nil, nil, structerr.NewInternal("reading object info: %w", err)+ }++ // One rebase engine serves the whole attempt, and the attempt ends with it. Its objects are+ // made durable by the publish of the attempt, as one pack (see publish).+ rebaser := newBatchRebaser(quarantineRepo)++ return &landingAttempt{+ server: s,+ repository: repository,+ quarantineDir: quarantineDir,+ repo: quarantineRepo,+ objectHash: objectHash,+ info: info,+ rebaser: rebaser,+ ancestry: checker,+ observe: observe,+ }, func() {+ releaseInfo()+ releaseChecker()+ if err := rebaser.Close(); err != nil {+ s.logger.WithError(err).Error("graft: closing the rebaser of a landing attempt")+ }+ cleanup()+ }, nil+}++// Tip returns the tip of the reference. 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())+ if err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ return "", nil+ }++ return "", structerr.NewInternal("resolving branch: %w", err)+ }++ return tip, nil+}++// decide lands the change of req onto tip in the quarantine of the attempt, without publishing it:+// it peels the commit ID, decides from one listing of the commits the change adds to tip whether it+// is already landed, fast-forwards tip or diverges, and rebases or merges a diverged change, resolving+// its conflicts when the request asks for it. The returned step tells what the change did to tip.+//+// The git work runs in ctx with the identity of the request, which decides conflict resolution.+func (a *landingAttempt) decide(ctx context.Context, tip git.ObjectID, req *landBatchRequest) (landStep, error) {+ ctx = requestIdentityContext(ctx, req)+ in := req.in++ commitID, err := a.objectHash.FromHex(in.GetCommitId())+ if err != nil {+ return landStep{tipIndependent: true}, structerr.NewInvalidArgument("cannot parse commit ID: %w", err).+ WithMetadata("commit_id", in.GetCommitId())+ }++ // The ID may refer to a tag object or another non-commit object that peels to a commit. A+ // branch can only point to a commit, so the peeled commit is the one that lands.+ commitID, err = a.peel(ctx, commitID)+ if err != nil {+ if errors.As(err, new(catfile.NotFoundError)) {+ return landStep{tipIndependent: true}, structerr.NewInvalidArgument("cannot resolve commit ID: %w", err).+ WithMetadata("commit_id", in.GetCommitId())+ }++ return landStep{}, structerr.NewInternal("peeling commit ID: %w", err)+ }++ if tip == "" {+ if !in.GetCreateBranch() {+ return landStep{}, structerr.NewNotFound("branch %q does not exist", string(in.GetBranch()))+ }++ // The branch is created at the commit. The compare-and-swap expects the zero OID, so a+ // concurrent creation is detected.+ return landStep{commit: commitID, tip: commitID, created: true}, nil+ }++ plan, err := a.plan(ctx, tip, commitID)+ if err != nil {+ return landStep{}, err+ }++ switch plan.Kind() {+ case rebase.LandAlreadyLanded:+ // The commit is already reachable from the tip, so there is nothing to do. This also covers+ // commit_id being equal to the tip.+ return landStep{commit: commitID, tip: tip, alreadyLanded: true}, nil++ case rebase.LandFastForward:+ return landStep{commit: commitID, tip: commitID}, nil+ }++ newTip, err := a.computeTip(ctx, in, plan, tip, commitID, req.committer)+ var resolution *landResolution+ if err != nil {+ if !in.GetResolveConflicts() || !isLandMergeConflict(err) {+ return landStep{}, err+ }++ // The conflict is resolved into a merge commit. When that fails, the request gets the+ // merge conflict it would have got without resolution.+ res, resolveErr := a.server.resolveLandConflicts(ctx, in, a.repo, tip, commitID, req.committer)+ if resolveErr != nil {+ a.server.logger.WithError(resolveErr).WithField("branch", string(in.GetBranch())).+ Info("graft: conflict resolution of land failed")+ return landStep{}, err+ }++ resolution = &res+ newTip = res.commit+ }++ // Rebasing drops the commits that become empty on top of the tip, so every commit of the change+ // was dropped when the tip already holds all of its changes, for example because they were+ // applied upstream as cherry-picks. commit_id itself is not reachable from the tip, so the change+ // is not reported as already landed: the step reports a landing that left the branch unchanged.+ if resolution == nil && newTip == tip {+ return landStep{commit: commitID, tip: tip}, nil+ }++ return landStep{commit: commitID, tip: newTip, rewritten: true, resolution: resolution}, nil+}++// peel returns the commit that commitID is or peels to, through the object info process of the+// attempt. commitID is a full object ID, so nothing is abbreviated or ambiguous. An object that does+// not exist or does not peel to a commit is a catfile.NotFoundError.+func (a *landingAttempt) peel(ctx context.Context, commitID git.ObjectID) (git.ObjectID, error) {+ info, err := a.info.Info(ctx, git.Revision(commitID.String()+"^{commit}"))+ if err != nil {+ return "", err+ }++ return info.Oid, nil+}++// plan decides how commitID lands onto tip from one listing of the commits it adds to tip (see+// rebase.PlanLanding).+func (a *landingAttempt) plan(ctx context.Context, tip, commitID git.ObjectID) (rebase.LandPlan, error) {+ defer a.observe(landBatchPhasePlan, time.Now())++ rng, err := a.ancestry.Range(ctx, tip, commitID)+ if err != nil {+ return rebase.LandPlan{}, structerr.NewInternal("listing the commits of the change: %w", err)+ }++ plan, err := rebase.PlanLanding(tip, commitID, rng)+ if err != nil {+ return rebase.LandPlan{}, structerr.NewInternal("planning the landing: %w", err)+ }++ return plan, nil+}++// computeTip computes the new tip for a commit whose plan is diverged: it is neither reachable from+// the tip nor a fast-forward of it. The strategy of the request decides: FF_ONLY fails with a+// NotAncestor error, MERGE merges the change into the tip, and REBASE replays the commits of the plan+// with the rebaser of the attempt. A rebase refuses a range with merge commits or without a common+// ancestor (InvalidArgument), and a conflict of the merge or the rebase is a merge conflict.+func (a *landingAttempt) computeTip(ctx context.Context, in *gitalypb.UserLandRequest, plan rebase.LandPlan, tip, commitID git.ObjectID, committer git.Signature) (git.ObjectID, error) {+ switch in.GetStrategy() {+ case gitalypb.UserLandRequest_STRATEGY_FF_ONLY:+ return "", structerr.NewFailedPrecondition("branch %q cannot be fast-forwarded to commit", string(in.GetBranch())).+ WithDetail(&gitalypb.UserLandError{+ Error: &gitalypb.UserLandError_NotAncestor{+ NotAncestor: &gitalypb.NotAncestorError{+ ParentRevision: []byte(tip.String()),+ ChildRevision: []byte(commitID.String()),+ },+ },+ })++ case gitalypb.UserLandRequest_STRATEGY_MERGE:+ mergeMessage := string(in.GetMergeMessage())+ if mergeMessage == "" {+ mergeMessage = fmt.Sprintf("Merge commit '%s' into %s", commitID, string(in.GetBranch()))+ }++ mergeCommitID, err := a.server.merge(ctx, a.repo,+ committer,+ committer,+ mergeMessage,+ tip.String(),+ commitID.String(),+ false,+ false,+ )+ if err != nil {+ var conflictErr *localrepo.MergeTreeConflictError+ if errors.As(err, &conflictErr) {+ return "", landMergeConflictError(tip, commitID, conflictingFileNames(conflictErr), conflictErr)+ }++ return "", structerr.NewInternal("merging commit: %w", err)+ }++ return git.ObjectID(mergeCommitID), nil++ case gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED:+ todo, err := plan.Replay()+ switch {+ case errors.Is(err, rebase.ErrMergesInRange):+ // Rebasing drops merge commits, so such a change can only be landed with STRATEGY_MERGE.+ return "", structerr.NewInvalidArgument("commit range contains merge commits, which cannot be rebased: use STRATEGY_MERGE")+ case errors.Is(err, rebase.ErrUnrelated):+ // Rebasing needs a merge base. Unrelated histories can only be landed with STRATEGY_MERGE.+ return "", structerr.NewInvalidArgument("commit shares no history with branch %q: use STRATEGY_MERGE to land unrelated history", string(in.GetBranch()))+ case err != nil:+ return "", structerr.NewInternal("planning the rebase: %w", err)+ }++ // Commits that are patch-equivalent to a commit of the tip's history are reapplied instead of+ // being dropped. The tip may have reverted such a commit since, and dropping it would lose its+ // changes while the landing succeeds. A reapplied commit whose changes the tip still holds+ // becomes empty and is dropped by the replay. One whose changes the tip modified further+ // conflicts, which is reported rather than guessed at.+ defer a.observe(landBatchPhaseReplay, time.Now())+ rebasedCommitID, err := a.rebaser.Replay(ctx, tip, todo, &committer)+ if err != nil {+ // The conflict error is passed as the cause itself, not wrapped, because structerr takes+ // the code of the innermost error.+ var conflictErr *localrepo.RebaseConflictError+ if errors.As(err, &conflictErr) {+ return "", landMergeConflictError(tip, commitID, conflictingFileNames(conflictErr.ConflictError), conflictErr)+ }++ return "", structerr.NewInternal("rebasing commit: %w", err)+ }++ return rebasedCommitID, nil++ default:+ return "", structerr.NewInvalidArgument("unknown strategy: %d", in.GetStrategy())+ }+}++// publish makes the objects that the attempt wrote durable before its compare-and-swap: it packs+// every loose object of the quarantine into one fsynced pack (see packAttemptObjects), which the+// quarantine migration of the update then moves into the repository. A failure fails the update of+// the attempt: nothing is published.+func (a *landingAttempt) publish(ctx context.Context) error {+ defer a.observe(landBatchPhasePack, time.Now())++ repoPath, err := a.repo.Path(ctx)+ if err != nil {+ return structerr.NewInternal("getting quarantine path: %w", err)+ }++ objectDir := a.quarantineDir.QuarantinedRepo().GetGitObjectDirectory()+ if !filepath.IsAbs(objectDir) {+ objectDir = filepath.Join(repoPath, objectDir)+ }++ packed, err := packBatchObjects(ctx, a.repo, objectDir)+ if err != nil {+ return structerr.NewInternal("packing the objects of the attempt: %w", err)+ }+ landBatchPackedObjectsTotal.Add(float64(packed))++ return nil+}diff --git a/internal/gitaly/service/operations/land_attempt_test.go b/internal/gitaly/service/operations/land_attempt_test.gonew file mode 100644index 0000000..99df632--- /dev/null+++ b/internal/gitaly/service/operations/land_attempt_test.go@@ -0,0 +1,90 @@+package operations++import (+ "context"+ "errors"+ "path/filepath"+ "sync/atomic"+ "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/testhelper"+ "github.com/stretchr/testify/require"+ "google.golang.org/grpc/codes"+)++// unbatchedLandFixture is a repository whose main branch is at base, with a change based on the+// parent of base, served by an operations service without batching (not standalone).+func unbatchedLandFixture(t *testing.T, ctx context.Context) (context.Context, stepperFixture, func(git.ObjectID) error) {+ t.Helper()++ ctx, cfg, client := setupOperationsService(t, ctx)+ f := newStepperFixture(t, ctx, cfg)++ land := func(commit git.ObjectID) error {+ resp, err := client.UserLand(ctx, landRequest(f.repoProto, git.DefaultBranch, commit))+ if err != nil {+ return err+ }+ require.Equal(t, int32(1), resp.GetBatchSize(), "the land is not batched")+ return nil+ }++ return ctx, f, land+}++// TestUserLand_unbatchedLandUsesTheLandingAttempt is not parallel: it replaces newBatchRebaser. An+// unbatched land decides and rebases like a batch attempt (one replay through the rebaser of the+// attempt) and publishes its objects as one pack: the rebased commit is not a loose object of the+// repository, and the repository is connected.+func TestUserLand_unbatchedLandUsesTheLandingAttempt(t *testing.T) {+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)++ var replays atomic.Int64+ original := newBatchRebaser+ newBatchRebaser = func(repo *localrepo.Repo) batchRebaser {+ return countingRebaser{batchRebaser: original(repo), rebases: &replays}+ }+ t.Cleanup(func() { newBatchRebaser = original })++ _, f, land := unbatchedLandFixture(t, ctx)+ packsBefore := len(packIndexes(t, filepath.Join(f.repoPath, "objects")))++ require.NoError(t, land(f.diverged1))+ require.Equal(t, int64(1), replays.Load(), "the diverged change is replayed once, through the rebaser")++ tip := gittest.ResolveRevision(t, f.cfg, f.repoPath, "refs/heads/"+git.DefaultBranch)+ require.NotEqual(t, f.diverged1, tip, "the change was rebased")+ require.NoFileExists(t, filepath.Join(f.repoPath, "objects", tip.String()[:2], tip.String()[2:]),+ "the rebased commit is not a loose object of the repository")+ require.Len(t, packIndexes(t, filepath.Join(f.repoPath, "objects")), packsBefore+1, "the land publishes one pack")+ gittest.Exec(t, f.cfg, "-C", f.repoPath, "fsck", "--connectivity-only", "--no-dangling")++ // A fast-forward writes no object, so it replays nothing and publishes no pack.+ forward := gittest.WriteCommit(t, f.cfg, f.repoPath, gittest.WithParents(tip), gittest.WithMessage("forward"))+ require.NoError(t, land(forward))+ require.Equal(t, int64(1), replays.Load())+ require.Len(t, packIndexes(t, filepath.Join(f.repoPath, "objects")), packsBefore+1)+ requireBranchAt(t, f.cfg, f.repoPath, git.DefaultBranch, forward)+}++// TestUserLand_unbatchedPackFailureFailsTheLand is not parallel: it replaces packBatchObjects. A land+// whose objects cannot be packed fails with Internal and leaves the branch where it was.+func TestUserLand_unbatchedPackFailureFailsTheLand(t *testing.T) {+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)++ errPack := errors.New("injected pack failure")+ original := packBatchObjects+ packBatchObjects = func(context.Context, *localrepo.Repo, string) (int, error) { return 0, errPack }+ t.Cleanup(func() { packBatchObjects = original })++ _, f, land := unbatchedLandFixture(t, ctx)++ err := land(f.diverged1)+ testhelper.RequireGrpcCode(t, err, codes.Internal)+ require.ErrorContains(t, err, errPack.Error())+ requireBranchAt(t, f.cfg, f.repoPath, git.DefaultBranch, f.base)+}diff --git a/internal/gitaly/service/operations/land_batch_executor_spec_test.go b/internal/gitaly/service/operations/land_batch_executor_spec_test.goindex 0963fac..dcb64e8 100644--- a/internal/gitaly/service/operations/land_batch_executor_spec_test.go+++ b/internal/gitaly/service/operations/land_batch_executor_spec_test.go@@ -86,19 +86,14 @@ func TestSpecAncestryNewEnabledAgreesWithGit(t *testing.T) {checker := ancestry.New(ctx, true, repo, nil, nil, testhelper.NewLogger(t))require.NotNil(t, checker)+ // Phase 4b: the checker answers one question, the range of a change (PLAN8).pairs := [][2]git.ObjectID{{base, merge}, {merge, base}, {left, right}, {right, left}, {base, base}}for _, p := range pairs {- want, wantErr := reference.IsAncestor(ctx, p[0], p[1])- got, gotErr := checker.IsAncestor(ctx, p[0], p[1])+ want, wantErr := reference.Range(ctx, p[0], p[1])+ got, gotErr := checker.Range(ctx, p[0], p[1])require.Equal(t, wantErr != nil, gotErr != nil)- require.Equal(t, want, got, "IsAncestor(%s, %s)", p[0], p[1])+ require.ElementsMatch(t, want, got, "Range(%s, %s)", p[0], p[1])}-- wantBase, wantFound, wantErr := reference.MergeBase(ctx, left, right)- gotBase, gotFound, gotErr := checker.MergeBase(ctx, left, right)- require.Equal(t, wantErr != nil, gotErr != nil)- require.Equal(t, wantFound, gotFound)- require.Equal(t, wantBase, gotBase)}// Spec: WP37 — CompleteEarly returns false unless ctx is an executor context ofdiff --git a/internal/gitaly/service/operations/land_batch_server.go b/internal/gitaly/service/operations/land_batch_server.goindex 9e57484..fc6515c 100644--- a/internal/gitaly/service/operations/land_batch_server.go+++ b/internal/gitaly/service/operations/land_batch_server.go@@ -3,21 +3,15 @@ package operationsimport ("context""errors"- "path/filepath""time""github.com/feder-cr/git-ai/internal/git"- "github.com/feder-cr/git-ai/internal/git/catfile"- "github.com/feder-cr/git-ai/internal/git/localrepo""github.com/feder-cr/git-ai/internal/git/mvcc"- "github.com/feder-cr/git-ai/internal/git/quarantine""github.com/feder-cr/git-ai/internal/gitaly/config""github.com/feder-cr/git-ai/internal/gitaly/hook""github.com/feder-cr/git-ai/internal/gitaly/hook/updateref"- "github.com/feder-cr/git-ai/internal/graft/ancestry""github.com/feder-cr/git-ai/internal/graft/changelog""github.com/feder-cr/git-ai/internal/graft/landlane"- "github.com/feder-cr/git-ai/internal/graft/rebase""github.com/feder-cr/git-ai/internal/log""github.com/feder-cr/git-ai/internal/structerr""github.com/feder-cr/git-ai/proto/go/gitalypb"@@ -182,58 +176,15 @@ func (s *Server) executeLandBatch(ctx context.Context, _ landlane.Key, reqs []*lreturn results, nil}-// newLandBatchStepper starts a batch attempt on repository: it opens the quarantine of the attempt-// and the processes that serve it. The returned function ends the attempt.+// newLandBatchStepper starts a batch attempt on repository. Its phases are observed in+// graft_land_batch_phase_seconds. The returned function ends the attempt.func (s *Server) newLandBatchStepper(ctx context.Context, repository *gitalypb.Repository) (*serverLandBatchStepper, func(), error) {- defer observeLandBatchPhase(landBatchPhaseQuarantine, time.Now())-- quarantineDir, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, repository)+ attempt, cleanup, err := s.newLandingAttempt(ctx, repository, observeLandBatchPhase)if err != nil {return nil, nil, err}- objectHash, err := quarantineRepo.ObjectHash(ctx)- if err != nil {- cleanup()- return nil, nil, structerr.NewInternal("detecting object hash: %w", err)- }-- checker, releaseChecker, err := s.landAncestry(ctx, quarantineRepo)- if err != nil {- cleanup()- return nil, nil, err- }-- // The commit IDs of the requests are peeled by the object info process of the- // quarantine, which the attempt starts once.- info, releaseInfo, err := s.catfileCache.ObjectInfoReader(ctx, quarantineRepo)- if err != nil {- releaseChecker()- cleanup()- return nil, nil, structerr.NewInternal("reading object info: %w", err)- }-- // One rebase engine serves the whole attempt, and the attempt ends with it. Its objects- // are made durable by the publish of the attempt, as one pack (see publish).- rebaser := newBatchRebaser(quarantineRepo)-- return &serverLandBatchStepper{- server: s,- repository: repository,- quarantineDir: quarantineDir,- repo: quarantineRepo,- objectHash: objectHash,- info: info,- rebaser: rebaser,- ancestry: checker,- }, func() {- releaseInfo()- releaseChecker()- if err := rebaser.Close(); err != nil {- s.logger.WithError(err).Error("graft: closing the rebaser of a land batch")- }- cleanup()- }, nil+ return &serverLandBatchStepper{landingAttempt: attempt}, cleanup, nil}// landBatchResult builds the result of a request of a batch.@@ -272,55 +223,17 @@ func landBatchGiveUp(req *landBatchRequest, cause error, attempts int) error {return landKeptMovingError(req.in, conflict, attempts)}-// batchRebaser replays the commits of the members of a batch attempt. It is rebase.Engine.-type batchRebaser interface {- Replay(ctx context.Context, upstream git.ObjectID, todo []git.ObjectID, committer *git.Signature) (git.ObjectID, error)- Close() error-}--// newBatchRebaser starts the rebaser of a batch attempt on the quarantine of the attempt. The objects-// it writes are not fsynced one by one: the publish of the attempt packs them into one fsynced pack-// before the reference points to them (see publish). It is a variable so that tests can observe the-// replays.-var newBatchRebaser = func(repo *localrepo.Repo) batchRebaser {- return rebase.New(repo, rebase.WithDeferredDurability())-}--// serverLandBatchStepper implements landBatchStepper on the quarantine of one batch attempt.+// serverLandBatchStepper implements landBatchStepper on one batch attempt: the landing attempt that+// every land uses (see landingAttempt), plus the in-process admission of each request and the+// combined update of the batch.type serverLandBatchStepper struct {- server *Server- repository *gitalypb.Repository- quarantineDir *quarantine.Dir- repo *localrepo.Repo- objectHash git.ObjectHash- // info peels the commit IDs of the requests. It serves the whole attempt.- info catfile.ObjectInfoReader- // rebaser replays the members of the attempt. It serves the whole attempt.- rebaser batchRebaser- // ancestry lists the commits that each request adds to the running tip, with the graph index- // when it is enabled.- ancestry ancestry.Checker-}--// Tip returns the tip of the reference. A missing reference yields an empty ID.-func (st *serverLandBatchStepper) Tip(ctx context.Context, reference git.ReferenceName) (git.ObjectID, error) {- defer observeLandBatchPhase(landBatchPhaseTip, time.Now())-- tip, err := st.repo.ResolveRevision(ctx, reference.Revision())- if err != nil {- if errors.Is(err, git.ErrReferenceNotFound) {- return "", nil- }-- return "", structerr.NewInternal("resolving branch: %w", err)- }-- return tip, nil+ *landingAttempt}-// Land lands the request onto tip. It mirrors the decision of landAttempt for a single request. The-// standalone policy is evaluated for the actor on the new tip the request would create, before the-// request changes the running tip.+// Land lands the request onto tip with the decision that every land makes (landingAttempt.decide).+// The standalone policy is then evaluated for the actor on the new tip the request would create,+// before the request changes the running tip: the combined update runs as graft-batch, so the hooks+// do not judge the request.//// The git work runs in the context of the batch, which a submitter does not cancel: the request is// part of the batch from the moment it lands. The identity is the one of the request, not the one of@@ -328,151 +241,26 @@ func (st *serverLandBatchStepper) Tip(ctx context.Context, reference git.Referenfunc (st *serverLandBatchStepper) Land(ctx context.Context, tip git.ObjectID, req *landBatchRequest) (landStep, error) {defer observeLandBatchPhase(landBatchPhaseLandMember, time.Now())- ctx = requestIdentityContext(ctx, req)- in := req.in- reference := req.reference()-- commitID, err := st.objectHash.FromHex(in.GetCommitId())+ step, err := st.decide(ctx, tip, req)if err != nil {- return landStep{tipIndependent: true}, structerr.NewInvalidArgument("cannot parse commit ID: %w", err).- WithMetadata("commit_id", in.GetCommitId())+ return step, err}- commitID, err = st.peel(ctx, commitID)- if err != nil {- if errors.As(err, new(catfile.NotFoundError)) {- return landStep{tipIndependent: true}, structerr.NewInvalidArgument("cannot resolve commit ID: %w", err).- WithMetadata("commit_id", in.GetCommitId())- }-- return landStep{}, structerr.NewInternal("peeling commit ID: %w", err)+ // A request that changes the running tip is admitted for the update it makes. A created branch+ // is updated from the zero OID.+ if step.alreadyLanded || (!step.created && step.tip == tip) {+ return step, nil}- if tip == "" {- if !in.GetCreateBranch() {- return landStep{}, structerr.NewNotFound("branch %q does not exist", string(in.GetBranch()))- }-- if err := st.admit(req, reference, st.objectHash.ZeroOID, commitID); err != nil {- return landStep{}, err- }-- return landStep{commit: commitID, tip: commitID, created: true}, nil+ oldTip := tip+ if step.created {+ oldTip = st.objectHash.ZeroOID}-- plan, err := st.plan(ctx, tip, commitID)- if err != nil {+ if err := st.admit(req, req.reference(), oldTip, step.tip); err != nil {return landStep{}, err}- switch plan.Kind() {- case rebase.LandAlreadyLanded:- return landStep{commit: commitID, tip: tip, alreadyLanded: true}, nil-- case rebase.LandFastForward:- if err := st.admit(req, reference, tip, commitID); err != nil {- return landStep{}, err- }-- return landStep{commit: commitID, tip: commitID}, nil- }-- newTip, err := st.computeTip(ctx, in, plan, tip, commitID, req.committer)- var resolution *landResolution- if err != nil {- if !in.GetResolveConflicts() || !isLandMergeConflict(err) {- return landStep{}, err- }-- res, resolveErr := st.server.resolveLandConflicts(ctx, in, st.repo, tip, commitID, req.committer)- if resolveErr != nil {- st.server.logger.WithError(resolveErr).WithField("branch", string(in.GetBranch())).- Info("graft: conflict resolution of land failed")- return landStep{}, err- }-- resolution = &res- newTip = res.commit- }-- // The rebase dropped the change because the tip already holds all of it. The request reports- // a landing that changed nothing, as landAttempt does.- if resolution == nil && newTip == tip {- return landStep{commit: commitID, tip: tip}, nil- }-- if err := st.admit(req, reference, tip, newTip); err != nil {- return landStep{}, err- }-- return landStep{commit: commitID, tip: newTip, rewritten: true, resolution: resolution}, nil-}--// peel returns the commit that commitID is or peels to, through the object info process of the-// attempt. commitID is a full object ID, so nothing is abbreviated or ambiguous. An object that does-// not exist or does not peel to a commit is a catfile.NotFoundError.-func (st *serverLandBatchStepper) peel(ctx context.Context, commitID git.ObjectID) (git.ObjectID, error) {- info, err := st.info.Info(ctx, git.Revision(commitID.String()+"^{commit}"))- if err != nil {- return "", err- }-- return info.Oid, nil-}--// plan decides how commitID lands onto tip from one listing of the commits it adds to tip (see-// rebase.PlanLanding).-func (st *serverLandBatchStepper) plan(ctx context.Context, tip, commitID git.ObjectID) (rebase.LandPlan, error) {- defer observeLandBatchPhase(landBatchPhasePlan, time.Now())-- rng, err := st.ancestry.Range(ctx, tip, commitID)- if err != nil {- return rebase.LandPlan{}, structerr.NewInternal("listing the commits of the change: %w", err)- }-- plan, err := rebase.PlanLanding(tip, commitID, rng)- if err != nil {- return rebase.LandPlan{}, structerr.NewInternal("planning the landing: %w", err)- }-- return plan, nil-}--// computeTip computes the new tip of the attempt for a commit whose plan is diverged: it is neither-// reachable from the tip nor a fast-forward of it. The merge and fast-forward-only strategies are-// computeLandedTip. The rebase strategy replays the commits of the plan with the rebaser of the-// attempt, with the errors of computeLandedTip: a range with merge commits or without a common-// ancestor is InvalidArgument, a conflict is a merge conflict.-func (st *serverLandBatchStepper) computeTip(ctx context.Context, in *gitalypb.UserLandRequest, plan rebase.LandPlan, tip, commitID git.ObjectID, committer git.Signature) (git.ObjectID, error) {- if in.GetStrategy() != gitalypb.UserLandRequest_STRATEGY_REBASE_UNSPECIFIED {- return st.server.computeLandedTip(ctx, in, st.repo, tip, commitID, committer)- }-- todo, err := plan.Replay()- switch {- case errors.Is(err, rebase.ErrMergesInRange):- return "", structerr.NewInvalidArgument("commit range contains merge commits, which cannot be rebased: use STRATEGY_MERGE")- case errors.Is(err, rebase.ErrUnrelated):- // Rebasing needs a merge base. Unrelated histories can only be landed with STRATEGY_MERGE.- return "", structerr.NewInvalidArgument("commit shares no history with branch %q: use STRATEGY_MERGE to land unrelated history", string(in.GetBranch()))- case err != nil:- return "", structerr.NewInternal("planning the rebase: %w", err)- }-- // The commits are reapplied as computeLandedTip reapplies cherry-picks: see the comment there.- defer observeLandBatchPhase(landBatchPhaseReplay, time.Now())- rebasedCommitID, err := st.rebaser.Replay(ctx, tip, todo, &committer)- if err != nil {- // The conflict error is passed as the cause itself, see computeLandedTip.- var conflictErr *localrepo.RebaseConflictError- if errors.As(err, &conflictErr) {- return "", landMergeConflictError(tip, commitID, conflictingFileNames(conflictErr.ConflictError), conflictErr)- }-- return "", structerr.NewInternal("rebasing commit: %w", err)- }-- return rebasedCommitID, nil+ return step, nil}// admit evaluates the standalone access policy for the actor of req on the update of reference from@@ -546,32 +334,6 @@ func (st *serverLandBatchStepper) UpdateSolo(ctx context.Context, req *landBatchreturn st.updateKind(st.server.updateLandedReference(ctx, req.in, st.quarantineDir, st.repo, reference, oldOID, newTip, rewritten))}-// publish makes the objects that the attempt wrote durable before its compare-and-swap: it packs-// every loose object of the quarantine into one fsynced pack (see packAttemptObjects), which the-// quarantine migration of the update then moves into the repository. A failure fails the update-// of the attempt, as a failed compare-and-swap that a rebuild does not fix: nothing is published.-func (st *serverLandBatchStepper) publish(ctx context.Context) error {- defer observeLandBatchPhase(landBatchPhasePack, time.Now())-- repoPath, err := st.repo.Path(ctx)- if err != nil {- return structerr.NewInternal("getting quarantine path: %w", err)- }-- objectDir := st.quarantineDir.QuarantinedRepo().GetGitObjectDirectory()- if !filepath.IsAbs(objectDir) {- objectDir = filepath.Join(repoPath, objectDir)- }-- packed, err := packBatchObjects(ctx, st.repo, objectDir)- if err != nil {- return structerr.NewInternal("packing the objects of the batch: %w", err)- }- landBatchPackedObjectsTotal.Add(float64(packed))-- return nil-}-// changelogOperationLand is the operation of an unbatched land in the changelog. The lanes run outside// of the gRPC method of the requests, so the operation is set for the update of a batch of one.const changelogOperationLand = "land"diff --git a/internal/graft/ancestry/ancestry.go b/internal/graft/ancestry/ancestry.goindex 4525f3c..c958899 100644--- a/internal/graft/ancestry/ancestry.go+++ b/internal/graft/ancestry/ancestry.go@@ -1,8 +1,8 @@-// Package ancestry answers the ancestry questions of a landing: whether a commit is reachable from-// another one, what their merge base is, and which commits a change adds to a branch. Git answers-// them by running git merge-base and git rev-list, and the graph checker answers them from the-// stored graph index (see internal/graft/derived/graph). Both answer the same questions with the-// same results, so a landing gives the same outcome with either.+// Package ancestry answers the history question of a landing: which commits a change adds to a+// branch (Checker.Range), from which the land decision follows (rebase.PlanLanding). Git answers it+// by running git rev-list, and the graph checker from the stored graph index (see+// internal/graft/derived/graph). Both return the same commits, so a landing gives the same outcome+// with either.package ancestryimport (@@ -15,7 +15,6 @@ import ("path/filepath""strings"- "github.com/feder-cr/git-ai/internal/command""github.com/feder-cr/git-ai/internal/git""github.com/feder-cr/git-ai/internal/git/catfile""github.com/feder-cr/git-ai/internal/git/gitcmd"@@ -32,16 +31,9 @@ const (graphKindVersion = graph.KindVersion)-// Checker answers the ancestry questions of one landing attempt. A Checker is used by one-// goroutine at a time.+// Checker answers the history question of one landing attempt. A Checker is used by one goroutine+// at a time.type Checker interface {- // IsAncestor reports whether ancestor is reachable from descendant, including when both are- // the same commit.- IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error)- // MergeBase returns the merge base of a and b. When several best common ancestors exist, it- // returns the one with the smallest object ID, so that Git and the graph checker agree. The- // boolean is false when a and b have no common ancestor.- MergeBase(ctx context.Context, a, b git.ObjectID) (git.ObjectID, bool, error)// Range returns the commits reachable from head and not from base, as git rev-list// base..head lists them, each with all of its parents, in no particular order. It is empty// when head is reachable from base.@@ -143,46 +135,11 @@ func requireAbsent(path string) error {}}-// gitChecker answers the questions with git.+// gitChecker answers with git.type gitChecker struct {repo *localrepo.Repo}-func (c gitChecker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {- return c.repo.IsAncestor(ctx, ancestor.Revision(), descendant.Revision())-}--func (c gitChecker) MergeBase(ctx context.Context, a, b git.ObjectID) (git.ObjectID, bool, error) {- var stdout, stderr bytes.Buffer- if err := c.repo.ExecAndWait(ctx,- gitcmd.Command{- Name: "merge-base",- Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}},- Args: []string{a.String(), b.String()},- },- gitcmd.WithStdout(&stdout),- gitcmd.WithStderr(&stderr),- ); err != nil {- // git merge-base exits with status 1 when there is no common ancestor.- if status, ok := command.ExitStatus(err); ok && status == 1 {- return "", false, nil- }- return "", false, fmt.Errorf("merge-base: %w, stderr: %q", err, stderr.String())- }-- var best string- for _, line := range strings.Fields(stdout.String()) {- if best == "" || line < best {- best = line- }- }- if best == "" {- return "", false, nil- }-- return git.ObjectID(best), true, nil-}-// Range lists the range with one git rev-list --parents process.func (c gitChecker) Range(ctx context.Context, base, head git.ObjectID) ([]RangeCommit, error) {objectHash, err := c.repo.ObjectHash(ctx)@@ -224,29 +181,13 @@ func (c gitChecker) Range(ctx context.Context, base, head git.ObjectID) ([]Rangereturn commits, nil}-// graphChecker answers the questions with the graph index.+// graphChecker answers with the graph index.type graphChecker struct {index *graph.Index// source reads the parents of the commits of a range.source graph.Source}-func (c graphChecker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {- return c.index.IsAncestor(ctx, ancestor, descendant)-}--func (c graphChecker) MergeBase(ctx context.Context, a, b git.ObjectID) (git.ObjectID, bool, error) {- base, err := c.index.MergeBase(ctx, a, b)- if err != nil {- return "", false, err- }- if base == "" {- return "", false, nil- }-- return base, true, nil-}-// Range walks down from head through the parents of the commits that the index does not find// reachable from base. The ancestors of a commit reachable from base are all reachable from base, so// the walk stops there without missing a commit of the range.diff --git a/internal/graft/ancestry/ancestry_spec_test.go b/internal/graft/ancestry/ancestry_spec_test.goindex 7bc791a..173205a 100644--- a/internal/graft/ancestry/ancestry_spec_test.go+++ b/internal/graft/ancestry/ancestry_spec_test.go@@ -7,7 +7,9 @@ package ancestry// - gittest.CreateRepository(t, ctx, cfg) returns (*gitalypb.Repository, repoPath).// - gittest.TreeEntry{Mode, Path, Content} is the tree-entry type for WithTreeEntries.// - testhelper.NewLogger(t) returns a log.Logger.-// - MergeBase's bool result means "a merge base was found".+//+// Phase 4b (PLAN8): the checker answers one question, Range; IsAncestor and MergeBase were removed+// with the land decision that used them. These tests check Range with the same fixtures.// - objectReader and derivedStore are placeholders for the catfile reader and the// derived store of the test repo; they fail the test until wired.@@ -15,6 +17,7 @@ import ("context""fmt""math/rand"+ "reflect""testing""github.com/feder-cr/git-ai/internal/git"@@ -89,27 +92,29 @@ func testLogger(t *testing.T) log.Logger {return testhelper.NewLogger(t)}-// assertSameAnswers checks that got and want agree on IsAncestor and MergeBase for (x, y).+// assertSameAnswers checks that got and want list the same range x..y.func assertSameAnswers(t *testing.T, fx *specFixture, want, got Checker, x, y git.ObjectID) {t.Helper()- wAnc, wErr := want.IsAncestor(fx.ctx, x, y)- gAnc, gErr := got.IsAncestor(fx.ctx, x, y)+ wRange, wErr := want.Range(fx.ctx, x, y)+ gRange, gErr := got.Range(fx.ctx, x, y)if wErr != nil || gErr != nil {- t.Fatalf("IsAncestor(%s, %s): want err %v, got err %v", x, y, wErr, gErr)+ t.Fatalf("Range(%s, %s): want err %v, got err %v", x, y, wErr, gErr)}- if wAnc != gAnc {- t.Fatalf("IsAncestor(%s, %s): want %v, got %v", x, y, wAnc, gAnc)+ if !reflect.DeepEqual(rangeSet(wRange), rangeSet(gRange)) {+ t.Fatalf("Range(%s, %s): want %v, got %v", x, y, rangeSet(wRange), rangeSet(gRange))}+}- wBase, wOK, wErr := want.MergeBase(fx.ctx, x, y)- gBase, gOK, gErr := got.MergeBase(fx.ctx, x, y)- if wErr != nil || gErr != nil {- t.Fatalf("MergeBase(%s, %s): want err %v, got err %v", x, y, wErr, gErr)- }- if wOK != gOK || wBase != gBase {- t.Fatalf("MergeBase(%s, %s): want (%s, %v), got (%s, %v)", x, y, wBase, wOK, gBase, gOK)+// mustRange returns the range base..head of chk as a set.+func mustRange(t *testing.T, fx *specFixture, chk Checker, base, head git.ObjectID) map[git.ObjectID][]git.ObjectID {+ t.Helper()++ rng, err := chk.Range(fx.ctx, base, head)+ if err != nil {+ t.Fatalf("Range(%s, %s): %v", base, head, err)}+ return rangeSet(rng)}// buildForkDAG builds root -> {left, right} -> merged plus an unrelated lone root.@@ -123,9 +128,9 @@ func buildForkDAG(t *testing.T, fx *specFixture) (root, left, right, merged, lonreturn}-// TestSpecGitIsAncestorAndMergeBaseLinear: Git(repo) answers IsAncestor and MergeBase-// on a linear history as Git does.-func TestSpecGitIsAncestorAndMergeBaseLinear(t *testing.T) {+// TestSpecGitRangeLinear: on a linear history Git(repo) lists the commits above the base with+// their parents, and nothing when the head is reachable from the base.+func TestSpecGitRangeLinear(t *testing.T) {fx := newSpecFixture(t)chk := Git(fx.repo)@@ -133,63 +138,29 @@ func TestSpecGitIsAncestorAndMergeBaseLinear(t *testing.T) {b := fx.commit(t, 2, a)c := fx.commit(t, 3, b)- cases := []struct {- name string- anc, desc git.ObjectID- wantAncest bool- }{- {"root is ancestor of tip", a, c, true},- {"parent is ancestor of child", b, c, true},- {"tip is not ancestor of root", c, a, false},- }- for _, tc := range cases {- got, err := chk.IsAncestor(fx.ctx, tc.anc, tc.desc)- if err != nil {- t.Fatalf("%s: IsAncestor: %v", tc.name, err)- }- if got != tc.wantAncest {- t.Fatalf("%s: IsAncestor = %v, want %v", tc.name, got, tc.wantAncest)- }+ if got, want := mustRange(t, fx, chk, a, c), map[git.ObjectID][]git.ObjectID{b: {a}, c: {b}}; !reflect.DeepEqual(got, want) {+ t.Fatalf("Range(root, tip) = %v, want %v", got, want)}-- base, found, err := chk.MergeBase(fx.ctx, a, c)- if err != nil {- t.Fatalf("MergeBase: %v", err)+ if got := mustRange(t, fx, chk, c, a); len(got) != 0 {+ t.Fatalf("Range(tip, root) = %v, want empty: the root is reachable from the tip", got)}- if !found || base != a {- t.Fatalf("MergeBase(root, tip) = (%s, %v), want (%s, true)", base, found, a)+ if got := mustRange(t, fx, chk, c, c); len(got) != 0 {+ t.Fatalf("Range(tip, tip) = %v, want empty", got)}}-// TestSpecGitMergeBaseForkAndUnrelated: MergeBase finds the fork point of two branches-// and reports no merge base for unrelated histories, without an error.-func TestSpecGitMergeBaseForkAndUnrelated(t *testing.T) {+// TestSpecGitRangeForkAndUnrelated: the range of a branch onto a sibling stops at their fork+// point, and an unrelated history is listed down to its root, without an error.+func TestSpecGitRangeForkAndUnrelated(t *testing.T) {fx := newSpecFixture(t)chk := Git(fx.repo)root, left, right, _, lone := buildForkDAG(t, fx)- base, found, err := chk.MergeBase(fx.ctx, left, right)- if err != nil {- t.Fatalf("MergeBase(left, right): %v", err)- }- if !found || base != root {- t.Fatalf("MergeBase(left, right) = (%s, %v), want (%s, true)", base, found, root)- }-- _, found, err = chk.MergeBase(fx.ctx, left, lone)- if err != nil {- t.Fatalf("MergeBase(left, lone): unexpected error %v", err)+ if got, want := mustRange(t, fx, chk, left, right), map[git.ObjectID][]git.ObjectID{right: {root}}; !reflect.DeepEqual(got, want) {+ t.Fatalf("Range(left, right) = %v, want %v", got, want)}- if found {- t.Fatalf("MergeBase(left, lone) found a base for unrelated histories")- }-- anc, err := chk.IsAncestor(fx.ctx, root, lone)- if err != nil {- t.Fatalf("IsAncestor(root, lone): %v", err)- }- if anc {- t.Fatalf("IsAncestor(root, lone) = true for unrelated histories")+ if got, want := mustRange(t, fx, chk, left, lone), map[git.ObjectID][]git.ObjectID{lone: nil}; !reflect.DeepEqual(got, want) {+ t.Fatalf("Range(left, lone) = %v, want %v", got, want)}}@@ -281,28 +252,22 @@ func TestSpecNewEnabledFallsBackToGit(t *testing.T) {}}-// TestSpecMergeBaseOfAncestorIsAncestor: when one commit is an ancestor of the other,-// MergeBase returns the ancestor (checked through the Graph checker too).-func TestSpecMergeBaseOfAncestorIsAncestor(t *testing.T) {+// TestSpecRangeOfDescendantWithMerge: the range from an ancestor to a merge lists both sides of+// the merge with all their parents (checked through the Graph checker too).+func TestSpecRangeOfDescendantWithMerge(t *testing.T) {fx := newSpecFixture(t)- root, left, _, merged, _ := buildForkDAG(t, fx)+ root, left, right, merged, _ := buildForkDAG(t, fx)graph := fx.mustGraph(t)for _, chk := range []Checker{Git(fx.repo), graph} {- base, found, err := chk.MergeBase(fx.ctx, root, merged)- if err != nil {- t.Fatalf("MergeBase(root, merged): %v", err)- }- if !found || base != root {- t.Fatalf("MergeBase(root, merged) = (%s, %v), want (%s, true)", base, found, root)+ want := map[git.ObjectID][]git.ObjectID{merged: {left, right}, left: {root}, right: {root}}+ if got := mustRange(t, fx, chk, root, merged); !reflect.DeepEqual(got, want) {+ t.Fatalf("Range(root, merged) = %v, want %v", got, want)}- base, found, err = chk.MergeBase(fx.ctx, left, merged)- if err != nil {- t.Fatalf("MergeBase(left, merged): %v", err)- }- if !found || base != left {- t.Fatalf("MergeBase(left, merged) = (%s, %v), want (%s, true)", base, found, left)+ want = map[git.ObjectID][]git.ObjectID{merged: {left, right}, right: {root}}+ if got := mustRange(t, fx, chk, left, merged); !reflect.DeepEqual(got, want) {+ t.Fatalf("Range(left, merged) = %v, want %v", got, want)}}}diff --git a/internal/graft/ancestry/ancestry_test.go b/internal/graft/ancestry/ancestry_test.goindex c8ae695..6995ac5 100644--- a/internal/graft/ancestry/ancestry_test.go+++ b/internal/graft/ancestry/ancestry_test.go@@ -154,18 +154,11 @@ func TestGraphMatchesGitOnRandomDAG(t *testing.T) {t.Helper()for _, p := range pairs {- wantAncestor, err := gitChecker.IsAncestor(ctx, p.a, p.b)+ want, err := gitChecker.Range(ctx, p.a, p.b)require.NoError(t, err)- gotAncestor, err := checker.IsAncestor(ctx, p.a, p.b)+ got, err := checker.Range(ctx, p.a, p.b)require.NoError(t, err)- require.Equal(t, wantAncestor, gotAncestor, "%s: IsAncestor(%s, %s)", label, p.a, p.b)-- wantBase, wantOK, err := gitChecker.MergeBase(ctx, p.a, p.b)- require.NoError(t, err)- gotBase, gotOK, err := checker.MergeBase(ctx, p.a, p.b)- require.NoError(t, err)- require.Equal(t, wantOK, gotOK, "%s: MergeBase(%s, %s) found", label, p.a, p.b)- require.Equal(t, wantBase, gotBase, "%s: MergeBase(%s, %s)", label, p.a, p.b)+ require.Equal(t, rangeSet(want), rangeSet(got), "%s: Range(%s, %s)", label, p.a, p.b)}}@@ -184,7 +177,7 @@ func TestGraphMatchesGitOnRandomDAG(t *testing.T) {}}-func TestMergeBaseWithoutCommonAncestor(t *testing.T) {+func TestRangeWithoutCommonAncestor(t *testing.T) {t.Parallel()ctx := testhelper.Context(t)@@ -199,14 +192,10 @@ func TestMergeBaseWithoutCommonAncestor(t *testing.T) {for name, checker := range map[string]Checker{"git": Git(repo), "graph": graphChecker} {t.Run(name, func(t *testing.T) {- base, ok, err := checker.MergeBase(ctx, a, b)+ // Nothing of b's history is reachable from a, so the range is b down to its root.+ rng, err := checker.Range(ctx, a, b)require.NoError(t, err)- require.False(t, ok)- require.Empty(t, base)-- ancestor, err := checker.IsAncestor(ctx, a, b)- require.NoError(t, err)- require.False(t, ancestor)+ require.Equal(t, map[git.ObjectID][]git.ObjectID{b: nil}, rangeSet(rng))})}}@@ -259,11 +248,15 @@ func TestNewChoosesCheckerAndFallsBack(t *testing.T) {}// rangeSet returns the range as a map from commit to its parents, which does not depend on the-// order of the listing.+// order of the listing. A root commit maps to nil.func rangeSet(commits []RangeCommit) map[git.ObjectID][]git.ObjectID {set := make(map[git.ObjectID][]git.ObjectID, len(commits))for _, c := range commits {- set[c.ID] = c.Parents+ var parents []git.ObjectID+ if len(c.Parents) > 0 {+ parents = c.Parents+ }+ set[c.ID] = parents}return set}@@ -313,8 +306,7 @@ func TestRangeGraphMatchesGit(t *testing.T) {require.Len(t, want, len(listed))// A range is empty exactly when head is reachable from base.- reachable, err := gitChecker.IsAncestor(ctx, p.head, p.base)- require.NoError(t, err)+ reachable := gittest.NewCommand(t, cfg, "-C", repoPath, "merge-base", "--is-ancestor", p.head.String(), p.base.String()).Run() == nilrequire.Equal(t, reachable, len(want) == 0)if len(want) > 0 {nonEmpty++