Git designed for AI agents.
localrepo: read references with show-ref --verify, and the default branch exactly
Federico Elia7f158483d662
diff --git a/internal/git/localrepo/refs.go b/internal/git/localrepo/refs.goindex d43a6fc..d80f762 100644--- a/internal/git/localrepo/refs.go+++ b/internal/git/localrepo/refs.go@@ -120,15 +120,16 @@ func (repo *Repo) ResolveRevisionWithReader(ctx context.Context, reader catfile.// git resolves a name with its DWIM rules, so a missing refs/heads/x resolves through// refs/refs/heads/x, refs/tags/refs/heads/x or refs/remotes/refs/heads/x when one of them exists.// The exact name is the first name git tries, so a branch that reader cannot resolve does not-// exist exactly either, and costs nothing more. The branches reader resolves are read by their-// exact names with one for-each-ref, whose values reader then peels.+// exist exactly either, and costs nothing more. Each branch reader resolves is read by its exact+// name (ResolveReferenceExactly), and reader then peels its value.func (repo *Repo) ResolveBranchesExactly(ctx context.Context, reader catfile.ObjectInfoReader, branches ...git.ReferenceName) (map[git.ReferenceName]git.ObjectID, error) {- wanted := make(map[git.ReferenceName]bool, len(branches))- var patterns []string+ resolved := make(map[git.ReferenceName]git.ObjectID, len(branches))+ seen := make(map[git.ReferenceName]bool, len(branches))for _, branch := range branches {- if wanted[branch] {+ if seen[branch] {continue}+ seen[branch] = trueif _, err := repo.ResolveRevisionWithReader(ctx, reader, branch.Revision()+"^{commit}"); err != nil {if errors.Is(err, git.ErrReferenceNotFound) {@@ -136,61 +137,63 @@ func (repo *Repo) ResolveBranchesExactly(ctx context.Context, reader catfile.Obj}return nil, fmt.Errorf("resolving %q: %w", branch, err)}- wanted[branch] = true- patterns = append(patterns, branch.String())- }-- resolved := make(map[git.ReferenceName]git.ObjectID, len(patterns))- if len(patterns) == 0 {- return resolved, nil- }- refs, err := gitcmd.GetReferences(ctx, repo, gitcmd.GetReferencesConfig{Patterns: patterns})- if err != nil {- return nil, fmt.Errorf("reading branches: %w", err)- }- for _, ref := range refs {- // A pattern also matches the references below it, so only exact names count.- if !wanted[ref.Name] {- continue+ value, err := repo.ResolveReferenceExactly(ctx, branch)+ if err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ continue+ }+ return nil, err}- // The value of the reference itself, as for-each-ref read it, peeled to a commit as- // `<branch>^{commit}` is.- commit, err := repo.ResolveRevisionWithReader(ctx, reader, git.Revision(ref.Target)+"^{commit}")+ // The value of the reference itself, peeled to a commit as `<branch>^{commit}` is.+ commit, err := repo.ResolveRevisionWithReader(ctx, reader, value.Revision()+"^{commit}")if err != nil {if errors.Is(err, git.ErrReferenceNotFound) {continue}- return nil, fmt.Errorf("peeling %q: %w", ref.Name, err)+ return nil, fmt.Errorf("peeling %q: %w", branch, err)}- resolved[ref.Name] = commit+ resolved[branch] = commit}return resolved, nil}// ResolveReferenceExactly returns the object ID that reference points to, read by its exact name-// with one for-each-ref, or git.ErrReferenceNotFound when it does not exist (also when references-// below its name do, refs/heads/<x>/y). Unlike ResolveRevision, whose revision git resolves with-// its DWIM rules, a missing refs/heads/<x> never resolves through refs/tags/refs/heads/<x> or-// another reference. The value is not peeled: it is the value that a compare-and-swap of the-// reference expects, as `git rev-parse <reference>` returns it when the reference exists. A-// symbolic reference yields the object its target points to. ResolveBranchesExactly is the peeled-// form, for the callers that want the commit.+// with `git show-ref --verify`, or git.ErrReferenceNotFound when it does not exist (also when+// references below its name do, refs/heads/<x>/y, and when the name is not a valid reference name,+// such as one with glob characters or a revision like main~1). Unlike ResolveRevision, whose+// revision git resolves with its DWIM rules, a missing refs/heads/<x> never resolves through+// refs/tags/refs/heads/<x> or another reference. The value is not peeled: it is the value that a+// compare-and-swap of the reference expects, as `git rev-parse <reference>` returns it when the+// reference exists. A symbolic reference yields the object its target points to.+// ResolveBranchesExactly is the peeled form, for the callers that want the commit.+//+// show-ref --verify reads the one reference, where for-each-ref <reference> lists every loose+// reference of its directory (50,000 loose branches: 250 ms against 3 ms).func (repo *Repo) ResolveReferenceExactly(ctx context.Context, reference git.ReferenceName) (git.ObjectID, error) {- // A pattern also matches the references below it, so only the exact name counts.- refs, err := repo.GetReferences(ctx, reference.String())- if err != nil {- return "", fmt.Errorf("reading %q: %w", reference, err)- }- for _, ref := range refs {- if ref.Name == reference && ref.Target != "" {- return git.ObjectID(ref.Target), nil+ var stdout, stderr bytes.Buffer+ if err := repo.ExecAndWait(ctx, gitcmd.Command{+ Name: "show-ref",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "--verify"}},+ Args: []string{reference.String()},+ }, gitcmd.WithStdout(&stdout), gitcmd.WithStderr(&stderr)); err != nil {+ // show-ref --verify exits 128 with "not a valid ref" for a reference that does not+ // exist or whose name is not valid; anything else is a failure.+ if code, ok := command.ExitStatus(err); ok && code == 128 && strings.Contains(stderr.String(), "not a valid ref") {+ return "", git.ErrReferenceNotFound}+ return "", fmt.Errorf("show-ref %q: %w, stderr: %q", reference, err, stderr.String())+ }++ // The line is "<object ID> <reference>".+ value, name, ok := strings.Cut(strings.TrimSuffix(stdout.String(), "\n"), " ")+ if !ok || name != reference.String() {+ return "", fmt.Errorf("show-ref %q: unexpected output %q", reference, stdout.String())}- return "", git.ErrReferenceNotFound+ return git.ObjectID(value), nil}// GetReference looks up and returns the given reference. Returns a@@ -422,17 +425,21 @@ func (repo *Repo) GetDefaultBranch(ctx context.Context) (git.ReferenceName, erro}// Ideally we would only use HEAD to determine the default branch, but- // gitlab-rails depends on the branch being determined like this.+ // gitlab-rails depends on the branch being determined like this. Each+ // candidate is read by its exact name: with its DWIM rules, git would take a+ // missing refs/heads/main for one of refs/refs/heads/main,+ // refs/tags/refs/heads/main, refs/heads/refs/heads/main or+ // refs/remotes/refs/heads/main.for _, headCandidate := range []git.ReferenceName{headReference, git.DefaultRef, git.LegacyDefaultRef,} {- has, err := repo.HasRevision(ctx, headCandidate.Revision())- if err != nil {+ if _, err := repo.ResolveReferenceExactly(ctx, headCandidate); err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ continue+ }return "", err}- if has {- return headCandidate, nil- }+ return headCandidate, nil}// If all else fails, return the first branch namediff --git a/internal/git/localrepo/refs_test.go b/internal/git/localrepo/refs_test.goindex 993fd00..c02599b 100644--- a/internal/git/localrepo/refs_test.go+++ b/internal/git/localrepo/refs_test.go@@ -212,6 +212,9 @@ func TestRepo_ResolveReferenceExactly(t *testing.T) {}{{reference: "refs/heads/main", expected: commit},{reference: "refs/heads/x", expectedErr: git.ErrReferenceNotFound},+ // Not reference names: never a pattern, never a revision.+ {reference: "refs/heads/x*", expectedErr: git.ErrReferenceNotFound},+ {reference: "refs/heads/main~1", expectedErr: git.ErrReferenceNotFound},{reference: "refs/keep/at-tag", expected: tag},{reference: "refs/heads/symbolic", expected: commit},} {@@ -223,6 +226,33 @@ func TestRepo_ResolveReferenceExactly(t *testing.T) {}}+// TestRepo_GetDefaultBranchReadsCandidatesExactly checks that the candidates of the default branch+// (HEAD's branch, main, master) are read by their exact names: a missing refs/heads/main is not the+// default branch because git's DWIM rules would resolve it through a decoy, and the default branch+// is then master.+func TestRepo_GetDefaultBranchReadsCandidatesExactly(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)++ for _, decoy := range []git.ReferenceName{+ "refs/refs/heads/main", "refs/tags/refs/heads/main", "refs/heads/refs/heads/main", "refs/remotes/refs/heads/main",+ } {+ t.Run(decoy.String(), func(t *testing.T) {+ t.Parallel()++ cfg, repo, repoPath := setupRepo(t, withDisabledHooks())+ gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/main")+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))+ gittest.WriteRef(t, cfg, repoPath, decoy, gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("decoy")))++ defaultBranch, err := repo.GetDefaultBranch(ctx)+ require.NoError(t, err)+ require.Equal(t, git.LegacyDefaultRef, defaultBranch)+ })+ }+}+func TestRepo_GetReferenceWithAmbiguousRefs(t *testing.T) {t.Parallel()diff --git a/internal/gitaly/service/operations/apply_patch.go b/internal/gitaly/service/operations/apply_patch.goindex 4b6f256..006b041 100644--- a/internal/gitaly/service/operations/apply_patch.go+++ b/internal/gitaly/service/operations/apply_patch.go@@ -82,6 +82,10 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyPif err != nil {return fmt.Errorf("default branch name: %w", err)}+ // A repository without a branch has no default branch.+ if defaultBranch == "" {+ return errNoDefaultBranch+ }parentCommitID, err = s.resolveBranchCommitExactly(ctx, repo, defaultBranch)if errors.Is(err, git.ErrReferenceNotFound) {diff --git a/internal/gitaly/service/operations/branch_exact_test.go b/internal/gitaly/service/operations/branch_exact_test.goindex 3462d90..3ece4e4 100644--- a/internal/gitaly/service/operations/branch_exact_test.go+++ b/internal/gitaly/service/operations/branch_exact_test.go@@ -115,6 +115,44 @@ func TestUserFFBranchResolvesBranchesExactly(t *testing.T) {requireDecoyUntouched(t, cfg, repoPath, decoy, decoyCommit)})}++ t.Run("a symbolic branch", func(t *testing.T) {+ t.Parallel()++ // A symbolic branch reads as the commit of its target, and the update moves the target.+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch))+ gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "refs/heads/sym", git.DefaultRef.String())+ commit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base))++ _, err := client.UserFFBranch(ctx, &gitalypb.UserFFBranchRequest{+ Repository: repo,+ User: gittest.TestUser,+ CommitId: commit.String(),+ Branch: []byte("sym"),+ })+ require.NoError(t, err)+ require.Equal(t, commit.String(), exactReferenceValue(t, cfg, repoPath, git.DefaultRef))+ })++ t.Run("glob characters in the name", func(t *testing.T) {+ t.Parallel()++ // x* is not a branch name, and never a pattern that matches xa.+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ xa := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("xa"))+ commit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(xa))++ _, err := client.UserFFBranch(ctx, &gitalypb.UserFFBranchRequest{+ Repository: repo,+ User: gittest.TestUser,+ CommitId: commit.String(),+ Branch: []byte("x*"),+ })+ testhelper.RequireGrpcCode(t, err, codes.InvalidArgument)+ require.ErrorContains(t, err, "reference not found")+ require.Equal(t, xa.String(), exactReferenceValue(t, cfg, repoPath, "refs/heads/xa"))+ })}// TestUserMergeBranchResolvesBranchesExactly checks that UserMergeBranch into a missing branch is@@ -175,6 +213,23 @@ func TestUserDeleteBranchResolvesBranchesExactly(t *testing.T) {requireDecoyUntouched(t, cfg, repoPath, decoy, decoyCommit)})}++ t.Run("glob characters in the name", func(t *testing.T) {+ t.Parallel()++ // x* is not a branch name, and never a pattern that matches xa.+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ xa := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("xa"))++ _, err := client.UserDeleteBranch(ctx, &gitalypb.UserDeleteBranchRequest{+ Repository: repo,+ User: gittest.TestUser,+ BranchName: []byte("x*"),+ })+ testhelper.RequireGrpcCode(t, err, codes.FailedPrecondition)+ require.ErrorContains(t, err, "branch not found")+ require.Equal(t, xa.String(), exactReferenceValue(t, cfg, repoPath, "refs/heads/xa"))+ })}// cherryPickOrRevertSetup is a repository whose branch main is at base, with commit, a commit on@@ -312,6 +367,28 @@ func TestUserUpdateSubmoduleResolvesBranchesExactly(t *testing.T) {require.ErrorContains(t, err, "Cannot find branch")requireDecoyUntouched(t, cfg, repoPath, decoy, decoyCommit)})++ t.Run("missing branch with an expected old object ID, "+decoy.String(), func(t *testing.T) {+ t.Parallel()++ // The expected old object ID passes its own check; the branch, read for the tree and the+ // parent, is still missing, whatever decoy git's DWIM rules would take for it.+ repo, repoPath, branchCommit, newSubCommit, submodule := setup(t)+ decoyCommit := writeDecoy(t, cfg, repoPath, decoy, submodule)++ _, err := client.UserUpdateSubmodule(ctx, &gitalypb.UserUpdateSubmoduleRequest{+ Repository: repo,+ User: gittest.TestUser,+ CommitSha: newSubCommit.String(),+ Branch: []byte("x"),+ Submodule: []byte("sub"),+ CommitMessage: []byte("update sub on x"),+ ExpectedOldOid: branchCommit.String(),+ })+ testhelper.RequireGrpcCode(t, err, codes.InvalidArgument)+ require.ErrorContains(t, err, legacyErrPrefixInvalidSubmodulePath)+ requireDecoyUntouched(t, cfg, repoPath, decoy, decoyCommit)+ })}t.Run("branch with a tag of its name", func(t *testing.T) {@@ -407,20 +484,41 @@ func TestUserApplyPatchResolvesBranchesExactly(t *testing.T) {require.Equal(t, decoyCommit.String(), exactReferenceValue(t, cfg, repoPath, decoy), "the decoy is untouched")})- t.Run("missing default branch, "+strings.Replace(decoy.String(), "/x", "/"+git.DefaultBranch, 1), func(t *testing.T) {+ decoyRef := git.ReferenceName(strings.Replace(decoy.String(), "/x", "/"+git.DefaultBranch, 1))+ t.Run("missing default branch, "+decoyRef.String(), func(t *testing.T) {t.Parallel()repo, repoPath := gittest.CreateRepository(t, ctx, cfg)- // The repository has no branch, so its default branch is HEAD's, refs/heads/main, which- // does not exist; the decoy is named for it.- decoyRef := git.ReferenceName(strings.Replace(decoy.String(), "/x", "/"+git.DefaultBranch, 1))+ // HEAD is refs/heads/main, which does not exist; the decoy is named for it.decoyCommit := writeDecoy(t, cfg, repoPath, decoyRef, file)- _, err := applyExactBranchPatch(t, ctx, client, repo, "x", decoyCommit)+ resp, err := applyExactBranchPatch(t, ctx, client, repo, "x", decoyCommit)+ if strings.HasPrefix(decoyRef.String(), "refs/heads/") {+ // refs/heads/refs/heads/main is a branch, the repository's only one: the default+ // branch falls back to it, as to the first branch of any repository.+ require.NoError(t, err)+ require.Equal(t, decoyCommit.String(), parentsOf(t, cfg, repoPath, resp.GetBranchUpdate().GetCommitId()))+ return+ }require.ErrorContains(t, err, errNoDefaultBranch.Error())require.Equal(t, decoyCommit.String(), exactReferenceValue(t, cfg, repoPath, decoyRef), "the decoy is untouched")require.Empty(t, exactReferenceValue(t, cfg, repoPath, "refs/heads/x"), "no branch x")})++ t.Run("default branch falls back to master, "+decoyRef.String(), func(t *testing.T) {+ t.Parallel()++ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ // HEAD is refs/heads/main, which does not exist, and master is the branch of the+ // repository: the default branch is master, whatever decoy is named for main.+ master := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"), gittest.WithTreeEntries(file))+ writeDecoy(t, cfg, repoPath, decoyRef, file)++ resp, err := applyExactBranchPatch(t, ctx, client, repo, "x", master)+ require.NoError(t, err)+ require.True(t, resp.GetBranchUpdate().GetBranchCreated())+ require.Equal(t, master.String(), parentsOf(t, cfg, repoPath, resp.GetBranchUpdate().GetCommitId()), "x starts from master")+ })}}diff --git a/internal/gitaly/service/operations/submodules.go b/internal/gitaly/service/operations/submodules.goindex cc2f0bc..ebbd7f2 100644--- a/internal/gitaly/service/operations/submodules.go+++ b/internal/gitaly/service/operations/submodules.go@@ -51,6 +51,9 @@ func (s *Server) UserUpdateSubmodule(ctx context.Context, req *gitalypb.UserUpdareferenceName := git.NewReferenceNameFromBranchName(string(req.GetBranch()))var oldOID git.ObjectID+ // branchCommit is the branch as read for the compare-and-swap, which the new commit then builds+ // on; empty when the request gives the expected old object ID instead.+ var branchCommit git.ObjectIDif expectedOldOID := req.GetExpectedOldOid(); expectedOldOID != "" {objectHash, err := quarantineRepo.ObjectHash(ctx)if err != nil {@@ -77,9 +80,10 @@ func (s *Server) UserUpdateSubmodule(ctx context.Context, req *gitalypb.UserUpda}return nil, structerr.NewInternal("resolving revision: %w", err)}+ branchCommit = oldOID}- commitID, err := s.updateSubmodule(ctx, quarantineRepo, req)+ commitID, err := s.updateSubmodule(ctx, quarantineRepo, req, branchCommit)if err != nil {errStr := strings.TrimSpace(err.Error())@@ -202,19 +206,27 @@ func (l *legacyGit2GoSubmoduleAlreadyAtShaError) Error() string {return fmt.Sprintf("The submodule %s is already at %s", l.submodulePath, l.commitSha)}-func (s *Server) updateSubmodule(ctx context.Context, quarantineRepo *localrepo.Repo, req *gitalypb.UserUpdateSubmoduleRequest) (string, error) {+// updateSubmodule writes the commit that updates the submodule on top of the branch. branchCommit is+// the branch as the caller read it for its compare-and-swap, so that the commit builds on the very+// value the update then expects; when it is empty (the request gives the expected old object ID),+// the branch is read here.+func (s *Server) updateSubmodule(ctx context.Context, quarantineRepo *localrepo.Repo, req *gitalypb.UserUpdateSubmoduleRequest, branchCommit git.ObjectID) (string, error) {var treeID git.ObjectID- // The tree and the parent of the new commit are those of the branch, read once by its exact- // name: git's DWIM rules would resolve the branch name to a tag of the same name before the- // branch, and a missing branch through a tag or another reference.- currentBranchCommit, err := quarantineRepo.ResolveReferenceExactly(ctx, git.NewReferenceNameFromBranchName(string(req.GetBranch())))- if err != nil {- if errors.Is(err, git.ErrReferenceNotFound) {- return "", fmt.Errorf("submodule: %s", legacyErrPrefixInvalidSubmodulePath)- }+ // The tree and the parent of the new commit are those of the branch, read by its exact name:+ // git's DWIM rules would resolve the branch name to a tag of the same name before the branch,+ // and a missing branch through a tag or another reference.+ currentBranchCommit := branchCommit+ if currentBranchCommit == "" {+ var err error+ currentBranchCommit, err = quarantineRepo.ResolveReferenceExactly(ctx, git.NewReferenceNameFromBranchName(string(req.GetBranch())))+ if err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ return "", fmt.Errorf("submodule: %s", legacyErrPrefixInvalidSubmodulePath)+ }- return "", fmt.Errorf("resolving submodule branch: %w", err)+ return "", fmt.Errorf("resolving submodule branch: %w", err)+ }}fullTree, err := quarantineRepo.ReadTree(ctx, currentBranchCommit.Revision(), localrepo.WithRecursive())