| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // A snapshot whose fetch fails because main moved after the listing reads |
| 16 | // origin again. Here origin rewrites main, and prunes its old tip, as the |
| 17 | // snapshot fetches. Once, and the read made again finds main at its new |
| 18 | // tip. On every fetch, and the read ends with the fetch's error after |
| 19 | // snapshotTries listings. |
| 20 | func TestASnapshotReadsAgainWhenMainMovesUnderIt(t *testing.T) { |
| 21 | for _, c := range []struct { |
| 22 | name string |
| 23 | races int |
| 24 | }{{"once", 1}, {"every time", snapshotTries}} { |
| 25 | t.Run(c.name, func(t *testing.T) { |
| 26 | w := newWorld(t) |
| 27 | w.moveMain("main.txt", "main\n") |
| 28 | listed := w.at("refs/heads/main") |
| 29 | reader := filepath.Join(t.TempDir(), "reader") |
| 30 | w.run(filepath.Dir(reader), "init", "--quiet", reader) |
| 31 | w.run(reader, "remote", "add", "origin", w.origin) |
| 32 | calls := filepath.Join(t.TempDir(), "calls") |
| 33 | w.run(reader, "config", "remote.origin.uploadpack", racing(t, calls, c.races)) |
| 34 | |
| 35 | s, err := ReadSnapshot(context.Background(), Git{Dir: reader, Env: w.env}, Verifier{}) |
| 36 | data, _ := os.ReadFile(calls) |
| 37 | n, _ := strconv.Atoi(strings.TrimSpace(string(data))) |
| 38 | if c.races == snapshotTries { |
| 39 | if err == nil || n != 2*snapshotTries { |
| 40 | t.Errorf("a read that raced main every time ended with %v, after %d calls to origin", err, n) |
| 41 | } |
| 42 | return |
| 43 | } |
| 44 | if err != nil { |
| 45 | t.Fatalf("the read did not survive main moving under it: %v", err) |
| 46 | } |
| 47 | if now := w.at("refs/heads/main"); s.Tip != now || now == listed || n != 4 { |
| 48 | t.Errorf("the snapshot has main at %s, origin at %s, after %d calls to origin", short(s.Tip), short(now), n) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // racing is an upload-pack that rewrites origin's main, and prunes its old |
| 55 | // tip, before each of the first races fetches it serves. A snapshot lists |
| 56 | // origin and then fetches, so every second call is a fetch. It counts its |
| 57 | // calls in the file given. |
| 58 | func racing(t *testing.T, calls string, races int) string { |
| 59 | t.Helper() |
| 60 | path := filepath.Join(t.TempDir(), "racing-upload-pack") |
| 61 | script := fmt.Sprintf(`#!/bin/sh |
| 62 | n=$(( $(cat '%[1]s' 2>/dev/null || echo 0) + 1 )) |
| 63 | echo "$n" > '%[1]s' |
| 64 | if [ $((n %% 2)) -eq 0 ] && [ $((n / 2)) -le %[2]d ]; then |
| 65 | tree=$(git -C "$1" rev-parse 'refs/heads/main^{tree}') |
| 66 | new=$(git -C "$1" commit-tree "$tree" -m "main rewritten, call $n") |
| 67 | git -C "$1" update-ref refs/heads/main "$new" |
| 68 | git -C "$1" gc --quiet --prune=now |
| 69 | fi |
| 70 | exec git upload-pack "$@" |
| 71 | `, calls, races) |
| 72 | if err := os.WriteFile(path, []byte(script), 0o755); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | return path |
| 76 | } |
| 77 | |
| 78 | // A snapshot answers whether one commit is an ancestor of another as git |
| 79 | // does, for every pair, from one read of main's history and each head's |
| 80 | // own commits, without asking git about any pair. Main here holds a |
| 81 | // merge. One branch forks below it, one from the side it merged, one from |
| 82 | // main's tip, and one merges main back in, so its history meets main's at |
| 83 | // two commits. |
| 84 | func TestAncestryIsAnsweredAsGitAnswersIt(t *testing.T) { |
| 85 | w, commits, m2 := ancestryWorld(t) |
| 86 | want := map[[2]string]bool{} |
| 87 | for _, a := range commits { |
| 88 | for _, b := range commits { |
| 89 | want[[2]string{a, b}] = w.ancestor(a, b) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | calls := logGit(t) |
| 94 | s, err := ReadSnapshot(context.Background(), w.g, Verifier{}) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | for _, a := range commits { |
| 99 | if got := s.Reaches(a); got != want[[2]string{a, m2}] { |
| 100 | t.Errorf("Reaches(%s) is %v, and git says %v", short(a), got, !got) |
| 101 | } |
| 102 | for _, b := range commits { |
| 103 | if got := s.StrictAncestor(a, b); got != (a != b && want[[2]string{a, b}]) { |
| 104 | t.Errorf("StrictAncestor(%s, %s) is %v, and git says %v", short(a), short(b), got, !got) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | log, _ := os.ReadFile(calls) |
| 109 | if n := strings.Count(string(log), "merge-base --is-ancestor"); n != 0 { |
| 110 | t.Errorf("the snapshot asked git about %d pairs:\n%s", n, log) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // What many commits reach that main does not, read in one batch |
| 115 | // (readReach), is what reading each alone gives, with one git for them |
| 116 | // all. A name that is no commit is left to be read alone. |
| 117 | func TestReachInOneBatchIsReachReadAlone(t *testing.T) { |
| 118 | w, commits, _ := ancestryWorld(t) |
| 119 | alone, err := ReadSnapshot(context.Background(), w.g, Verifier{}) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | calls := logGit(t) |
| 124 | batched, err := ReadSnapshot(context.Background(), w.g, Verifier{}) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | os.Remove(calls) |
| 129 | batched.readReach(append([]string{"no-such-commit"}, commits...)) |
| 130 | log, _ := os.ReadFile(calls) |
| 131 | if n, alone := strings.Count(string(log), "rev-list --parents --ignore-missing --stdin"), strings.Count(string(log), " ^"); n != 1 || alone != 0 { |
| 132 | t.Errorf("the batch started %d rev-lists, and read %d commits alone:\n%s", n, alone, log) |
| 133 | } |
| 134 | if _, read := batched.reachOf["no-such-commit"]; read { |
| 135 | t.Errorf("the batch answered for a name that is no commit") |
| 136 | } |
| 137 | sorted := func(s []string) string { |
| 138 | s = append([]string{}, s...) |
| 139 | sort.Strings(s) |
| 140 | return strings.Join(s, " ") |
| 141 | } |
| 142 | for _, c := range commits { |
| 143 | want, got := alone.reached(c), batched.reachOf[c] |
| 144 | if fmt.Sprint(want.commits) != fmt.Sprint(got.commits) || sorted(want.below) != sorted(got.below) { |
| 145 | t.Errorf("%s reaches %v, below %v, alone, and %v, below %v, in the batch", short(c), want.commits, want.below, got.commits, got.below) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // logGit puts a shim on PATH that logs every git started, one line of its |
| 151 | // arguments each, to the file it returns. |
| 152 | func logGit(t *testing.T) string { |
| 153 | t.Helper() |
| 154 | real, err := exec.LookPath("git") |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | shim, calls := t.TempDir(), filepath.Join(t.TempDir(), "calls") |
| 159 | script := fmt.Sprintf("#!/bin/sh\necho \"$*\" >> '%s'\nexec '%s' \"$@\"\n", calls, real) |
| 160 | if err := os.WriteFile(filepath.Join(shim, "git"), []byte(script), 0o755); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | t.Setenv("PATH", shim+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 164 | return calls |
| 165 | } |
| 166 | |
| 167 | // ancestryWorld is an origin whose main holds a merge. One branch forks |
| 168 | // below it, one from the side it merged, one from main's tip, and one |
| 169 | // merges main back in, so its history meets main's at two commits. It |
| 170 | // returns every commit, and main's tip. |
| 171 | func ancestryWorld(t *testing.T) (w *world, commits []string, tip string) { |
| 172 | t.Helper() |
| 173 | w = newWorld(t) |
| 174 | base := w.git("rev-parse", "HEAD") |
| 175 | m1 := w.commit("m1", "main.txt", "1\n") |
| 176 | w.git("checkout", "--quiet", "-b", "side", base) |
| 177 | s1 := w.commit("s1", "side.txt", "s\n") |
| 178 | w.git("checkout", "--quiet", "main") |
| 179 | w.git("merge", "--quiet", "--no-ff", "-m", "merge side", "side") |
| 180 | merge := w.git("rev-parse", "HEAD") |
| 181 | m2 := w.commit("m2", "main.txt", "2\n") |
| 182 | branch := func(name, from string) string { |
| 183 | w.git("checkout", "--quiet", "-b", name, from) |
| 184 | return w.commit(name, name+".txt", name+"\n") |
| 185 | } |
| 186 | x1 := branch("x", m1) |
| 187 | x2 := w.commit("x2", "x.txt", "x2\n") |
| 188 | y := branch("y", m2) |
| 189 | s2 := branch("s", s1) |
| 190 | w.git("checkout", "--quiet", "-b", "z", x2) |
| 191 | w.git("merge", "--quiet", "--no-ff", "-m", "merge main", m2) |
| 192 | z := w.git("rev-parse", "HEAD") |
| 193 | w.git("push", "--quiet", "origin", "main", "x", "y", "s", "z") |
| 194 | return w, []string{base, m1, s1, merge, m2, x1, x2, y, s2, z}, m2 |
| 195 | } |