package workspace import ( "context" "errors" "os" "os/exec" "path/filepath" "strings" "syscall" "testing" ) func taskGit(t *testing.T, dir string, args ...string) string { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com") out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v %s", args, err, out) } return strings.TrimSpace(string(out)) } // taskClone is a bare clone whose files ask for a filter, with hooks and // a hostile config of its own. func taskClone(t *testing.T) (string, string) { t.Helper() dir := t.TempDir() seed := filepath.Join(dir, "seed") taskGit(t, dir, "init", "-q", "-b", "main", seed) os.MkdirAll(filepath.Join(seed, "panes"), 0700) os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh panes\n"), 0600) os.WriteFile(filepath.Join(seed, "panes", "swipe.go"), []byte("package panes\n"), 0600) os.WriteFile(filepath.Join(seed, ".gitattributes"), []byte("* filter=evil diff=evil\n"), 0600) os.Symlink("README.md", filepath.Join(seed, "LINK")) taskGit(t, seed, "add", "-A") taskGit(t, seed, "commit", "-q", "-m", "seed") clone := filepath.Join(dir, "clone.git") taskGit(t, dir, "clone", "-q", "--bare", seed, clone) marker := filepath.Join(dir, "ran") taskGit(t, clone, "config", "filter.evil.clean", "touch "+marker) taskGit(t, clone, "config", "filter.evil.smudge", "touch "+marker) taskGit(t, clone, "config", "core.hooksPath", filepath.Join(dir, "hooks")) os.MkdirAll(filepath.Join(dir, "hooks"), 0700) os.WriteFile(filepath.Join(dir, "hooks", "post-checkout"), []byte("#!/bin/sh\ntouch "+marker+"\n"), 0700) return clone, taskGit(t, seed, "rev-parse", "HEAD") } func TestTaskTree(t *testing.T) { ctx := context.Background() clone, base := taskClone(t) root := t.TempDir() tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-typo", filepath.Join(root, "work"), filepath.Join(root, "private", "git")) if err != nil { t.Fatal(err) } if _, err := os.Lstat(filepath.Join(tree.Dir, ".git")); !errors.Is(err, os.ErrNotExist) { t.Fatalf("the working copy has a .git: %v", err) } if raw, _ := os.ReadFile(filepath.Join(tree.Dir, "README.md")); string(raw) != "teh panes\n" { t.Fatalf("checkout %q", raw) } if link, _ := os.Readlink(filepath.Join(tree.Dir, "LINK")); link != "README.md" { t.Fatalf("symlink %q", link) } if _, err := tree.Commit(ctx, "nothing", "Equipo", "equipo@example.com"); !errors.Is(err, ErrNoChanges) { t.Fatalf("no change: %v", err) } os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the panes\n"), 0600) os.WriteFile(filepath.Join(tree.Dir, "panes", "new.go"), []byte("package panes\n"), 0600) sha, err := tree.Commit(ctx, "Fix the typo\n\nTask tk1.", "Equipo Engineering", "equipo@example.com") if err != nil { t.Fatal(err) } if parent := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", sha+"^"); parent != base { t.Fatalf("parent %s", parent) } if head := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", "refs/heads/equipo/tk1-typo"); head != sha { t.Fatalf("branch %s", head) } files, err := tree.ChangedFiles(ctx, sha) if err != nil || strings.Join(files, ",") != "README.md,panes/new.go" { t.Fatalf("changed %v %v", files, err) } if author := taskGit(t, root, "--git-dir="+tree.GitDir, "log", "-1", "--format=%an <%ae>|%s", sha); author != "Equipo Engineering |Fix the typo" { t.Fatalf("author %q", author) } // Nothing the repository configured ran: no filter, no hook. if _, err := os.Stat(filepath.Join(filepath.Dir(clone), "ran")); !errors.Is(err, os.ErrNotExist) { t.Fatal("a filter or hook from the clone ran") } if err := tree.Remove(); err != nil { t.Fatal(err) } if _, err := os.Stat(tree.Dir); !errors.Is(err, os.ErrNotExist) { t.Fatal("tree left behind") } } func TestTaskTreeRefusesNestedGitAndSpecialFiles(t *testing.T) { ctx := context.Background() clone, base := taskClone(t) for name, plant := range map[string]func(dir string){ "nested .git dir": func(dir string) { os.MkdirAll(filepath.Join(dir, "panes", ".git"), 0700) }, "a .git file": func(dir string) { os.WriteFile(filepath.Join(dir, ".git"), []byte("gitdir: /etc\n"), 0600) }, "upper-case .GIT": func(dir string) { os.MkdirAll(filepath.Join(dir, "x", ".GIT"), 0700) }, "a named pipe": func(dir string) { syscall.Mkfifo(filepath.Join(dir, "pipe"), 0600) }, } { root := t.TempDir() tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-x", filepath.Join(root, "work"), filepath.Join(root, "git")) if err != nil { t.Fatal(err) } os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("changed\n"), 0600) plant(tree.Dir) if _, err := tree.Commit(ctx, "x", "Equipo", "e@example.com"); !errors.Is(err, ErrTreeRefused) { t.Fatalf("%s: %v", name, err) } } root := t.TempDir() if _, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, strings.Repeat("e", 40), "equipo/tk1-x", filepath.Join(root, "w"), filepath.Join(root, "g")); !errors.Is(err, ErrTreeRefused) { t.Fatalf("unknown base: %v", err) } } // TestTaskTreeFromShallowClone: the project clone may be shallow (the // context index fetches with depth 1). The private repository keeps the // clone's shallow boundary, so the candidate's ancestry and connectivity // check out without the history beyond it. func TestTaskTreeFromShallowClone(t *testing.T) { ctx := context.Background() dir := t.TempDir() seed := filepath.Join(dir, "seed") taskGit(t, dir, "init", "-q", "-b", "main", seed) for i, body := range []string{"one\n", "teh panes\n"} { os.WriteFile(filepath.Join(seed, "README.md"), []byte(body), 0600) taskGit(t, seed, "add", "-A") taskGit(t, seed, "commit", "-q", "-m", "c"+string(rune('0'+i))) } clone := filepath.Join(dir, "clone.git") taskGit(t, dir, "-c", "protocol.file.allow=always", "clone", "-q", "--bare", "--depth=1", "file://"+seed, clone) base := taskGit(t, seed, "rev-parse", "HEAD") tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-x", filepath.Join(dir, "work"), filepath.Join(dir, "git")) if err != nil { t.Fatal(err) } os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the panes\n"), 0600) sha, err := tree.Commit(ctx, "fix", "Equipo", "e@example.com") if err != nil { t.Fatal(err) } for _, args := range [][]string{{"merge-base", "--is-ancestor", base, sha}, {"fsck", "--no-reflogs", "--connectivity-only", sha}} { cmd := exec.Command("git", append([]string{"--git-dir=" + tree.GitDir}, args...)...) cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null") if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("%v: %v %s", args, err, out) } } } // TestTaskTreeRebase: a change made on an old base is put on top of the // default branch's new tip. A clean change applies as it is; a // conflicting one leaves git's markers in exactly the conflicted files, // and the runner's commit sits on the new tip. No filter from the // repository runs. func TestTaskTreeRebase(t *testing.T) { ctx := context.Background() clone, base := taskClone(t) dir := filepath.Dir(clone) seed := filepath.Join(dir, "seed") // The task's change on the old base, and the default branch moving on. taskGit(t, seed, "checkout", "-q", "-b", "task") os.WriteFile(filepath.Join(seed, "README.md"), []byte("the panes\n"), 0600) os.WriteFile(filepath.Join(seed, "panes", "edge.go"), []byte("package panes\n\nvar Edge = true\n"), 0600) taskGit(t, seed, "add", "-A") taskGit(t, seed, "commit", "-q", "-m", "task") change := taskGit(t, seed, "rev-parse", "HEAD") taskGit(t, seed, "checkout", "-q", "main") os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh sliding panes\n"), 0600) os.WriteFile(filepath.Join(seed, "NEWS"), []byte("news\n"), 0600) taskGit(t, seed, "add", "-A") taskGit(t, seed, "commit", "-q", "-m", "main moved") tip := taskGit(t, seed, "rev-parse", "HEAD") taskGit(t, clone, "fetch", "-q", seed, "+refs/heads/*:refs/heads/*") root := t.TempDir() tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, tip, "equipo/tk1-typo", filepath.Join(root, "work"), filepath.Join(root, "private", "git")) if err != nil { t.Fatal(err) } conflicts, err := tree.Rebase(ctx, base, change) if err != nil || len(conflicts) != 1 || conflicts[0] != "README.md" { t.Fatalf("conflicts %v %v", conflicts, err) } readme, _ := os.ReadFile(filepath.Join(tree.Dir, "README.md")) edge, _ := os.ReadFile(filepath.Join(tree.Dir, "panes", "edge.go")) news, _ := os.ReadFile(filepath.Join(tree.Dir, "NEWS")) if !strings.Contains(string(readme), "<<<<<<<") || !strings.Contains(string(readme), "teh sliding panes") || !strings.Contains(string(readme), "the panes") || string(edge) == "" || string(news) != "news\n" { t.Fatalf("rebased tree: README %q edge %q NEWS %q", readme, edge, news) } if _, err := os.Lstat(filepath.Join(dir, "ran")); !errors.Is(err, os.ErrNotExist) { t.Fatal("a filter or hook from the repository ran") } // The specialist resolves it; the runner commits on the new tip. os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the sliding panes\n"), 0600) sha, err := tree.Commit(ctx, "rebased", "Equipo", "e@example.com") if err != nil { t.Fatal(err) } if parent := taskGit(t, tree.GitDir, "--git-dir="+tree.GitDir, "rev-parse", sha+"^"); parent != tip { t.Fatalf("parent %s, want the new tip %s", parent, tip) } files, _ := tree.ChangedFiles(ctx, sha) if strings.Join(files, ",") != "README.md,panes/edge.go" { t.Fatalf("changed %v", files) } // A change that does not conflict applies cleanly. clean, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, tip, "equipo/tk2-edge", filepath.Join(root, "work2"), filepath.Join(root, "private2", "git")) if err != nil { t.Fatal(err) } if conflicts, err := clean.Rebase(ctx, base, base); err != nil || len(conflicts) != 0 { t.Fatalf("nothing to rebase: %v %v", conflicts, err) } if _, err := clean.Rebase(ctx, base, "nothex"); !errors.Is(err, ErrTreeRefused) { t.Fatalf("bad commit: %v", err) } } // A repository with no commits has no base: the tree starts empty on the // task branch, unborn, and the first commit is the repository's initial // commit (no parent). Nothing to commit is still no change; the changed // files are everything the commit holds. func TestTaskTreeWithoutBase(t *testing.T) { ctx := context.Background() dir := t.TempDir() clone := filepath.Join(dir, "clone.git") taskGit(t, dir, "init", "-q", "--bare", "-b", "main", clone) root := t.TempDir() tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, "", "equipo/tk1-mockups", filepath.Join(root, "work"), filepath.Join(root, "private", "git")) if err != nil { t.Fatal(err) } if entries, err := os.ReadDir(tree.Dir); err != nil || len(entries) != 0 { t.Fatalf("the tree is not empty: %v %v", entries, err) } if head := taskGit(t, root, "--git-dir="+tree.GitDir, "symbolic-ref", "HEAD"); head != "refs/heads/equipo/tk1-mockups" { t.Fatalf("HEAD %s", head) } if _, err := tree.Commit(ctx, "nothing", "Equipo", "equipo@example.com"); !errors.Is(err, ErrNoChanges) { t.Fatalf("no change: %v", err) } os.MkdirAll(filepath.Join(tree.Dir, "book"), 0700) os.WriteFile(filepath.Join(tree.Dir, "index.html"), []byte("

Barber

\n"), 0600) os.WriteFile(filepath.Join(tree.Dir, "book", "index.html"), []byte("Back\n"), 0600) sha, err := tree.Commit(ctx, "Add the barber mockups", "Equipo Engineering", "equipo@example.com") if err != nil { t.Fatal(err) } if parents := taskGit(t, root, "--git-dir="+tree.GitDir, "log", "-1", "--format=%P", sha); parents != "" { t.Fatalf("the first commit has parents %q", parents) } if head := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", "refs/heads/equipo/tk1-mockups"); head != sha { t.Fatalf("branch %s", head) } files, err := tree.ChangedFiles(ctx, sha) if err != nil || strings.Join(files, ",") != "book/index.html,index.html" { t.Fatalf("changed %v %v", files, err) } // A base, when there is one, must still be a commit. if _, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, "main", "equipo/tk1-mockups", filepath.Join(root, "w2"), filepath.Join(root, "p2", "git")); !errors.Is(err, ErrTreeRefused) { t.Fatalf("a base that is not an object id: %v", err) } }