#!/usr/bin/env bash # gstack-upgrade — move this gstack checkout to the tip of its remote default # branch, unattended and fast-forward only. The deterministic twin of the # /gstack-upgrade skill's git fast path, for parent agents and fix lines. # # Usage: # gstack-upgrade --check fetch (refs only) and print `gstack-upgrade: current ` # (exit 0) or `gstack-upgrade: behind -> # ( commits)` (exit 1); changes nothing else # gstack-upgrade fast-forward, then reinstall: bin/gstack-capy-install on a # Capy install (the marker it writes in the git dir), # else ./setup --refresh-registered (setup runs migrations) # gstack-upgrade --ff-only-no-reinstall # fast-forward with every refusal below, print the upgraded # line and exit 0 without reinstalling (for a caller about # to run the installer itself: bin/gstack-capy-session) # # Refuses with exit 3 and changes nothing (UPGRADE_REFUSED) when the directory # is not a gstack checkout, HEAD is detached or not on the remote default # branch (a pin), any tracked byte is dirty, a local commit diverged, the # incoming release's Bun floor holds, or another updater holds the setup # lock; never stashes, never resets. An unreachable remote is # UPGRADE_FETCH_FAILED (exit 3). While another live session holds a workflow # lease on this installation (bin/gstack-lease live) it refuses with # UPGRADE_BUSY (exit 3); GSTACK_UPGRADE_FORCE=1 overrides. Prints # `gstack-upgrade: upgraded -> ` before reinstalling; a failed # reinstall exits 1 and the next run completes it. # Bash 3.2 plus git; the fetch is bounded by `timeout` when present. set -u BASH_COMPAT=50 ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" CHECK=0 REINSTALL=1 case "${1:-}" in --check) CHECK=1 ;; --ff-only-no-reinstall) REINSTALL=0 ;; '') ;; -h|--help) sed -n '2,28p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "gstack-upgrade: unknown argument '$1'" >&2; exit 2 ;; esac refuse() { echo "gstack-upgrade: $1; nothing was changed; fix: $2 (UPGRADE_REFUSED)" >&2; exit 3; } fetch_failed() { echo "gstack-upgrade: could not reach origin ($1); fix: check network access to $(git -C "$ROOT" remote get-url origin 2>/dev/null || echo origin), then re-run $ROOT/bin/gstack-upgrade (UPGRADE_FETCH_FAILED)" >&2; exit 3; } bounded() { if command -v timeout >/dev/null 2>&1; then timeout "${GSTACK_UPGRADE_FETCH_TIMEOUT:-60}" "$@"; else "$@"; fi; } # Egress receipts (_receipted_git), fail-open like the session-start updater: # a user-run upgrade must not die over an audit-log hiccup. # shellcheck source=bin/gstack-egress-lib.sh . "$ROOT/bin/gstack-egress-lib.sh" 2>/dev/null || _receipted_git() { shift 5; "$@"; } [ -f "$ROOT/VERSION" ] && [ -f "$ROOT/setup" ] && [ "$(git -C "$ROOT" rev-parse --show-toplevel 2>/dev/null)" = "$ROOT" ] \ || refuse "$ROOT is not a gstack checkout" "run gstack-upgrade from a git checkout of https://github.com/garrytan/gstack" export GIT_TERMINAL_PROMPT=0 HOST=$(git -C "$ROOT" remote get-url origin 2>/dev/null); HOST="${HOST#*://}"; HOST="${HOST#*@}"; HOST="${HOST%%[/:]*}" R="user ran gstack-upgrade" INSTALLED=$(head -1 "$ROOT/VERSION") BRANCH=$(git -C "$ROOT" symbolic-ref -q --short HEAD) \ || refuse "HEAD is detached at $(git -C "$ROOT" rev-parse --short HEAD) (a pinned revision)" "git -C $ROOT checkout , then re-run" DEFAULT=$(_receipted_git open gstack-upgrade "${HOST:-unknown}" gstack-self-update-pull "$R" bounded git -C "$ROOT" ls-remote --symref origin HEAD 2>/dev/null | sed -n 's|^ref: refs/heads/\([^[:space:]]*\)[[:space:]]*HEAD$|\1|p' | head -1) [ -n "$DEFAULT" ] || fetch_failed "git ls-remote origin HEAD failed" [ "$BRANCH" = "$DEFAULT" ] || refuse "on branch $BRANCH, not the remote default branch $DEFAULT (a pin)" "git -C $ROOT checkout $DEFAULT, then re-run" _receipted_git open gstack-upgrade "${HOST:-unknown}" gstack-self-update-pull "$R" bounded git -C "$ROOT" fetch --quiet origin "$DEFAULT" 2>/dev/null || fetch_failed "git fetch origin $DEFAULT failed" INCOMING=$(git -C "$ROOT" rev-parse FETCH_HEAD) REMOTE_VER=$(git -C "$ROOT" show FETCH_HEAD:VERSION 2>/dev/null | head -1) PENDING="$(git -C "$ROOT" rev-parse --absolute-git-dir)/gstack-upgrade.pending" reinstall() { # FROM — run the install step; a pending marker survives a failure printf '%s\n' "$1" > "$PENDING" cd "$ROOT" || exit 1 if [ -f "$(git rev-parse --absolute-git-dir 2>/dev/null)/gstack-capy-install.state" ] && [ -x "$ROOT/bin/gstack-capy-install" ]; then GSTACK_CAPY_UPGRADED_FROM="$1" "$ROOT/bin/gstack-capy-install" &2 exit 1 } if [ "$INCOMING" = "$(git -C "$ROOT" rev-parse HEAD)" ] || git -C "$ROOT" merge-base --is-ancestor FETCH_HEAD HEAD 2>/dev/null; then if [ -f "$PENDING" ]; then echo "gstack-upgrade: current $INSTALLED; reinstall pending from $(head -1 "$PENDING")" [ "$CHECK" -eq 1 ] && exit 1 reinstall "$(head -1 "$PENDING")" fi echo "gstack-upgrade: current $INSTALLED" exit 0 fi if [ "$CHECK" -eq 1 ]; then echo "gstack-upgrade: behind $INSTALLED -> ${REMOTE_VER:-unknown} ($(git -C "$ROOT" rev-list --count HEAD..FETCH_HEAD 2>/dev/null || echo '?') commits)" exit 1 fi DIRTY=$(git -C "$ROOT" status --porcelain --untracked-files=no 2>/dev/null | cut -c4- | tr '\n' ' ' | sed 's/ $//') [ -z "$DIRTY" ] || refuse "tracked files changed: $DIRTY" "commit or move those changes (git -C $ROOT status --short), then re-run" if ! git -C "$ROOT" merge-base --is-ancestor HEAD FETCH_HEAD 2>/dev/null; then _receipted_git open gstack-upgrade "${HOST:-unknown}" gstack-self-update-pull "$R" git -C "$ROOT" fetch --quiet --unshallow origin "$DEFAULT" 2>/dev/null || true git -C "$ROOT" merge-base --is-ancestor HEAD FETCH_HEAD 2>/dev/null \ || refuse "diverged: $(git -C "$ROOT" rev-list --count FETCH_HEAD..HEAD 2>/dev/null || echo '?') local commits are not on origin/$DEFAULT" "git -C $ROOT log --oneline FETCH_HEAD..HEAD shows them; move them to another branch, then re-run" fi # Workflow leases (bin/gstack-lease): sessions sharing this installation that # are alive and not this process's own chain. Moving the checkout under them # is what the lease exists to prevent; the caller's own session never counts. if [ "${GSTACK_UPGRADE_FORCE:-}" != 1 ] && [ -x "$ROOT/bin/gstack-lease" ] \ && BUSY=$("$ROOT/bin/gstack-lease" live --root "$ROOT" --exclude-ancestry-of $$ 2>/dev/null); then HOLDERS="" while read -r _pid _age; do HOLDERS="$HOLDERS${HOLDERS:+, }pid $_pid ($_age)"; done <&2 exit 3 fi # shellcheck source=bin/gstack-bun-version.sh . "$ROOT/bin/gstack-bun-version.sh" 2>/dev/null && HOLD=$(gstack_bun_incoming_hold "$ROOT" "$INCOMING") \ && refuse "the incoming release holds: $HOLD" "install Bun $(git -C "$ROOT" show "$INCOMING:bin/gstack-bun-version.sh" 2>/dev/null | sed -n 's/^GSTACK_BUN_TESTED="\(.*\)"/\1/p') (bin/gstack-capy-install on Capy), then re-run" # The same lock directory the session-start auto-updater (bin/gstack-session-update) # holds while it pulls and runs setup; a live holder means refuse, never wait. GSTACK_STATE_ROOT=$("$ROOT/bin/gstack-paths" --get GSTACK_STATE_ROOT); : "${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}" LOCK_DIR="$GSTACK_STATE_ROOT/.setup-lock" mkdir -p "$GSTACK_STATE_ROOT" 2>/dev/null mkdir "$LOCK_DIR" 2>/dev/null || refuse "another gstack updater holds $LOCK_DIR (pid $(cat "$LOCK_DIR/pid" 2>/dev/null || echo unknown))" "wait for it to finish, then re-run" echo "$$" > "$LOCK_DIR/pid" trap '[ "$(cat "$LOCK_DIR/pid" 2>/dev/null)" = "$$" ] && rm -rf "$LOCK_DIR" 2>/dev/null' EXIT git -C "$ROOT" merge --ff-only -q FETCH_HEAD 2>/dev/null || refuse "git merge --ff-only FETCH_HEAD failed" "git -C $ROOT status, then re-run" NEW=$(head -1 "$ROOT/VERSION") echo "gstack-upgrade: upgraded $INSTALLED -> $NEW" rm -rf "$LOCK_DIR"; trap - EXIT [ "$REINSTALL" -eq 1 ] || exit 0 reinstall "$INSTALLED"