Skip to content

Git designed for AI agents.

gitai land: fixes of the re-review of 54189d1 (c1)

Federico Elia38d8bb1cd93e
diff --git a/cmd/graft/conflicts.go b/cmd/graft/conflicts.go
index d5bca6b..1af9fb9 100644
--- a/cmd/graft/conflicts.go
+++ b/cmd/graft/conflicts.go
@@ -10,6 +10,8 @@ import (
"os"
"path"
"path/filepath"
+ "runtime"
+ "strconv"
"strings"
"google.golang.org/protobuf/encoding/protojson"
@@ -77,12 +79,14 @@ func printConflict(out io.Writer, c *gitalypb.LandConflict) {
// conflictPath returns the file under sub of the conflicts directory for a conflicting path. It
// refuses a path that would leave the directory or that this system cannot hold as a file name
-// under it: an absolute or unclean path, a backslash or a colon (a separator or a drive or stream
-// on Windows), a NUL, and any path filepath.IsLocal rejects once in the system's form (such as a
-// reserved name like NUL or COM1 on Windows).
+// under it: an absolute or unclean path, a backslash (a separator on Windows, so a path that names
+// two different files on two systems), a NUL, on Windows a colon (a drive or a stream), and any
+// path filepath.IsLocal rejects once in the system's form (such as a reserved name like NUL or
+// COM1 on Windows).
func conflictPath(dir, sub, p string) (string, error) {
local := filepath.FromSlash(p)
- if p == "" || path.IsAbs(p) || path.Clean(p) != p || strings.ContainsAny(p, "\\:\x00") || !filepath.IsLocal(local) {
+ if p == "" || path.IsAbs(p) || path.Clean(p) != p || strings.ContainsAny(p, "\\\x00") ||
+ (runtime.GOOS == "windows" && strings.Contains(p, ":")) || !filepath.IsLocal(local) {
return "", fmt.Errorf("conflicting path %q cannot be written into the conflicts directory on this system", p)
}
return filepath.Join(dir, sub, local), nil
@@ -104,25 +108,8 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
}
blobs, missing := fetchSides(ctx, ws, c)
- written := map[string]string{}
- create := func(target, p string, data []byte) error {
- if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
- return fmt.Errorf("%s: %w", p, err)
- }
- f, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
- if errors.Is(err, fs.ErrExist) {
- return fmt.Errorf("conflicting paths %q and %q are the same file on this file system (names that differ only in case or Unicode normalization): resolve this conflict on a case-sensitive file system", written[target], p)
- }
- if err != nil {
- return fmt.Errorf("%s: %w", p, err)
- }
- written[target] = p
- if _, err := f.Write(data); err != nil {
- _ = f.Close()
- return fmt.Errorf("%s: %w", p, err)
- }
- return f.Close()
- }
+ w := conflictsWriter{written: map[string]string{}}
+ create := w.create
for _, f := range c.GetFiles() {
p := string(f.GetPath())
@@ -160,16 +147,7 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
}
content := conflictFileContent(contents)
if !readable {
- // Without every side there is no three-way merge: the file to edit is a side that
- // can be read, or a conflict the agent must resolve.
- switch {
- case contents[1] != nil:
- content = contents[1]
- case contents[2] != nil:
- content = contents[2]
- default:
- content = []byte(conflictsUnreadable)
- }
+ content = conflictFileWithout(f, contents)
}
if err := create(target, p, content); err != nil {
return err
@@ -183,6 +161,85 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.La
return create(filepath.Join(dir, conflictsPackage), conflictsPackage, append(raw, '\n'))
}
+// conflictsWriter creates the files of a conflicts directory, each once.
+type conflictsWriter struct {
+ // written maps each file created to its conflicting path, by the target folded to lower case,
+ // so that a collision on a case-insensitive file system names the path that took the file.
+ written map[string]string
+}
+
+// create writes a new file target for the conflicting path p. A target that exists already is
+// another conflicting path that this file system takes for the same file, which is an error.
+func (w conflictsWriter) create(target, p string, data []byte) error {
+ if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
+ return fmt.Errorf("%s: %w", p, err)
+ }
+ f, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
+ if errors.Is(err, fs.ErrExist) {
+ other, ok := w.written[strings.ToLower(target)]
+ if !ok {
+ other = "another conflicting path"
+ } else {
+ other = strconv.Quote(other)
+ }
+ return fmt.Errorf("conflicting paths %s and %q are the same file on this file system (names that differ only in case or Unicode normalization): resolve this conflict on a case-sensitive file system", other, p)
+ }
+ if err != nil {
+ return fmt.Errorf("%s: %w", p, err)
+ }
+ w.written[strings.ToLower(target)] = p
+ if _, err := f.Write(data); err != nil {
+ _ = f.Close()
+ return fmt.Errorf("%s: %w", p, err)
+ }
+ return f.Close()
+}
+
+// conflictFileWithout is the file to edit when a present side cannot be read. Without the base it
+// is the two-way merge of the sides. Without ours or theirs it is a conflict the marker rule of the
+// server refuses until the agent resolves it: the side that can be read (named by its file under
+// sides/ when it is binary) against a note that the other is unavailable. A side is never dropped
+// silently.
+func conflictFileWithout(f *gitalypb.LandConflictFile, sides [3][]byte) []byte {
+ ours, theirs := sides[1], sides[2]
+ oursPresent, theirsPresent := f.GetOurs() != nil, f.GetTheirs() != nil
+ switch {
+ case ours != nil && theirs != nil:
+ return conflictFileContent([3][]byte{nil, ours, theirs})
+ case ours == nil && theirs == nil:
+ return []byte(conflictsUnreadable)
+ }
+ const unavailable = "unavailable, see conflict.json"
+ body := func(name string, b []byte) []byte {
+ if isBinaryContent(b) {
+ return []byte(fmt.Sprintf("(binary: see sides/%s/%s)\n", name, string(f.GetPath())))
+ }
+ if len(b) > 0 && b[len(b)-1] != '\n' {
+ return append(append([]byte{}, b...), '\n')
+ }
+ return b
+ }
+ var out bytes.Buffer
+ switch {
+ case ours == nil && oursPresent:
+ out.WriteString("<<<<<<< ours (the branch tip): " + unavailable + "\n=======\n")
+ out.Write(body("theirs", theirs))
+ out.WriteString(">>>>>>> theirs (the change)\n")
+ case theirs == nil && theirsPresent:
+ out.WriteString("<<<<<<< ours (the branch tip)\n")
+ out.Write(body("ours", ours))
+ out.WriteString("=======\n>>>>>>> theirs (the change): " + unavailable + "\n")
+ default:
+ // The side that is missing is absent (a deletion), and the one there is cannot be read
+ // apart from the base: the side there is, as for any deletion conflict.
+ if ours != nil {
+ return ours
+ }
+ return theirs
+ }
+ return out.Bytes()
+}
+
// fetchSides reads the sides of the package that have an object ID, all at once. When the server
// cannot return them all, it reads them one by one, so that a side it cannot return costs only that
// side: missing gives the error of each side that could not be read.
diff --git a/cmd/graft/conflicts_server_test.go b/cmd/graft/conflicts_server_test.go
index eb3cf48..066b26e 100644
--- a/cmd/graft/conflicts_server_test.go
+++ b/cmd/graft/conflicts_server_test.go
@@ -177,7 +177,16 @@ func TestCLILandConflictsWithAnUnavailableSide(t *testing.T) {
require.Equal(t, "added by theirs\n", string(theirs))
edit, err := os.ReadFile(filepath.Join(conflicts, "files", "c.txt"))
require.NoError(t, err)
- require.Equal(t, "added by theirs\n", string(edit), "without every side the file to edit is the side there is")
+ require.Equal(t, "<<<<<<< ours (the branch tip): unavailable, see conflict.json\n=======\nadded by theirs\n>>>>>>> theirs (the change)\n", string(edit),
+ "the side there is against a note on the other, between markers")
+
+ // Landed unedited, the file is refused by the marker rule: the tip's side is never dropped.
+ code, _, stderr = runCLI(t, "land", "-C", dir, "-author", "Dev <[email protected]>", "-resolved", conflicts)
+ require.Equal(t, exitError, code)
+ require.Contains(t, stderr, "merge conflict")
+ code, stdout, _ = runCLI(t, "cat", "-C", dir, "main:c.txt")
+ require.Equal(t, exitOK, code)
+ require.Equal(t, lines(map[int]string{19: "nineteen-ours"}), stdout, "the tip is unchanged")
resolved := lines(map[int]string{2: "two-theirs", 19: "nineteen-ours"}) + "added by theirs\n"
require.NoError(t, os.WriteFile(filepath.Join(conflicts, "files", "c.txt"), []byte(resolved), 0o644))
diff --git a/cmd/graft/conflicts_test.go b/cmd/graft/conflicts_test.go
index 303f59c..a245f86 100644
--- a/cmd/graft/conflicts_test.go
+++ b/cmd/graft/conflicts_test.go
@@ -4,6 +4,8 @@ import (
"context"
"os"
"path/filepath"
+ "runtime"
+ "strings"
"testing"
"github.com/stretchr/testify/require"
@@ -21,12 +23,22 @@ func TestConflictPath(t *testing.T) {
for _, p := range []string{
"", "/etc/passwd", "../x", "a/../../x", "a//b", "a/./b", "a/",
- // A separator, a drive or a stream on Windows, whatever the system the CLI runs on.
- `a\b`, `..\x`, "c:x", "a:stream", "a\x00b",
+ // A separator on Windows, whatever the system the CLI runs on.
+ `a\b`, `..\x`, "a\x00b",
} {
_, err := conflictPath(dir, conflictsFilesDir, p)
require.Error(t, err, "%q", p)
}
+
+ // A colon is a drive or a stream on Windows only.
+ for _, p := range []string{"c:x", "a:stream"} {
+ _, err := conflictPath(dir, conflictsFilesDir, p)
+ if runtime.GOOS == "windows" {
+ require.Error(t, err, "%q", p)
+ } else {
+ require.NoError(t, err, "%q", p)
+ }
+ }
}
// TestWriteConflictsRefusesAnExistingDirectory: the files of an earlier package, which the agent
@@ -39,3 +51,43 @@ func TestWriteConflictsRefusesAnExistingDirectory(t *testing.T) {
err := writeConflicts(context.Background(), nil, &gitalypb.LandConflict{}, dir, nil)
require.ErrorContains(t, err, "already holds a conflicts directory")
}
+
+// TestConflictsWriterNamesBothPathsOfACollision: on a file system that folds case, the second of
+// two paths finds the first one's file. Here the folding is simulated by a target that exists under
+// the folded name; the error names both paths.
+func TestConflictsWriterNamesBothPathsOfACollision(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ w := conflictsWriter{written: map[string]string{}}
+ require.NoError(t, w.create(filepath.Join(dir, "readme.md"), "readme.md", []byte("a")))
+ // A case-insensitive file system would resolve README.md to readme.md.
+ w.written[strings.ToLower(filepath.Join(dir, "readme.md"))] = "README.md"
+ err := w.create(filepath.Join(dir, "readme.md"), "readme.md", []byte("b"))
+ require.ErrorContains(t, err, `conflicting paths "README.md" and "readme.md" are the same file`)
+}
+
+// TestConflictFileWithoutASide: a side that cannot be read is never dropped silently.
+func TestConflictFileWithoutASide(t *testing.T) {
+ t.Parallel()
+
+ side := &gitalypb.LandConflictSide{Mode: "100644"}
+ file := &gitalypb.LandConflictFile{Path: []byte("c.txt"), Base: side, Ours: side, Theirs: side}
+
+ // Without the base: the two-way merge of the sides, with markers where they differ.
+ got := string(conflictFileWithout(file, [3][]byte{nil, []byte("a\nours\n"), []byte("a\ntheirs\n")}))
+ require.Contains(t, got, "<<<<<<< ours (the branch tip)\n")
+ require.Contains(t, got, "ours\n")
+ require.Contains(t, got, "theirs\n")
+
+ // Without ours: the side there is against a note, between markers.
+ got = string(conflictFileWithout(file, [3][]byte{[]byte("b\n"), nil, []byte("added by theirs")}))
+ require.Equal(t, "<<<<<<< ours (the branch tip): unavailable, see conflict.json\n=======\nadded by theirs\n>>>>>>> theirs (the change)\n", got)
+
+ // Without theirs, a binary ours is named by its side file rather than embedded.
+ got = string(conflictFileWithout(file, [3][]byte{[]byte("b\n"), []byte("\x00bin"), nil}))
+ require.Equal(t, "<<<<<<< ours (the branch tip)\n(binary: see sides/ours/c.txt)\n=======\n>>>>>>> theirs (the change): unavailable, see conflict.json\n", got)
+
+ // Without either: a conflict to resolve.
+ require.Equal(t, conflictsUnreadable, string(conflictFileWithout(file, [3][]byte{})))
+}
diff --git a/doc/graft/PLAN10.md b/doc/graft/PLAN10.md
index 5bb3218..ed467ae 100644
--- a/doc/graft/PLAN10.md
+++ b/doc/graft/PLAN10.md
@@ -568,3 +568,17 @@ way.
paths it cannot write on its system and paths the file system folds together, and
never overwrites a conflicts directory; the memory backend refuses a resolution for
another commit.
+* **Re-review of 54189d1** (PM: fix before R1).
+ * The re-land of an agent is bounded: a merge with more than 1000 conflicting files is refused
+ before anything is read, and its check goes in requests of at most 32 MiB of file data each,
+ checked one after the other (resolver.MaxCheckRequestBytes = 64 MiB encoded, which the client
+ also enforces), so that the server and graft-resolve hold one bounded request at a time.
+ * The CLI never drops a side: without the base the file to edit is the two-way merge; without
+ ours or theirs it is a conflict between markers (the side there is against a note), which the
+ marker rule refuses until the agent resolves it.
+ * A file with an unavailable side gets no hunks; the tree of every merge base counts as kept (a
+ criss-cross base side that is a real blob keeps its OID); a gitlink side is unavailable.
+ * Minors: the collision error names both paths; the colon is refused on Windows only; the NTFS
+ names follow git's rules, the hashed 8.3 forms included; only the last paragraph of a message
+ is its trailer block, and the server's trailers are always that paragraph (documented,
+ tested).
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index c9e48ea..3a97eca 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -80,12 +80,15 @@ A client asks for resolution per call:
directory mirrors the tree of the merge, so no repository path can collide
with a side, another path's side or the package. A side the package gives no
object ID (see `unavailable` below), or that the server cannot return, is
- left out and reported; it fails neither its file nor the directory. The
- file to edit is then the side there is, or, when no side can be read, a
- conflict that the marker rule refuses until it is resolved. `DIR` must not
+ left out and reported; it fails neither its file nor the directory, and it is
+ never dropped silently. Without the base, the file to edit is the two-way
+ merge of the sides, with markers where they differ. Without ours or theirs,
+ it is a conflict between markers: the side there is (named by its file under
+ `sides/` when it is binary) against a note that the other is unavailable, so
+ that the marker rule refuses it until the agent resolves it. `DIR` must not
hold a conflicts directory yet: one being edited is never overwritten. A
- path that cannot be a file under `DIR` on this system (a backslash, a
- colon, a name the system reserves) is an error, and so are two paths that
+ path that cannot be a file under `DIR` on this system (a backslash, on
+ Windows a colon or a name the system reserves) is an error, and so are two paths that
the file system takes for one file (names that differ only in case on a
case-insensitive file system). Edit the files under `DIR/files`, then
`graft land --resolved DIR` lands them: a present file is written (with the
@@ -235,8 +238,11 @@ The server accepts an answer only if all of these hold:
`REJECTED`, and the land fails with the package.
- No symbolic link is named `.gitmodules`, `.gitattributes`, `.gitignore` or
`.mailmap` in any directory, as git fsck and checkout require, nor any name
- a file system takes for one of them (other case, the code points HFS+
- ignores, NTFS trailing dots and spaces, streams and 8.3 short names).
+ a file system takes for one of them, by git's rules: other case and the
+ code points HFS+ ignores; on NTFS trailing dots and spaces, the 8.3 short
+ name (`GITMOD~1` to `~4`) and the hashed short names (such as `gi7eb~12`).
+ A name with an NTFS stream (`:stream`) is cut there first, which refuses
+ more than git does.
- Every content is at most `max_file_bytes` after decoding.
- Text files contain no conflict marker lines. A marker line is one that
starts with `<<<<<<< ` or `>>>>>>> `, or one that is `=======` followed by
@@ -354,11 +360,13 @@ the error unchanged. The package holds:
`CONFLICT (rename/delete)`, the text), which also describe a conflict that
has no conflicting file.
-A side has a blob OID only when the blob is in the tree of the merge base,
-the tip or the landed commit, which the repository keeps. A side that the
-merge wrote for the conflict alone (such as the content merge of a renamed
-file) is discarded with the failed land, so it has no OID and `unavailable`
-says why; its hunks are still given.
+A side has a blob OID only when the blob is in the tree of a merge base, the
+tip or the landed commit, which the repository keeps. A side that the merge
+wrote for the conflict alone (such as the content merge of a renamed file, or
+the virtual base of a criss-cross history) is discarded with the failed land,
+so it has no OID and `unavailable` says why, as does a submodule side (a
+gitlink names a commit, not a blob). A file with an unavailable side has no
+hunks (`hunks_omitted` says so): they would name lines the agent cannot read.
Each hunk gives line ranges of each side, 1-based and half-open (`[start,
end)`), each counting the lines of the side it names. An empty range is an
@@ -394,6 +402,9 @@ content) or `delete`. The server accepts it only when:
- every file passes the acceptance rules of an answer (modes, size, conflict
markers, no symbolic link named like a file git reads from the tree), and
writes a mode one of the conflict's sides has;
+- the merge has at most 1000 conflicting files, the bound of a package (a
+ merge with more cannot be resolved through a land, and nothing is read or
+ checked);
- every conflicting path, and the actor, can be written in a commit trailer:
one line of printable UTF-8 (otherwise `FailedPrecondition`; such a path is
never sent to a resolver either, so its conflict cannot be resolved through
@@ -401,6 +412,12 @@ content) or `delete`. The server accepts it only when:
- when a resolver is configured, the files that may be sent to it pass its
`check`: the syntax check and the configured checks, as an `llm` answer.
A check that cannot run fails the land, with the reason in the package.
+ The files go to the check in requests of at most 32 MiB of file data each
+ (about 43 MiB encoded, within the 64 MiB a check request may take and the
+ 128 MiB graft-resolve reads by default), checked one after the other, so
+ that neither side holds more than one such request at a time. A single file
+ larger than that is a request of its own: raise graft-resolve's
+ `--max-request-bytes` with `max_file_bytes`.
A file with a `syntax: unchecked` verdict lands, and is counted in
`graft_land_resolution_syntax_unchecked_total`, once, when the land
succeeds, and in the server log.
@@ -412,6 +429,12 @@ is no `Graft-Checked-By`: the resolution got only the conflict-marker rule and
the server's structural validation. The resolution travels in the request, so
the whole request is bounded by the 4 MiB gRPC message limit.
+Only the last paragraph of a commit message is its trailer block (git's rule,
+which `graft show`, `graft-verify` and superfan follow), and the server always
+writes the `Graft-*` trailers as that paragraph. Lines of a caller's
+`merge_message` that look like `Graft-*` trailers are part of an earlier
+paragraph, so they never count.
+
## Strategies of graft-resolve
`graft-resolve` tries an ordered list of strategies per file. The first
diff --git a/internal/gitaly/service/operations/land_package.go b/internal/gitaly/service/operations/land_package.go
index 5c011a8..f285234 100644
--- a/internal/gitaly/service/operations/land_package.go
+++ b/internal/gitaly/service/operations/land_package.go
@@ -45,14 +45,19 @@ const (
landMaxPackageCells = 64 << 20
)
+// landModeGitlink is the mode of a submodule entry.
+const landModeGitlink = "160000"
+
// Reasons of hunks_omitted and of an unavailable side.
const (
- landHunksTooLarge = "a side is larger than max_file_bytes, so no diff is computed"
- landHunksBinary = "a side is binary, which has no line hunks"
- landHunksDiffCost = "the diff of the sides is over the cost bound of one file"
- landHunksBudget = "the package's bound on the sides it reads and diffs is used up"
- landHunksPackage = "the package would be over 4 MiB with its hunks"
- landSideUnavailable = "the merge wrote this blob for the conflict (such as the content merge of a rename), and the repository does not keep it after the land fails"
+ landHunksTooLarge = "a side is larger than max_file_bytes, so no diff is computed"
+ landHunksBinary = "a side is binary, which has no line hunks"
+ landHunksDiffCost = "the diff of the sides is over the cost bound of one file"
+ landHunksBudget = "the package's bound on the sides it reads and diffs is used up"
+ landHunksPackage = "the package would be over 4 MiB with its hunks"
+ landHunksUnavailable = "a side is unavailable, so the hunks would name lines the agent cannot read"
+ landSideGitlink = "the side is a submodule (a gitlink), which names a commit, not a blob"
+ landSideUnavailable = "the merge wrote this blob for the conflict (such as the content merge of a rename), and the repository does not keep it after the land fails"
)
// Reasons of the files the server does not send to the resolver, besides the content rules of
@@ -81,6 +86,8 @@ type landFileState struct {
// hunks are the file's hunks in the package, and hunksOmitted why they are absent or cut.
hunks []*gitalypb.LandConflictHunk
hunksOmitted string
+ // unavailable is set when a present side's blob is not kept in the repository.
+ unavailable bool
}
// sent reports whether the file is sent to the resolver.
@@ -206,20 +213,18 @@ func (l landListing) conflictPackage(ctx context.Context, reason string) (*gital
return pkg, nil
}
- if err := l.prepareHunks(ctx); err != nil {
- return nil, err
- }
+ // The sides first: a file with a side the agent cannot read gets no hunks.
listings := map[git.ObjectID]map[git.ObjectID][]string{}
- for _, f := range l.files {
+ files := make([]*gitalypb.LandConflictFile, len(l.files))
+ for i, f := range l.files {
pf := &gitalypb.LandConflictFile{
Path: []byte(f.conflict.path),
Kind: landKindProto(f.kind),
Outcome: f.outcome,
Reason: landCutString(f.reason, landPackageReasonBytes),
SyntaxUnchecked: f.syntaxUnchecked,
- Hunks: f.hunks,
- HunksOmitted: f.hunksOmitted,
}
+ f.unavailable = false
for _, s := range []struct {
blob *landBlob
rev git.ObjectID
@@ -228,20 +233,35 @@ func (l landListing) conflictPackage(ctx context.Context, reason string) (*gital
if s.blob == nil {
continue
}
- kept, err := l.sideKept(ctx, listings, s.blob.oid, f.conflict.path, s.rev)
- if err != nil {
- return nil, err
- }
side := &gitalypb.LandConflictSide{Mode: s.blob.mode}
- if kept {
- side.Oid = s.blob.oid.String()
- } else {
- side.Unavailable = landSideUnavailable
+ switch {
+ case s.blob.mode == landModeGitlink:
+ side.Unavailable = landSideGitlink
+ default:
+ kept, err := l.sideKept(ctx, listings, s.blob.oid, f.conflict.path, s.rev)
+ if err != nil {
+ return nil, err
+ }
+ if kept {
+ side.Oid = s.blob.oid.String()
+ } else {
+ side.Unavailable = landSideUnavailable
+ }
+ }
+ if side.Unavailable != "" {
+ f.unavailable = true
}
*s.dst = side
}
- pkg.Files = append(pkg.Files, pf)
+ files[i] = pf
}
+ if err := l.prepareHunks(ctx); err != nil {
+ return nil, err
+ }
+ for i, f := range l.files {
+ files[i].Hunks, files[i].HunksOmitted = f.hunks, f.hunksOmitted
+ }
+ pkg.Files = files
if proto.Size(pkg) > landMaxPackageBytes {
for _, pf := range pkg.GetFiles() {
@@ -269,6 +289,10 @@ func (l landListing) prepareHunks(ctx context.Context) error {
if f.kind != resolver.KindContent && f.kind != resolver.KindAddAdd {
continue
}
+ if f.unavailable {
+ f.hunksOmitted = landHunksUnavailable
+ continue
+ }
if !f.read {
reason, err := l.readSides(ctx, f, &readLeft)
if err != nil {
@@ -356,16 +380,14 @@ func (l landListing) sideKept(ctx context.Context, listings map[git.ObjectID]map
info, err := l.repo.ReadObjectInfo(ctx, git.Revision(own.String()+":"+p))
var notFound localrepo.InvalidObjectError
switch {
- case err == nil && info.Oid == blob:
+ case err == nil && info.Oid == blob && info.Type == "blob":
return true, nil
case err != nil && !errors.As(err, &notFound):
return false, fmt.Errorf("looking up %q in %s: %w", p, own, err)
}
}
- revs := []git.ObjectID{l.tip, l.commitID}
- if l.hasBase {
- revs = append(revs, l.base)
- }
+ // Every merge base is a kept tree: with a criss-cross history the base side may come from any.
+ revs := append([]git.ObjectID{l.tip, l.commitID}, l.mergeBases...)
for _, rev := range revs {
listing, ok := listings[rev]
if !ok {
diff --git a/internal/gitaly/service/operations/land_package_test.go b/internal/gitaly/service/operations/land_package_test.go
index db7d0c5..9ee5664 100644
--- a/internal/gitaly/service/operations/land_package_test.go
+++ b/internal/gitaly/service/operations/land_package_test.go
@@ -343,6 +343,68 @@ func TestUserLand_reland(t *testing.T) {
require.Len(t, resolverRequest(t, dir, 1).Resolution, 2)
})
+ t.Run("the check goes in bounded requests, one batch at a time", func(t *testing.T) {
+ t.Parallel()
+
+ // A bound of one byte: every file is a check request of its own, answered in turn.
+ dir, argv := resolverScript(t, `cp "$DIR/answer-$N.json" "$DIR/answer.json"`)
+ for n, p := range []string{"a", "b"} {
+ raw, err := json.Marshal(resolver.CheckResponse{
+ Version: resolver.ProtocolVersion, Operation: resolver.OperationCheck, Resolver: "graft-resolve",
+ Files: []resolver.CheckFile{{Path: p, OK: true, Syntax: resolver.SyntaxUnchecked}},
+ })
+ require.NoError(t, err)
+ require.NoError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("answer-%d.json", n+1)), raw, 0o600))
+ }
+ fx := newResolveFixture(t, config.GraftResolver{})
+ s := fx.server(t, enabledResolver(argv))
+ s.checkBatchBytes = 1
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+
+ res, err := s.landWithResolution(fx.ctx, req, fx.repo, fx.tip, fx.commit, fx.committer())
+ require.NoError(t, err)
+ require.Equal(t, 2, resolverCalls(t, dir))
+ for n, p := range []string{"a", "b"} {
+ check := resolverRequest(t, dir, n+1)
+ require.Len(t, check.Files, 1)
+ require.Equal(t, p, check.Files[0].Path)
+ }
+ require.Equal(t, 2, res.syntaxUnchecked, "the verdicts of every batch count")
+ commit, err := fx.repo.ReadCommit(fx.ctx, res.commit.Revision())
+ require.NoError(t, err)
+ require.Contains(t, string(commit.GetBody()), "Graft-Checked-By: graft-resolve\n")
+ })
+
+ t.Run("a merge with more conflicting files than a package lists is refused", func(t *testing.T) {
+ t.Parallel()
+
+ dir, argv := resolverScript(t, "exit 1")
+ var base, tip, commit []gittest.TreeEntry
+ for i := 0; i <= landMaxPackageFiles; i++ {
+ p := fmt.Sprintf("f%04d", i)
+ base = append(base, gittest.TreeEntry{Mode: "100644", Path: p, Content: "base\n"})
+ tip = append(tip, gittest.TreeEntry{Mode: "100644", Path: p, Content: "ours\n"})
+ commit = append(commit, gittest.TreeEntry{Mode: "100644", Path: p, Content: "theirs\n"})
+ }
+ fx := newResolveFixtureWith(t, enabledResolver(argv), base, tip, commit)
+ req := fx.request("Land")
+ req.ResolveConflicts = false
+ req.Resolution = &gitalypb.LandResolution{Tip: fx.tip.String(), Commit: fx.commit.String(), Files: []*gitalypb.LandResolvedFile{
+ {Path: []byte("f0000"), Action: gitalypb.LandResolvedFile_ACTION_DELETE},
+ }}
+
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ require.Empty(t, pkg.GetFiles())
+ require.Contains(t, pkg.GetReason(), fmt.Sprintf("more than the %d that a conflict package lists", landMaxPackageFiles))
+ require.Equal(t, 0, resolverCalls(t, dir), "nothing is read or checked")
+ requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, fx.tip)
+ })
+
t.Run("a written mode that no side has is rejected", func(t *testing.T) {
t.Parallel()
@@ -580,11 +642,50 @@ func TestUserLand_conflictPackageSidesAreKept(t *testing.T) {
require.Equal(t, landSideUnavailable, c.GetOurs().GetUnavailable())
require.Equal(t, "100644", c.GetOurs().GetMode())
require.NotEmpty(t, c.GetTheirs().GetOid())
- // The hunks were computed from the side while the request could still read it.
- require.NotEmpty(t, c.GetHunks())
+ // No hunks: they would give line ranges of a side the agent cannot read.
+ require.Empty(t, c.GetHunks())
+ require.Equal(t, landHunksUnavailable, c.GetHunksOmitted())
requirePackageOIDsKept(t, fx, pkg)
})
+ t.Run("a submodule side is not a blob", func(t *testing.T) {
+ t.Parallel()
+
+ // The tip turns s into a submodule, the change edits it: the tip's side is a gitlink,
+ // which names a commit of another repository.
+ fx := newResolveFixture(t, config.GraftResolver{})
+ submodule := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "x", Content: "x\n"}))
+ baseTree := []gittest.TreeEntry{{Mode: "100644", Path: "s", Content: "base\n"}}
+ base := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithTreeEntries(baseTree...))
+ tip := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(base), gittest.WithBranch(git.DefaultBranch),
+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "160000", Path: "s", OID: submodule}))
+ commit := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(base),
+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "s", Content: "theirs\n"}))
+ fx.base, fx.tip, fx.commit = base, tip, commit
+
+ req := fx.request("Land")
+ req.Strategy = gitalypb.UserLandRequest_STRATEGY_MERGE
+ _, err := fx.client.UserLand(fx.ctx, req)
+ requireMergeConflict(t, err)
+ pkg := landConflictDetail(err)
+ require.NotNil(t, pkg)
+ // git records the distinct types on two paths: s holds the submodule, s~<label> the file.
+ s := packageFile(t, pkg, "s")
+ require.Equal(t, "160000", s.GetOurs().GetMode())
+ require.Empty(t, s.GetOurs().GetOid())
+ require.Equal(t, landSideGitlink, s.GetOurs().GetUnavailable())
+ require.Nil(t, s.GetTheirs())
+ var file *gitalypb.LandConflictFile
+ for _, f := range pkg.GetFiles() {
+ if strings.HasPrefix(string(f.GetPath()), "s~") {
+ file = f
+ }
+ }
+ require.NotNil(t, file, "the file side under its new name")
+ require.NotEmpty(t, file.GetBase().GetOid(), "a blob found under another name in a kept tree is kept")
+ require.NotEmpty(t, file.GetTheirs().GetOid())
+ })
+
t.Run("a criss-cross history", func(t *testing.T) {
t.Parallel()
@@ -592,16 +693,18 @@ func TestUserLand_conflictPackageSidesAreKept(t *testing.T) {
dir, argv := resolverScript(t, "")
writeAnswer(t, dir, validAnswer())
fx := newResolveFixture(t, enabledResolver(argv))
- // Two branches merge each other: tip and commit have two merge bases, x and y, and both
- // change a.
+ // Two branches merge each other: tip and commit have two merge bases, x and y. They agree
+ // on a, which tip and commit change differently, so the base side of a is x's (and y's)
+ // blob, a tree the repository keeps; they differ on b, which tip and commit both keep as
+ // "xy", so b does not conflict.
x := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base), gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "x\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "mid\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "x\n"}))
y := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(fx.base), gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "y\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "mid\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "y\n"}))
tip := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(x, y), gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "tip\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "tip\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "xy\n"}))
commit := gittest.WriteCommit(t, fx.cfg, fx.repoPath, gittest.WithParents(y, x), gittest.WithTreeEntries(
- gittest.TreeEntry{Mode: "100644", Path: "a", Content: "commit\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "base\n"}))
+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "commit\n"}, gittest.TreeEntry{Mode: "100644", Path: "b", Content: "xy\n"}))
fx.tip, fx.commit = tip, commit
req := fx.request("Land")
@@ -612,6 +715,8 @@ func TestUserLand_conflictPackageSidesAreKept(t *testing.T) {
require.NotNil(t, pkg)
require.Contains(t, pkg.GetReason(), "2 merge bases")
require.Len(t, pkg.GetFiles(), 1)
+ a := packageFile(t, pkg, "a")
+ require.NotEmpty(t, a.GetBase().GetOid(), "a base side in the trees of the merge bases is kept")
requirePackageOIDsKept(t, fx, pkg)
require.Equal(t, 0, resolverCalls(t, dir))
requireBranchAt(t, fx.cfg, fx.repoPath, git.DefaultBranch, tip)
diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.go
index 0211f33..27ce705 100644
--- a/internal/gitaly/service/operations/land_reland.go
+++ b/internal/gitaly/service/operations/land_reland.go
@@ -66,6 +66,11 @@ func (s *Server) landWithResolution(
if len(l.files) == 0 {
return landResolution{}, l.notAttempted(landNoConflictingFiles, nil)
}
+ // A merge with more conflicting files than a package lists cannot be resolved through a land,
+ // whatever the resolution: its package says so, and the land refuses it before reading anything.
+ if len(l.files) > landMaxPackageFiles {
+ return landResolution{}, l.notAttempted(fmt.Sprintf("the merge has %d conflicting files, more than the %d that a conflict package lists: it cannot be resolved through a land", len(l.files), landMaxPackageFiles), nil)
+ }
if r.GetTip() != tip.String() {
return landResolution{}, l.notAttempted(fmt.Sprintf("the branch tip moved from %s to %s since the resolution was made: resolve the conflicts of this package", r.GetTip(), tip), nil)
}
@@ -175,11 +180,25 @@ func landResolutionFiles(r *gitalypb.LandResolution, files []*landFileState) (ma
return resolved, ""
}
+// landCheckBatchBytes bounds the file data (the sides and the resolution) of one check request:
+// half of resolver.MaxCheckRequestBytes, since base64 and JSON make the request about 4/3 of it.
+// A test may set a smaller bound on the server (checkBatchBytes).
+func (s *Server) landCheckBatchBytes() int64 {
+ if s.checkBatchBytes > 0 {
+ return s.checkBatchBytes
+ }
+ return resolver.MaxCheckRequestBytes / 2
+}
+
// landCheckResolution runs the resolver's checks on the files of the resolution that may be sent
// to the resolver (landClassify), and sets the outcome of each checked file that fails them to
// REJECTED. It returns the checker's name for the Graft-Checked-By trailer, or "" when no file
-// could be sent, and the number of files accepted without a syntax verdict. The check fails
-// closed: a check that cannot run is a *landUnresolvedError with a fresh package.
+// could be sent, and the number of files accepted without a syntax verdict.
+//
+// The files go in check requests of at most landCheckBatchBytes of file data each, so that
+// neither the server nor graft-resolve holds more than one bounded request at a time: each batch
+// is read, checked and released before the next. Every batch must name the same checker. The
+// check fails closed: a check that cannot run is a *landUnresolvedError with a fresh package.
func (s *Server) landCheckResolution(
ctx context.Context,
in *gitalypb.UserLandRequest,
@@ -188,68 +207,125 @@ func (s *Server) landCheckResolution(
actor string,
resolved map[string]resolver.ResponseFile,
) (string, int, error) {
- if err := s.landClassify(ctx, repo, l.files, l.base, l.tip, l.commitID); err != nil {
+ batches, err := landCheckBatches(ctx, repo, l.files, resolved, s.landCheckBatchBytes())
+ if err != nil {
return "", 0, err
}
- req := resolver.Request{
- Repository: resolver.Repository{
- Storage: in.GetRepository().GetStorageName(),
- RelativePath: in.GetRepository().GetRelativePath(),
- },
- Branch: string(in.GetBranch()),
- Tip: l.tip.String(),
- Commit: l.commitID.String(),
- Messages: resolver.Messages{Tip: l.tipMessage, Commit: l.commitMessage},
- Actor: actor,
- }
- if l.hasBase {
- req.MergeBase = l.base.String()
- }
- var checked []*landFileState
+ checkedBy := ""
+ syntaxUnchecked, checkedFiles := 0, 0
var unchecked []string
- for _, f := range l.files {
- if !f.sent() {
- // The file may not go to the resolver: it gets the server's rules only.
- unchecked = append(unchecked, fmt.Sprintf("%s (%s)", f.conflict.path, f.reason))
- f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_UNSPECIFIED, ""
+ for _, batch := range batches {
+ if err := s.landClassify(ctx, repo, batch, l.base, l.tip, l.commitID); err != nil {
+ return "", 0, err
+ }
+ req := resolver.Request{
+ Repository: resolver.Repository{
+ Storage: in.GetRepository().GetStorageName(),
+ RelativePath: in.GetRepository().GetRelativePath(),
+ },
+ Branch: string(in.GetBranch()),
+ Tip: l.tip.String(),
+ Commit: l.commitID.String(),
+ Messages: resolver.Messages{Tip: l.tipMessage, Commit: l.commitMessage},
+ Actor: actor,
+ }
+ if l.hasBase {
+ req.MergeBase = l.base.String()
+ }
+ var checked []*landFileState
+ for _, f := range batch {
+ if !f.sent() {
+ // The file may not go to the resolver: it gets the server's rules only.
+ unchecked = append(unchecked, fmt.Sprintf("%s (%s)", f.conflict.path, f.reason))
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_UNSPECIFIED, ""
+ continue
+ }
+ req.Files = append(req.Files, landRequestFile(f))
+ req.Resolution = append(req.Resolution, resolved[f.conflict.path])
+ checked = append(checked, f)
+ }
+ // The sides of the batch are released once it is checked: the next batch reads its own, and
+ // a package reads again, within its own bounds, what its hunks need.
+ release := func() {
+ for _, f := range batch {
+ f.sides, f.read = [3][]byte{}, false
+ }
+ }
+ if len(checked) == 0 {
+ release()
continue
}
- req.Files = append(req.Files, landRequestFile(f))
- req.Resolution = append(req.Resolution, resolved[f.conflict.path])
- checked = append(checked, f)
- }
- if len(checked) == 0 {
- s.logger.WithField("unchecked", unchecked).InfoContext(ctx, "graft: no file of the agent's resolution may be sent to the resolver's checks")
- return "", 0, nil
- }
- // Fail closed: a check that cannot run (an error, an answer that is no valid check response, a
- // resolver that does not know the check operation) fails the land with a fresh package.
- resp, err := s.conflictResolver.Check(ctx, req)
- if err != nil {
- return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: %v", err), err)
- }
- if !landResolverNameValid(resp.Resolver) {
- return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: the check names an invalid resolver %q", resp.Resolver), nil)
- }
- syntaxUnchecked := 0
- for i, v := range resp.Files {
- f := checked[i]
- if !v.OK {
- f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_REJECTED, v.Reason
- continue
+ // Fail closed: a check that cannot run (an error, an answer that is no valid check
+ // response, a resolver that does not know the check operation) fails the land.
+ resp, err := s.conflictResolver.Check(ctx, req)
+ release()
+ if err != nil {
+ return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: %v", err), err)
+ }
+ if !landResolverNameValid(resp.Resolver) {
+ return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: the check names an invalid resolver %q", resp.Resolver), nil)
}
- if v.Syntax == resolver.SyntaxUnchecked {
- f.syntaxUnchecked = true
- syntaxUnchecked++
+ if checkedBy != "" && resp.Resolver != checkedBy {
+ return "", 0, l.notAttempted(fmt.Sprintf("the resolution could not be checked: its checks named the resolvers %q and %q", checkedBy, resp.Resolver), nil)
}
+ checkedBy = resp.Resolver
+ for i, v := range resp.Files {
+ f := checked[i]
+ if !v.OK {
+ f.outcome, f.reason = gitalypb.LandConflictFile_OUTCOME_REJECTED, v.Reason
+ continue
+ }
+ if v.Syntax == resolver.SyntaxUnchecked {
+ f.syntaxUnchecked = true
+ syntaxUnchecked++
+ }
+ }
+ checkedFiles += len(checked)
+ }
+ if checkedFiles == 0 {
+ s.logger.WithField("unchecked", unchecked).InfoContext(ctx, "graft: no file of the agent's resolution may be sent to the resolver's checks")
+ return "", 0, nil
}
s.logger.WithFields(log.Fields{
- "checked_by": resp.Resolver,
- "checked": len(checked),
+ "checked_by": checkedBy,
+ "checked": checkedFiles,
+ "check_requests": len(batches),
"syntax_unchecked": syntaxUnchecked,
"not_checked": unchecked,
}).InfoContext(ctx, "graft: agent's resolution checked")
- return resp.Resolver, syntaxUnchecked, nil
+ return checkedBy, syntaxUnchecked, nil
+}
+
+// landCheckBatches splits the files into batches of at most budget bytes of file data: the sizes
+// of their sides (from the object info, before reading) and of their resolution. A file larger than
+// the budget is a batch of its own; its sides are within max_file_bytes, or it is not sent.
+func landCheckBatches(ctx context.Context, repo *localrepo.Repo, files []*landFileState, resolved map[string]resolver.ResponseFile, budget int64) ([][]*landFileState, error) {
+ var batches [][]*landFileState
+ var cur []*landFileState
+ var size int64
+ for _, f := range files {
+ n := int64(len(resolved[f.conflict.path].Content))
+ for _, b := range []*landBlob{f.conflict.base, f.conflict.ours, f.conflict.theirs} {
+ if b == nil || (b.mode != resolver.ModeRegular && b.mode != resolver.ModeExecutable && b.mode != resolver.ModeSymlink) {
+ continue
+ }
+ info, err := repo.ReadObjectInfo(ctx, b.oid.Revision())
+ if err != nil {
+ return nil, fmt.Errorf("reading size of %q: %w", f.conflict.path, err)
+ }
+ n += info.Size
+ }
+ if len(cur) > 0 && size+n > budget {
+ batches = append(batches, cur)
+ cur, size = nil, 0
+ }
+ cur = append(cur, f)
+ size += n
+ }
+ if len(cur) > 0 {
+ batches = append(batches, cur)
+ }
+ return batches, nil
}
diff --git a/internal/gitaly/service/operations/land_resolve.go b/internal/gitaly/service/operations/land_resolve.go
index bd106cd..201f026 100644
--- a/internal/gitaly/service/operations/land_resolve.go
+++ b/internal/gitaly/service/operations/land_resolve.go
@@ -209,7 +209,9 @@ type landListing struct {
tip, commitID git.ObjectID
base git.ObjectID
hasBase bool
- // bases is the number of merge bases; more than one is a criss-cross history.
+ // mergeBases are all the merge bases, and bases their number; more than one is a criss-cross
+ // history.
+ mergeBases []git.ObjectID
bases int
mergedTree git.ObjectID
conflicts []landFileConflict
@@ -234,9 +236,10 @@ var errLandNoConflicts = errors.New("the merge of the tip and the change is clea
func landListConflicts(ctx context.Context, repo *localrepo.Repo, tip, commitID git.ObjectID, maxFileBytes int64) (landListing, error) {
l := landListing{repo: repo, maxFileBytes: maxFileBytes, tip: tip, commitID: commitID}
var err error
- if l.base, l.hasBase, l.bases, err = landMergeBase(ctx, repo, tip, commitID); err != nil {
+ if l.base, l.hasBase, l.mergeBases, err = landMergeBase(ctx, repo, tip, commitID); err != nil {
return l, err
}
+ l.bases = len(l.mergeBases)
var clean bool
if l.mergedTree, l.conflicts, l.messages, clean, err = landMergeTree(ctx, repo, tip, commitID, l.base, l.hasBase); err != nil {
return l, err
@@ -710,6 +713,9 @@ func landListTree(ctx context.Context, repo *localrepo.Repo, rev git.ObjectID) (
if !ok || len(parts) != 3 {
return nil, fmt.Errorf("unexpected ls-tree record %q", record)
}
+ if parts[1] != "blob" {
+ continue
+ }
oid, err := objectHash.FromHex(parts[2])
if err != nil {
return nil, fmt.Errorf("parsing object ID of %q: %w", p, err)
@@ -758,7 +764,7 @@ func landCommitBody(ctx context.Context, repo *localrepo.Repo, commit git.Object
// history has several merge bases, whose number is returned: the base the resolver would be given
// would differ from the base of the recursive merge strategy, so such landings are not resolved,
// and hasBase is false.
-func landMergeBase(ctx context.Context, repo *localrepo.Repo, a, b git.ObjectID) (base git.ObjectID, hasBase bool, bases int, err error) {
+func landMergeBase(ctx context.Context, repo *localrepo.Repo, a, b git.ObjectID) (base git.ObjectID, hasBase bool, bases []git.ObjectID, err error) {
var stdout, stderr bytes.Buffer
if err := repo.ExecAndWait(ctx,
gitcmd.Command{Name: "merge-base", Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}}, Args: []string{a.String(), b.String()}},
@@ -766,25 +772,26 @@ func landMergeBase(ctx context.Context, repo *localrepo.Repo, a, b git.ObjectID)
gitcmd.WithStderr(&stderr),
); err != nil {
if exitStatus, ok := command.ExitStatus(err); ok && exitStatus == 1 {
- return "", false, 0, nil
+ return "", false, nil, nil
}
- return "", false, 0, fmt.Errorf("merge-base: %w, stderr: %q", err, stderr.String())
- }
-
- all := strings.Fields(stdout.String())
- if len(all) != 1 {
- return "", false, len(all), nil
+ return "", false, nil, fmt.Errorf("merge-base: %w, stderr: %q", err, stderr.String())
}
objectHash, err := repo.ObjectHash(ctx)
if err != nil {
- return "", false, 0, fmt.Errorf("detecting object hash: %w", err)
+ return "", false, nil, fmt.Errorf("detecting object hash: %w", err)
}
- base, err = objectHash.FromHex(all[0])
- if err != nil {
- return "", false, 0, fmt.Errorf("parsing merge base: %w", err)
+ for _, hex := range strings.Fields(stdout.String()) {
+ oid, err := objectHash.FromHex(hex)
+ if err != nil {
+ return "", false, nil, fmt.Errorf("parsing merge base: %w", err)
+ }
+ bases = append(bases, oid)
+ }
+ if len(bases) != 1 {
+ return "", false, bases, nil
}
- return base, true, 1, nil
+ return bases[0], true, bases, nil
}
// landMergeTree runs merge-tree of tip and commitID. It returns the tree of the merge, which holds
diff --git a/internal/gitaly/service/operations/land_resolve_test.go b/internal/gitaly/service/operations/land_resolve_test.go
index 2a91896..6797265 100644
--- a/internal/gitaly/service/operations/land_resolve_test.go
+++ b/internal/gitaly/service/operations/land_resolve_test.go
@@ -751,6 +751,16 @@ func TestLandResolveCommitMessage(t *testing.T) {
require.Equal(t, "Merge 1111111 into main\n\nGraft-Resolved-By: graft-resolve/x\nGraft-Resolved-Path: a\n", msg)
})
+ t.Run("trailers in the caller's message never form the trailer block", func(t *testing.T) {
+ // The trailer block is the last paragraph (git's rule, as graft show, graft-verify and
+ // superfan read it); the server's trailers are always that paragraph.
+ in := &gitalypb.UserLandRequest{Branch: []byte("main"), MergeMessage: []byte("Land\n\nGraft-Resolved-By: forged\nGraft-Checked-By: forged\n")}
+ msg, err := landResolveCommitMessage(in, commit, "agent/a", "", []string{"a"})
+ require.NoError(t, err)
+ block := msg[strings.LastIndex(strings.TrimRight(msg, "\n"), "\n\n")+2:]
+ require.Equal(t, "Graft-Resolved-By: agent/a\nGraft-Resolved-Path: a\n", block)
+ })
+
t.Run("a path that is no trailer line is refused", func(t *testing.T) {
in := &gitalypb.UserLandRequest{Branch: []byte("main")}
_, err := landResolveCommitMessage(in, commit, "graft-resolve/x", "", []string{lineBreakPath})
diff --git a/internal/gitaly/service/operations/server.go b/internal/gitaly/service/operations/server.go
index a932d85..aedae40 100644
--- a/internal/gitaly/service/operations/server.go
+++ b/internal/gitaly/service/operations/server.go
@@ -35,6 +35,9 @@ type Server struct {
localRepoFactory localrepo.Factory
// resolveCfg configures conflict resolution of UserLand.
resolveCfg config.GraftResolver
+ // checkBatchBytes, when set, replaces the bound of the file data of one check request
+ // (landCheckBatchBytes); only tests set it.
+ checkBatchBytes int64
// conflictResolver answers the resolve requests of UserLand. Nil when resolution is disabled.
conflictResolver *resolver.Client
// landLanes batches the UserLand requests of a branch. Nil disables batching, and every
diff --git a/internal/graft/resolver/check.go b/internal/graft/resolver/check.go
index b39f934..bd3e511 100644
--- a/internal/graft/resolver/check.go
+++ b/internal/graft/resolver/check.go
@@ -7,6 +7,11 @@ import (
"fmt"
)
+// MaxCheckRequestBytes bounds the encoded check request. graft-resolve reads 128 MiB by default
+// (protocol.DefaultMaxRequestBytes); the server splits an agent's resolution into check requests
+// within this bound.
+const MaxCheckRequestBytes = 64 << 20
+
// ErrCheckUnsupported is returned by Check when the resolver answers a check request with
// something other than a check response, as a resolver that predates the check operation does.
var ErrCheckUnsupported = errors.New("the resolver does not support the check operation")
@@ -41,6 +46,9 @@ func (c *Client) Check(ctx context.Context, req Request) (*CheckResponse, error)
if err != nil {
return nil, fmt.Errorf("encode check request: %w", err)
}
+ if len(body) > MaxCheckRequestBytes {
+ return nil, fmt.Errorf("check request of %d bytes is over the bound of %d", len(body), MaxCheckRequestBytes)
+ }
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
diff --git a/internal/graft/resolver/client_test.go b/internal/graft/resolver/client_test.go
index e525e9c..ee3be60 100644
--- a/internal/graft/resolver/client_test.go
+++ b/internal/graft/resolver/client_test.go
@@ -435,6 +435,25 @@ func TestCheck_runsForEveryActorAndFileCount(t *testing.T) {
require.ErrorContains(t, err, `actor "agent-7" may not request conflict resolution`)
}
+// TestCheck_refusesARequestOverItsBound: a check request is bounded in bytes whatever its files;
+// one over MaxCheckRequestBytes is refused without running the resolver (the server splits an
+// agent's resolution below it).
+func TestCheck_refusesARequestOverItsBound(t *testing.T) {
+ requireUnix(t)
+
+ marker := filepath.Join(t.TempDir(), "ran")
+ cfg := commandConfig([]string{writeScript(t, fmt.Sprintf("touch '%s'\ncat > /dev/null\n", marker))})
+ cfg.MaxFileBytes = MaxCheckRequestBytes
+ client := newClient(t, cfg)
+
+ req := sampleRequest()
+ req.Files[0].Ours.Content = make([]byte, MaxCheckRequestBytes*3/4+1)
+ req.Resolution = validResponse("merged\n").Files
+ _, err := client.Check(testhelper.Context(t), req)
+ require.ErrorContains(t, err, "is over the bound")
+ require.NoFileExists(t, marker)
+}
+
func TestResolve_RequestChecksDoNotRunTheResolver(t *testing.T) {
requireUnix(t)
diff --git a/internal/graft/resolver/validate.go b/internal/graft/resolver/validate.go
index ea9e8aa..4825e6d 100644
--- a/internal/graft/resolver/validate.go
+++ b/internal/graft/resolver/validate.go
@@ -82,7 +82,7 @@ func validateFile(f ResponseFile, maxBytes int64) error {
if f.Mode == ModeSymlink {
if special := gitSpecialFile(path.Base(f.Path)); special != "" {
- return fmt.Errorf("a symbolic link named %s is refused: git fsck and checkout do not allow %s, .gitmodules, .gitattributes, .gitignore and .mailmap to be symbolic links", path.Base(f.Path), special)
+ return fmt.Errorf("a symbolic link named %s is refused: it stands for %s, and git fsck and checkout do not allow .gitmodules, .gitattributes, .gitignore or .mailmap to be a symbolic link", path.Base(f.Path), special)
}
}
@@ -123,12 +123,14 @@ var gitSpecialFiles = []struct {
}
// gitSpecialFile returns the special file that the tree entry name stands for on some file
-// system, or "". git compares names the way the file systems that check out the tree do:
-// - case-insensitively, and ignoring the code points that HFS+ ignores (zero-width and
-// directional marks, U+FEFF);
-// - on NTFS, also with trailing spaces and dots, an alternate data stream (":stream"), and as
-// an 8.3 short name: the first six letters of the name, or the stem above, then "~" and a
-// digit.
+// system, or "". It follows the rules git uses for these names:
+// - HFS+: the name, ignoring the code points HFS+ ignores (zero-width and directional marks,
+// U+FEFF), case-insensitively;
+// - NTFS (ntfsSpecialName): the name followed by spaces and periods only, the 8.3 short name
+// (the first six letters, "~" and a digit 1 to 4), and the hashed short name (up to six
+// letters of the stem above, "~", a digit 1 to 9 and more digits, eight characters in all),
+// each followed by spaces and periods only. An alternate data stream (":stream") is cut
+// first, which refuses more than git does.
func gitSpecialFile(name string) string {
folded := strings.ToLower(strings.Map(func(r rune) rune {
if hfsIgnorable(r) {
@@ -140,18 +142,51 @@ func gitSpecialFile(name string) string {
if i := strings.IndexByte(ntfs, ':'); i >= 0 {
ntfs = ntfs[:i]
}
- ntfs = strings.TrimRight(ntfs, " .")
for _, s := range gitSpecialFiles {
- if folded == s.name || ntfs == s.name {
+ if folded == s.name || ntfsSpecialName(ntfs, s.name[1:], s.shortHash) {
return s.name
}
- for _, stem := range []string{s.name[1:7], s.shortHash} {
- if len(ntfs) == len(stem)+2 && strings.HasPrefix(ntfs, stem+"~") && ntfs[len(stem)+1] >= '1' && ntfs[len(stem)+1] <= '9' {
- return s.name
+ }
+ return ""
+}
+
+// ntfsSpecialName reports whether the lower-case name stands on NTFS for "." + dotName, whose
+// hashed 8.3 short name starts with shortHash (see gitSpecialFile).
+func ntfsSpecialName(name, dotName, shortHash string) bool {
+ if strings.HasPrefix(name, "."+dotName) {
+ return onlySpacesAndPeriods(name[1+len(dotName):])
+ }
+ if len(name) >= 8 && name[:6] == dotName[:6] && name[6] == '~' && name[7] >= '1' && name[7] <= '4' {
+ return onlySpacesAndPeriods(name[8:])
+ }
+ sawTilde := false
+ i := 0
+ for ; i < 8; i++ {
+ if i >= len(name) {
+ return false
+ }
+ c := name[i]
+ switch {
+ case sawTilde:
+ if c < '0' || c > '9' {
+ return false
+ }
+ case c == '~':
+ i++
+ if i >= len(name) || name[i] < '1' || name[i] > '9' {
+ return false
}
+ sawTilde = true
+ case i >= 6 || c != shortHash[i]:
+ return false
}
}
- return ""
+ return onlySpacesAndPeriods(name[i:])
+}
+
+// onlySpacesAndPeriods reports whether s holds nothing but spaces and periods.
+func onlySpacesAndPeriods(s string) bool {
+ return strings.Trim(s, " .") == ""
}
// hfsIgnorable reports whether HFS+ ignores r when it compares names.
diff --git a/internal/graft/resolver/validate_test.go b/internal/graft/resolver/validate_test.go
index b4a1c92..cba70cd 100644
--- a/internal/graft/resolver/validate_test.go
+++ b/internal/graft/resolver/validate_test.go
@@ -290,7 +290,9 @@ func TestValidateFile_specialFileSymlinks(t *testing.T) {
for _, name := range []string{
".gitmodules", "sub/.gitattributes", ".gitignore", ".mailmap",
".GITMODULES", ".gitmodules ", ".gitmodules...", ".gitmodules::$DATA",
- "GITMOD~1", "gi7eba~4", "GI7D29~1", "maba30~2", "gitign~1",
+ "GITMOD~1", "gi7eba~4", "GI7D29~1", "maba30~2", "gitign~1", "gitmod~4 .",
+ // The hashed 8.3 forms: fewer letters of the stem, more digits, eight characters in all.
+ "gi7eb~12", "gi7e~123", "GI7EBA~9", "mabA3~12.",
// HFS+ ignores zero-width code points.
".git\u200cmodules", "\ufeff.gitattributes",
} {
@@ -300,7 +302,11 @@ func TestValidateFile_specialFileSymlinks(t *testing.T) {
require.NoError(t, ValidateFile(ResponseFile{Path: name, Action: ActionWrite, Mode: ModeRegular, Content: []byte("x")}, 1<<20), "%q", name)
}
- for _, name := range []string{".gitmodules-old", "gitmodules", "GITMOD~A", "gitmod~10", "x.gitignore", "link"} {
+ for _, name := range []string{
+ ".gitmodules-old", "gitmodules", "GITMOD~A", "gitmod~10", "x.gitignore", "link",
+ // git's short name takes ~1 to ~4; a hashed form is eight characters with its digits.
+ "gitmod~5", "gi7eba~0", "gi7eb~1x", "gi7eba~12x", "gi7ebx~1", "maba3~1.",
+ } {
require.NoError(t, ValidateFile(ResponseFile{Path: name, Action: ActionWrite, Mode: ModeSymlink, Content: []byte("target")}, 1<<20), "%q", name)
}
}