Never remove a worktree testflight.sh didn't make
Astra's Task 16 finding 1 (blocker): with --upload, a worktree already at the fixed base-tf-BUILD path made git worktree add fail, and the EXIT trap then force-removed that existing worktree, unsaved work and all. testflight.sh now makes its worktree in a fresh mktemp directory (scripts/worktree-lib.sh), sets the ownership flag only after a successful add, and removes only what it made; its build scratch is a fresh directory too, where it used to rm -rf a fixed path. worktree-lib_test.sh, in check-native.sh, proves in a disposable repository that a dirty worktree at the old path survives a failed run and a good one. release-mac.sh makes no worktree, and removes only its own mktemp directory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+88
−8
native/scripts/check-native.sh+1 −0native/scripts/testflight.sh+14 −8native/scripts/worktree-lib.sh+30 −0native/scripts/worktree-lib_test.sh+43 −0
diff --git a/native/scripts/check-native.sh b/native/scripts/check-native.sh| index e4f3d0e..d9056d9 100755 |
| --- a/native/scripts/check-native.sh |
| +++ b/native/scripts/check-native.sh |
| @@ -15,6 +15,7 @@ for argument in "$@"; do |
| esac |
| done |
| python3 scripts/check-strings.py |
| +scripts/worktree-lib_test.sh |
| [ "$core_only" = 1 ] && exit 0 |
| if [ "$(uname -s)" != Darwin ]; then |
| echo "the Apple checks need macOS; use --core-only elsewhere" >&2; exit 2 |
diff --git a/native/scripts/testflight.sh b/native/scripts/testflight.sh| index daad55c..7f2ce7c 100755 |
| --- a/native/scripts/testflight.sh |
| +++ b/native/scripts/testflight.sh |
| @@ -12,7 +12,8 @@ |
| # command. --upload runs them in a detached worktree of the commit: the |
| # Apple Distribution identity, Base's App Store profile (PROFILE, by name) |
| # and the App Store Connect record "Oficina Base" (Paul's portal steps) |
| -# must exist. The worktree goes when it's done. |
| +# must exist. The worktree is a fresh one under $TMPDIR, and only a |
| +# worktree this run made is removed when it's done (worktree-lib.sh). |
| set -eu |
| VERSION=${1:?usage: testflight.sh VERSION BUILD [--upload]} |
| BUILD=${2:?usage: testflight.sh VERSION BUILD [--upload]} |
| @@ -24,8 +25,6 @@ COMMIT=$(git -C "$REPO" rev-parse HEAD) |
| TEAM=9U4A84C4WK |
| PROFILE=${PROFILE:-Oficina Base iOS App Store 2026-11-23} |
| KEYS=$HOME/private_keys/oficina |
| -WT=$(dirname "$REPO")/base-tf-$BUILD |
| -OUT=${TMPDIR:-/tmp}/base-tf-build-$BUILD |
| run() { echo " + $*"; [ "$UPLOAD" = 0 ] || "$@"; } |
| die() { echo "error: $*" >&2; exit 1; } |
| @@ -47,15 +46,22 @@ else |
| echo " checked only with --upload: the distribution identity, the App Store Connect key, profile '$PROFILE'" |
| fi |
| -cleanup() { [ "$UPLOAD" = 0 ] || git -C "$REPO" worktree remove --force "$WT" 2>/dev/null || true; } |
| -trap cleanup EXIT |
| -run git -C "$REPO" worktree add -q --detach "$WT" "$COMMIT" |
| +. "$NATIVE/scripts/worktree-lib.sh" |
| +trap 'own_worktree_cleanup "$REPO"' EXIT |
| +if [ "$UPLOAD" = 1 ]; then |
| + own_worktree_add "$REPO" "$COMMIT" "base-tf-$BUILD" || die "could not make a worktree of $COMMIT" |
| + WT=$OWN_WT |
| + echo " + git worktree add --detach $WT $COMMIT" |
| +else |
| + WT='$TMPDIR/base-tf-'$BUILD'.XXXXXX' |
| + echo " + git worktree add --detach $WT $COMMIT (a fresh directory; only it is removed after)" |
| +fi |
| # The font is fetched, never committed: the worktree gets its own copy. |
| run mkdir -p "$WT/internal/look/static/fonts/timeless/1.094" |
| run cp "$REPO/internal/look/static/fonts/timeless/1.094/TimelessSansVF.woff2" "$WT/internal/look/static/fonts/timeless/1.094/" |
| run "$WT/native/scripts/fonts-into-bundle.sh" --release |
| -run rm -rf "$OUT" |
| -run mkdir -p "$OUT" |
| +# The build's own scratch, fresh: never a directory someone else left. |
| +if [ "$UPLOAD" = 1 ]; then OUT=$(mktemp -d "${TMPDIR:-/tmp}/base-tf-build-$BUILD.XXXXXX"); else OUT='$TMPDIR/base-tf-build-'$BUILD'.XXXXXX'; fi |
| run xcodegen generate --quiet --spec "$WT/native/ios/project.yml" |
| run xcodebuild archive -project "$WT/native/ios/BasePhone.xcodeproj" -scheme BasePhone -destination generic/platform=iOS \ |
| -derivedDataPath "$OUT/derived" -archivePath "$OUT/BasePhone.xcarchive" \ |
diff --git a/native/scripts/worktree-lib.sh b/native/scripts/worktree-lib.sh| new file mode 100644 |
| index 0000000..4f0ba6a |
| --- /dev/null |
| +++ b/native/scripts/worktree-lib.sh |
| @@ -0,0 +1,30 @@ |
| +# Sourced by the release scripts: a worktree this run makes, and only that |
| +# one removed at the end. The destination is a fresh, unique, empty |
| +# directory (mktemp), so it can never be someone's existing worktree; the |
| +# ownership flag is set only once `git worktree add` has succeeded, so a |
| +# failed add removes nothing but the empty directory this run made. |
| +# |
| +# own_worktree_add REPO COMMIT PREFIX # sets OWN_WT to its path |
| +# own_worktree_cleanup REPO # in the EXIT trap |
| +OWN_WT="" |
| +OWN_WT_ADDED=0 |
| +OWN_WT_MADE=0 |
| + |
| +own_worktree_add() { |
| + _repo=$1 _commit=$2 _prefix=$3 |
| + OWN_WT=$(mktemp -d "${TMPDIR:-/tmp}/$_prefix.XXXXXX") || return 1 |
| + OWN_WT_MADE=1 |
| + # mktemp made it, empty: anything else there is a collision, refused. |
| + if [ -n "$(ls -A "$OWN_WT")" ]; then echo "worktree: $OWN_WT is not empty" >&2; return 1; fi |
| + git -C "$_repo" worktree add -q --detach "$OWN_WT" "$_commit" || return 1 |
| + OWN_WT_ADDED=1 |
| +} |
| + |
| +own_worktree_cleanup() { |
| + _repo=$1 |
| + if [ "$OWN_WT_ADDED" = 1 ] && [ -n "$OWN_WT" ]; then |
| + git -C "$_repo" worktree remove --force "$OWN_WT" 2>/dev/null || true |
| + elif [ "$OWN_WT_MADE" = 1 ] && [ -n "$OWN_WT" ]; then |
| + rmdir "$OWN_WT" 2>/dev/null || true |
| + fi |
| +} |
diff --git a/native/scripts/worktree-lib_test.sh b/native/scripts/worktree-lib_test.sh| new file mode 100755 |
| index 0000000..0a27b7b |
| --- /dev/null |
| +++ b/native/scripts/worktree-lib_test.sh |
| @@ -0,0 +1,43 @@ |
| +#!/bin/sh |
| +# The release scripts never remove a worktree they didn't make (Astra's |
| +# Task 16 finding 1): in a disposable repository, a dirty worktree already |
| +# at the old fixed path survives a failed run and a good one; a run's own |
| +# worktree goes; a failed add leaves nothing behind. |
| +set -eu |
| +here=$(cd "$(dirname "$0")" && pwd) |
| +scratch=$(mktemp -d "${TMPDIR:-/tmp}/base-wt-test.XXXXXX") |
| +trap 'rm -rf "$scratch"' EXIT |
| +repo=$scratch/repo |
| +git init -q "$repo" |
| +git -C "$repo" -c user.name=t -c user.email=t@x commit -q --allow-empty -m one |
| +commit=$(git -C "$repo" rev-parse HEAD) |
| +# Someone's worktree at the path the old script used, with unsaved work. |
| +theirs=$scratch/base-tf-1 |
| +git -C "$repo" worktree add -q --detach "$theirs" "$commit" |
| +echo unsaved > "$theirs/unsaved.txt" |
| +fail() { echo "worktree-lib_test: $*" >&2; exit 1; } |
| + |
| +# A failed run: the add fails (no such commit); cleanup removes nothing of theirs. |
| +( |
| + export TMPDIR=$scratch |
| + . "$here/worktree-lib.sh" |
| + if own_worktree_add "$repo" deadbeefdeadbeef base-tf-1 2>/dev/null; then exit 3; fi |
| + own_worktree_cleanup "$repo" |
| + [ ! -e "$OWN_WT" ] || exit 4 |
| +) || fail "a failed add left something behind (status $?)" |
| +[ -f "$theirs/unsaved.txt" ] || fail "a failed run deleted an existing worktree" |
| +git -C "$repo" worktree list | grep -q "$(cd "$theirs" && pwd -P)" || fail "a failed run unregistered an existing worktree" |
| + |
| +# A good run: its own worktree, somewhere new, removed at the end; theirs stays. |
| +mine=$( |
| + export TMPDIR=$scratch |
| + . "$here/worktree-lib.sh" |
| + own_worktree_add "$repo" "$commit" base-tf-1 |
| + [ "$OWN_WT" != "$theirs" ] || exit 5 |
| + [ -f "$OWN_WT/.git" ] || exit 6 |
| + echo "$OWN_WT" |
| + own_worktree_cleanup "$repo" |
| +) || fail "a good run failed" |
| +[ ! -e "$mine" ] || fail "a run's own worktree was left behind" |
| +[ -f "$theirs/unsaved.txt" ] || fail "a good run deleted an existing worktree" |
| +echo "worktree-lib_test: ok" |