Skip to content

Git designed for AI agents.

operations: resolve the start repository's branch by its exact name

Federico Elia7153d0416f21
diff --git a/doc/graft/PLAN9.md b/doc/graft/PLAN9.md
index 92c8126..fa11476 100644
--- a/doc/graft/PLAN9.md
+++ b/doc/graft/PLAN9.md
@@ -730,8 +730,9 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):
exactly by one `for-each-ref`, whose values the reader peels to commits. So
exactly one `for-each-ref` runs per request: the exact read, or, when no branch
resolved, `HasBranches` (B, L). `RepoCreated` is false when the target or the
- start branch exists exactly. A start branch in another repository is still resolved
- there by `FindCommit`, unchanged.
+ start branch exists exactly. A branch resolved in another repository (the request's
+ start repository) is resolved there by its exact name too, with `FindBranch`
+ (`remoterepo.ResolveBranch`) instead of `FindCommit` on `refs/heads/<x>^{commit}`.
4. **Unchanged:** E, one `hash-object --path` per blob: the attributes of the path decide
the bytes of the blob, and a batched writer cannot apply per-path attributes without a
`check-attr` process, which costs as much as it saves for one or two files. K, one
@@ -769,8 +770,10 @@ resolution of item 3.)
it required every sequence to write, the same class in `chmod`: a chmod of a directory
gave a tree entry a file's mode (a tree git refuses), and a chmod of a submodule turned
the gitlink into a file. `chmod` now applies to files only and answers "A file with this
- name doesn't exist" otherwise, as `update` does. A chmod of a symlink still turns it
- into a regular file, as before; that is unchanged.
+ name doesn't exist" otherwise, as `update` does.
+* **Known, pre-existing, kept on purpose:** an executable chmod of a symlink (mode
+ `120000`) turns it into a regular executable file (`100755`) with the link's target as
+ its content. It was so before WP96 and is unchanged; the PM chose to keep it (2026-10-19).
* **The object reader goes back to the catfile cache** at the end of the request, on every
path (a deferred release), as today's `ReadTree` reader does. Its cache key holds the
quarantine directory, so no other request reuses it, and the cache's TTL ends it.
@@ -789,7 +792,9 @@ resolution of item 3.)
absent, in a repository with branches and in one without, a target branch `x` gets an
orphan commit on a new branch, and a start branch `x` is not found, or, without
branches, gives the orphan commit of the issue-3294 workaround. The decoy's commit is
- never the parent.
+ never the parent. `TestUserCommitFilesResolvesStartRepositoryBranchesExactly` does the
+ same with the decoys in the start repository, and `TestRepository_ResolveBranch` for
+ `remoterepo`.
* `TestApplyActionLazyTreeMatchesRecursiveTree` applies seeded random sequences of the six
actions to the whole tree with today's writer and to the lazy tree with the tree writer.
It asserts the same error for every action, the same tree ID for every sequence (no
diff --git a/internal/git/remoterepo/repository.go b/internal/git/remoterepo/repository.go
index 47e87bc..b30aaf7 100644
--- a/internal/git/remoterepo/repository.go
+++ b/internal/git/remoterepo/repository.go
@@ -90,6 +90,37 @@ func (rr *Repo) ResolveRevision(ctx context.Context, revision git.Revision) (git
return oid, nil
}
+// ResolveBranch will dial to the remote repository and resolve the branch named branch (without
+// refs/heads/) by its exact reference name to the commit it points to. Unlike ResolveRevision,
+// whose revision git resolves with its DWIM rules, a missing branch never resolves through
+// refs/tags/refs/heads/<branch> or another reference: it returns git.ErrReferenceNotFound.
+func (rr *Repo) ResolveBranch(ctx context.Context, branch string) (git.ObjectID, error) {
+ resp, err := gitalypb.NewRefServiceClient(rr.conn).FindBranch(ctx, &gitalypb.FindBranchRequest{
+ Repository: rr.Repository,
+ Name: []byte(branch),
+ })
+ if err != nil {
+ return "", fmt.Errorf("find branch: %w", err)
+ }
+
+ oidHex := resp.GetBranch().GetTargetCommit().GetId()
+ if oidHex == "" {
+ return "", git.ErrReferenceNotFound
+ }
+
+ objectHash, err := rr.ObjectHash(ctx)
+ if err != nil {
+ return "", fmt.Errorf("detecting object hash: %w", err)
+ }
+
+ oid, err := objectHash.FromHex(oidHex)
+ if err != nil {
+ return "", fmt.Errorf("parsing object ID: %w", err)
+ }
+
+ return oid, nil
+}
+
// HasBranches will dial to the remote repository and check whether the repository has any branches.
func (rr *Repo) HasBranches(ctx context.Context) (bool, error) {
resp, err := gitalypb.NewRepositoryServiceClient(rr.conn).HasLocalBranches(
diff --git a/internal/git/remoterepo/repository_test.go b/internal/git/remoterepo/repository_test.go
index 0d7c5a4..f981221 100644
--- a/internal/git/remoterepo/repository_test.go
+++ b/internal/git/remoterepo/repository_test.go
@@ -66,6 +66,44 @@ func TestRepository(t *testing.T) {
)
}
+// TestRepository_ResolveBranch checks that a branch resolves by its exact name only: with
+// refs/heads/x missing, the references git's DWIM rules would try for it do not answer.
+func TestRepository_ResolveBranch(t *testing.T) {
+ t.Parallel()
+
+ cfg := setupGitalyServer(t)
+
+ ctx := testhelper.Context(t)
+ ctx, err := storage.InjectGitalyServers(ctx, "default", cfg.SocketPath, cfg.Auth.Token)
+ require.NoError(t, err)
+ ctx = metadata.OutgoingToIncoming(ctx)
+
+ pool := client.NewPool()
+ defer testhelper.MustClose(t, pool)
+
+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ branchCommit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))
+ decoyCommit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("decoy"))
+ for _, decoy := range []git.ReferenceName{"refs/refs/heads/x", "refs/tags/refs/heads/x", "refs/remotes/refs/heads/x"} {
+ gittest.WriteRef(t, cfg, repoPath, decoy, decoyCommit)
+ }
+
+ repo, err := remoterepo.New(ctx, repoProto, pool)
+ require.NoError(t, err)
+
+ oid, err := repo.ResolveBranch(ctx, "main")
+ require.NoError(t, err)
+ require.Equal(t, branchCommit, oid)
+
+ _, err = repo.ResolveBranch(ctx, "x")
+ require.Equal(t, git.ErrReferenceNotFound, err)
+
+ // The DWIM resolution, for contrast, answers with the decoy.
+ oid, err = repo.ResolveRevision(ctx, "refs/heads/x^{commit}")
+ require.NoError(t, err)
+ require.Equal(t, decoyCommit, oid)
+}
+
func TestRepository_ObjectHash(t *testing.T) {
t.Parallel()
diff --git a/internal/gitaly/service/operations/commit_files.go b/internal/gitaly/service/operations/commit_files.go
index 64298fc..1e43401 100644
--- a/internal/gitaly/service/operations/commit_files.go
+++ b/internal/gitaly/service/operations/commit_files.go
@@ -899,7 +899,9 @@ func (s *Server) resolveParentCommit(
return targetBranchCommit, false, nil
}
- commit, err := repo.ResolveRevision(ctx, git.Revision(refish))
+ // By its exact name, as in the local repository: a missing branch does not resolve through
+ // a tag or another reference of the start repository.
+ commit, err := repo.ResolveBranch(ctx, strings.TrimPrefix(branch.String(), "refs/heads/"))
if err != nil {
return "", false, fmt.Errorf("resolving refish %q in %T: %w", refish, repo, err)
}
diff --git a/internal/gitaly/service/operations/commit_files_exact_test.go b/internal/gitaly/service/operations/commit_files_exact_test.go
index 7f25b36..7518ead 100644
--- a/internal/gitaly/service/operations/commit_files_exact_test.go
+++ b/internal/gitaly/service/operations/commit_files_exact_test.go
@@ -76,3 +76,67 @@ func TestUserCommitFilesResolvesBranchesExactly(t *testing.T) {
}
}
}
+
+// TestUserCommitFilesResolvesStartRepositoryBranchesExactly is
+// TestUserCommitFilesResolvesBranchesExactly for a branch resolved in another repository, the
+// request's start repository: there too, a missing branch never resolves through a decoy.
+func TestUserCommitFilesResolvesStartRepositoryBranchesExactly(t *testing.T) {
+ t.Parallel()
+
+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
+ ctx, cfg, client := setupOperationsService(t, ctx)
+
+ for _, decoy := range []git.ReferenceName{"refs/refs/heads/x", "refs/tags/refs/heads/x", "refs/remotes/refs/heads/x"} {
+ for _, tc := range []struct {
+ desc string
+ hasBranch bool
+ branch string
+ startBranch string
+ // expectedErr is set when the request must fail: the branch does not exist in the
+ // start repository.
+ expectedErr string
+ }{
+ {desc: "start branch, with branches", hasBranch: true, branch: "new", startBranch: "x", expectedErr: "reference not found"},
+ {desc: "target branch, with branches", hasBranch: true, branch: "x", expectedErr: "reference not found"},
+ {desc: "start branch, without branches", branch: "new", startBranch: "x"},
+ } {
+ t.Run(string(decoy)+", "+tc.desc, func(t *testing.T) {
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))
+
+ startRepo, startRepoPath := gittest.CreateRepository(t, ctx, cfg)
+ if tc.hasBranch {
+ gittest.WriteCommit(t, cfg, startRepoPath, gittest.WithBranch("main"))
+ }
+ decoyCommit := gittest.WriteCommit(t, cfg, startRepoPath, gittest.WithMessage("decoy"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "decoy", Mode: "100644", Content: "decoy\n"}))
+ gittest.WriteRef(t, cfg, startRepoPath, decoy, decoyCommit)
+
+ header := headerRequest(repo, gittest.TestUser, tc.branch, []byte("message"), tc.startBranch, "")
+ setStartRepository(header, startRepo)
+
+ stream, err := client.UserCommitFiles(ctx)
+ require.NoError(t, err)
+ require.NoError(t, stream.Send(header))
+ require.NoError(t, stream.Send(createFileHeaderRequest("file")))
+ require.NoError(t, stream.Send(actionContentRequest("content\n")))
+ resp, err := stream.CloseAndRecv()
+
+ if tc.expectedErr != "" {
+ require.ErrorContains(t, err, tc.expectedErr)
+ require.NotContains(t, string(gittest.Exec(t, cfg, "-C", repoPath, "for-each-ref", "refs/heads/")), "refs/heads/"+tc.branch)
+ return
+ }
+
+ // The start repository has no branch: the orphan commit of the issue-3294
+ // workaround, never the decoy's commit.
+ require.NoError(t, err)
+ require.Empty(t, resp.GetIndexError())
+ require.True(t, resp.GetBranchUpdate().GetBranchCreated())
+ commitID := resp.GetBranchUpdate().GetCommitId()
+ require.Empty(t, strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "log", "-1", "--format=%P", commitID))))
+ require.Equal(t, "file\n", string(gittest.Exec(t, cfg, "-C", repoPath, "ls-tree", "--name-only", commitID)))
+ })
+ }
+ }
+}