oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package workspace
2
3import (
4 "context"
5 "errors"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "syscall"
11 "testing"
12)
13
14func taskGit(t *testing.T, dir string, args ...string) string {
15 t.Helper()
16 cmd := exec.Command("git", args...)
17 cmd.Dir = dir
18 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")
19 out, err := cmd.CombinedOutput()
20 if err != nil {
21 t.Fatalf("git %v: %v %s", args, err, out)
22 }
23 return strings.TrimSpace(string(out))
24}
25
26// taskClone is a bare clone whose files ask for a filter, with hooks and
27// a hostile config of its own.
28func taskClone(t *testing.T) (string, string) {
29 t.Helper()
30 dir := t.TempDir()
31 seed := filepath.Join(dir, "seed")
32 taskGit(t, dir, "init", "-q", "-b", "main", seed)
33 os.MkdirAll(filepath.Join(seed, "panes"), 0700)
34 os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh panes\n"), 0600)
35 os.WriteFile(filepath.Join(seed, "panes", "swipe.go"), []byte("package panes\n"), 0600)
36 os.WriteFile(filepath.Join(seed, ".gitattributes"), []byte("* filter=evil diff=evil\n"), 0600)
37 os.Symlink("README.md", filepath.Join(seed, "LINK"))
38 taskGit(t, seed, "add", "-A")
39 taskGit(t, seed, "commit", "-q", "-m", "seed")
40 clone := filepath.Join(dir, "clone.git")
41 taskGit(t, dir, "clone", "-q", "--bare", seed, clone)
42 marker := filepath.Join(dir, "ran")
43 taskGit(t, clone, "config", "filter.evil.clean", "touch "+marker)
44 taskGit(t, clone, "config", "filter.evil.smudge", "touch "+marker)
45 taskGit(t, clone, "config", "core.hooksPath", filepath.Join(dir, "hooks"))
46 os.MkdirAll(filepath.Join(dir, "hooks"), 0700)
47 os.WriteFile(filepath.Join(dir, "hooks", "post-checkout"), []byte("#!/bin/sh\ntouch "+marker+"\n"), 0700)
48 return clone, taskGit(t, seed, "rev-parse", "HEAD")
49}
50
51func TestTaskTree(t *testing.T) {
52 ctx := context.Background()
53 clone, base := taskClone(t)
54 root := t.TempDir()
55 tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-typo", filepath.Join(root, "work"), filepath.Join(root, "private", "git"))
56 if err != nil {
57 t.Fatal(err)
58 }
59 if _, err := os.Lstat(filepath.Join(tree.Dir, ".git")); !errors.Is(err, os.ErrNotExist) {
60 t.Fatalf("the working copy has a .git: %v", err)
61 }
62 if raw, _ := os.ReadFile(filepath.Join(tree.Dir, "README.md")); string(raw) != "teh panes\n" {
63 t.Fatalf("checkout %q", raw)
64 }
65 if link, _ := os.Readlink(filepath.Join(tree.Dir, "LINK")); link != "README.md" {
66 t.Fatalf("symlink %q", link)
67 }
68 if _, err := tree.Commit(ctx, "nothing", "Equipo", "equipo@example.com"); !errors.Is(err, ErrNoChanges) {
69 t.Fatalf("no change: %v", err)
70 }
71 os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the panes\n"), 0600)
72 os.WriteFile(filepath.Join(tree.Dir, "panes", "new.go"), []byte("package panes\n"), 0600)
73 sha, err := tree.Commit(ctx, "Fix the typo\n\nTask tk1.", "Equipo Engineering", "equipo@example.com")
74 if err != nil {
75 t.Fatal(err)
76 }
77 if parent := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", sha+"^"); parent != base {
78 t.Fatalf("parent %s", parent)
79 }
80 if head := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", "refs/heads/equipo/tk1-typo"); head != sha {
81 t.Fatalf("branch %s", head)
82 }
83 files, err := tree.ChangedFiles(ctx, sha)
84 if err != nil || strings.Join(files, ",") != "README.md,panes/new.go" {
85 t.Fatalf("changed %v %v", files, err)
86 }
87 if author := taskGit(t, root, "--git-dir="+tree.GitDir, "log", "-1", "--format=%an <%ae>|%s", sha); author != "Equipo Engineering <equipo@example.com>|Fix the typo" {
88 t.Fatalf("author %q", author)
89 }
90 // Nothing the repository configured ran: no filter, no hook.
91 if _, err := os.Stat(filepath.Join(filepath.Dir(clone), "ran")); !errors.Is(err, os.ErrNotExist) {
92 t.Fatal("a filter or hook from the clone ran")
93 }
94 if err := tree.Remove(); err != nil {
95 t.Fatal(err)
96 }
97 if _, err := os.Stat(tree.Dir); !errors.Is(err, os.ErrNotExist) {
98 t.Fatal("tree left behind")
99 }
100}
101
102func TestTaskTreeRefusesNestedGitAndSpecialFiles(t *testing.T) {
103 ctx := context.Background()
104 clone, base := taskClone(t)
105 for name, plant := range map[string]func(dir string){
106 "nested .git dir": func(dir string) { os.MkdirAll(filepath.Join(dir, "panes", ".git"), 0700) },
107 "a .git file": func(dir string) { os.WriteFile(filepath.Join(dir, ".git"), []byte("gitdir: /etc\n"), 0600) },
108 "upper-case .GIT": func(dir string) { os.MkdirAll(filepath.Join(dir, "x", ".GIT"), 0700) },
109 "a named pipe": func(dir string) { syscall.Mkfifo(filepath.Join(dir, "pipe"), 0600) },
110 } {
111 root := t.TempDir()
112 tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-x", filepath.Join(root, "work"), filepath.Join(root, "git"))
113 if err != nil {
114 t.Fatal(err)
115 }
116 os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("changed\n"), 0600)
117 plant(tree.Dir)
118 if _, err := tree.Commit(ctx, "x", "Equipo", "e@example.com"); !errors.Is(err, ErrTreeRefused) {
119 t.Fatalf("%s: %v", name, err)
120 }
121 }
122 root := t.TempDir()
123 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) {
124 t.Fatalf("unknown base: %v", err)
125 }
126}
127
128// TestTaskTreeFromShallowClone: the project clone may be shallow (the
129// context index fetches with depth 1). The private repository keeps the
130// clone's shallow boundary, so the candidate's ancestry and connectivity
131// check out without the history beyond it.
132func TestTaskTreeFromShallowClone(t *testing.T) {
133 ctx := context.Background()
134 dir := t.TempDir()
135 seed := filepath.Join(dir, "seed")
136 taskGit(t, dir, "init", "-q", "-b", "main", seed)
137 for i, body := range []string{"one\n", "teh panes\n"} {
138 os.WriteFile(filepath.Join(seed, "README.md"), []byte(body), 0600)
139 taskGit(t, seed, "add", "-A")
140 taskGit(t, seed, "commit", "-q", "-m", "c"+string(rune('0'+i)))
141 }
142 clone := filepath.Join(dir, "clone.git")
143 taskGit(t, dir, "-c", "protocol.file.allow=always", "clone", "-q", "--bare", "--depth=1", "file://"+seed, clone)
144 base := taskGit(t, seed, "rev-parse", "HEAD")
145 tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, base, "equipo/tk1-x", filepath.Join(dir, "work"), filepath.Join(dir, "git"))
146 if err != nil {
147 t.Fatal(err)
148 }
149 os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the panes\n"), 0600)
150 sha, err := tree.Commit(ctx, "fix", "Equipo", "e@example.com")
151 if err != nil {
152 t.Fatal(err)
153 }
154 for _, args := range [][]string{{"merge-base", "--is-ancestor", base, sha}, {"fsck", "--no-reflogs", "--connectivity-only", sha}} {
155 cmd := exec.Command("git", append([]string{"--git-dir=" + tree.GitDir}, args...)...)
156 cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null")
157 if out, err := cmd.CombinedOutput(); err != nil {
158 t.Fatalf("%v: %v %s", args, err, out)
159 }
160 }
161}
162
163// TestTaskTreeRebase: a change made on an old base is put on top of the
164// default branch's new tip. A clean change applies as it is; a
165// conflicting one leaves git's markers in exactly the conflicted files,
166// and the runner's commit sits on the new tip. No filter from the
167// repository runs.
168func TestTaskTreeRebase(t *testing.T) {
169 ctx := context.Background()
170 clone, base := taskClone(t)
171 dir := filepath.Dir(clone)
172 seed := filepath.Join(dir, "seed")
173 // The task's change on the old base, and the default branch moving on.
174 taskGit(t, seed, "checkout", "-q", "-b", "task")
175 os.WriteFile(filepath.Join(seed, "README.md"), []byte("the panes\n"), 0600)
176 os.WriteFile(filepath.Join(seed, "panes", "edge.go"), []byte("package panes\n\nvar Edge = true\n"), 0600)
177 taskGit(t, seed, "add", "-A")
178 taskGit(t, seed, "commit", "-q", "-m", "task")
179 change := taskGit(t, seed, "rev-parse", "HEAD")
180 taskGit(t, seed, "checkout", "-q", "main")
181 os.WriteFile(filepath.Join(seed, "README.md"), []byte("teh sliding panes\n"), 0600)
182 os.WriteFile(filepath.Join(seed, "NEWS"), []byte("news\n"), 0600)
183 taskGit(t, seed, "add", "-A")
184 taskGit(t, seed, "commit", "-q", "-m", "main moved")
185 tip := taskGit(t, seed, "rev-parse", "HEAD")
186 taskGit(t, clone, "fetch", "-q", seed, "+refs/heads/*:refs/heads/*")
187 root := t.TempDir()
188 tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, tip, "equipo/tk1-typo", filepath.Join(root, "work"), filepath.Join(root, "private", "git"))
189 if err != nil {
190 t.Fatal(err)
191 }
192 conflicts, err := tree.Rebase(ctx, base, change)
193 if err != nil || len(conflicts) != 1 || conflicts[0] != "README.md" {
194 t.Fatalf("conflicts %v %v", conflicts, err)
195 }
196 readme, _ := os.ReadFile(filepath.Join(tree.Dir, "README.md"))
197 edge, _ := os.ReadFile(filepath.Join(tree.Dir, "panes", "edge.go"))
198 news, _ := os.ReadFile(filepath.Join(tree.Dir, "NEWS"))
199 if !strings.Contains(string(readme), "<<<<<<<") || !strings.Contains(string(readme), "teh sliding panes") || !strings.Contains(string(readme), "the panes") || string(edge) == "" || string(news) != "news\n" {
200 t.Fatalf("rebased tree: README %q edge %q NEWS %q", readme, edge, news)
201 }
202 if _, err := os.Lstat(filepath.Join(dir, "ran")); !errors.Is(err, os.ErrNotExist) {
203 t.Fatal("a filter or hook from the repository ran")
204 }
205 // The specialist resolves it; the runner commits on the new tip.
206 os.WriteFile(filepath.Join(tree.Dir, "README.md"), []byte("the sliding panes\n"), 0600)
207 sha, err := tree.Commit(ctx, "rebased", "Equipo", "e@example.com")
208 if err != nil {
209 t.Fatal(err)
210 }
211 if parent := taskGit(t, tree.GitDir, "--git-dir="+tree.GitDir, "rev-parse", sha+"^"); parent != tip {
212 t.Fatalf("parent %s, want the new tip %s", parent, tip)
213 }
214 files, _ := tree.ChangedFiles(ctx, sha)
215 if strings.Join(files, ",") != "README.md,panes/edge.go" {
216 t.Fatalf("changed %v", files)
217 }
218 // A change that does not conflict applies cleanly.
219 clean, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, tip, "equipo/tk2-edge", filepath.Join(root, "work2"), filepath.Join(root, "private2", "git"))
220 if err != nil {
221 t.Fatal(err)
222 }
223 if conflicts, err := clean.Rebase(ctx, base, base); err != nil || len(conflicts) != 0 {
224 t.Fatalf("nothing to rebase: %v %v", conflicts, err)
225 }
226 if _, err := clean.Rebase(ctx, base, "nothex"); !errors.Is(err, ErrTreeRefused) {
227 t.Fatalf("bad commit: %v", err)
228 }
229}
230
231// A repository with no commits has no base: the tree starts empty on the
232// task branch, unborn, and the first commit is the repository's initial
233// commit (no parent). Nothing to commit is still no change; the changed
234// files are everything the commit holds.
235func TestTaskTreeWithoutBase(t *testing.T) {
236 ctx := context.Background()
237 dir := t.TempDir()
238 clone := filepath.Join(dir, "clone.git")
239 taskGit(t, dir, "init", "-q", "--bare", "-b", "main", clone)
240 root := t.TempDir()
241 tree, err := PrepareTaskTree(ctx, "/usr/bin/git", clone, "", "equipo/tk1-mockups", filepath.Join(root, "work"), filepath.Join(root, "private", "git"))
242 if err != nil {
243 t.Fatal(err)
244 }
245 if entries, err := os.ReadDir(tree.Dir); err != nil || len(entries) != 0 {
246 t.Fatalf("the tree is not empty: %v %v", entries, err)
247 }
248 if head := taskGit(t, root, "--git-dir="+tree.GitDir, "symbolic-ref", "HEAD"); head != "refs/heads/equipo/tk1-mockups" {
249 t.Fatalf("HEAD %s", head)
250 }
251 if _, err := tree.Commit(ctx, "nothing", "Equipo", "equipo@example.com"); !errors.Is(err, ErrNoChanges) {
252 t.Fatalf("no change: %v", err)
253 }
254 os.MkdirAll(filepath.Join(tree.Dir, "book"), 0700)
255 os.WriteFile(filepath.Join(tree.Dir, "index.html"), []byte("<h1>Barber</h1>\n"), 0600)
256 os.WriteFile(filepath.Join(tree.Dir, "book", "index.html"), []byte("<a href=../>Back</a>\n"), 0600)
257 sha, err := tree.Commit(ctx, "Add the barber mockups", "Equipo Engineering", "equipo@example.com")
258 if err != nil {
259 t.Fatal(err)
260 }
261 if parents := taskGit(t, root, "--git-dir="+tree.GitDir, "log", "-1", "--format=%P", sha); parents != "" {
262 t.Fatalf("the first commit has parents %q", parents)
263 }
264 if head := taskGit(t, root, "--git-dir="+tree.GitDir, "rev-parse", "refs/heads/equipo/tk1-mockups"); head != sha {
265 t.Fatalf("branch %s", head)
266 }
267 files, err := tree.ChangedFiles(ctx, sha)
268 if err != nil || strings.Join(files, ",") != "book/index.html,index.html" {
269 t.Fatalf("changed %v %v", files, err)
270 }
271 // A base, when there is one, must still be a commit.
272 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) {
273 t.Fatalf("a base that is not an object id: %v", err)
274 }
275}
276