interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / scripts / test_rebase_crumb.sh

186 lines · 11041 bytes Raw Blame History
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.
6set -u
7here="$(cd "$(dirname "$0")" && pwd)"
8script="$here/rebase-crumb.sh"
9tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
10export GIT_AUTHOR_NAME=T GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=T GIT_COMMITTER_EMAIL=t@example.invalid
11export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
12FAKEURL="https://github.com/aien-dev/fake-repo"
13
14mkdir -p "$tmp/bin"
15cat > "$tmp/bin/crumb" <<S
16#!/usr/bin/env bash
17# retired: any call is a test failure (recorded, then exit 99)
18echo "\$*" >> "$tmp/crumb-called"
19exit 99
20S
21chmod +x "$tmp/bin/crumb"
22export PATH="$tmp/bin:$PATH" CRUMB="$tmp/bin/crumb"
23RETIRED="crumb: retired (DRA-97), conflicting .crumb files keep main's copy; no compile, verify or crumb commit"
24nocrumbcommit() { test "$(git -C "$1" log --format=%s origin/main..HEAD | grep -c '^crumb compile (after rebase onto main)$')" = 0; }
25
26pass=0; bad=0
27ok() { pass=$((pass+1)); }
28no() { bad=$((bad+1)); echo "TEST FAIL: $1"; }
29expect() { # 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}
33check() { # 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.
38mk() {
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}
48advance_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}
51ohead() { git -C "$O" rev-parse "refs/heads/feat"; }
52
53# 1 dirty tree
54mk t1; echo x >> "$W/feat.txt"
55expect 2 "dirty tree refused" "$W" feat
56# 2 branch main
57mk t2
58expect 2 "branch main refused" "$W" main
59# 3 wrong remote
60mk t3; git -C "$W" remote set-url origin https://github.com/other-org/x
61expect 2 "wrong remote refused" "$W" feat
62# 4 lock present (fresh)
63mk t4; touch "$W/.git/rebase-crumb.lock"
64expect 2 "fresh lock refused" "$W" feat
65check "fresh lock left in place" test -e "$W/.git/rebase-crumb.lock"
66# 5 not a git checkout
67mkdir -p "$tmp/plain"
68expect 2 "non-git directory refused" "$tmp/plain" feat
69# 6 origin branch missing
70mk t6
71expect 2 "missing origin branch refused" "$W" nosuchbranch
72# 7 clean rebase, push, no crumb commit
73mk t7; advance_main "$tmp/t7/seed" main-only.txt m
74before=$(ohead)
75expect 0 "clean rebase succeeds" "$W" feat
76check "push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)"
77check "origin head moved" test "$(ohead)" != "$before"
78check "last commit is the feat source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat"
79check "no crumb compile commit added" nocrumbcommit "$W"
80check "retired crumb notice printed" bash -c 'echo "$1" | grep -qF "$2"' _ "$OUT" "$RETIRED"
81check "main commit is an ancestor" git -C "$W" merge-base --is-ancestor origin/main HEAD
82check "lock removed on exit" test ! -e "$W/.git/rebase-crumb.lock"
83check "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
85mk t8; touch -d '3 hours ago' "$W/.git/rebase-crumb.lock"; advance_main "$tmp/t8/seed" main-only.txt m
86expect 0 "stale lock ignored" "$W" feat
87# 9 CODE CONFLICT
88mk 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 )
90advance_main "$tmp/t9/seed" src.txt mainside
91before=$(ohead)
92expect 3 "code conflict exits 3" "$W" feat
93check "CODE CONFLICT message names file" bash -c "echo '$OUT' | grep -q 'CODE CONFLICT: src.txt'"
94check "no rebase left in progress" test ! -d "$W/.git/rebase-merge"
95check "code conflict pushed nothing" test "$(ohead)" = "$before"
96check "lock removed after conflict" test ! -e "$W/.git/rebase-crumb.lock"
97# 10 crumb-only conflict resolved with main's copy
98mk t10
99( cd "$tmp/t10/w" && echo featcrumb > .crumb && git add -A && git commit -q -m "feat crumb" && git push -q origin feat )
100advance_main "$tmp/t10/seed" .crumb maincrumb
101expect 0 "crumb-only conflict resolved" "$W" feat
102check "main's crumb copy kept as is (not compiled)" test "$(cat "$W/.crumb")" = "maincrumb"
103check "feat source change kept" test -f "$W/feat.txt"
104check "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
106mk t11; advance_main "$tmp/t11/seed" main-only.txt m; before=$(ohead)
107OUT=$(CRUMB=/nonexistent/crumb "$script" "$W" feat 2>&1); rc=$?
108if [ "$rc" = 0 ]; then ok; else no "missing crumb tool still rebases and exits 0 (rc=$rc): $OUT"; fi
109check "missing crumb tool: push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)"
110check "missing crumb tool: origin head moved" test "$(ohead)" != "$before"
111# 12 repo with no crumbs: same retired notice, no crumb commit
112mk t12 nocrumb; advance_main "$tmp/t12/seed" main-only.txt m
113expect 0 "no-crumb repo passes" "$W" feat
114check "retired notice printed" bash -c 'echo "$1" | grep -qF "$2"' _ "$OUT" "$RETIRED"
115check "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
117mk t13; advance_main "$tmp/t13/seed" main-only.txt m
118cat > "$tmp/bin/racegit" <<S
119#!/usr/bin/env bash
120# real git, but just before the push another writer moves origin/feat
121if [ "\$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 )
123fi
124exec git "\$@"
125S
126chmod +x "$tmp/bin/racegit"
127OUT=$(GIT="$tmp/bin/racegit" "$script" "$W" feat 2>&1); rc=$?
128if [ "$rc" = 4 ]; then ok; else no "lease violation exits 4 (rc=$rc): $OUT"; fi
129check "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
132mk t14
133( cd "$tmp/t14/w" && echo oldcompile > .crumb && git add -A && git commit -q -m "crumb compile" && git push -q origin feat )
134advance_main "$tmp/t14/seed" .crumb maincrumb
135advance_main "$tmp/t14/seed" main-only.txt m
136before=$(ohead)
137expect 0 "prior crumb-only commit rebases" "$W" feat
138check "t14 push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)"
139check "t14 origin head moved" test "$(ohead)" != "$before"
140check "t14 rebased onto main" git -C "$W" merge-base --is-ancestor origin/main HEAD
141check "t14 old crumb-only commit dropped" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0
142check "t14 last commit is the feat source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat"
143check "t14 no crumb compile commit added" nocrumbcommit "$W"
144check "t14 crumb is main's copy" test "$(cat "$W/.crumb")" = "maincrumb"
145check "t14 feat source change kept" test -f "$W/feat.txt"
146check "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
150mk 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 )
155before=$(ohead)
156expect 0 "consecutive crumb conflicts rebase" "$W" feat
157check "t15 push reached origin" test "$(ohead)" = "$(git -C "$W" rev-parse HEAD)"
158check "t15 origin head moved" test "$(ohead)" != "$before"
159check "t15 no old crumb compile commits left" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0
160check "t15 last commit is the feat2 source commit" test "$(git -C "$W" log -1 --format=%s)" = "feat2"
161check "t15 no crumb compile commit added" nocrumbcommit "$W"
162check "t15 crumbs are main's copy" test "$(cat "$W/.crumb") $(cat "$W/docs/.crumb")" = "maincrumb mdoc"
163check "t15 feat2 source change kept" test -f "$W/feat2.txt"
164check "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
167mk 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 )
173before=$(ohead)
174expect 0 "stacked branch after base squash rebases" "$W" feat
175check "t16 origin head moved" test "$(ohead)" != "$before"
176check "t16 base source not duplicated" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^base src$')" = 0
177check "t16 child source kept" test -f "$W/feat3.txt"
178check "t16 last commit is the child source commit" test "$(git -C "$W" log -1 --format=%s)" = "child src"
179check "t16 no crumb compile commit added" nocrumbcommit "$W"
180check "t16 crumb is main's copy" test "$(cat "$W/.crumb")" = "squashcrumb"
181check "t16 no old crumb compile commits left" test "$(git -C "$W" log --format=%s origin/main..HEAD | grep -c '^crumb compile$')" = 0
182
183if [ ! -e "$tmp/crumb-called" ]; then ok; else no "crumb tool was called: $(cat "$tmp/crumb-called")"; fi
184
185echo "PASS $pass FAIL $bad"
186[ "$bad" -eq 0 ]