Files
buzz/scripts/mobile-worktree-overrides.sh
T
1a56b7cc9e feat(mobile): add worktree-aware debug identities (#2858)
**Category:** improvement  
**User Impact:** Developers can identify which worktree produced a
mobile debug app, keep a bounded set of worktree builds installed side
by side, and preserve each worktree app's login and local state while
switching branches.

**Problem:** Mobile debug builds from every checkout currently appear as
the same “Buzz” app and share one application identity, so the running
source is ambiguous and one worktree build replaces another. A
branch-keyed identity would avoid replacement but create stale installs
and fresh app state on every branch switch.

**Solution:** Give each linked worktree a stable Debug-only application
identity derived from its sanitized directory name. Show the sanitized
branch name (or short commit SHA when detached) in the display label,
persist generated native overrides for direct IDE builds, and leave
Release/Profile identities unchanged. Worktree defaults remain lower
precedence than a developer's iOS `AppOverrides.xcconfig`. `just
mobile-clean` provides a safe cleanup path for suffixed worktree
installs while preserving production Buzz.

<details>
<summary>File changes</summary>

**.github/workflows/ci.yml**  
Runs the expanded worktree override contract when relevant mobile or
native configuration changes.

**AGENTS.md**  
Documents worktree-aware mobile development and cleanup for contributors
and agents.

**Justfile**  
Generates overrides before mobile development and Android debug builds,
and exposes `just mobile-clean`.

**mobile/README.md**  
Explains stable per-worktree identities, branch/SHA labels, direct IDE
usage, cleanup, and Release/Profile guarantees.

**mobile/android/.gitignore**  
Ignores generated worktree properties.

**mobile/android/app/build.gradle.kts**  
Loads and validates generated properties, then applies the application
ID suffix and display label to Android Debug only.

**mobile/android/app/src/main/AndroidManifest.xml**  
Resolves the Android app label through an overridable string resource.

**mobile/ios/.gitignore**  
Ignores generated iOS worktree settings.

**mobile/ios/Flutter/Debug.xcconfig**  
Loads generated worktree defaults before developer `AppOverrides`, so
personal signing overrides retain precedence.

**mobile/ios/Flutter/Release.xcconfig**  
Pins the production display name and bundle identifier for
Release/Profile builds.

**mobile/ios/Runner/Info.plist**  
Resolves the visible iOS app name from build settings.

**scripts/mobile-worktree-overrides.sh**  
Detects linked worktrees, derives a stable directory-keyed identity,
sanitizes branch/SHA display context, writes native Debug overrides, and
removes stale overrides in the main checkout.

**scripts/mobile-worktree-clean.sh**  
Lists or removes suffixed Buzz worktree installs from booted iOS
simulators and connected Android emulators without matching production
IDs; supports `--dry-run`.

**scripts/test-mobile-worktree-overrides.sh**  
Covers worktree detection, branch-switch identity stability, detached
HEAD fallback, special-character sanitization, iOS override precedence,
brace-aware Release/Profile purity, cleanup safety, ignores, and command
integration.

</details>

## Reproduction steps

1. From a linked worktree, activate the repository toolchain and run
`just mobile-dev`.
2. Inspect the running app: its label should be `Buzz
(<sanitized-branch>)`, while its application ID suffix is derived from
the worktree directory.
3. Switch branches in the same worktree, rerun the override script, and
confirm the application ID remains stable while the display label
updates. In detached HEAD, confirm the label uses a short SHA.
4. Build Debug from a second worktree and confirm both apps remain
installed side by side with independent state.
5. Build from Xcode after setting `AppOverrides.xcconfig` and confirm
developer overrides still win over generated worktree defaults.
6. Run `just mobile-clean --dry-run`, then `just mobile-clean`, and
confirm suffixed worktree installs are targeted while the production app
is preserved.
7. Build Release/Profile and confirm the production name and application
identity remain unchanged.
8. Run `scripts/test-mobile-worktree-overrides.sh`, `just mobile-check`,
`just mobile-test`, and `just mobile-build-android`.

## Screenshots / demos

| iOS — labeled app switcher | iOS — side-by-side installs |
| --- | --- |
| <img width="360" alt="Buzz worktree label in the iOS app switcher"
src="https://github.com/user-attachments/assets/4bcae067-7ce5-4333-bb11-2803c4107663"
/> | <img width="360" alt="Buzz production and worktree debug apps
installed side by side on iOS"
src="https://github.com/user-attachments/assets/08a107b5-fdf2-463a-8a4c-81d41d7bf5e7"
/> |

| Android — side-by-side installs | Android — labeled app switcher |
| --- | --- |
| <img width="360" alt="Buzz production and worktree debug apps
installed side by side on Android"
src="https://github.com/user-attachments/assets/4f5841a1-adae-42da-ae84-47c09ec85fb9"
/> | <img width="360" alt="Buzz worktree label in the Android app
switcher"
src="https://github.com/user-attachments/assets/0546ff51-efcc-4cb6-a4bd-2a3af26cd60f"
/> |

---------

Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@buzz.block.builderlab.xyz>
2026-07-26 13:47:35 -07:00

94 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Writes worktree-aware identity overrides for mobile debug builds, mirroring
# the desktop dev experience in scripts/instance-env.sh: debug builds produced
# from a git worktree get a unique app identifier keyed to the WORKTREE
# DIRECTORY NAME (stable across branch switches, so installs and login state
# are bounded by worktree count) plus a display-only branch label (short SHA
# when detached).
#
# In a worktree this writes two gitignored override files consumed by the
# native build systems (so direct Xcode / Android Studio / `flutter run`
# builds pick them up too):
# mobile/ios/Flutter/WorktreeOverrides.xcconfig (Debug builds only; a
# developer's AppOverrides.xcconfig takes precedence per variable)
# mobile/android/worktree.properties (debug build type only)
# In the main checkout it removes any stale override files. Release and
# profile builds never read these overrides.
set -euo pipefail
# `just mobile-*` recipes unset these before invoking Flutter; do the same so
# an inherited GIT_DIR pointing elsewhere cannot misdetect the worktree.
unset GIT_DIR GIT_WORK_TREE
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ios_overrides="$repo_root/mobile/ios/Flutter/WorktreeOverrides.xcconfig"
android_props="$repo_root/mobile/android/worktree.properties"
# Worktree detection: in the main working tree --git-dir and --git-common-dir
# are identical; in any linked worktree they differ (same check as
# scripts/instance-env.sh).
in_worktree=0
if git -C "$repo_root" rev-parse --is-inside-work-tree &>/dev/null; then
git_dir=$(git -C "$repo_root" rev-parse --git-dir)
git_common_dir=$(git -C "$repo_root" rev-parse --git-common-dir 2>/dev/null)
if [[ -n "$git_common_dir" && "$git_dir" != "$git_common_dir" ]]; then
in_worktree=1
fi
fi
if [[ "$in_worktree" != "1" ]]; then
rm -f "$ios_overrides" "$android_props"
exit 0
fi
# Install identity comes from the worktree directory name: stable across
# branch switches, so one worktree keeps one installed app (and its login
# state) no matter how many branches it visits.
worktree_name="$(basename "$repo_root")"
# Display label is context only: the branch name, or a short SHA when the
# worktree is detached. Sanitized to [A-Za-z0-9._-] so no valid Git ref can
# break Android string resources or xcconfig values.
branch=$(git -C "$repo_root" rev-parse --abbrev-ref HEAD)
if [[ "$branch" == "HEAD" ]]; then
label_raw=$(git -C "$repo_root" rev-parse --short HEAD)
else
label_raw="${branch##*/}"
fi
label=$(printf '%s' "$label_raw" | sed -e 's/[^A-Za-z0-9._-]/-/g' -e 's/--*/-/g' -e 's/^-//' -e 's/-$//')
[[ -n "$label" ]] || label="worktree"
# iOS bundle-identifier slug: lowercase, non-alphanumerics collapsed to
# hyphens (hyphens are valid in bundle identifiers).
ios_slug=$(printf '%s' "$worktree_name" | tr '[:upper:]' '[:lower:]' | sed -e 's/[^a-z0-9]/-/g' -e 's/--*/-/g' -e 's/^-//' -e 's/-$//')
[[ -n "$ios_slug" ]] || ios_slug="worktree"
# Android applicationId segments must match [a-z][a-z0-9_]*: swap hyphens for
# underscores and prefix a letter when the slug starts with a digit.
android_slug="${ios_slug//-/_}"
case "$android_slug" in
[0-9]*) android_slug="w_$android_slug" ;;
esac
ios_bundle_id="com.buzz.buzzMobile.${ios_slug}"
android_suffix=".${android_slug}"
cat > "$ios_overrides" <<XCCONFIG
// Generated by scripts/mobile-worktree-overrides.sh — gitignored, do not edit.
// Applies to Debug builds only (included from Flutter/Debug.xcconfig before
// AppOverrides.xcconfig, so a developer's app-specific overrides win).
BUNDLE_IDENTIFIER = ${ios_bundle_id}
APP_DISPLAY_NAME = Buzz (${label})
XCCONFIG
cat > "$android_props" <<PROPERTIES
# Generated by scripts/mobile-worktree-overrides.sh — gitignored, do not edit.
# Applies to the debug build type only (read by android/app/build.gradle.kts).
label=${label}
applicationIdSuffix=${android_suffix}
PROPERTIES
# The printed identifiers are the generated defaults; on iOS a developer's
# AppOverrides.xcconfig may override them per variable.
echo "📱 Worktree ${worktree_name}: label \"${label}\" (iOS default ${ios_bundle_id}, Android suffix ${android_suffix})"