Skip to content

Git designed for AI agents.

operations: fixes from the review of WP96

Federico Elia9db0ac7c8bb4
diff --git a/doc/graft/PLAN9.md b/doc/graft/PLAN9.md
index fa11476..6b9b7e6 100644
--- a/doc/graft/PLAN9.md
+++ b/doc/graft/PLAN9.md
@@ -703,8 +703,14 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):
repository, when it is missing, is unchanged; the reader then sees the fetched pack). G is
dropped (F already answered it).
* I is replaced by a lazy tree: the root is read with `Object()` and parsed (in
- `localrepo`, into the entries `ls-tree -r -t` gives: a six-digit mode and the type
- from the mode's format bits), and a subtree is read only when an action descends into it
+ `localrepo`, into the entries `ls-tree -r -t` gives). Each mode becomes git's
+ `canon_mode`, which every tree read applies and `ls-tree` prints: a regular file
+ becomes `100755` when its mode has `0100`, else `100644`; a symlink becomes `120000`;
+ a directory `040000`; anything else `160000`, a submodule. The type follows the mode.
+ A legacy mode (`100664`, `100600`, `120777`, `40755`) is so written back canonical,
+ as before WP96. (Amended after review: the first version kept the raw mode, and a
+ legacy sibling in a changed directory made the writer refuse the tree; probe:
+ `m3-work/wp96/probe_canon.sh`.) A subtree is read only when an action descends into it
(create, update, delete, move, chmod and create-dir each address one path).
Untouched subtrees keep their ID, so `Write` skips them as today. The cost no
longer grows with the size of the repository's tree.
@@ -731,8 +737,13 @@ Each one, traced to its call site (all through `localrepo.Repo.Exec`):
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 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}`.
+ start repository) is resolved there by its exact name too (`remoterepo.ResolveBranch`),
+ instead of with `FindCommit` on `refs/heads/<x>^{commit}`. `ListRefs` reads the
+ reference, and only the exact name counts, so a branch below the name
+ (`refs/heads/<x>/y`) leaves `<x>` missing, not ambiguous. `FindCommit` then peels the
+ object ID it read to a commit, as `^{commit}` did, so a branch that does not peel to a
+ commit does not resolve, as before. (The first version used `FindBranch`, which
+ answered "ambiguous" in the first case and a different error in the second.)
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
@@ -765,18 +776,34 @@ resolution of item 3.)
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
+ refuses the path (`FileInPathError`), and the request fails with the index error "A file
+ with this name already exists", which names the file in the way (`README`). 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.
+ name doesn't exist" otherwise, as `update` does. This includes a chmod that clears the
+ executable bit of a directory, which used to succeed and changed nothing: a deliberate
+ compatibility change, accepted in review.
+* **Changed on purpose: a moved file stays a file.** A move added the moved entry without
+ its type, so the actions after it in the same request did not see a file. A chmod of
+ the moved file was refused (since the check above), an update of it was refused
+ ("doesn't exist", since the base import: the update check is that old), and a create
+ onto the moved path replaced it silently (neither "exists" branch of `Add` matched an
+ entry without type). The move now keeps the entry's type: the chmod and the update
+ apply, and the create is refused with "A file with this name already exists".
* **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.
+* **Known, pre-existing (review minor 5):** the object reader goes back to the catfile cache
+ at the end of the request, on every path (a deferred release), as the old `ReadTree`
+ reader did, once per request. Its cache key holds the quarantine directory, so no other
+ request reuses it: the entry is useless until the cache's TTL ends it.
+* **Known, pre-existing (review minor 6):** a crash of the server between `os.CreateTemp`
+ and the removal of a tree object file leaves the `tree-object-*` file in the storage's
+ temporary directory, as the commit writer's `commit-object-*` files can be left. The
+ storage's temporary directory cleaner (`internal/tempdir`, `StartCleaning`) removes it
+ once it is older than its maximum age (7 days).
* **Git features:** `hash-object --stdin-paths` (long-standing, already used by
`CommitWriter`) and `cat-file --batch-command` (2.36, already used by the object
reader). The server already needs newer git
@@ -800,8 +827,21 @@ resolution of item 3.)
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.
+ of a created file, directory or move target. Half of its base trees are written with
+ `hash-object --literally` and legacy modes. A failure names the seed
+ (`-lazy-tree-seed` replays it). It fails when the lazy read is removed from the walk,
+ and when the parser keeps raw modes. The two trees share the semantics of the actions,
+ so the following tests check those semantics directly.
+* `TestUserCommitFilesScenarios`: a move then a chmod, an update, or a create of the moved
+ file; a file below a file names the file in the way; a chmod of a directory is refused
+ either way. `TestUserCommitFilesLegacyModes`: a commit into a directory with `100664`,
+ `100600` and `120777` entries writes them back canonical (the reviewer's probe).
+ `TestReadTreeLazily_canonicalModes`: every mode class against `ReadTree`.
+ `TestUserCommitFilesFetchesNestedTrees`: a parent fetched from the start repository
+ after the reader started, with nested trees that the lazy tree then reads.
+ `TestRepository_ResolveBranch`: a branch below the name, a branch to an annotated tag
+ (peeled) and to a tree (not found; files backend only, as update-ref refuses to write
+ one).
* `TestTreeWriter_matchesWriteEntries` covers edge names, git's directory order, every
mode, missing objects (accepted), and duplicate names, file/directory clashes, invalid
modes and empty IDs (refused with today's error). Writes after a refusal fail, and one
diff --git a/internal/git/localrepo/tree.go b/internal/git/localrepo/tree.go
index caa0a67..1f191f6 100644
--- a/internal/git/localrepo/tree.go
+++ b/internal/git/localrepo/tree.go
@@ -108,8 +108,8 @@ func (t *TreeEntry) ensureLoaded() error {
return nil
}
-// read returns the entries of the tree oid as ls-tree describes them: a six-digit mode, the type
-// derived from the mode and the base name. Its subtrees are left to be read on demand.
+// read returns the entries of the tree oid as ls-tree describes them: the canonical mode, the type
+// it implies and the base name. Its subtrees are left to be read on demand.
func (l *treeLoader) read(oid git.ObjectID) ([]*TreeEntry, error) {
object, err := l.reader.Object(l.ctx, oid.Revision())
if err != nil {
@@ -141,25 +141,28 @@ func (l *treeLoader) read(oid git.ObjectID) ([]*TreeEntry, error) {
}
data = rest[rawSize:]
- // As ls-tree prints it: the octal value with six digits, and the type from its format
- // bits.
modeValue, err := strconv.ParseUint(string(mode), 8, 32)
if err != nil {
return nil, fmt.Errorf("parsing tree %s: invalid mode %q", oid, mode)
}
entry := &TreeEntry{
OID: entryOID,
- Mode: fmt.Sprintf("%06o", modeValue),
Path: string(name),
}
+ // As ls-tree prints it: git's canon_mode, which reading a tree applies, so that a
+ // legacy mode (100664, 120777, 040755) is written back canonical, and six digits.
switch modeValue & 0o170000 {
+ case 0o100000:
+ entry.Mode, entry.Type = "100644", Blob
+ if modeValue&0o100 != 0 {
+ entry.Mode = "100755"
+ }
+ case 0o120000:
+ entry.Mode, entry.Type = "120000", Blob
case 0o040000:
- entry.Type = Tree
- entry.loader = l
- case 0o160000:
- entry.Type = Submodule
+ entry.Mode, entry.Type, entry.loader = "040000", Tree, l
default:
- entry.Type = Blob
+ entry.Mode, entry.Type = "160000", Submodule
}
entries = append(entries, entry)
}
@@ -183,8 +186,6 @@ var (
ErrEntryNotFound = errors.New("entry not found")
// ErrEntryExists indicates an entry already exists.
ErrEntryExists = errors.New("entry already exists")
- // ErrFileInPath indicates that a directory of the path is a file or a submodule.
- ErrFileInPath = errors.New("file in the way of the path")
// ErrPathTraversal indicates a path contains a traversal.
ErrPathTraversal = errors.New("path contains traversal")
// ErrAbsolutePath indicates the path is an absolute path
@@ -195,6 +196,17 @@ var (
ErrDisallowedPath = errors.New("disallowed path")
)
+// FileInPathError is returned by Add when a directory of the path is a file or a submodule, so
+// that nothing can be added below it.
+type FileInPathError struct {
+ // Path is the path of the file or submodule in the way.
+ Path string
+}
+
+func (e FileInPathError) Error() string {
+ return fmt.Sprintf("file in the way of the path: %q", e.Path)
+}
+
func validateFileCreationPath(path string) (string, error) {
path = filepath.Clean(path)
@@ -314,7 +326,7 @@ Loop:
// A file or a submodule is in the way: nothing can be added below it.
if entry.Type != Tree {
- return ErrFileInPath
+ return FileInPathError{Path: filepath.Join(parentDirs, entry.Path)}
}
// We found the next directory to walk into.
diff --git a/internal/git/localrepo/tree_writer_test.go b/internal/git/localrepo/tree_writer_test.go
index f37afb4..9039f66 100644
--- a/internal/git/localrepo/tree_writer_test.go
+++ b/internal/git/localrepo/tree_writer_test.go
@@ -1,7 +1,9 @@
package localrepo
import (
+ "bytes"
"context"
+ "fmt"
"os"
"strings"
"testing"
@@ -12,6 +14,7 @@ import (
"github.com/feder-cr/git-ai/internal/git/gittest"
"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/helper/text"
"github.com/feder-cr/git-ai/internal/testhelper"
"github.com/feder-cr/git-ai/internal/testhelper/testcfg"
)
@@ -285,6 +288,69 @@ func TestReadTreeLazily_matchesReadTree(t *testing.T) {
require.ErrorContains(t, err, "check object type: object not found")
}
+// TestReadTreeLazily_canonicalModes checks that the lazy tree reads legacy modes, which git no
+// longer writes but still reads, as ls-tree prints them: canonical, as git's canon_mode makes
+// them. Written back, they are then modes the tree writer accepts.
+func TestReadTreeLazily_canonicalModes(t *testing.T) {
+ t.Parallel()
+
+ ctx, cfg, repo, repoPath := setupTreeWriterRepo(t)
+
+ blob := gittest.WriteBlob(t, cfg, repoPath, []byte("content\n"))
+ subtree := gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{{Path: "f", Mode: "100644", OID: blob}})
+ raw := func(oid git.ObjectID) []byte {
+ b, err := oid.Bytes()
+ require.NoError(t, err)
+ return b
+ }
+ var content bytes.Buffer
+ for _, entry := range []struct {
+ mode, name string
+ oid git.ObjectID
+ }{
+ // In git's order: a directory sorts as its name with a slash.
+ {"100600", "a", blob},
+ {"100664", "b", blob},
+ {"100775", "c", blob},
+ {"120777", "d", blob},
+ {"40755", "e", subtree},
+ {"644", "f", blob},
+ } {
+ fmt.Fprintf(&content, "%s %s\x00", entry.mode, entry.name)
+ content.Write(raw(entry.oid))
+ }
+ root := git.ObjectID(text.ChompBytes(gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: &content},
+ "-C", repoPath, "hash-object", "-t", "tree", "-w", "--literally", "--stdin")))
+
+ expected, err := repo.ReadTree(ctx, root.Revision(), WithRecursive())
+ require.NoError(t, err)
+
+ reader, cancel, err := repo.RequestObjectReader(ctx)
+ require.NoError(t, err)
+ defer cancel()
+ lazy, err := repo.ReadTreeLazily(ctx, reader, root)
+ require.NoError(t, err)
+ require.NoError(t, lazy.Walk(func(string, *TreeEntry) error { return nil }))
+ require.Equal(t, expected, lazy)
+
+ typeNames := map[ObjectType]string{Blob: "blob", Tree: "tree", Submodule: "commit"}
+ var modes []string
+ for _, entry := range lazy.Entries {
+ modes = append(modes, entry.Mode+" "+typeNames[entry.Type]+" "+entry.Path)
+ }
+ require.Equal(t, []string{
+ "100644 blob a", "100644 blob b", "100755 blob c", "120000 blob d", "040000 tree e", "160000 commit f",
+ }, modes)
+
+ // Written back, the tree holds the canonical modes, which the tree writer accepts.
+ lazy.OID = ""
+ writer, err := repo.NewTreeWriter(ctx)
+ require.NoError(t, err)
+ require.NoError(t, lazy.WriteWith(writer))
+ require.NoError(t, writer.Close())
+ require.NotEqual(t, root, lazy.OID)
+}
+
// TestTreeWriter_quarantine checks that the request's reader and tree writer run on the
// quarantined repository: they write into the quarantine and read what only it holds.
func TestTreeWriter_quarantine(t *testing.T) {
diff --git a/internal/git/remoterepo/repository.go b/internal/git/remoterepo/repository.go
index b30aaf7..8a63543 100644
--- a/internal/git/remoterepo/repository.go
+++ b/internal/git/remoterepo/repository.go
@@ -2,7 +2,9 @@ package remoterepo
import (
"context"
+ "errors"
"fmt"
+ "io"
"sync"
"github.com/feder-cr/git-ai/internal/git"
@@ -91,34 +93,44 @@ func (rr *Repo) ResolveRevision(ctx context.Context, revision git.Revision) (git
}
// 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.
+// refs/heads/) by its exact reference name to the commit it points to, as
+// `refs/heads/<branch>^{commit}` peels it. 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 when the branch does not exist (also
+// when branches below its name do, refs/heads/<branch>/x) or does not peel to a commit.
func (rr *Repo) ResolveBranch(ctx context.Context, branch string) (git.ObjectID, error) {
- resp, err := gitalypb.NewRefServiceClient(rr.conn).FindBranch(ctx, &gitalypb.FindBranchRequest{
+ name := git.NewReferenceNameFromBranchName(branch)
+
+ // ListRefs matches references below the pattern too: only the exact name counts.
+ stream, err := gitalypb.NewRefServiceClient(rr.conn).ListRefs(ctx, &gitalypb.ListRefsRequest{
Repository: rr.Repository,
- Name: []byte(branch),
+ Patterns: [][]byte{[]byte(name)},
})
if err != nil {
- return "", fmt.Errorf("find branch: %w", err)
- }
-
- oidHex := resp.GetBranch().GetTargetCommit().GetId()
- if oidHex == "" {
- return "", git.ErrReferenceNotFound
+ return "", fmt.Errorf("list refs: %w", err)
}
-
- objectHash, err := rr.ObjectHash(ctx)
- if err != nil {
- return "", fmt.Errorf("detecting object hash: %w", err)
+ var target string
+ for {
+ resp, err := stream.Recv()
+ if err != nil {
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ return "", fmt.Errorf("list refs: %w", err)
+ }
+ for _, ref := range resp.GetReferences() {
+ if git.ReferenceName(ref.GetName()) == name {
+ target = ref.GetTarget()
+ }
+ }
}
-
- oid, err := objectHash.FromHex(oidHex)
- if err != nil {
- return "", fmt.Errorf("parsing object ID: %w", err)
+ if target == "" {
+ return "", git.ErrReferenceNotFound
}
- return oid, nil
+ // The object ID read above, peeled as `<branch>^{commit}` is: an object ID is never
+ // resolved through a reference.
+ return rr.ResolveRevision(ctx, git.Revision(target)+"^{commit}")
}
// HasBranches will dial to the remote repository and check whether the repository has any branches.
diff --git a/internal/git/remoterepo/repository_test.go b/internal/git/remoterepo/repository_test.go
index f981221..876b16b 100644
--- a/internal/git/remoterepo/repository_test.go
+++ b/internal/git/remoterepo/repository_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"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/gittest"
"github.com/feder-cr/git-ai/internal/git/remoterepo"
"github.com/feder-cr/git-ai/internal/gitaly/storage"
@@ -91,6 +92,9 @@ func TestRepository_ResolveBranch(t *testing.T) {
repo, err := remoterepo.New(ctx, repoProto, pool)
require.NoError(t, err)
+ // A branch below the missing name: x does not exist, it is not ambiguous.
+ gittest.WriteRef(t, cfg, repoPath, "refs/heads/x/below", branchCommit)
+
oid, err := repo.ResolveBranch(ctx, "main")
require.NoError(t, err)
require.Equal(t, branchCommit, oid)
@@ -98,6 +102,31 @@ func TestRepository_ResolveBranch(t *testing.T) {
_, err = repo.ResolveBranch(ctx, "x")
require.Equal(t, git.ErrReferenceNotFound, err)
+ t.Run("branches that point to other objects", func(t *testing.T) {
+ // update-ref refuses to point a branch to anything but a commit, so only a raw write
+ // of a loose reference makes one, which only the files backend has.
+ backend, err := gitcmd.DetectReferenceBackend(ctx, repoPath)
+ require.NoError(t, err)
+ if backend.Name != "files" {
+ t.Skipf("a branch to a non-commit cannot be written in the %s backend", backend.Name)
+ }
+
+ tag := gittest.WriteTag(t, cfg, repoPath, "annotated", branchCommit.Revision(), gittest.WriteTagConfig{Message: "annotated"})
+ tree := gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{{Path: "f", Mode: "100644", Content: "f\n"}})
+ for name, target := range map[string]git.ObjectID{"tagged": tag, "tree": tree} {
+ require.NoError(t, os.WriteFile(filepath.Join(repoPath, "refs", "heads", name), []byte(target+"\n"), mode.File))
+ }
+
+ // An annotated tag peels to its commit, as `<branch>^{commit}` does.
+ oid, err := repo.ResolveBranch(ctx, "tagged")
+ require.NoError(t, err)
+ require.Equal(t, branchCommit, oid)
+
+ // A tree does not peel to a commit: the branch does not resolve, as before.
+ _, err = repo.ResolveBranch(ctx, "tree")
+ 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)
diff --git a/internal/gitaly/service/operations/commit_files.go b/internal/gitaly/service/operations/commit_files.go
index 1e43401..b3ccf93 100644
--- a/internal/gitaly/service/operations/commit_files.go
+++ b/internal/gitaly/service/operations/commit_files.go
@@ -327,6 +327,8 @@ func applyAction(
OID: git.ObjectID(action.OID),
Mode: mode,
Path: filepath.Base(action.NewPath),
+ // The moved entry is a file: a later action of the request sees it as one.
+ Type: entry.Type,
},
localrepo.WithOverwriteDirectory(),
); err != nil {
@@ -409,6 +411,15 @@ func applyAction(
// translateLocalrepoError converts errors returned by the `localrepo` package into nice errors that we can return to the caller.
// Most importantly, these errors will carry metadata that helps to figure out what exactly has gone wrong.
func translateError(err error, path string) error {
+ // A file in the way of the path: the error names that file, which already exists.
+ var fileInPath localrepo.FileInPathError
+ if errors.As(err, &fileInPath) {
+ return indexError{
+ path: fileInPath.Path,
+ errorType: errFileExists,
+ }
+ }
+
switch {
case errors.Is(err, localrepo.ErrEntryNotFound) ||
errors.Is(err, localrepo.ErrObjectNotFound):
@@ -431,8 +442,7 @@ func translateError(err error, path string) error {
path: path,
errorType: errDirectoryTraversal,
}
- case errors.Is(err, localrepo.ErrEntryExists) ||
- errors.Is(err, localrepo.ErrFileInPath):
+ case errors.Is(err, localrepo.ErrEntryExists):
return indexError{
path: path,
errorType: errFileExists,
diff --git a/internal/gitaly/service/operations/commit_files_lazy_test.go b/internal/gitaly/service/operations/commit_files_lazy_test.go
index 2fbf256..369f391 100644
--- a/internal/gitaly/service/operations/commit_files_lazy_test.go
+++ b/internal/gitaly/service/operations/commit_files_lazy_test.go
@@ -1,11 +1,13 @@
package operations
import (
+ "bytes"
"errors"
"flag"
"fmt"
"math/rand/v2"
"path/filepath"
+ "sort"
"strings"
"testing"
"time"
@@ -15,6 +17,8 @@ import (
"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/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/helper/text"
"github.com/feder-cr/git-ai/internal/testhelper"
"github.com/feder-cr/git-ai/internal/testhelper/testcfg"
)
@@ -96,8 +100,22 @@ func TestApplyActionLazyTreeMatchesRecursiveTree(t *testing.T) {
return false
}
+ // The legacy modes that git's canon_mode reads as each canonical mode.
+ legacyModes := map[string][]string{
+ "100644": {"100644", "100664", "100600"},
+ "100755": {"100755", "100775", "100700"},
+ "120000": {"120000", "120777"},
+ "040000": {"40000", "40755"},
+ "160000": {"160000"},
+ }
+ legacyMode := func(entry *localrepo.TreeEntry) string {
+ modes := legacyModes[entry.Mode]
+ return modes[rng.IntN(len(modes))]
+ }
+
// writeBaseTree writes a random valid tree with regular, executable, symlink and submodule
- // entries, through today's writer.
+ // entries: through today's writer, or, for half of the trees, with legacy modes that git no
+ // longer writes but still reads.
writeBaseTree := func() git.ObjectID {
basePaths = nil
root := &localrepo.TreeEntry{Mode: "040000", Type: localrepo.Tree}
@@ -115,13 +133,16 @@ func TestApplyActionLazyTreeMatchesRecursiveTree(t *testing.T) {
entry.Mode = "100644"
}
if err := root.Add(path, entry); err != nil {
- if !errors.Is(err, localrepo.ErrFileInPath) {
+ if !errors.As(err, &localrepo.FileInPathError{}) {
require.ErrorIs(t, err, localrepo.ErrEntryExists)
}
continue
}
basePaths = append(basePaths, path)
}
+ if rng.IntN(2) == 0 {
+ return writeLiteralTree(t, cfg, repoPath, root.Entries, legacyMode)
+ }
require.NoError(t, root.Write(ctx, repo))
return root.OID
}
@@ -210,6 +231,32 @@ func TestApplyActionLazyTreeMatchesRecursiveTree(t *testing.T) {
}
}
+// writeLiteralTree writes the tree of entries and the trees below it with `hash-object
+// --literally`, each entry with the mode that mode gives it, and returns the tree's ID. The tree
+// can so hold modes that git no longer writes (100664, 120777, 40755): those of legacy trees,
+// which git still reads, through canon_mode.
+func writeLiteralTree(tb testing.TB, cfg config.Cfg, repoPath string, entries []*localrepo.TreeEntry, mode func(*localrepo.TreeEntry) string) git.ObjectID {
+ tb.Helper()
+
+ sorted := append([]*localrepo.TreeEntry(nil), entries...)
+ sort.Stable(localrepo.TreeEntriesByPath(sorted))
+
+ var content bytes.Buffer
+ for _, entry := range sorted {
+ oid := entry.OID
+ if entry.Type == localrepo.Tree && oid == "" {
+ oid = writeLiteralTree(tb, cfg, repoPath, entry.Entries, mode)
+ }
+ raw, err := oid.Bytes()
+ require.NoError(tb, err)
+ fmt.Fprintf(&content, "%s %s\x00", mode(entry), entry.Path)
+ content.Write(raw)
+ }
+
+ return git.ObjectID(text.ChompBytes(gittest.ExecOpts(tb, cfg, gittest.ExecConfig{Stdin: &content},
+ "-C", repoPath, "hash-object", "-t", "tree", "-w", "--literally", "--stdin")))
+}
+
// outcome names the outcome of an action: its type, and either ok or its error, with "at depth"
// when the path that failed is below the top level.
func outcome(action commitAction, err error) string {
diff --git a/internal/gitaly/service/operations/commit_files_scenarios_test.go b/internal/gitaly/service/operations/commit_files_scenarios_test.go
new file mode 100644
index 0000000..248d049
--- /dev/null
+++ b/internal/gitaly/service/operations/commit_files_scenarios_test.go
@@ -0,0 +1,179 @@
+package operations
+
+import (
+ "context"
+ "sort"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/featureflag"
+ "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/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/proto/go/gitalypb"
+)
+
+// commitFiles sends one UserCommitFiles request with header and requests, and returns its result.
+func commitFiles(t *testing.T, ctx context.Context, client gitalypb.OperationServiceClient, header *gitalypb.UserCommitFilesRequest, requests ...*gitalypb.UserCommitFilesRequest) (*gitalypb.UserCommitFilesResponse, error) {
+ t.Helper()
+
+ stream, err := client.UserCommitFiles(ctx)
+ require.NoError(t, err)
+ require.NoError(t, stream.Send(header))
+ for _, request := range requests {
+ require.NoError(t, stream.Send(request))
+ }
+ return stream.CloseAndRecv()
+}
+
+// lsTree lists the tree of commit recursively as `<mode> <path>` lines.
+func lsTree(t *testing.T, cfg config.Cfg, repoPath, commit string) string {
+ t.Helper()
+ return string(gittest.Exec(t, cfg, "-C", repoPath, "ls-tree", "-r", "--format=%(objectmode) %(path)", commit))
+}
+
+// TestUserCommitFilesScenarios runs requests whose actions depend on each other, which the
+// differential test cannot see: its two trees share the semantics of the actions.
+func TestUserCommitFilesScenarios(t *testing.T) {
+ t.Parallel()
+
+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
+ ctx, cfg, client := setupOperationsService(t, ctx)
+
+ setup := func(t *testing.T) (*gitalypb.Repository, string) {
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "a.txt", Mode: "100644", Content: "a\n"}))
+ return repo, repoPath
+ }
+
+ t.Run("a move, then a chmod of the moved file", func(t *testing.T) {
+ repo, repoPath := setup(t)
+ resp, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ moveFileHeaderRequest("a.txt", "b.txt", true),
+ chmodFileHeaderRequest("b.txt", true))
+ require.NoError(t, err)
+ require.Equal(t, "100755 b.txt\n", lsTree(t, cfg, repoPath, resp.GetBranchUpdate().GetCommitId()))
+ })
+
+ t.Run("a move, then an update of the moved file", func(t *testing.T) {
+ repo, repoPath := setup(t)
+ resp, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ moveFileHeaderRequest("a.txt", "b.txt", true),
+ updateFileHeaderRequest("b.txt"), actionContentRequest("updated\n"))
+ require.NoError(t, err)
+ commitID := resp.GetBranchUpdate().GetCommitId()
+ require.Equal(t, "100644 b.txt\n", lsTree(t, cfg, repoPath, commitID))
+ require.Equal(t, "updated\n", string(gittest.Exec(t, cfg, "-C", repoPath, "show", commitID+":b.txt")))
+ })
+
+ t.Run("a move, then a create onto the moved path", func(t *testing.T) {
+ // The moved file exists: the create is refused, where it used to replace it silently.
+ repo, _ := setup(t)
+ _, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ moveFileHeaderRequest("a.txt", "b.txt", true),
+ createFileHeaderRequest("b.txt"), actionContentRequest("created\n"))
+ testhelper.RequireGrpcError(t, structuredIndexError(t, &indexError{path: "b.txt", errorType: errFileExists}), err)
+ })
+
+ t.Run("a file below a file names the file in the way", func(t *testing.T) {
+ repo, _ := setup(t)
+ _, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ createFileHeaderRequest("a.txt/b/c.txt"), actionContentRequest("c\n"))
+ testhelper.RequireGrpcError(t, structuredIndexError(t, &indexError{path: "a.txt", errorType: errFileExists}), err)
+ })
+
+ t.Run("a chmod of a directory", func(t *testing.T) {
+ // Refused whatever the requested mode, also to clear the executable bit: a directory
+ // has no executable mode (PLAN9 §15.3).
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "dir", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath,
+ []gittest.TreeEntry{{Path: "f", Mode: "100644", Content: "f\n"}})}))
+ for _, executable := range []bool{true, false} {
+ _, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ chmodFileHeaderRequest("dir", executable))
+ testhelper.RequireGrpcError(t, structuredIndexError(t, &indexError{path: "dir", errorType: errFileNotFound}), err)
+ }
+ })
+}
+
+// TestUserCommitFilesLegacyModes commits into a directory that holds entries with modes git no
+// longer writes (100664, 100600, 120777): the directory is written back with their canonical
+// modes, as before WP96, instead of a tree that hash-object refuses.
+func TestUserCommitFilesLegacyModes(t *testing.T) {
+ t.Parallel()
+
+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
+ ctx, cfg, client := setupOperationsService(t, ctx)
+
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ blob := gittest.WriteBlob(t, cfg, repoPath, []byte("legacy\n"))
+ legacy := map[string]string{"group-writable": "100664", "private": "100600", "link": "120777"}
+ var entries []*localrepo.TreeEntry
+ for name, mode := range legacy {
+ canonical := "100644"
+ if mode == "120777" {
+ canonical = "120000"
+ }
+ entries = append(entries, &localrepo.TreeEntry{Path: name, Mode: canonical, Type: localrepo.Blob, OID: blob})
+ }
+ dir := writeLiteralTree(t, cfg, repoPath, entries, func(entry *localrepo.TreeEntry) string { return legacy[entry.Path] })
+ root := writeLiteralTree(t, cfg, repoPath, []*localrepo.TreeEntry{{Path: "dir", Mode: "040000", Type: localrepo.Tree, OID: dir}},
+ func(*localrepo.TreeEntry) string { return "40000" })
+ parent := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTree(root), gittest.WithBranch("master"))
+ // The raw tree holds the legacy mode (cat-file -p would print it canonical).
+ require.Contains(t, string(gittest.Exec(t, cfg, "-C", repoPath, "cat-file", "tree", dir.String())), "100664 group-writable")
+
+ resp, err := commitFiles(t, ctx, client, headerRequest(repo, gittest.TestUser, "master", []byte("message"), "", ""),
+ createFileHeaderRequest("dir/new"), actionContentRequest("new\n"))
+ require.NoError(t, err)
+
+ commitID := resp.GetBranchUpdate().GetCommitId()
+ require.Equal(t, parent.String(), strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "log", "-1", "--format=%P", commitID))))
+ listed := strings.Split(strings.TrimSuffix(lsTree(t, cfg, repoPath, commitID), "\n"), "\n")
+ sort.Strings(listed)
+ require.Equal(t, []string{
+ "100644 dir/group-writable",
+ "100644 dir/new",
+ "100644 dir/private",
+ "120000 dir/link",
+ }, listed)
+ // The rewritten directory holds the canonical modes.
+ rewritten := string(gittest.Exec(t, cfg, "-C", repoPath, "cat-file", "tree", commitID+":dir"))
+ require.Contains(t, rewritten, "100644 group-writable")
+ require.NotContains(t, rewritten, "100664")
+ require.NotContains(t, rewritten, "120777")
+}
+
+// TestUserCommitFilesFetchesNestedTrees takes the parent from a start repository whose commit is
+// missing locally: the commit and its nested trees are fetched into the quarantine after the
+// request's object reader started, and the lazy tree reads them through it.
+func TestUserCommitFilesFetchesNestedTrees(t *testing.T) {
+ t.Parallel()
+
+ ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
+ ctx, cfg, client := setupOperationsService(t, ctx)
+
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))
+
+ startRepo, startRepoPath := gittest.CreateRepository(t, ctx, cfg)
+ inner := gittest.WriteTree(t, cfg, startRepoPath, []gittest.TreeEntry{{Path: "c.txt", Mode: "100644", Content: "c\n"}})
+ middle := gittest.WriteTree(t, cfg, startRepoPath, []gittest.TreeEntry{{Path: "b", Mode: "040000", OID: inner}})
+ startCommit := gittest.WriteCommit(t, cfg, startRepoPath, gittest.WithBranch("main"),
+ gittest.WithTreeEntries(gittest.TreeEntry{Path: "a", Mode: "040000", OID: middle}))
+
+ header := headerRequest(repo, gittest.TestUser, "new", []byte("message"), "main", "")
+ setStartRepository(header, startRepo)
+ resp, err := commitFiles(t, ctx, client, header, createFileHeaderRequest("a/b/new.txt"), actionContentRequest("new\n"))
+ require.NoError(t, err)
+
+ commitID := resp.GetBranchUpdate().GetCommitId()
+ require.Equal(t, startCommit.String(), strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "log", "-1", "--format=%P", commitID))))
+ require.Equal(t, "100644 a/b/c.txt\n100644 a/b/new.txt\n", lsTree(t, cfg, repoPath, commitID))
+ require.Equal(t, commitID, strings.TrimSpace(string(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/new"))))
+}