Git designed for AI agents.
graft land --conflicts-dir: disjoint namespaces for the files and the sides
Federico Elia5033352e8b45
diff --git a/cmd/graft/conflicts.go b/cmd/graft/conflicts.goindex 843f420..332c2bb 100644--- a/cmd/graft/conflicts.go+++ b/cmd/graft/conflicts.go@@ -21,20 +21,22 @@ import (// The conflicts directory of graft land --conflicts-dir DIR holds, for every conflicting path://-// DIR/<path> the file as the merge leaves it, with git's diff3 conflict-// markers around each conflict (or the tip's side when the-// file is not text, or a side is absent)-// DIR/.graft/sides/<path>.base the three sides-// DIR/.graft/sides/<path>.ours-// DIR/.graft/sides/<path>.theirs-// DIR/.graft/conflict.json the conflict package+// DIR/files/<path> the file as the merge leaves it, with git's diff3 conflict markers+// around each conflict (or the tip's side when the file is not text,+// or a side is absent)+// DIR/sides/<path>.base the three sides+// DIR/sides/<path>.ours+// DIR/sides/<path>.theirs+// DIR/conflict.json the conflict package//-// graft land --resolved DIR lands the files of DIR as the resolution: a path whose file is present-// is written, one whose file is absent is deleted.+// The files to edit and the sides live under separate directories, so that no repository path,+// whatever its name, can collide with a side or with the package. graft land --resolved DIR lands+// the files of DIR/files as the resolution: a path whose file is present is written, one whose+// file is absent is deleted.const (- conflictsMetaDir = ".graft"- conflictsPackage = "conflict.json"+ conflictsFilesDir = "files"conflictsSidesDir = "sides"+ conflictsPackage = "conflict.json")// printConflict describes a conflict package: the reason the server gave, and each file with its@@ -54,13 +56,13 @@ func printConflict(out io.Writer, c *gitalypb.LandConflict) {}}-// conflictPath returns the file of the conflicts directory for a conflicting path, refusing a-// path that would leave the directory.-func conflictPath(dir, p string) (string, error) {- if p == "" || path.IsAbs(p) || path.Clean(p) != p || p == ".." || strings.HasPrefix(p, "../") || p == conflictsMetaDir || strings.HasPrefix(p, conflictsMetaDir+"/") {+// conflictPath returns the file under sub of the conflicts directory for a conflicting path,+// refusing a path that would leave it.+func conflictPath(dir, sub, p string) (string, error) {+ if p == "" || path.IsAbs(p) || path.Clean(p) != p || p == ".." || strings.HasPrefix(p, "../") {return "", fmt.Errorf("conflicting path %q cannot be written into the conflicts directory", p)}- return filepath.Join(dir, filepath.FromSlash(p)), nil+ return filepath.Join(dir, sub, filepath.FromSlash(p)), nil}// writeConflicts writes the conflicts directory of a conflict package (see the layout above).@@ -93,7 +95,11 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.Lafor _, f := range c.GetFiles() {p := string(f.GetPath())- target, err := conflictPath(dir, p)+ target, err := conflictPath(dir, conflictsFilesDir, p)+ if err != nil {+ return err+ }+ sideBase, err := conflictPath(dir, conflictsSidesDir, p)if err != nil {return err}@@ -107,8 +113,7 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.Laif contents[i] == nil {continue}- sidePath := filepath.Join(dir, conflictsMetaDir, conflictsSidesDir, filepath.FromSlash(p)+"."+ext)- if err := writeFileAll(sidePath, contents[i]); err != nil {+ if err := writeFileAll(sideBase+"."+ext, contents[i]); err != nil {return err}}@@ -121,7 +126,7 @@ func writeConflicts(ctx context.Context, ws *workspace.Workspace, c *gitalypb.Laif err != nil {return err}- return writeFileAll(filepath.Join(dir, conflictsMetaDir, conflictsPackage), append(raw, '\n'))+ return writeFileAll(filepath.Join(dir, conflictsPackage), append(raw, '\n'))}// conflictFileContent is the file an agent edits: the merge with markers when both sides are text,@@ -152,21 +157,21 @@ func writeFileAll(p string, data []byte) error {}// readResolution reads the resolution of a conflicts directory: the package's tip and commit, and-// for every conflicting path its file, or a deletion when the file is absent. The mode is the+// for every conflicting path its file under DIR/files, or a deletion when the file is absent. The mode is the// tip's side's, else the landed commit's.func readResolution(dir string) (*workspace.Resolution, error) {- raw, err := os.ReadFile(filepath.Join(dir, conflictsMetaDir, conflictsPackage))+ raw, err := os.ReadFile(filepath.Join(dir, conflictsPackage))if err != nil {return nil, fmt.Errorf("reading the conflict package of %s (written by graft land --conflicts-dir): %w", dir, err)}var c gitalypb.LandConflictif err := protojson.Unmarshal(raw, &c); err != nil {- return nil, fmt.Errorf("%s: %w", filepath.Join(dir, conflictsMetaDir, conflictsPackage), err)+ return nil, fmt.Errorf("%s: %w", filepath.Join(dir, conflictsPackage), err)}res := &workspace.Resolution{Tip: c.GetTip(), Commit: c.GetCommit()}for _, f := range c.GetFiles() {p := string(f.GetPath())- target, err := conflictPath(dir, p)+ target, err := conflictPath(dir, conflictsFilesDir, p)if err != nil {return nil, err}diff --git a/cmd/graft/conflicts_server_test.go b/cmd/graft/conflicts_server_test.goindex 451c0e4..ebdc0a5 100644--- a/cmd/graft/conflicts_server_test.go+++ b/cmd/graft/conflicts_server_test.go@@ -40,18 +40,18 @@ func TestCLILandConflictsAndResolved(t *testing.T) {require.Contains(t, stdout, "graft land --resolved "+conflicts)// The file to edit holds the merge with git's diff3 markers; the sides are beside it.- marked, err := os.ReadFile(filepath.Join(conflicts, "x.txt"))+ marked, err := os.ReadFile(filepath.Join(conflicts, "files", "x.txt"))require.NoError(t, err)require.Equal(t, "1\n<<<<<<< ours (the branch tip)\ntip\n||||||| base\nbase\n=======\nlocal\n>>>>>>> theirs (the change)\n3\n", string(marked))for side, want := range map[string]string{"base": "1\nbase\n3\n", "ours": "1\ntip\n3\n", "theirs": "1\nlocal\n3\n"} {- got, err := os.ReadFile(filepath.Join(conflicts, ".graft", "sides", "x.txt."+side))+ got, err := os.ReadFile(filepath.Join(conflicts, "sides", "x.txt."+side))require.NoError(t, err)require.Equal(t, want, string(got), side)}- require.FileExists(t, filepath.Join(conflicts, ".graft", "conflict.json"))+ require.FileExists(t, filepath.Join(conflicts, "conflict.json"))// The agent resolves the file and lands the resolution.- require.NoError(t, os.WriteFile(filepath.Join(conflicts, "x.txt"), []byte("1\ntip and local\n3\n"), 0o644))+ require.NoError(t, os.WriteFile(filepath.Join(conflicts, "files", "x.txt"), []byte("1\ntip and local\n3\n"), 0o644))code, stdout, stderr = runCLI(t, "land", "-C", dir, "-author", "Dev <[email protected]>", "-resolved", conflicts)require.Equal(t, exitOK, code, stderr)require.Contains(t, stdout, "landed at ")@@ -74,6 +74,51 @@ func TestCLILandConflictsAndResolved(t *testing.T) {require.NotContains(t, shown.Trailers, "Graft-Checked-By", "no resolver checked it")}+// TestCLILandConflictsDirectoryNamespacesAreDisjoint: conflicting paths named like the package+// or like a side of another file are written and resolved like any other.+func TestCLILandConflictsDirectoryNamespacesAreDisjoint(t *testing.T) {+ env := startServer(t)+ names := []string{"conflict.json", "x.txt", "sides/x.txt.base", "files/x.txt"}+ seed := map[string]string{}+ for _, n := range names {+ seed[n] = "base\n"+ }+ base := env.seedMain(t, seed)++ dir := filepath.Join(t.TempDir(), "ws")+ code, _, stderr := runCLI(t, "clone", "-storage", env.storage, env.addr, env.relPath, dir)+ require.Equal(t, exitOK, code, stderr)++ remote := map[string]string{}+ for _, n := range names {+ remote[n] = "remote " + n + "\n"+ require.NoError(t, os.WriteFile(filepath.Join(dir, filepath.FromSlash(n)), []byte("local "+n+"\n"), 0o644))+ }+ gittest.WriteCommit(t, env.cfg, env.repoPath, gittest.WithTree(env.writeTree(t, remote)), gittest.WithParents(base), gittest.WithBranch("main"))+ code, _, stderr = runCLI(t, "commit", "-C", dir, "-author", "Dev <[email protected]>", "-m", "local")+ require.Equal(t, exitOK, code, stderr)++ conflicts := filepath.Join(t.TempDir(), "conflicts")+ code, stdout, _ := runCLI(t, "land", "-C", dir, "-author", "Dev <[email protected]>", "-resolve", "-conflicts-dir", conflicts)+ require.Equal(t, exitError, code, stdout)+ for _, n := range names {+ marked, err := os.ReadFile(filepath.Join(conflicts, "files", filepath.FromSlash(n)))+ require.NoError(t, err, n)+ require.Contains(t, string(marked), "local "+n, n)+ require.FileExists(t, filepath.Join(conflicts, "sides", filepath.FromSlash(n)+".theirs"), n)+ require.NoError(t, os.WriteFile(filepath.Join(conflicts, "files", filepath.FromSlash(n)), []byte("resolved "+n+"\n"), 0o644))+ }+ require.FileExists(t, filepath.Join(conflicts, "conflict.json"))++ code, _, stderr = runCLI(t, "land", "-C", dir, "-author", "Dev <[email protected]>", "-resolved", conflicts)+ require.Equal(t, exitOK, code, stderr)+ for _, n := range names {+ code, stdout, _ = runCLI(t, "cat", "-C", dir, "main:"+n)+ require.Equal(t, exitOK, code, n)+ require.Equal(t, "resolved "+n+"\n", stdout, n)+ }+}+func TestCLILandConflictFlagsAreChecked(t *testing.T) {dir := filepath.Join(t.TempDir(), "ws")code, _, stderr := runCLI(t, "land", "-C", dir, "-conflicts-dir", "x")diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex 5dd4e2f..182ccf8 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -66,12 +66,13 @@ A client asks for resolution per call:(4 MiB) before sending it.- CLI: `graft land --resolve`, `graft commit --land BRANCH --resolve`.`graft land --resolve` prints the package when the land fails, and with- `--conflicts-dir DIR` writes it: `DIR/<path>` is the merge with git's+ `--conflicts-dir DIR` writes it: `DIR/files/<path>` is the merge with git'sdiff3 markers (the clean changes of both sides applied), the sides are in- `DIR/.graft/sides/<path>.base|.ours|.theirs`, the package in- `DIR/.graft/conflict.json`. Edit the files, then `graft land --resolved DIR`- lands them: a present file is written (with the tip side's mode), an absent- one is deleted.+ `DIR/sides/<path>.base|.ours|.theirs`, the package in `DIR/conflict.json`.+ The files and the sides live in separate directories, so no repository+ path can collide with a side or the package. Edit the files under+ `DIR/files`, then `graft land --resolved DIR` lands them: a present file is+ written (with the tip side's mode), an absent one is deleted.- RPC: `UserLandRequest.resolve_conflicts` (field 10). The response carries`resolved_paths` (field 7) and `resolver` (field 8). The re-land is`UserLandRequest.resolution` (field 11).