|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ── Resolve the repo root from this script's location ──────────────────────── |
| 5 | +REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)" |
| 6 | + |
| 7 | +# ── Usage ──────────────────────────────────────────────────────────────────── |
| 8 | +if [[ $# -lt 1 ]]; then |
| 9 | + echo "Usage: $0 <destination> [branch]" |
| 10 | + echo "" |
| 11 | + echo " destination Path for the new worktree (relative or absolute)" |
| 12 | + echo " branch Optional branch/ref to base off (default: origin/main)" |
| 13 | + echo "" |
| 14 | + echo "Examples:" |
| 15 | + echo " $0 ../alpine-fix-modal" |
| 16 | + echo " $0 /tmp/alpine-experiment some-branch" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +DEST="$(cd "$(dirname "$1")" 2>/dev/null && pwd)/$(basename "$1")" || { |
| 21 | + # Parent dir doesn't exist yet — resolve what we can |
| 22 | + DEST="$(pwd)/$1" |
| 23 | +} |
| 24 | +BASE="${2:-origin/main}" |
| 25 | + |
| 26 | +# ── Check if it already exists ─────────────────────────────────────────────── |
| 27 | +if [[ -d "$DEST" ]]; then |
| 28 | + echo "Worktree already exists at $DEST" |
| 29 | + echo "" |
| 30 | + echo " cd $DEST" |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +# ── Fetch & create ─────────────────────────────────────────────────────────── |
| 35 | +echo "Fetching latest from origin..." |
| 36 | +git -C "$REPO_DIR" fetch origin |
| 37 | + |
| 38 | +echo "Creating worktree at $DEST from $BASE..." |
| 39 | +git -C "$REPO_DIR" worktree add --detach "$DEST" "$BASE" |
| 40 | + |
| 41 | +# ── Install deps ───────────────────────────────────────────────────────────── |
| 42 | +echo "Installing npm dependencies..." |
| 43 | +(cd "$DEST" && npm install --silent 2>&1) || { |
| 44 | + echo " WARNING: npm install failed" |
| 45 | +} |
| 46 | + |
| 47 | +# ── Done ───────────────────────────────────────────────────────────────────── |
| 48 | +echo "" |
| 49 | +echo "════════════════════════════════════════" |
| 50 | +echo " Worktree ready!" |
| 51 | +echo "" |
| 52 | +echo " cd $DEST" |
| 53 | +echo "════════════════════════════════════════" |
0 commit comments