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 / rebase-crumb.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Rebase a PR branch onto origin/main, resolve conflicts that are only in .crumb files by taking |
| 3 | # main's copy, and push with a lease on the old head. Crumb compile/verify is retired (2026-11-07, |
| 4 | # DRA-97): the crumb tool is never run and no crumb commit is added; .crumb files are history. |
| 5 | # |
| 6 | # Usage: rebase-crumb.sh <worktree> <branch> |
| 7 | # Env: GIT (default git), so tests can stub it. |
| 8 | # Exit: 0 done, 2 precondition refused, 3 CODE CONFLICT (rebase aborted), 4 checkout, rebase or push |
| 9 | # failure (nothing pushed). |
| 10 | set -u |
| 11 | GIT=${GIT:-git} |
| 12 | |
| 13 | refuse() { echo "REFUSED: $1" >&2; exit 2; } |
| 14 | |
| 15 | [ $# -eq 2 ] || { echo "usage: rebase-crumb.sh <worktree> <branch>" >&2; exit 2; } |
| 16 | W=$1; B=$2 |
| 17 | cd "$W" 2>/dev/null || refuse "worktree $W does not exist" |
| 18 | $GIT rev-parse --is-inside-work-tree >/dev/null 2>&1 || refuse "$W is not a git checkout" |
| 19 | [ -z "$($GIT status --porcelain)" ] || refuse "working tree is not clean" |
| 20 | [ "$B" != "main" ] || refuse "branch is main" |
| 21 | URL=$($GIT config --get remote.origin.url 2>/dev/null) || refuse "no origin remote" |
| 22 | case "$URL" in |
| 23 | *github.com/aien-dev/*|*git@github.com:aien-dev/*) ;; |
| 24 | *) refuse "origin URL is not under aien-dev: $URL" ;; |
| 25 | esac |
| 26 | $GIT fetch -q origin || refuse "fetch failed" |
| 27 | $GIT rev-parse -q --verify "refs/remotes/origin/$B" >/dev/null || refuse "origin/$B does not exist" |
| 28 | |
| 29 | GD=$($GIT rev-parse --git-dir) |
| 30 | LOCK="$GD/rebase-crumb.lock" |
| 31 | if [ -e "$LOCK" ] && [ -z "$(find "$LOCK" -mmin +120 2>/dev/null)" ]; then |
| 32 | refuse "lock $LOCK is fresh (another run, or younger than 2 hours)" |
| 33 | fi |
| 34 | echo $$ > "$LOCK" || refuse "cannot create lock" |
| 35 | trap 'rm -f "$LOCK"' EXIT |
| 36 | |
| 37 | LEASE=$($GIT rev-parse "origin/$B") |
| 38 | $GIT checkout -q -B "$B" "origin/$B" || { echo "checkout failed" >&2; exit 4; } |
| 39 | |
| 40 | rebasing() { [ -d "$($GIT rev-parse --git-path rebase-merge)" ] || [ -d "$($GIT rev-parse --git-path rebase-apply)" ]; } |
| 41 | |
| 42 | # Advance past the commit that was just resolved. `rebase --continue` (and --skip) also exit non-zero |
| 43 | # when they stop at the NEXT commit's conflict; that is progress, not failure, so the loop must see it. |
| 44 | # Returns 0 when the loop should look again, 1 on a real failure. |
| 45 | advance() { |
| 46 | GIT_EDITOR=true $GIT rebase --continue >/dev/null 2>&1 && return 0 |
| 47 | rebasing || return 0 |
| 48 | [ -z "$($GIT diff --name-only --diff-filter=U)" ] || return 0 |
| 49 | # A resolved commit that became empty cannot be continued; skip it. |
| 50 | if [ -z "$($GIT diff --cached --name-only)" ]; then |
| 51 | $GIT rebase --skip >/dev/null 2>&1 && return 0 |
| 52 | rebasing || return 0 |
| 53 | [ -z "$($GIT diff --name-only --diff-filter=U)" ] || return 0 |
| 54 | fi |
| 55 | return 1 |
| 56 | } |
| 57 | fail_rebase() { echo "rebase continue failed" >&2; $GIT status --short | head; $GIT rebase --abort; exit 4; } |
| 58 | |
| 59 | $GIT rebase -q origin/main >/dev/null 2>&1 |
| 60 | while rebasing; do |
| 61 | C=$($GIT diff --name-only --diff-filter=U) |
| 62 | if [ -z "$C" ]; then |
| 63 | advance || fail_rebase |
| 64 | continue |
| 65 | fi |
| 66 | NONCRUMB=$(echo "$C" | grep -v '\.crumb$' || true) |
| 67 | if [ -n "$NONCRUMB" ]; then |
| 68 | echo "CODE CONFLICT: $(echo "$NONCRUMB" | tr '\n' ' ')" |
| 69 | $GIT rebase --abort |
| 70 | exit 3 |
| 71 | fi |
| 72 | # During a rebase "ours" is the branch being rebased onto, which is main. |
| 73 | echo "$C" | while IFS= read -r f; do |
| 74 | if $GIT cat-file -e "origin/main:$f" 2>/dev/null; then |
| 75 | $GIT checkout -q --ours -- "$f" && $GIT add -- "$f" |
| 76 | else |
| 77 | $GIT rm -q -f -- "$f" |
| 78 | fi |
| 79 | done |
| 80 | advance || fail_rebase |
| 81 | done |
| 82 | |
| 83 | echo "crumb: retired (DRA-97), conflicting .crumb files keep main's copy; no compile, verify or crumb commit" |
| 84 | |
| 85 | $GIT push -q --force-with-lease="refs/heads/$B:$LEASE" origin "$B" || { echo "push failed (lease or network)" >&2; exit 4; } |
| 86 | echo "rebased $B onto $($GIT rev-parse --short origin/main): $($GIT log --oneline -1 | cut -c1-70)" |