Git designed for AI agents.
operations: UserCommitFiles resolves branches by their exact names
Federico Elia285a057ef4aa
diff --git a/doc/graft/PLAN9.md b/doc/graft/PLAN9.mdindex 7ab4946..92c8126 100644--- a/doc/graft/PLAN9.md+++ b/doc/graft/PLAN9.md@@ -674,7 +674,7 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):| | Process | Count | From | Needed? ||---|---|---|---|---|| A | `rev-parse --verify refs/heads/<branch>^{commit}` | 1 | `commit_files.go:569` | a read: the request's object reader can answer it |-| B | `for-each-ref --count=1 refs/heads/` (`HasBranches`) | new branch: 1 | `commit_files.go:799` (issue-3294 workaround) | only when the start branch does not resolve |+| B | `for-each-ref --count=1 refs/heads/` (`HasBranches`) | new branch: 1 | `commit_files.go:799` (issue-3294 workaround) | only when no branch resolves; otherwise one `for-each-ref` reads the resolved branches by their exact names (15.2, item 3) || C | `rev-parse --verify refs/heads/<start>^{commit}` | new branch: 1 | `commit_files.go:829` | a read || D | `rev-parse --verify <oid>^{commit}` (`fetchMissingCommit`) | new branch: 1 | `commit_files.go:843` | redundant: C resolved and peeled it in the same repository || E | `hash-object --stdin -w --path=<path>` | 1 per blob | `commit_files.go:655` | yes: `--path` applies the repository's attributes (clean and eol filters) to the content |@@ -684,7 +684,7 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):| I | `ls-tree -z -r -t <tree>:` | 1 | `tree.go:407` | reads the whole tree recursively; only the trees on the changed paths are needed || J | `hash-object -t tree -w --stdin` | 1 per changed tree (depth + 1) | `tree.go:623` | one process can write every tree || K | `commit-tree` | 1 | `commit.go:203` | yes (see 15.2, item 4) |-| L | `for-each-ref --count=1 refs/heads/` | 1 | `commit_files.go:732` | only for `RepoCreated`, which A or C already decides |+| L | `for-each-ref --count=1 refs/heads/` | 1 | `commit_files.go:732` | folded into B: `RepoCreated` is decided by the exact read || M | `update-ref -z --stdin` | 1 | `update_with_hooks.go:286` | yes |### 15.2 Design@@ -719,19 +719,29 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):missing, which today's writer accepts. Probe:`m3-work/wp96/probe_mktree2.sh`.) A malformed tree makes the process exit, so therequest fails as today; the writer is not reused after an error.-3. **`for-each-ref` only when nothing else decides it.** `RepoCreated` is false when A or- C resolved a branch; `HasBranches` runs only when neither did (an orphan commit,- possibly into an empty repository). B runs only when the start branch does not- resolve, which is where the issue-3294 workaround matters.+3. **Branches resolve by their exact names, with one `for-each-ref`.** (Amended after+ review.) git resolves `refs/heads/<x>` with its DWIM rules: when the branch is+ missing, `refs/refs/heads/<x>`, `refs/tags/refs/heads/<x>` or+ `refs/remotes/refs/heads/<x>` answers for it, both in `rev-parse` and in `cat-file`+ (probe: `m3-work/wp96/probe_dwim.sh`). A commit could then silently start from a+ tag. `ResolveBranchesExactly` asks the reader first; a branch the reader cannot+ resolve does not exist exactly either, since the exact name is the first name git+ tries. The branches it resolves (the target, and a local start branch) are read+ 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.4. **Unchanged:** E, one `hash-object --path` per blob: the attributes of the path decidethe 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`commit-tree`: the existing `CommitWriter` costs two processes per request (its `gitconfig` lookup and its writer) unless it outlives the request. M.-Expected, one created file, any depth, existing or new branch: **5 processes**-(cat-file, hash-object for the blob, hash-object for the trees, commit-tree, update-ref). n files: 4 + n.-An orphan commit into an empty repository: 6 (`for-each-ref`).+Expected, one created file, any depth, existing or new branch, or an orphan commit into an+empty repository: **6 processes** (cat-file, for-each-ref, hash-object for the blob,+hash-object for the trees, commit-tree, update-ref). n files: 5 + n. (5 before the exact+resolution of item 3.)### 15.3 What stays identical@@ -745,21 +755,22 @@ An orphan commit into an empty repository: 6 (`for-each-ref`).* **Errors:** the same codes and messages for a missing branch, a missing start branch, anexisting file, a missing file to update, delete or move, and directory conflicts; theexisting `UserCommitFiles` tests run unchanged.-* **Two answers decided differently, in one corner.** With the start branch in the same- repository, it is now resolved before `HasBranches`, which runs only if it does not- resolve; and `RepoCreated` is false without `for-each-ref` when the target or the start- branch resolved. Both rely on "a resolved `refs/heads/<x>` is a branch". `get_oid` also- tries `refs/refs/heads/<x>`, `refs/tags/refs/heads/<x>` and `refs/remotes/refs/heads/<x>`- when `refs/heads/<x>` is absent, so a repository with no branch but such a ref now- starts from that ref, where today it starts an orphan commit (probe:- `m3-work/wp96/probe_dwim.sh`; `rev-parse --verify` and `cat-file` resolve it alike). Today the same rules- already apply to the target branch and, in a repository with branches, to the start- branch.-* **Kept as today, found by the differential test:** a file created below an existing- file (`README/x`) walks into the blob entry, which loses its ID; the tree is then- malformed and both writers refuse it (`writing tree: exit status 128`, an internal error- rather than an index error). WP96 keeps this; making it an index error is a separate- change.+* **Changed on purpose: a missing branch is missing.** Before WP96, a missing target or+ start branch resolved through a DWIM fallback reference (item 3). The commit then+ started from it, or `update-ref` refused the request. Now the request behaves as if the+ branch did not exist: a new branch (an orphan commit when nothing else gives a parent),+ or a missing start branch.+* **Changed on purpose: a file below a file.** Creating a file, a directory or a move+ target below an existing file or submodule (`README/x`) walked into the entry and+ cleared its ID. The tree was then malformed, the writer refused it, and the request+ failed with an internal error (`writing tree: exit status 128`). `TreeEntry.Add` now+ refuses the path (`ErrFileInPath`), and the request fails with the index error "A file+ with this name already exists" for the path. The differential test found it, and, once+ 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.* **The object reader goes back to the catfile cache** at the end of the request, on everypath (a deferred release), as today's `ReadTree` reader does. Its cache key holds thequarantine directory, so no other request reuses it, and the cache's TTL ends it.@@ -770,28 +781,66 @@ An orphan commit into an empty repository: 6 (`for-each-ref`).### 15.4 Proof-* `TestUserCommitFilesProcessCount` asserts the processes by subcommand: 5 for the four- cases above, 7 for three files (4 + n), 6 for the orphan commit into an empty repository.- A process that comes back fails it.+* `TestUserCommitFilesProcessCount` asserts the processes by subcommand: 6 for the four+ cases above and for the orphan commit into an empty repository, 8 for three files+ (5 + n). A process that comes back fails it.+* `TestUserCommitFilesResolvesBranchesExactly`: with `refs/refs/heads/x`,+ `refs/tags/refs/heads/x` or `refs/remotes/refs/heads/x` present and `refs/heads/x`+ 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.* `TestApplyActionLazyTreeMatchesRecursiveTree` applies seeded random sequences of the sixactions 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 or the same refusal for- every sequence, and that every action type succeeds and every index error occurs below- the top level. A failure names the seed (`-lazy-tree-seed` replays it). It fails when- the lazy read is removed from the walk.+ It asserts the same error for every action, the same tree ID for every sequence (no+ action leaves a tree a writer refuses), and that every action type succeeds. It also+ asserts that every index error occurs below the top level, including a file in the way+ of a created file, directory or move target. A failure names the seed+ (`-lazy-tree-seed` replays it). It fails when the lazy read is removed from the walk.* `TestTreeWriter_matchesWriteEntries` covers edge names, git's directory order, everymode, missing objects (accepted), and duplicate names, file/directory clashes, invalidmodes and empty IDs (refused with today's error). Writes after a refusal fail, and oneprocess writes every tree. `TestReadTreeLazily_matchesReadTree` checks that the walkedlazy tree equals `ReadTree`. `TestTreeWriter_quarantine` checks objects that exist onlyin the quarantine.-* `TestUserCommitFilesReleasesProcessesAndFiles` runs failing requests (index error,- refused tree, missing start branch) and a successful one. After each, evicting the cache- leaves no cat-file process and the storage's temporary directory holds no tree object- file. It fails without the deferred release, or without the file removal.+* `TestUserCommitFilesReleasesProcessesAndFiles` runs failing requests (index errors,+ missing start branch), a request that panics while it holds its object reader+ (injected through the server's test hook), and a successful one. After each, evicting+ the cache leaves no cat-file process and the storage's temporary directory holds no+ tree object file. It fails without the deferred release, or without the file removal.+ `TestTreeWriter_panic` panics inside a write while the tree object file exists and the+ process runs: the file is removed and the deferred `Close` ends the process.* The existing operations tests.* `UserCommitFiles` latency at 50 agents (graft-bench, as in 2.2), before and after,- alternated, in a window the PM grants.+ alternated, in a window the PM grants. Measured 2026-10-19: before `d584aac`, after+ `b677dec` (5 processes; without the exact read of 15.2 item 3, one `for-each-ref` less+ than now). Each run used a fresh server with batching on, `--mode land`, 50 agents of+ 5 changes, conflict rate 0.2 and seed 1. The order was ABBA three times, each run+ started once the 1-minute load was below 2. Scripts: `m3-work/wp96/window/`.++ | run | lands/min | p50 (ms) | p95 (ms) | p99 (ms) | server mean (ms) |+ |---|---:|---:|---:|---:|---:|+ | before 1-1 | 1309 | 577 | 923 | 981 | 540 |+ | after 1-1 | 1147 | 329 | 1333 | 1919 | 449 |+ | after 1-2 | 1611 | 290 | 371 | 777 | 272 |+ | before 1-2 | 1647 | 279 | 408 | 416 | 248 |+ | before 2-1 | 2014 | 232 | 391 | 409 | 241 |+ | after 2-1 | 1701 | 171 | 804 | 809 | 282 |+ | after 2-2 | 1756 | 213 | 293 | 306 | 187 |+ | before 2-2 | 1592 | 286 | 363 | 371 | 244 |+ | before 3-1 | 1439 | 375 | 1104 | 1321 | 396 |+ | after 3-1 | 984 | 385 | 1029 | 1081 | 412 |+ | after 3-2 | 1948 | 177 | 304 | 320 | 186 |+ | before 3-2 | 1909 | 200 | 520 | 547 | 266 |+ | **median before** | 1619 | 282 | 464 | 481 | 257 |+ | **median after** | 1656 | 251 | 588 | 793 | 277 |++ p50 is lower after (-31 ms in the median, lower in 4 of 6 adjacent before/after pairs)+ and lands per minute are level. The tail is dominated by whole-run stalls: a run either+ has a p99 close to its p95 or one above 800 ms. Such stalls hit 3 of 6 runs after and+ 2 of 6 before, and with 250 calls per run that cannot be told apart from noise. A first, back-to-back window+ without cooldown (load rising from 4.7 to 11.9) gave the same p50 gain and noisier+ tails.### 15.5 Ownership neededdiff --git a/internal/git/localrepo/export_test.go b/internal/git/localrepo/export_test.gonew file mode 100644index 0000000..387a733--- /dev/null+++ b/internal/git/localrepo/export_test.go@@ -0,0 +1,6 @@+package localrepo++// SetTestHook installs a function called at the named points of a write of w.+func (w *TreeWriter) SetTestHook(hook func(point string)) {+ w.testHook = hook+}diff --git a/internal/git/localrepo/refs.go b/internal/git/localrepo/refs.goindex 5e5ad92..5dc087d 100644--- a/internal/git/localrepo/refs.go+++ b/internal/git/localrepo/refs.go@@ -113,6 +113,63 @@ func (repo *Repo) ResolveRevisionWithReader(ctx context.Context, reader catfile.return info.Oid, nil}+// ResolveBranchesExactly returns the commit of each of branches that exists under its exact+// reference name, read through reader, the request's object reader. A branch that does not exist,+// or that does not peel to a commit, is left out of the map.+//+// 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.+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+ for _, branch := range branches {+ if wanted[branch] {+ continue+ }++ if _, err := repo.ResolveRevisionWithReader(ctx, reader, branch.Revision()+"^{commit}"); err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ continue+ }+ 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+ }++ // 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}")+ if err != nil {+ if errors.Is(err, git.ErrReferenceNotFound) {+ continue+ }+ return nil, fmt.Errorf("peeling %q: %w", ref.Name, err)+ }+ resolved[ref.Name] = commit+ }++ return resolved, nil+}+// GetReference looks up and returns the given reference. Returns a// ReferenceNotFound error if the reference was not found.func (repo *Repo) GetReference(ctx context.Context, reference git.ReferenceName) (git.Reference, error) {diff --git a/internal/git/localrepo/tree.go b/internal/git/localrepo/tree.goindex dda2550..caa0a67 100644--- a/internal/git/localrepo/tree.go+++ b/internal/git/localrepo/tree.go@@ -914,6 +914,9 @@ type TreeWriter struct {stderr bytes.Buffercancel context.CancelFuncerr error++ // testHook, when set by a test, is called at named points of a write.+ testHook func(point string)}var errTreeWriterClosed = errors.New("tree writer is closed")@@ -972,6 +975,10 @@ func (w *TreeWriter) write(entries []*TreeEntry) (_ git.ObjectID, returnedErr erreturn "", err}}+ if w.testHook != nil {+ // The tree object file exists and the process runs.+ w.testHook("started")+ }if _, err := fmt.Fprintf(w.cmd.Stdin(), "%s\n", file.Name()); err != nil {return "", w.processError(fmt.Errorf("sending tree object path: %w", err))diff --git a/internal/git/localrepo/tree_writer_test.go b/internal/git/localrepo/tree_writer_test.goindex f253042..f37afb4 100644--- a/internal/git/localrepo/tree_writer_test.go+++ b/internal/git/localrepo/tree_writer_test.go@@ -173,6 +173,41 @@ func TestTreeWriter_matchesWriteEntries(t *testing.T) {}}+// TestTreeWriter_panic checks that a panic during a write, while the tree object file exists and+// the process runs, removes the file, and that Close, as a deferred call runs it on the panic's+// way out, ends the process.+func TestTreeWriter_panic(t *testing.T) {+ t.Parallel()++ ctx, cfg, repo, repoPath := setupTreeWriterRepo(t)+ blob := gittest.WriteBlob(t, cfg, repoPath, []byte("content\n"))++ writer, err := repo.NewTreeWriter(ctx)+ require.NoError(t, err)+ var fileSeen bool+ writer.SetTestHook(func(point string) {+ require.Equal(t, "started", point)+ tempDir, err := repo.StorageTempDir()+ require.NoError(t, err)+ entries, err := os.ReadDir(tempDir)+ require.NoError(t, err)+ for _, entry := range entries {+ fileSeen = fileSeen || strings.HasPrefix(entry.Name(), "tree-object-")+ }+ require.NotNil(t, writer.cmd)+ panic("injected")+ })++ require.PanicsWithValue(t, "injected", func() {+ defer func() { require.NoError(t, writer.Close()) }()+ _, _ = writer.write([]*TreeEntry{{Path: "file", Mode: "100644", Type: Blob, OID: blob}})+ })++ require.True(t, fileSeen, "the hook ran while the tree object file existed")+ require.Nil(t, writer.cmd)+ requireNoTreeObjectFiles(t, repo)+}+// TestTreeEntry_WriteWith checks that WriteWith writes the trees Write writes.func TestTreeEntry_WriteWith(t *testing.T) {t.Parallel()diff --git a/internal/gitaly/service/operations/commit_files.go b/internal/gitaly/service/operations/commit_files.goindex 54fce32..64298fc 100644--- a/internal/gitaly/service/operations/commit_files.go+++ b/internal/gitaly/service/operations/commit_files.go@@ -595,16 +595,25 @@ func (s *Server) userCommitFiles(return err}defer cancelReader()+ if s.testHook != nil {+ s.testHook("UserCommitFiles: reader taken")+ }+ // The branches are resolved by their exact names: a missing branch never resolves through a+ // tag or another reference that git's DWIM rules would try after it.targetBranchName := git.NewReferenceNameFromBranchName(string(header.GetBranchName()))- targetBranchCommit, err := quarantineRepo.ResolveRevisionWithReader(ctx, reader, targetBranchName.Revision()+"^{commit}")+ branchNames := []git.ReferenceName{targetBranchName}+ var startBranchName git.ReferenceName+ if header.GetStartSha() == "" && remoteRepo == nil && len(header.GetStartBranchName()) > 0 {+ startBranchName = git.NewReferenceNameFromBranchName(string(header.GetStartBranchName()))+ branchNames = append(branchNames, startBranchName)+ }+ branches, err := quarantineRepo.ResolveBranchesExactly(ctx, reader, branchNames...)if err != nil {- if !errors.Is(err, git.ErrReferenceNotFound) {- return fmt.Errorf("resolve target branch commit: %w", err)- }-- // the branch is being created+ return fmt.Errorf("resolve branches: %w", err)}+ // Empty when the branch is being created.+ targetBranchCommit := branches[targetBranchName]var parentCommitOID git.ObjectID// startBranchResolved is whether the start branch resolved in the repository, which then has@@ -614,10 +623,11 @@ func (s *Server) userCommitFiles(parentCommitOID, startBranchResolved, err = s.resolveParentCommit(ctx,quarantineRepo,- reader,+ branches,remoteRepo,targetBranchName,targetBranchCommit,+ startBranchName,string(header.GetStartBranchName()),)if err != nil {@@ -816,14 +826,16 @@ func sameRepository(repoA, repoB *gitalypb.Repository) bool {}// resolveParentCommit returns the parent commit of the new commit, and whether it is the start-// branch of local, which then has a branch.+// branch of local, which then has a branch. localBranches holds the commits of the target and+// start branches of local that exist under their exact names.func (s *Server) resolveParentCommit(ctx context.Context,local *localrepo.Repo,- localReader catfile.ObjectInfoReader,+ localBranches map[git.ReferenceName]git.ObjectID,remote *gitalypb.Repository,targetBranch git.ReferenceName,targetBranchCommit git.ObjectID,+ localStartBranch git.ReferenceName,startBranch string,) (git.ObjectID, bool, error) {if remote == nil && startBranch == "" {@@ -837,21 +849,25 @@ func (s *Server) resolveParentCommit(refish := branch + "^{commit}"if remote == nil {- // The start branch is resolved first, through the request's reader: a repository where- // it resolves has branches, so the workaround below matters only when it does not.- commit, err := local.ResolveRevisionWithReader(ctx, localReader, git.Revision(refish))- if err == nil {+ if commit, ok := localBranches[localStartBranch]; ok {return commit, true, nil}- if hasBranches, hasBranchesErr := local.HasBranches(ctx); hasBranchesErr != nil {- return "", false, fmt.Errorf("has branches: %w", hasBranchesErr)- } else if !hasBranches {+ // A repository whose target branch exists has branches: the workaround below matters+ // only when it does not.+ hasBranches := targetBranchCommit != ""+ if !hasBranches {+ var err error+ if hasBranches, err = local.HasBranches(ctx); err != nil {+ return "", false, fmt.Errorf("has branches: %w", err)+ }+ }+ if !hasBranches {// The first branch is being created: see the workaround below.return targetBranchCommit, false, nil}- return "", false, fmt.Errorf("resolving refish %q in %T: %w", refish, local, err)+ return "", false, fmt.Errorf("resolving refish %q in %T: %w", refish, local, git.ErrReferenceNotFound)}repo, err := remoterepo.New(ctx, remote, s.conns)diff --git a/internal/gitaly/service/operations/commit_files_cleanup_test.go b/internal/gitaly/service/operations/commit_files_cleanup_test.goindex 2114e82..b720208 100644--- a/internal/gitaly/service/operations/commit_files_cleanup_test.go+++ b/internal/gitaly/service/operations/commit_files_cleanup_test.go@@ -3,6 +3,7 @@ package operationsimport ("os""strings"+ "sync/atomic""testing""github.com/prometheus/client_golang/prometheus/testutil"@@ -23,7 +24,7 @@ import ()// TestUserCommitFilesReleasesProcessesAndFiles checks that a UserCommitFiles request, whether it-// fails or succeeds, gives back its object reader and stops its tree writer, and leaves no+// fails, panics or succeeds, gives back its object reader and stops its tree writer, and leaves no// temporary tree object file in the storage (PLAN9, WP96): once the cache evicts its processes,// no cat-file process is left running.func TestUserCommitFilesReleasesProcessesAndFiles(t *testing.T) {@@ -35,9 +36,17 @@ func TestUserCommitFilesReleasesProcessesAndFiles(t *testing.T) {testcfg.BuildGitalyHooks(t, cfg)var processCache *catfile.ProcessCache+ // panicNext makes the next request panic once it holds its object reader.+ var panicNext atomic.Boolcfg.SocketPath = testserver.RunGitalyServer(t, cfg, func(srv *grpc.Server, deps *service.Dependencies) {processCache = deps.GetCatfileCache().(*catfile.ProcessCache)- gitalypb.RegisterOperationServiceServer(srv, NewServer(deps))+ operationServer := NewServer(deps)+ operationServer.testHook = func(point string) {+ if point == "UserCommitFiles: reader taken" && panicNext.CompareAndSwap(true, false) {+ panic("injected")+ }+ }+ gitalypb.RegisterOperationServiceServer(srv, operationServer)gitalypb.RegisterHookServiceServer(srv, hook.NewServer(deps))gitalypb.RegisterRepositoryServiceServer(srv, repository.NewServer(deps))})@@ -54,10 +63,17 @@ func TestUserCommitFilesReleasesProcessesAndFiles(t *testing.T) {for _, tc := range []struct {desc string+ panics boolstartBranch stringrequests []*gitalypb.UserCommitFilesRequestexpectedErr string}{+ {+ desc: "a panic while the request holds its object reader",+ panics: true,+ requests: []*gitalypb.UserCommitFilesRequest{createFileHeaderRequest("file"), actionContentRequest("content\n")},+ expectedErr: "injected",+ },{desc: "a file to delete that does not exist",requests: []*gitalypb.UserCommitFilesRequest{deleteFileHeaderRequest("missing.txt")},@@ -80,6 +96,7 @@ func TestUserCommitFilesReleasesProcessesAndFiles(t *testing.T) {},} {t.Run(tc.desc, func(t *testing.T) {+ panicNext.Store(tc.panics)stream, err := client.UserCommitFiles(ctx)require.NoError(t, err)require.NoError(t, stream.Send(headerRequest(repo, gittest.TestUser, "master", []byte("message"), tc.startBranch, "")))diff --git a/internal/gitaly/service/operations/commit_files_exact_test.go b/internal/gitaly/service/operations/commit_files_exact_test.gonew file mode 100644index 0000000..7f25b36--- /dev/null+++ b/internal/gitaly/service/operations/commit_files_exact_test.go@@ -0,0 +1,78 @@+package operations++import (+ "strings"+ "testing"++ "github.com/stretchr/testify/require"++ "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/testhelper"+)++// TestUserCommitFilesResolvesBranchesExactly checks that the target and the start branch resolve+// only by their exact names: when refs/heads/x is missing, git's DWIM rules would resolve x through+// refs/refs/heads/x, refs/tags/refs/heads/x or refs/remotes/refs/heads/x, and the commit would+// silently start from a commit such a reference (a tag an agent may create) chose. Both with+// branches in the repository and without, the request must behave as if x did not exist.+func TestUserCommitFilesResolvesBranchesExactly(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 start branch does not exist.+ expectedErr string+ repoCreated bool+ }{+ {desc: "target branch, with branches", hasBranch: true, branch: "x"},+ {desc: "target branch, without branches", branch: "x", repoCreated: true},+ {desc: "start branch, with branches", hasBranch: true, branch: "new", startBranch: "x", expectedErr: "reference not found"},+ {desc: "start branch, without branches", branch: "new", startBranch: "x", repoCreated: true},+ } {+ t.Run(string(decoy)+", "+tc.desc, func(t *testing.T) {+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ if tc.hasBranch {+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))+ }+ decoyCommit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithMessage("decoy"),+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "decoy", Mode: "100644", Content: "decoy\n"}))+ gittest.WriteRef(t, cfg, repoPath, decoy, decoyCommit)++ stream, err := client.UserCommitFiles(ctx)+ require.NoError(t, err)+ require.NoError(t, stream.Send(headerRequest(repo, gittest.TestUser, tc.branch, []byte("message"), tc.startBranch, "")))+ 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+ }++ require.NoError(t, err)+ require.Empty(t, resp.GetIndexError())+ require.True(t, resp.GetBranchUpdate().GetBranchCreated())+ require.Equal(t, tc.repoCreated, resp.GetBranchUpdate().GetRepoCreated())++ // An orphan commit: the decoy is not its parent, and its tree holds only the file.+ 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)))+ require.Equal(t, commitID, strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/"+tc.branch))))+ // The decoy is untouched.+ require.Equal(t, decoyCommit.String(), strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", decoy.String()))))+ })+ }+ }+}diff --git a/internal/gitaly/service/operations/commit_files_processes_test.go b/internal/gitaly/service/operations/commit_files_processes_test.goindex 55b27a2..8ca90a7 100644--- a/internal/gitaly/service/operations/commit_files_processes_test.go+++ b/internal/gitaly/service/operations/commit_files_processes_test.go@@ -15,8 +15,9 @@ import (// TestUserCommitFilesProcessCount counts the git processes of one UserCommitFiles request, by// subcommand (PLAN9, WP96): one cat-file for every read, one hash-object per blob, one hash-object-// for every tree, commit-tree and update-ref. A for-each-ref runs only when no branch resolved,-// for the orphan commit into an empty repository. A process that comes back fails the test.+// for every tree, commit-tree and update-ref, and one for-each-ref: the exact read of the branches+// that resolved or, when none did (the orphan commit into an empty repository), the check for+// branches. A process that comes back fails the test.func TestUserCommitFilesProcessCount(t *testing.T) {t.Parallel()@@ -31,29 +32,29 @@ func TestUserCommitFilesProcessCount(t *testing.T) {{desc: "existing branch, top level",paths: []string{"file.txt"},- expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "update-ref": 1},+ expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "for-each-ref": 1, "update-ref": 1},},{desc: "existing branch, nested",paths: []string{"a/b/file.txt"},- expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "update-ref": 1},+ expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "for-each-ref": 1, "update-ref": 1},},{desc: "new branch, top level",newBranch: true,paths: []string{"file.txt"},- expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "update-ref": 1},+ expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "for-each-ref": 1, "update-ref": 1},},{desc: "new branch, nested",newBranch: true,paths: []string{"a/b/file.txt"},- expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "update-ref": 1},+ expected: map[string]uint64{"cat-file": 1, "hash-object": 2, "commit-tree": 1, "for-each-ref": 1, "update-ref": 1},},{desc: "three files",paths: []string{"one.txt", "a/two.txt", "a/b/three.txt"},- expected: map[string]uint64{"cat-file": 1, "hash-object": 4, "commit-tree": 1, "update-ref": 1},+ expected: map[string]uint64{"cat-file": 1, "hash-object": 4, "commit-tree": 1, "for-each-ref": 1, "update-ref": 1},},{desc: "orphan commit into an empty repository",diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.goindex a932d85..2230dfc 100644--- a/internal/gitaly/service/operations/server.go+++ b/internal/gitaly/service/operations/server.go@@ -46,6 +46,8 @@ type Server struct {// storageSync catches up the local repository from the storage core before a land is retried,// see doc/graft/STORAGE.md. It does nothing unless the storage core is authoritative.storageSync corestore.Freshener+ // testHook, when set by a test, is called at named points of a request.+ testHook func(point string)}// NewServer creates a new instance of a grpc OperationServiceServer