| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | // conflictProject is a repository at root with its own origin, made the |
| 17 | // current directory, whose main holds f and g, each "0". It returns git |
| 18 | // run in it, git's output, and commit, which commits files on a new |
| 19 | // branch off from and pushes it, returning the head. A git shim on PATH |
| 20 | // logs the arguments of each git start to starts (runMerges). |
| 21 | func conflictProject(t *testing.T, root string) (git func(...string) string, commit func(branch, from string, files map[string]string) string, starts string) { |
| 22 | t.Helper() |
| 23 | t.Setenv("HOME", root) |
| 24 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "config")) |
| 25 | t.Setenv("XDG_CACHE_HOME", filepath.Join(root, "cache")) |
| 26 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 27 | t.Setenv("VALLEY_KNOWN_SIGNERS", "") |
| 28 | real, err := exec.LookPath("git") |
| 29 | if err != nil { |
| 30 | t.Skip("no git to run") |
| 31 | } |
| 32 | bin, starts := filepath.Join(root, "bin"), filepath.Join(root, "starts") |
| 33 | os.MkdirAll(bin, 0o755) |
| 34 | shim := fmt.Sprintf("#!/bin/sh\necho \"$*\" >> '%s'\nexec '%s' \"$@\"\n", starts, real) |
| 35 | if err := os.WriteFile(filepath.Join(bin, "git"), []byte(shim), 0o755); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 39 | origin, work := filepath.Join(root, "origin.git"), filepath.Join(root, "work") |
| 40 | git = func(args ...string) string { |
| 41 | t.Helper() |
| 42 | cmd := exec.Command(real, args...) |
| 43 | cmd.Dir = work |
| 44 | out, err := cmd.Output() |
| 45 | if err != nil { |
| 46 | t.Fatalf("git %s: %v", strings.Join(args, " "), err) |
| 47 | } |
| 48 | return strings.TrimSpace(string(out)) |
| 49 | } |
| 50 | if out, err := exec.Command(real, "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil { |
| 51 | t.Fatalf("git init: %v\n%s", err, out) |
| 52 | } |
| 53 | if out, err := exec.Command(real, "clone", "--quiet", origin, work).CombinedOutput(); err != nil { |
| 54 | t.Fatalf("git clone: %v\n%s", err, out) |
| 55 | } |
| 56 | git("config", "user.name", "operator") |
| 57 | git("config", "user.email", "operator@example.org") |
| 58 | git("config", "valley.principal", "operator") |
| 59 | git("checkout", "--quiet", "-b", "main") |
| 60 | git("commit", "--quiet", "--allow-empty", "-m", "first") |
| 61 | commit = func(branch, from string, files map[string]string) string { |
| 62 | t.Helper() |
| 63 | git("checkout", "--quiet", "-B", branch, from) |
| 64 | for name, text := range files { |
| 65 | if err := os.WriteFile(filepath.Join(work, name), []byte(text+"\n"), 0o644); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | } |
| 69 | git("add", ".") |
| 70 | git("commit", "--quiet", "-m", branch) |
| 71 | git("push", "--quiet", "--force", "origin", branch) |
| 72 | return git("rev-parse", "HEAD") |
| 73 | } |
| 74 | commit("main", "main", map[string]string{"f": "0", "g": "0"}) |
| 75 | t.Chdir(work) |
| 76 | return git, commit, starts |
| 77 | } |
| 78 | |
| 79 | // runMerges runs valley with args, and returns what it printed and how |
| 80 | // many git merge-tree processes the shim logged to starts. |
| 81 | func runMerges(t *testing.T, starts string, args ...string) (string, int) { |
| 82 | t.Helper() |
| 83 | os.Remove(starts) |
| 84 | var stdout, stderr bytes.Buffer |
| 85 | if code := run(args, &stdout, &stderr, os.Exit); code != 0 { |
| 86 | t.Fatalf("%s: %s", strings.Join(args, " "), stderr.String()) |
| 87 | } |
| 88 | log, _ := os.ReadFile(starts) |
| 89 | merges := 0 |
| 90 | for _, line := range strings.Split(string(log), "\n") { |
| 91 | if slices.Contains(strings.Fields(line), "merge-tree") { |
| 92 | merges++ |
| 93 | } |
| 94 | } |
| 95 | return stdout.String(), merges |
| 96 | } |
| 97 | |
| 98 | // readyAs readies branch as the change id. |
| 99 | func readyAs(t *testing.T, id, branch string) { |
| 100 | t.Helper() |
| 101 | var stdout, stderr bytes.Buffer |
| 102 | if code := run([]string{"ready", "--change", id, branch}, &stdout, &stderr, os.Exit); code != 0 { |
| 103 | t.Fatalf("ready: %s", stderr.String()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // underRow is the lines indented under the row that names name. |
| 108 | func underRow(out, name string) []string { |
| 109 | lines := strings.Split(out, "\n") |
| 110 | for i, line := range lines { |
| 111 | if !strings.HasPrefix(line, " ") || !strings.Contains(line, " "+name+" v") { |
| 112 | continue |
| 113 | } |
| 114 | var under []string |
| 115 | for _, l := range lines[i+1:] { |
| 116 | if !strings.HasPrefix(l, " ") { |
| 117 | break |
| 118 | } |
| 119 | under = append(under, strings.TrimSpace(l)) |
| 120 | } |
| 121 | return under |
| 122 | } |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // pendingJSON is valley-pending/v1 or valley-changes/v1, with args, as |
| 127 | // the changes it lists by handle. |
| 128 | func pendingJSON(t *testing.T, args ...string) map[string]jsonChange { |
| 129 | t.Helper() |
| 130 | var stdout, stderr bytes.Buffer |
| 131 | if code := run(append(args, "--json"), &stdout, &stderr, os.Exit); code != 0 { |
| 132 | t.Fatalf("%s: %s", strings.Join(args, " "), stderr.String()) |
| 133 | } |
| 134 | var doc jsonChanges |
| 135 | if err := json.Unmarshal(stdout.Bytes(), &doc); err != nil || len(doc.Repos) != 1 { |
| 136 | t.Fatalf("%s --json does not read: %v", strings.Join(args, " "), err) |
| 137 | } |
| 138 | byHandle := map[string]jsonChange{} |
| 139 | for _, c := range doc.Repos[0].Changes { |
| 140 | byHandle[c.Handle] = c |
| 141 | } |
| 142 | return byHandle |
| 143 | } |
| 144 | |
| 145 | // conflictsOf is a change's conflicts and the pairs not computed, as text |
| 146 | // a test compares: "<handle> <paths> <total>" and "<handle>: <why>". |
| 147 | func conflictsOf(c jsonChange) string { |
| 148 | if c.Conflicts == nil || c.ConflictsUnknown == nil { |
| 149 | return "absent" |
| 150 | } |
| 151 | var says []string |
| 152 | for _, x := range *c.Conflicts { |
| 153 | says = append(says, fmt.Sprintf("%s %s %d", x.Handle, strings.Join(x.Paths, ","), x.PathsTotal)) |
| 154 | } |
| 155 | for _, x := range *c.ConflictsUnknown { |
| 156 | says = append(says, x.Handle+": "+x.Why) |
| 157 | } |
| 158 | return strings.Join(says, "; ") |
| 159 | } |
| 160 | |
| 161 | // pending marks each ready change's conflicts with the others, by the |
| 162 | // merge onto main's tip: two that change one file conflict, and the |
| 163 | // third shows nothing. A warm listing runs no merge; a cold one runs one |
| 164 | // for each pair, up to the bound, and says the rest were not compared. |
| 165 | // An approved change under moving is paired with a waiting one, layers |
| 166 | // of one stack are not paired, and a change not on main's tip awaits a |
| 167 | // restack, in text and JSON, never reading as clean. |
| 168 | func TestPendingMarksReadyChangesThatConflict(t *testing.T) { |
| 169 | root := t.TempDir() |
| 170 | git, commit, starts := conflictProject(t, root) |
| 171 | first := git("rev-parse", "main~1") |
| 172 | commit("one", "main", map[string]string{"f": "1"}) |
| 173 | commit("two", "main", map[string]string{"f": "2"}) |
| 174 | commit("three", "main", map[string]string{"h": "3"}) |
| 175 | k, m, n := strings.Repeat("k", 32), strings.Repeat("m", 32), strings.Repeat("n", 32) |
| 176 | readyAs(t, k, "one") |
| 177 | readyAs(t, m, "two") |
| 178 | readyAs(t, n, "three") |
| 179 | |
| 180 | bound := pairBound |
| 181 | pairBound = 2 |
| 182 | out, merges := runMerges(t, starts, "pending") |
| 183 | pairBound = bound |
| 184 | if merges != 2 || !strings.Contains(out, "\n1 pair not compared: past the bound of 2 merges\n") { |
| 185 | t.Fatalf("with a bound of 2 merges, pending ran %d and printed:\n%s", merges, out) |
| 186 | } |
| 187 | |
| 188 | os.RemoveAll(filepath.Join(root, "cache")) |
| 189 | cold, merges := runMerges(t, starts, "pending") |
| 190 | if merges != 3 { |
| 191 | t.Errorf("a cold pending ran %d merges for three pairs", merges) |
| 192 | } |
| 193 | for name, want := range map[string]string{"kkkk": "conflicts with mmmm", "mmmm": "conflicts with kkkk", "nnnn": ""} { |
| 194 | under := underRow(cold, name) |
| 195 | if len(under) == 0 || strings.Join(under[1:], "\n") != want { |
| 196 | t.Errorf("under %s pending prints %q, not its title and %q:\n%s", name, under, want, cold) |
| 197 | } |
| 198 | } |
| 199 | if strings.Contains(cold, "not compared") { |
| 200 | t.Errorf("pending says a pair was not compared:\n%s", cold) |
| 201 | } |
| 202 | warm, merges := runMerges(t, starts, "pending") |
| 203 | ages := regexp.MustCompile(`(?m) +[0-9]+[smhd]$`) |
| 204 | if merges != 0 || ages.ReplaceAllString(warm, "") != ages.ReplaceAllString(cold, "") { |
| 205 | t.Errorf("a warm pending ran %d merges and printed:\n%s\nwhere the cold one printed:\n%s", merges, warm, cold) |
| 206 | } |
| 207 | verbose, _ := runMerges(t, starts, "pending", "-v") |
| 208 | if under := strings.Join(underRow(verbose, "kkkk"), "\n") + "\n"; !strings.Contains(under, "\nconflicts with mmmm: f\n") { |
| 209 | t.Errorf("pending -v does not name the conflicted path under kkkk:\n%s", verbose) |
| 210 | } |
| 211 | for _, verb := range []string{"pending", "changes"} { |
| 212 | got := pendingJSON(t, verb) |
| 213 | for name, want := range map[string]string{"kkkk": "mmmm f 1", "mmmm": "kkkk f 1", "nnnn": ""} { |
| 214 | if says := conflictsOf(got[name]); says != want { |
| 215 | t.Errorf("%s --json gives %s the conflicts %q, not %q", verb, name, says, want) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // mmmm is approved, pppp is a layer on kkkk, and rrrr is on main's |
| 221 | // parent, not its tip. |
| 222 | git("push", "--quiet", "origin", "two:refs/the-valley/integration-requests/main/c-"+m) |
| 223 | commit("four", "one", map[string]string{"l": "4"}) |
| 224 | commit("five", first, map[string]string{"r": "5"}) |
| 225 | p, r := strings.Repeat("p", 32), strings.Repeat("r", 32) |
| 226 | readyAs(t, p, "four") |
| 227 | readyAs(t, r, "five") |
| 228 | out, _ = runMerges(t, starts, "pending") |
| 229 | if under := underRow(out, "mmmm"); !strings.Contains(out, "◐ mmmm v1 approved") || strings.Join(under, "\n") != "conflicts with kkkk, pppp" { |
| 230 | t.Errorf("the approved mmmm does not say it conflicts with kkkk and pppp:\n%s", out) |
| 231 | } |
| 232 | if under := underRow(out, "pppp"); len(under) != 2 || under[1] != "conflicts with mmmm" { |
| 233 | t.Errorf("the row of the stack of kkkk and pppp does not say it conflicts with mmmm:\n%s", out) |
| 234 | } |
| 235 | if !strings.Contains(out, "\n4 pairs not compared: awaiting restack\n") { |
| 236 | t.Errorf("pending does not say rrrr's 4 pairs await a restack:\n%s", out) |
| 237 | } |
| 238 | got := pendingJSON(t, "pending") |
| 239 | for name, want := range map[string]string{ |
| 240 | "kkkk": "mmmm f 1; rrrr: awaiting restack", |
| 241 | "pppp": "mmmm f 1; rrrr: awaiting restack", |
| 242 | "mmmm": "kkkk f 1; pppp f 1; rrrr: awaiting restack", |
| 243 | "nnnn": "rrrr: awaiting restack", |
| 244 | "rrrr": "kkkk: awaiting restack; mmmm: awaiting restack; nnnn: awaiting restack; pppp: awaiting restack", |
| 245 | } { |
| 246 | if says := conflictsOf(got[name]); says != want { |
| 247 | t.Errorf("pending --json gives %s the conflicts %q, not %q", name, says, want) |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Two heads that share an unlanded ancestor are compared on main's tip, |
| 253 | // as the integrator merges the second once the first lands, and not on |
| 254 | // their own merge base. The ancestor changes f from 0 to 1, one head |
| 255 | // restores 0 and the other makes it 2: from the ancestor that is a |
| 256 | // conflict, and landing either head first, the integrator merges the |
| 257 | // other cleanly, so pending marks no conflict. |
| 258 | func TestPendingComparesForkedHeadsAsTheIntegratorMerges(t *testing.T) { |
| 259 | git, commit, starts := conflictProject(t, t.TempDir()) |
| 260 | tip := git("rev-parse", "main") |
| 261 | commit("ancestor", "main", map[string]string{"f": "1"}) |
| 262 | a := commit("restores", "ancestor", map[string]string{"f": "0"}) |
| 263 | b := commit("changes", "ancestor", map[string]string{"f": "2"}) |
| 264 | if out, err := exec.Command("git", "merge-tree", "--write-tree", a, b).Output(); err == nil { |
| 265 | t.Fatalf("the heads merge cleanly from their own merge base:\n%s", out) |
| 266 | } |
| 267 | // integrate lands head on main as the integrator does |
| 268 | // (integrator/refs.go): one commit on the tip whose tree is the head's |
| 269 | // when the head's merge base with the tip is the tip, and otherwise |
| 270 | // the merge of tip and head from that merge base (mergeDelta). |
| 271 | integrate := func(tip, head string) (landed string, clean bool) { |
| 272 | t.Helper() |
| 273 | tree := head + "^{tree}" |
| 274 | if base := git("merge-base", tip, head); base != tip { |
| 275 | out, err := exec.Command("git", "merge-tree", "--write-tree", "--merge-base="+base, tip, head).Output() |
| 276 | if err != nil { |
| 277 | return "", false |
| 278 | } |
| 279 | tree, _, _ = strings.Cut(string(out), "\n") |
| 280 | } |
| 281 | return git("commit-tree", tree, "-p", tip, "-m", "landed"), true |
| 282 | } |
| 283 | for _, order := range [][2]string{{a, b}, {b, a}} { |
| 284 | landed, _ := integrate(tip, order[0]) |
| 285 | next, clean := integrate(landed, order[1]) |
| 286 | if !clean || git("show", next+":f") != "2" { |
| 287 | t.Fatalf("the integrator does not land the second head cleanly with f at 2 after the first") |
| 288 | } |
| 289 | } |
| 290 | readyAs(t, strings.Repeat("k", 32), "restores") |
| 291 | readyAs(t, strings.Repeat("m", 32), "changes") |
| 292 | out, merges := runMerges(t, starts, "pending") |
| 293 | if merges != 1 || strings.Contains(out, "conflicts with") || strings.Contains(out, "not compared") { |
| 294 | t.Errorf("pending ran %d merges and marks the forked heads as the integrator does not:\n%s", merges, out) |
| 295 | } |
| 296 | for name, c := range pendingJSON(t, "pending") { |
| 297 | if says := conflictsOf(c); says != "" { |
| 298 | t.Errorf("pending --json gives %s the conflicts %q", name, says) |
| 299 | } |
| 300 | } |
| 301 | } |