| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // A snapshot whose fetch fails because main moved after the listing reads |
| 14 | // origin again. Here origin rewrites main, and prunes its old tip, as the |
| 15 | // snapshot fetches. Once, and the read made again finds main at its new |
| 16 | // tip. On every fetch, and the read ends with the fetch's error after |
| 17 | // snapshotTries listings. |
| 18 | func TestASnapshotReadsAgainWhenMainMovesUnderIt(t *testing.T) { |
| 19 | for _, c := range []struct { |
| 20 | name string |
| 21 | races int |
| 22 | }{{"once", 1}, {"every time", snapshotTries}} { |
| 23 | t.Run(c.name, func(t *testing.T) { |
| 24 | w := newWorld(t) |
| 25 | w.moveMain("main.txt", "main\n") |
| 26 | listed := w.at("refs/heads/main") |
| 27 | reader := filepath.Join(t.TempDir(), "reader") |
| 28 | w.run(filepath.Dir(reader), "init", "--quiet", reader) |
| 29 | w.run(reader, "remote", "add", "origin", w.origin) |
| 30 | calls := filepath.Join(t.TempDir(), "calls") |
| 31 | w.run(reader, "config", "remote.origin.uploadpack", racing(t, calls, c.races)) |
| 32 | |
| 33 | s, err := ReadSnapshot(context.Background(), Git{Dir: reader, Env: w.env}, Verifier{}) |
| 34 | data, _ := os.ReadFile(calls) |
| 35 | n, _ := strconv.Atoi(strings.TrimSpace(string(data))) |
| 36 | if c.races == snapshotTries { |
| 37 | if err == nil || n != 2*snapshotTries { |
| 38 | t.Errorf("a read that raced main every time ended with %v, after %d calls to origin", err, n) |
| 39 | } |
| 40 | return |
| 41 | } |
| 42 | if err != nil { |
| 43 | t.Fatalf("the read did not survive main moving under it: %v", err) |
| 44 | } |
| 45 | if now := w.at("refs/heads/main"); s.Tip != now || now == listed || n != 4 { |
| 46 | t.Errorf("the snapshot has main at %s, origin at %s, after %d calls to origin", short(s.Tip), short(now), n) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // racing is an upload-pack that rewrites origin's main, and prunes its old |
| 53 | // tip, before each of the first races fetches it serves. A snapshot lists |
| 54 | // origin and then fetches, so every second call is a fetch. It counts its |
| 55 | // calls in the file given. |
| 56 | func racing(t *testing.T, calls string, races int) string { |
| 57 | t.Helper() |
| 58 | path := filepath.Join(t.TempDir(), "racing-upload-pack") |
| 59 | script := fmt.Sprintf(`#!/bin/sh |
| 60 | n=$(( $(cat '%[1]s' 2>/dev/null || echo 0) + 1 )) |
| 61 | echo "$n" > '%[1]s' |
| 62 | if [ $((n %% 2)) -eq 0 ] && [ $((n / 2)) -le %[2]d ]; then |
| 63 | tree=$(git -C "$1" rev-parse 'refs/heads/main^{tree}') |
| 64 | new=$(git -C "$1" commit-tree "$tree" -m "main rewritten, call $n") |
| 65 | git -C "$1" update-ref refs/heads/main "$new" |
| 66 | git -C "$1" gc --quiet --prune=now |
| 67 | fi |
| 68 | exec git upload-pack "$@" |
| 69 | `, calls, races) |
| 70 | if err := os.WriteFile(path, []byte(script), 0o755); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | return path |
| 74 | } |