| 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 | touch "$out" |