Git designed for AI agents.
operations: a full SHA start point is the object, and revisions keep their suffix
Federico Eliac604e0d5d232
diff --git a/internal/gitaly/service/operations/branch_exact_test.go b/internal/gitaly/service/operations/branch_exact_test.goindex 3ece4e4..535da3d 100644--- a/internal/gitaly/service/operations/branch_exact_test.go+++ b/internal/gitaly/service/operations/branch_exact_test.go@@ -565,6 +565,48 @@ func TestStartBranchResolvesTheBranchFirst(t *testing.T) {require.Equal(t, setup.base.String(), parentsOf(t, cfg, setup.repoPath, resp.GetBranchUpdate().GetCommitId()))})+ t.Run("cherry-pick, a SHA as the start with a branch named like the SHA", func(t *testing.T) {+ t.Parallel()++ // git takes a full object ID for the object, not for a reference named like it.+ setup := setupCherryPickOrRevert(t, ctx, cfg)+ writeDecoy(t, cfg, setup.repoPath, git.ReferenceName("refs/heads/"+setup.base.String()),+ gittest.TreeEntry{Path: "base", Mode: "100644", Content: "base\n"})++ resp, err := cherryPick(setup, setup.base.String())+ require.NoError(t, err)+ require.Equal(t, setup.base.String(), parentsOf(t, cfg, setup.repoPath, resp.GetBranchUpdate().GetCommitId()),+ "the SHA, not the branch named like it, is the parent")+ })++ // revisionSetup moves main to the commit of the setup, so that main~1 is base, and tags base as+ // the annotated tag v1.+ revisionSetup := func(t *testing.T) cherryPickOrRevertSetup {+ setup := setupCherryPickOrRevert(t, ctx, cfg)+ gittest.WriteRef(t, cfg, setup.repoPath, git.DefaultRef, git.ObjectID(setup.commit.GetId()))+ gittest.WriteTag(t, cfg, setup.repoPath, "v1", setup.base.Revision(), gittest.WriteTagConfig{Message: "v1"})+ return setup+ }+ for _, start := range []string{+ "refs/heads/" + git.DefaultBranch + "~1",+ git.DefaultBranch + "~1",+ "refs/tags/v1",+ "refs/tags/v1^{}",+ "v1^{}",+ } {+ t.Run("cherry-pick, a revision as the start, "+start, func(t *testing.T) {+ t.Parallel()++ setup := revisionSetup(t)+ // A tag named like the branch: main~1 is the branch's parent, not the tag's.+ writeDecoy(t, cfg, setup.repoPath, git.ReferenceName("refs/tags/"+git.DefaultBranch))++ resp, err := cherryPick(setup, start)+ require.NoError(t, err)+ require.Equal(t, setup.base.String(), parentsOf(t, cfg, setup.repoPath, resp.GetBranchUpdate().GetCommitId()))+ })+ }+t.Run("cherry-pick, a tag without a branch of its name as the start", func(t *testing.T) {t.Parallel()@@ -637,4 +679,47 @@ func TestStartBranchResolvesTheBranchFirst(t *testing.T) {require.Equal(t, setup.remoteCommit.String(), parentsOf(t, cfg, setup.localRepoPath, first.GetRebaseSha()),"the rebase is onto the remote branch, not onto the tag of its name")})++ t.Run("rebase onto a remote SHA with a branch named like the SHA", func(t *testing.T) {+ t.Parallel()++ setup := setupRebasableRepositories(t, ctx, cfg, true)+ writeDecoy(t, cfg, setup.remoteRepoPath, git.ReferenceName("refs/heads/"+setup.remoteCommit.String()))++ stream, err := client.UserRebaseConfirmable(ctx)+ require.NoError(t, err)+ require.NoError(t, stream.Send(buildUserRebaseConfirmableHeaderRequest(setup.localRepo, gittest.TestUser, "1",+ setup.localBranch, setup.localCommit, setup.remoteRepo, setup.remoteCommit.String())))+ first, err := stream.Recv()+ require.NoError(t, err)+ require.NoError(t, stream.Send(buildUserRebaseConfirmableApplyRequest(true)))+ second, err := stream.Recv()+ require.NoError(t, err)+ require.True(t, second.GetRebaseApplied())+ _, err = stream.Recv()+ require.Equal(t, io.EOF, err)++ require.Equal(t, setup.remoteCommit.String(), parentsOf(t, cfg, setup.localRepoPath, first.GetRebaseSha()),+ "the rebase is onto the SHA, not onto the branch named like it")+ })+}++// TestSplitRevisionSuffix checks the split of a start revision into its name and its suffix.+func TestSplitRevisionSuffix(t *testing.T) {+ t.Parallel()++ for revision, expected := range map[string][2]string{+ "main": {"main", ""},+ "refs/heads/main": {"refs/heads/main", ""},+ "main~1": {"main", "~1"},+ "refs/heads/main^2~3": {"refs/heads/main", "^2~3"},+ "refs/tags/v1^{}": {"refs/tags/v1", "^{}"},+ "main@{1}": {"main", "@{1}"},+ "main:path/file": {"main", ":path/file"},+ "feature/x~1": {"feature/x", "~1"},+ "": {"", ""},+ } {+ base, suffix := splitRevisionSuffix(revision)+ require.Equal(t, expected, [2]string{base, suffix}, revision)+ }}diff --git a/internal/gitaly/service/operations/revert.go b/internal/gitaly/service/operations/revert.goindex 737f96f..1e30d59 100644--- a/internal/gitaly/service/operations/revert.go+++ b/internal/gitaly/service/operations/revert.go@@ -206,31 +206,64 @@ func (s *Server) UserRevert(ctx context.Context, req *gitalypb.UserRevertRequest}, nil}-// resolveStartBranch resolves the start branch name of a request to its commit. exact reads a-// reference by its exact name, peeled to its commit, and resolve is git's resolution of a revision,-// both in the repository the start branch is read from.+// resolveStartBranch resolves the start branch name of a request, a revision, to its commit.+// objectHash is the object format of the repository the start branch is read from; exact reads a+// reference there by its exact name, peeled to its commit, and resolve is git's resolution of a+// revision there.//// git resolves a bare name x with its DWIM rules, to refs/tags/x before refs/heads/x, so a tag named-// like the start branch would answer for the branch. A name x is therefore read as the branch-// refs/heads/x first. When there is no such branch, the name is resolved as before this rule, by-// design: callers pass a SHA or a tag as a start point, and a missing branch keeps that DWIM-// resolution for compatibility. A full reference name (refs/...) is read exactly as given, with no-// fallback: a missing refs/heads/x does not resolve through refs/refs/heads/x or another decoy.+// like the start branch would answer for the branch. The revision is split into the name it starts+// with and its suffix (~1, ^{}, @{1}, :path), and the name is resolved by these rules:+// - a full object ID is the object, as git takes it, even when a reference is named like it;+// - a full reference name (refs/...) is read exactly as given, with no fallback: a missing+// refs/heads/x does not resolve through refs/refs/heads/x or another decoy;+// - any other name x is read as the branch refs/heads/x first. When there is no such branch, the+// revision is resolved as before this rule, by design: callers pass a SHA or a tag as a start+// point, and a missing branch keeps that DWIM resolution for compatibility.+//+// The suffix then applies to the reference that was read, by its full name: that is the first name+// git's DWIM rules try, so a reference that exists is read exactly.func resolveStartBranch(name string,+ objectHash git.ObjectHash,exact func(git.ReferenceName) (git.ObjectID, error),resolve func(git.Revision) (git.ObjectID, error),) (git.ObjectID, error) {- if strings.HasPrefix(name, "refs/") {- return exact(git.ReferenceName(name))+ base, suffix := splitRevisionSuffix(name)+ if objectHash.ValidateHex(base) == nil {+ return resolve(git.Revision(name + "^{commit}"))}- commit, err := exact(git.NewReferenceNameFromBranchName(name))- if !errors.Is(err, git.ErrReferenceNotFound) {- return commit, err+ reference := git.NewReferenceNameFromBranchName(base)+ if strings.HasPrefix(base, "refs/") {+ reference = git.ReferenceName(base)}- return resolve(git.Revision(name + "^{commit}"))+ commit, err := exact(reference)+ switch {+ case err == nil:+ if suffix == "" {+ return commit, nil+ }+ return resolve(git.Revision(reference.String() + suffix + "^{commit}"))+ case !errors.Is(err, git.ErrReferenceNotFound) || strings.HasPrefix(base, "refs/"):+ return "", err+ default:+ return resolve(git.Revision(name + "^{commit}"))+ }+}++// splitRevisionSuffix splits a revision into the name it starts with and the suffix that applies to+// it (~<n>, ^<n>, ^{<type>}, @{<n>}, :<path>). A reference name never holds ~, ^, : or @{ (git+// check-ref-format), so the name ends at the first of them.+func splitRevisionSuffix(revision string) (string, string) {+ end := len(revision)+ for _, marker := range []string{"~", "^", ":", "@{"} {+ if i := strings.Index(revision, marker); i >= 0 && i < end {+ end = i+ }+ }+ return revision[:end], revision[end:]}type requestFetchingStartRevision interface {@@ -250,6 +283,7 @@ func (s *Server) fetchStartRevision(}// The start branch is read in the start repository, or in the local one without it.+ objectHashOf := localRepo.ObjectHashexact := func(name git.ReferenceName) (git.ObjectID, error) {return s.resolveBranchCommitExactly(ctx, localRepo, name)}@@ -261,6 +295,7 @@ func (s *Server) fetchStartRevision(if err != nil {return "", structerr.NewInternal("%w", err)}+ objectHashOf = remoteRepo.ObjectHashexact = func(name git.ReferenceName) (git.ObjectID, error) {return remoteRepo.ResolveReference(ctx, name)}@@ -268,8 +303,12 @@ func (s *Server) fetchStartRevision(return remoteRepo.ResolveRevision(ctx, revision)}}+ objectHash, err := objectHashOf(ctx)+ if err != nil {+ return "", structerr.NewInternal("detecting object hash: %w", err)+ }- startRevision, err := resolveStartBranch(string(startBranchName), exact, resolve)+ startRevision, err := resolveStartBranch(string(startBranchName), objectHash, exact, resolve)if err != nil {return "", structerr.NewInvalidArgument("resolve start ref: %w", err)}