gunk-dev/the-valley

mirror-e2e.sh

sourcef4ba77eea55dmain's published tip history
3.4 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11wait_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}
19heads() { git -C mirror.git for-each-ref --format='%(refname)' refs/heads; }
20tags() { git -C mirror.git for-each-ref --format='%(refname)' refs/tags; }
21mirror_main_is() { [ "$(git -C mirror.git rev-parse main)" = "$tip" ]; }
22only_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.
26dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)"
27mhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-mirror-pilot' "$initScriptPath" | head -n1)"
28deadhook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-dead-mirror' "$initScriptPath" | head -n1)"
29test -x "$dispatch" && test -x "$mhook" && test -x "$deadhook"
30wire() {
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.
39git init --quiet --bare mirror-pilot.git
40git init --quiet --bare mirror.git
41git clone --quiet "$PWD/mirror-pilot.git" work
42git -C work commit --quiet --allow-empty -m one
43git -C work tag v1
44git -C work tag doomed
45for b in idea/one idea/two; do git -C work branch "$b"; done
46git -C work push --quiet origin \
47 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
48git -C mirror-pilot.git push --quiet ../mirror.git \
49 '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'
50[ "$(heads | wc -l)" -eq 3 ]
51wire 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.
56git -C work commit --quiet --allow-empty -m two
57git -C work tag -d doomed >/dev/null
58git -C work branch idea/three
59git -C work push --quiet --follow-tags origin main idea/three
60git -C work push --quiet --delete origin doomed
61tip="$(git -C work rev-parse HEAD)"
62wait_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.
67wait_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.
72git -C mirror.git branch sneaky main
73git -C work commit --quiet --allow-empty -m three
74git -C work push --quiet origin main
75tip="$(git -C work rev-parse HEAD)"
76wait_for mirror_main_is
77wait_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.
81git init --quiet --bare dead-mirror.git
82wire dead-mirror.git "$deadhook"
83git clone --quiet "$PWD/dead-mirror.git" deadwork
84git -C deadwork commit --quiet --allow-empty -m one
85timeout 60 git -C deadwork push --quiet origin main
86[ "$(git -C dead-mirror.git rev-parse main)" = "$(git -C deadwork rev-parse HEAD)" ]
87
88touch "$out"