#!/usr/bin/env bash # # Sync this personal Tigerfly build with its Witchsky upstream. # # Runs the deterministic, mechanical parts of the "Updating from upstream" # process documented in AGENTS.md and STOPS wherever human judgment (merge # conflicts) or the physical device (Release build/install) is required. # # It never pushes, never auto-resolves conflicts, and never reads or writes # .env.local. # # Usage: # scripts/sync-upstream.sh [options] # # Options: # --rebase Rebase onto upstream instead of merging. Rewrites local # history; avoid when main is already pushed to origin. # --prebuild After a clean sync, regenerate ios/ (needs Ruby via mise). # --post-merge Skip fetch/merge and only run the post-merge housekeeping # (install, intl, typecheck). Use this after resolving a # conflict and running `git merge --continue`. # --skip-install Skip `pnpm install --frozen-lockfile`. # --skip-intl Skip the locale extract/compile step. # --skip-typecheck Skip `pnpm typecheck:ios`. # --remote Upstream remote to sync from (default: witchsky). # --branch Upstream branch to sync from (default: main). # -h, --help Show this help and exit. set -euo pipefail UPSTREAM_REMOTE="witchsky" UPSTREAM_BRANCH="main" PUBLISH_REMOTE="origin" PM="pnpm@11.21.0" RUBY="ruby@2.7.6" USE_REBASE=0 DO_PREBUILD=0 POST_MERGE_ONLY=0 SKIP_INSTALL=0 SKIP_INTL=0 SKIP_TYPECHECK=0 # Paths where an upstream conflict needs deliberate, fork-aware resolution # (see the "Age-assurance" and "Current personal-build patch" notes in AGENTS.md). SENSITIVE_PATHS=( "app.config.js" "plugins/" "src/lib/notifications/" "src/ageAssurance/" "src/env/common.ts" ) bold() { printf '\033[1m%s\033[0m\n' "$1"; } info() { printf '\033[36m›\033[0m %s\n' "$1"; } warn() { printf '\033[33m! %s\033[0m\n' "$1"; } die() { printf '\033[31m✗ %s\033[0m\n' "$1" >&2 exit 1 } run() { printf '\033[2m$ %s\033[0m\n' "$*" "$@" } usage() { sed -n '2,/^set -euo/p' "$0" | sed 's/^# \{0,1\}//; s/^#//; /^set -euo/d' exit "${1:-0}" } while [ $# -gt 0 ]; do case "$1" in --rebase) USE_REBASE=1 ;; --prebuild) DO_PREBUILD=1 ;; --post-merge) POST_MERGE_ONLY=1 ;; --skip-install) SKIP_INSTALL=1 ;; --skip-intl) SKIP_INTL=1 ;; --skip-typecheck) SKIP_TYPECHECK=1 ;; --remote) shift; UPSTREAM_REMOTE="${1:?--remote needs a value}" ;; --branch) shift; UPSTREAM_BRANCH="${1:?--branch needs a value}" ;; -h|--help) usage 0 ;; *) die "Unknown option: $1 (see --help)" ;; esac shift done cd "$(git rev-parse --show-toplevel)" pnpm_run() { run mise exec "$PM" -- pnpm "$@"; } post_merge() { bold "Post-merge housekeeping" command -v mise >/dev/null 2>&1 || die "mise not found; install it, then re-run with --post-merge." if [ "$SKIP_INSTALL" -eq 0 ]; then info "Installing dependencies (frozen lockfile)" pnpm_run install --frozen-lockfile fi if [ "$SKIP_INTL" -eq 0 ]; then info "Refreshing locale catalogs" pnpm_run intl:extract pnpm_run intl:compile fi if [ "$SKIP_TYPECHECK" -eq 0 ]; then info "Type-checking iOS project" pnpm_run typecheck:ios fi if [ "$DO_PREBUILD" -eq 1 ]; then info "Regenerating ios/ (prebuild --clean)" run env EXPO_NO_GIT_STATUS=1 mise exec "$RUBY" "$PM" -- pnpm prebuild --platform ios fi } next_steps() { bold "Next steps (not automated — need the device / your call)" cat </dev/null 2>&1 \ || die "Remote '$UPSTREAM_REMOTE' not found. Configure it or pass --remote." BRANCH="$(git symbolic-ref --short -q HEAD || echo DETACHED)" [ "$BRANCH" != "DETACHED" ] || die "HEAD is detached; check out a branch first." info "On branch: $BRANCH" [ "$BRANCH" = "main" ] || warn "Not on 'main'; upstream will be integrated into '$BRANCH'." if [ -e .git/MERGE_HEAD ] || [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ]; then die "A merge/rebase is already in progress. Finish it (git merge --continue) or abort it, then re-run with --post-merge." fi if [ -n "$(git status --porcelain --untracked-files=no)" ]; then git status --short --untracked-files=no die "Working tree has uncommitted tracked changes. Commit or stash them first (the personal-build patch is already committed on this fork)." fi [ -f .env.local ] || warn ".env.local is missing; the Release build will need it (bundle id / Apple team)." BEFORE="$(git rev-parse HEAD)" # --- Fetch ----------------------------------------------------------------- bold "Fetching $UPSTREAM_REMOTE" run git fetch "$UPSTREAM_REMOTE" --prune UPSTREAM_REF="$UPSTREAM_REMOTE/$UPSTREAM_BRANCH" git rev-parse --verify --quiet "$UPSTREAM_REF" >/dev/null \ || die "Ref '$UPSTREAM_REF' not found after fetch." INCOMING="$(git rev-list --count "HEAD..$UPSTREAM_REF")" if [ "$INCOMING" -eq 0 ]; then info "Already up to date with $UPSTREAM_REF (at $(git rev-parse --short HEAD))." exit 0 fi bold "$INCOMING new upstream commit(s) from $UPSTREAM_REF" git --no-pager log -40 --oneline --no-decorate "HEAD..$UPSTREAM_REF" [ "$INCOMING" -le 40 ] || info "(showing first 40 of $INCOMING)" echo git diff --stat "HEAD...$UPSTREAM_REF" | tail -1 # --- Integrate ------------------------------------------------------------- handle_conflicts() { local op="$1" local unmerged unmerged="$(git diff --name-only --diff-filter=U || true)" if [ -z "$unmerged" ]; then die "$op failed (no conflicts detected — see the git output above)." fi echo warn "$op stopped on conflicts. Resolve these, then run: git $op --continue" echo "$unmerged" | sed 's/^/ /' local hit while IFS= read -r f; do for s in "${SENSITIVE_PATHS[@]}"; do case "$f" in "$s"*) hit="${hit} $f (resolve deliberately — see AGENTS.md)\n" ;; esac done done <<< "$unmerged" if [ -n "${hit:-}" ]; then echo warn "Fork-critical files in this set — do NOT blindly take upstream:" printf "$hit" fi echo info "After 'git $op --continue', finish with: scripts/sync-upstream.sh --post-merge" info "To back out entirely: git $op --abort" exit 2 } if [ "$USE_REBASE" -eq 1 ]; then bold "Rebasing $BRANCH onto $UPSTREAM_REF" warn "Rebase rewrites local history; you will need to force-push if $BRANCH is already published." if ! run git rebase "$UPSTREAM_REF"; then handle_conflicts rebase; fi else bold "Merging $UPSTREAM_REF into $BRANCH" if ! run git merge --no-edit "$UPSTREAM_REF"; then handle_conflicts merge; fi fi info "Integrated cleanly: $(git rev-parse --short "$BEFORE") → $(git rev-parse --short HEAD)" # --- Housekeeping + handoff ------------------------------------------------ post_merge echo next_steps