aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / scripts / test_rebase_crumb.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Offline test for rebase-crumb.sh. Real temporary git repos (a bare "origin" plus a worktree whose |
| 3 | # origin URL looks like an aien-dev URL, redirected to the bare repo with insteadOf). Crumb compile and |
| 4 | # verify are retired (DRA-97): a `crumb` stub that exits 99 is on PATH and in $CRUMB, and the test |
| 5 | # proves it is never called and no crumb commit is added. No network, no auth. |
| 6 | set -u |
| 7 | here="$(cd "$(dirname "$0")" && pwd)" |
| 8 | script="$here/rebase-crumb.sh" |
| 9 | tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT |
| 10 | export GIT_AUTHOR_NAME=T GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=T GIT_COMMITTER_EMAIL=t@example.invalid |
| 11 | export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null |
| 12 | FAKEURL="https://github.com/aien-dev/fake-repo" |
| 13 | |
| 14 | mkdir -p "$tmp/bin" |
| 15 | cat > "$tmp/bin/crumb" <<S |
| 16 | #!/usr/bin/env bash |
| 17 | # retired: any call is a test failure (recorded, then exit 99) |
| 18 | echo "\$*" >> "$tmp/crumb-called" |
| 19 | exit 99 |
| 20 | S |
| 21 | chmod +x "$tmp/bin/crumb" |
| 22 | export PATH="$tmp/bin:$PATH" CRUMB="$tmp/bin/crumb" |
| 23 | RETIRED="crumb: retired (DRA-97), conflicting .crumb files keep main's copy; no compile, verify or crumb commit" |
| 24 | nocrumbcommit() { test "$(git -C "$1" log --format=%s origin/main..HEAD | grep -c '^crumb compile (after rebase onto main)$')" = 0; } |
| 25 | |
| 26 | pass=0; bad=0 |
| 27 | ok() { pass=$((pass+1)); } |
| 28 | no() { bad=$((bad+1)); echo "TEST FAIL: $1"; } |
| 29 | expect() { # expect <rc> <label> <worktree> <branch> (output in $OUT) |
| 30 | OUT=$("$script" "$3" "$4" 2>&1); rc=$? |
| 31 | if [ "$rc" = "$1" ]; then ok; else no "$2 (rc=$rc, want $1): $OUT"; fi |
| 32 | } |
| 33 | check() { # check <label> <command...> |
| 34 | l=$1; shift; if "$@" >/dev/null 2>&1; then ok; else no "$l"; fi |
| 35 | } |
| 36 | |
| 37 | # mk <name> [nocrumb] : origin (bare) + worktree w on branch feat (pushed). main has src.txt and .crumb. |
| 38 | mk() { |
| 39 | d="$tmp/$1"; mkdir -p "$d"; git init -q --bare -b main "$d/origin.git" |
| 40 | git init -q -b main "$d/seed" |
| 41 | ( cd "$d/seed" && echo base > src.txt && { [ "${2:-}" = nocrumb ] || echo c0 > .crumb; } \ |
| 42 | && git add -A && git commit -q -m base && git remote add origin "$d/origin.git" && git push -q origin main ) |
| 43 | git clone -q "$d/origin.git" "$d/w" |
| 44 | ( cd "$d/w" && git checkout -q -b feat && echo feat > feat.txt && git add -A && git commit -q -m feat && git push -q origin feat \ |
| 45 | && git remote set-url origin "$FAKEURL" && git config "url.$d/origin.git.insteadOf" "$FAKEURL" ) |
| 46 | W="$d/w"; O="$d/origin.git" |
| 47 | } |
| 48 | advance_main() { # advance_main <seed-dir> <file> <content> |
| 49 | ( cd "$1" && git checkout -q main && echo "$3" > "$2" && git add -A && git commit -q -m "main $2" && git push -q origin main ) |
| 50 | } |
| 51 | ohead() { git -C "$O" rev-parse "refs/heads/feat"; } |
| 52 | |
| 53 | # 1 dirty tree |
| 54 | mk t1; echo x >> "$W/feat.txt" |
| 55 | expect 2 "dirty tree refused" "$W" feat |
| 56 | # 2 branch main |
| 57 | mk t2 |
| 58 | expect 2 "branch main refused" "$W" main |
| 59 | # 3 wrong remote |
| 60 | mk t3; git -C "$W" remote set-url origin https://github.com/other-org/x |
| 61 | expect 2 "wrong remote refused" "$W" feat |
| 62 | # 4 lock present (fresh) |
| 63 | mk t4; touch "$W/.git/rebase-crumb.lock" |
| 64 | expect 2 "fresh lock refused" "$W" feat |
| 65 | check "fresh lock left in place" test -e "$W/.git/rebase-crumb.lock" |
| 66 | # 5 not a git checkout |
| 67 | mkdir -p "$tmp/plain" |
| 68 | expect 2 "non-git directory refused" "$tmp/plain" feat |
| 69 | # 6 origin branch missing |
| 70 | mk t6 |
| 71 | expect 2 "missing origin branch refused" "$W" nosuchbranch |
| 72 | # 7 clean rebase, push, no crumb commit |
| 73 | mk t7; advance_main "$tmp/t7/seed" main-only.txt m |
| 74 | before=$(ohead) |
| 75 | expect 0 "clean rebase succeeds" "$W" feat |
| 76 | check "push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)" |
| 77 | check "origin head moved" test "$(ohead)" != "$before" |
| 78 | check "last commit is the feat source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat" |
| 79 | check "no crumb compile commit added" nocrumbcommit "$W" |
| 80 | check "retired crumb notice printed" bash -c 'echo "$1" | grep -qF "$2"' _ "$OUT" "$RETIRED" |
| 81 | check "main commit is an ancestor" git -C "$W" merge-base --is-ancestor origin/main HEAD |
| 82 | check "lock removed on exit" test ! -e "$W/.git/rebase-crumb.lock" |
| 83 | check "final line format" bash -c 'echo "$1" | tail -1 | grep -q "^rebased feat onto "' _ "$OUT" |
| 84 | # 8 stale lock (older than 2h) does not block |
| 85 | mk t8; touch -d '3 hours ago' "$W/.git/rebase-crumb.lock"; advance_main "$tmp/t8/seed" main-only.txt m |
| 86 | expect 0 "stale lock ignored" "$W" feat |
| 87 | # 9 CODE CONFLICT |
| 88 | mk t9 |
| 89 | ( cd "$tmp/t9/w" && git checkout -q feat && echo featside > src.txt && git add -A && git commit -q -m "feat src" && git push -q origin feat ) |
| 90 | advance_main "$tmp/t9/seed" src.txt mainside |
| 91 | before=$(ohead) |
| 92 | expect 3 "code conflict exits 3" "$W" feat |
| 93 | check "CODE CONFLICT message names file" bash -c "echo '$OUT' | grep -q 'CODE CONFLICT: src.txt'" |
| 94 | check "no rebase left in progress" test ! -d "$W/.git/rebase-merge" |
| 95 | check "code conflict pushed nothing" test "$(ohead)" = "$before" |
| 96 | check "lock removed after conflict" test ! -e "$W/.git/rebase-crumb.lock" |
| 97 | # 10 crumb-only conflict resolved with main's copy |
| 98 | mk t10 |
| 99 | ( cd "$tmp/t10/w" && echo featcrumb > .crumb && git add -A && git commit -q -m "feat crumb" && git push -q origin feat ) |
| 100 | advance_main "$tmp/t10/seed" .crumb maincrumb |
| 101 | expect 0 "crumb-only conflict resolved" "$W" feat |
| 102 | check "main's crumb copy kept as is (not compiled)" test "$(cat "$W/.crumb")" = "maincrumb" |
| 103 | check "feat source change kept" test -f "$W/feat.txt" |
| 104 | check "t10 no crumb compile commit added" nocrumbcommit "$W" |
| 105 | # 11 no crumb tool at all (CRUMB points nowhere, PATH stub would exit 99): still exit 0 and pushed |
| 106 | mk t11; advance_main "$tmp/t11/seed" main-only.txt m; before=$(ohead) |
| 107 | OUT=$(CRUMB=/nonexistent/crumb "$script" "$W" feat 2>&1); rc=$? |
| 108 | if [ "$rc" = 0 ]; then ok; else no "missing crumb tool still rebases and exits 0 (rc=$rc): $OUT"; fi |
| 109 | check "missing crumb tool: push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)" |
| 110 | check "missing crumb tool: origin head moved" test "$(ohead)" != "$before" |
| 111 | # 12 repo with no crumbs: same retired notice, no crumb commit |
| 112 | mk t12 nocrumb; advance_main "$tmp/t12/seed" main-only.txt m |
| 113 | expect 0 "no-crumb repo passes" "$W" feat |
| 114 | check "retired notice printed" bash -c 'echo "$1" | grep -qF "$2"' _ "$OUT" "$RETIRED" |
| 115 | check "no crumb commit made" test "$(git -C "$W" log -1 --format=%s)" = "feat" |
| 116 | # 13 lease protects against someone else's push between fetch and push |
| 117 | mk t13; advance_main "$tmp/t13/seed" main-only.txt m |
| 118 | cat > "$tmp/bin/racegit" <<S |
| 119 | #!/usr/bin/env bash |
| 120 | # real git, but just before the push another writer moves origin/feat |
| 121 | if [ "\$1" = push ]; then |
| 122 | git clone -q "$tmp/t13/origin.git" "$tmp/t13/racer" && ( cd "$tmp/t13/racer" && git checkout -q feat && echo other > other.txt && git add -A && git commit -q -m other && git push -q origin feat ) |
| 123 | fi |
| 124 | exec git "\$@" |
| 125 | S |
| 126 | chmod +x "$tmp/bin/racegit" |
| 127 | OUT=$(GIT="$tmp/bin/racegit" "$script" "$W" feat 2>&1); rc=$? |
| 128 | if [ "$rc" = 4 ]; then ok; else no "lease violation exits 4 (rc=$rc): $OUT"; fi |
| 129 | check "racer commit not overwritten" test "$(git -C "$O" log -1 --format=%s feat)" = "other" |
| 130 | |
| 131 | # 14 branch history already holds a crumb-only commit that conflicts with main's .crumb change |
| 132 | mk t14 |
| 133 | ( cd "$tmp/t14/w" && echo oldcompile > .crumb && git add -A && git commit -q -m "crumb compile" && git push -q origin feat ) |
| 134 | advance_main "$tmp/t14/seed" .crumb maincrumb |
| 135 | advance_main "$tmp/t14/seed" main-only.txt m |
| 136 | before=$(ohead) |
| 137 | expect 0 "prior crumb-only commit rebases" "$W" feat |
| 138 | check "t14 push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)" |
| 139 | check "t14 origin head moved" test "$(ohead)" != "$before" |
| 140 | check "t14 rebased onto main" git -C "$W" merge-base --is-ancestor origin/main HEAD |
| 141 | check "t14 old crumb-only commit dropped" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0 |
| 142 | check "t14 last commit is the feat source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat" |
| 143 | check "t14 no crumb compile commit added" nocrumbcommit "$W" |
| 144 | check "t14 crumb is main's copy" test "$(cat "$W/.crumb")" = "maincrumb" |
| 145 | check "t14 feat source change kept" test -f "$W/feat.txt" |
| 146 | check "t14 no rebase left in progress" test ! -d "$W/.git/rebase-merge" |
| 147 | |
| 148 | # 15 two commits in a row conflict on .crumb (a source commit touching .crumb, then a crumb-only commit), |
| 149 | # so `rebase --continue` after the first resolution exits non-zero because it stops at the second conflict |
| 150 | mk t15 |
| 151 | ( cd "$tmp/t15/w" && mkdir -p docs && echo d0 > docs/.crumb && echo f2 > feat2.txt && echo srcside > .crumb && git add -A && git commit -q -m "feat2" \ |
| 152 | && echo oc1 > .crumb && echo od1 > docs/.crumb && git add -A && git commit -q -m "crumb compile" \ |
| 153 | && echo oc2 > .crumb && echo od2 > docs/.crumb && git add -A && git commit -q -m "crumb compile" && git push -q origin feat ) |
| 154 | ( cd "$tmp/t15/seed" && git checkout -q main && mkdir -p docs && echo mdoc > docs/.crumb && echo maincrumb > .crumb && git add -A && git commit -q -m m1 && git push -q origin main ) |
| 155 | before=$(ohead) |
| 156 | expect 0 "consecutive crumb conflicts rebase" "$W" feat |
| 157 | check "t15 push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)" |
| 158 | check "t15 origin head moved" test "$(ohead)" != "$before" |
| 159 | check "t15 no old crumb compile commits left" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0 |
| 160 | check "t15 last commit is the feat2 source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat2" |
| 161 | check "t15 no crumb compile commit added" nocrumbcommit "$W" |
| 162 | check "t15 crumbs are main's copy" test "$(cat "$W/.crumb") $(cat "$W/docs/.crumb")" = "maincrumb mdoc" |
| 163 | check "t15 feat2 source change kept" test -f "$W/feat2.txt" |
| 164 | check "t15 no rebase left in progress" test ! -d "$W/.git/rebase-merge" |
| 165 | |
| 166 | # 16 stacked branch: feat sits on a base branch (source commit + crumb commit); base is squash-merged into main |
| 167 | mk t16 |
| 168 | ( cd "$tmp/t16/w" && git checkout -q -B feat origin/main && echo b > base.txt && git add -A && git commit -q -m "base src" \ |
| 169 | && echo basecrumb > .crumb && git add -A && git commit -q -m "crumb compile" \ |
| 170 | && echo f3 > feat3.txt && git add -A && git commit -q -m "child src" \ |
| 171 | && echo childcrumb > .crumb && git add -A && git commit -q -m "crumb compile" && git push -q -f origin feat ) |
| 172 | ( cd "$tmp/t16/seed" && git checkout -q main && echo b > base.txt && echo squashcrumb > .crumb && git add -A && git commit -q -m "squash of base" && git push -q origin main ) |
| 173 | before=$(ohead) |
| 174 | expect 0 "stacked branch after base squash rebases" "$W" feat |
| 175 | check "t16 origin head moved" test "$(ohead)" != "$before" |
| 176 | check "t16 base source not duplicated" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^base src$')" = 0 |
| 177 | check "t16 child source kept" test -f "$W/feat3.txt" |
| 178 | check "t16 last commit is the child source commit" test "$(git -C "$W" log -1 --format=%s)" = "child src" |
| 179 | check "t16 no crumb compile commit added" nocrumbcommit "$W" |
| 180 | check "t16 crumb is main's copy" test "$(cat "$W/.crumb")" = "squashcrumb" |
| 181 | check "t16 no old crumb compile commits left" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0 |
| 182 | |
| 183 | if [ ! -e "$tmp/crumb-called" ]; then ok; else no "crumb tool was called: $(cat "$tmp/crumb-called")"; fi |
| 184 | |
| 185 | echo "PASS $pass FAIL $bad" |
| 186 | [ "$bad" -eq 0 ] |