| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | cd "$TMPDIR" || exit 1 |
| 10 | |
| 11 | wait_for() { |
| 12 | for _ in $(seq 1 150); do |
| 13 | "$@" >/dev/null 2>&1 && return 0 |
| 14 | sleep 0.2 |
| 15 | done |
| 16 | echo "mirror-e2e: timed out waiting for: $*" >&2 |
| 17 | return 1 |
| 18 | } |
| 19 | heads() { git -C mirror.git for-each-ref --format='%(refname)' refs/heads; } |
| 20 | tags() { git -C mirror.git for-each-ref --format='%(refname)' refs/tags; } |
| 21 | mirror_main_is() { [ "$(git -C mirror.git rev-parse main)" = "$tip" ]; } |
| 22 | only_main() { [ "$(heads)" = refs/heads/main ]; } |
| 23 | |
| 24 | # A primary repo wired exactly as valley-init wires it, with |
| 25 | # the real store paths followed from the rendered init script. |
| 26 | dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)" |
| 27 | mhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-mirror-pilot' "$initScriptPath" | head -n1)" |
| 28 | deadhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-dead-mirror' "$initScriptPath" | head -n1)" |
| 29 | test -x "$dispatch" && test -x "$mhook" && test -x "$deadhook" |
| 30 | wire() { |
| 31 | mkdir -p "$1/hooks/post-receive.d" |
| 32 | ln -s "$dispatch" "$1/hooks/post-receive" |
| 33 | ln -s "$2" "$1/hooks/post-receive.d/valley-mirrors" |
| 34 | } |
| 35 | |
| 36 | # The mirror as it stands today: every head and tag of the |
| 37 | # primary, topic branches included. Seeded before the hook is |
| 38 | # wired, so nothing sweeps it out from under the setup. |
| 39 | git init --quiet --bare mirror-pilot.git |
| 40 | git init --quiet --bare mirror.git |
| 41 | git clone --quiet "$PWD/mirror-pilot.git" work |
| 42 | git -C work commit --quiet --allow-empty -m one |
| 43 | git -C work tag v1 |
| 44 | git -C work tag doomed |
| 45 | for b in idea/one idea/two; do git -C work branch "$b"; done |
| 46 | git -C work push --quiet origin \ |
| 47 | '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' |
| 48 | git -C mirror-pilot.git push --quiet ../mirror.git \ |
| 49 | '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' |
| 50 | [ "$(heads | wc -l)" -eq 3 ] |
| 51 | wire mirror-pilot.git "$mhook" |
| 52 | |
| 53 | # A real push to the primary — a topic branch alongside main, |
| 54 | # and a tag deleted. The hook replicates asynchronously, so |
| 55 | # wait on main's new tip before judging the rest. |
| 56 | git -C work commit --quiet --allow-empty -m two |
| 57 | git -C work tag -d doomed >/dev/null |
| 58 | git -C work branch idea/three |
| 59 | git -C work push --quiet --follow-tags origin main idea/three |
| 60 | git -C work push --quiet --delete origin doomed |
| 61 | tip="$(git -C work rev-parse HEAD)" |
| 62 | wait_for mirror_main_is |
| 63 | |
| 64 | # main and the tags are published; nothing else is, and the |
| 65 | # topic branches that were on the mirror are gone. The sweep |
| 66 | # is a second push, so it converges just after main lands. |
| 67 | wait_for only_main |
| 68 | [ "$(tags)" = refs/tags/v1 ] |
| 69 | |
| 70 | # A branch that appears on the mirror by any other route is |
| 71 | # unpublished on the next push. |
| 72 | git -C mirror.git branch sneaky main |
| 73 | git -C work commit --quiet --allow-empty -m three |
| 74 | git -C work push --quiet origin main |
| 75 | tip="$(git -C work rev-parse HEAD)" |
| 76 | wait_for mirror_main_is |
| 77 | wait_for only_main |
| 78 | |
| 79 | # An unreachable mirror costs a log line, never the push: the |
| 80 | # hook must return promptly and the push must succeed. |
| 81 | git init --quiet --bare dead-mirror.git |
| 82 | wire dead-mirror.git "$deadhook" |
| 83 | git clone --quiet "$PWD/dead-mirror.git" deadwork |
| 84 | git -C deadwork commit --quiet --allow-empty -m one |
| 85 | timeout 60 git -C deadwork push --quiet origin main |
| 86 | [ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ] |
| 87 | |
| 88 | # Two publishers of one project. git reads the local refs before it |
| 89 | # contacts the mirror, so a publisher that stalls between the two can |
| 90 | # force-push the old main it read over a newer one. The race mirror is |
| 91 | # an ssh URL, and this stand-in for ssh runs the remote git here. The |
| 92 | # first connection made while `hold` exists takes it and waits for `go`: |
| 93 | # that stalls one publisher in the window, every time. |
| 94 | racehook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-race' "$initScriptPath" | head -n1)" |
| 95 | pusher="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-race' "$racehook" | head -n1)" |
| 96 | test -x "$racehook" && test -x "$pusher" |
| 97 | cat >fake-ssh <<EOF |
| 98 | #!$(command -v sh) |
| 99 | if mv "$PWD/hold" "$PWD/held" 2>/dev/null; then |
| 100 | while [ ! -e "$PWD/go" ]; do sleep 0.1; done |
| 101 | fi |
| 102 | exec sh -c "\$2" |
| 103 | EOF |
| 104 | chmod +x fake-ssh |
| 105 | git init --quiet --bare race.git |
| 106 | git init --quiet --bare race-mirror.git |
| 107 | git -C race.git config core.sshCommand "$PWD/fake-ssh" |
| 108 | git -C race.git config ssh.variant simple |
| 109 | git init --quiet racework |
| 110 | git -C racework commit --quiet --allow-empty -m old |
| 111 | git -C racework commit --quiet --allow-empty -m new |
| 112 | old="$(git -C racework rev-parse HEAD~1)" |
| 113 | new="$(git -C racework rev-parse HEAD)" |
| 114 | # A fetch runs no hook, so the commits arrive unpublished. |
| 115 | git -C race.git fetch --quiet ../racework "+$new:refs/heads/main" |
| 116 | |
| 117 | set_main() { git -C race.git update-ref refs/heads/main "$1"; } |
| 118 | race_main_is() { [ "$(git -C race-mirror.git rev-parse --verify --quiet main)" = "$1" ]; } |
| 119 | race_reset() { |
| 120 | rm -f hold held go |
| 121 | git -C race-mirror.git update-ref -d refs/heads/main 2>/dev/null || true |
| 122 | set_main "$old" |
| 123 | } |
| 124 | # The pusher alone, as an unserialized publisher runs it. |
| 125 | unlocked() { (cd race.git && "$pusher"); } |
| 126 | # The real hook, as post-receive runs it: detached, under the lock. |
| 127 | hooked() { (cd race.git && "$racehook" </dev/null 8>&-); } |
| 128 | lock_free() { flock -n race.git/valley-publish.flock true; } |
| 129 | |
| 130 | # The negative control. Without the lock, the publisher that read the |
| 131 | # old main stalls, the other pushes the new main, and the first then |
| 132 | # rewinds the mirror to the old one. |
| 133 | race_reset |
| 134 | touch hold |
| 135 | unlocked & |
| 136 | stalled=$! |
| 137 | wait_for test -e held |
| 138 | set_main "$new" |
| 139 | unlocked |
| 140 | race_main_is "$new" |
| 141 | touch go |
| 142 | wait "$stalled" |
| 143 | race_main_is "$old" |
| 144 | |
| 145 | # With the lock, the same ordering ends at the new main. The first |
| 146 | # publisher stalls holding the lock; the second, started after main |
| 147 | # moved, waits for it and pushes last. |
| 148 | race_reset |
| 149 | touch hold |
| 150 | hooked |
| 151 | wait_for test -e held |
| 152 | set_main "$new" |
| 153 | hooked |
| 154 | sleep 1 |
| 155 | race_main_is "" |
| 156 | touch go |
| 157 | wait_for race_main_is "$new" |
| 158 | wait_for lock_free |
| 159 | race_main_is "$new" |
| 160 | |
| 161 | # A publisher takes the lock before it reads main. Hold the lock here, |
| 162 | # start a publisher, and move main while it waits: it publishes the |
| 163 | # main it finds once it holds the lock. |
| 164 | race_reset |
| 165 | exec 8>>race.git/valley-publish.flock |
| 166 | flock 8 |
| 167 | hooked |
| 168 | sleep 1 |
| 169 | race_main_is "" |
| 170 | set_main "$new" |
| 171 | flock -u 8 |
| 172 | exec 8>&- |
| 173 | wait_for race_main_is "$new" |
| 174 | wait_for lock_free |
| 175 | race_main_is "$new" |
| 176 | |
| 177 | touch "$out" |