Files
windmill/scripts/worktree-env
centdix 696b8de1ed fix: assign worktree ports by index instead of scanning for free ports
Determine the slot from the worktree's position in workmux list rather
than probing ports. Keeps the port-in-use safety check. Removes unused
find_port helper.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 15:45:24 +00:00

86 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
port_in_use() {
lsof -nP -iTCP:"$1" -sTCP:LISTEN &>/dev/null
}
if [[ -z "${WM_SLOT:-}" ]]; then
# Auto-assign slot based on worktree index (position among non-main worktrees)
# Slot 0 (8000/3000) is reserved for the main worktree
current_dir="$(basename "$(pwd)")"
slot=1
while IFS= read -r line; do
branch=$(echo "$line" | awk '{print $1}')
[[ -z "$branch" ]] && continue
if [[ "$branch" == "$current_dir" ]]; then
WM_SLOT=$slot
break
fi
((slot++))
done < <(workmux list 2>/dev/null | tail -n +2 | grep -v '(here)')
if [[ -z "${WM_SLOT:-}" ]]; then
# Fallback: count existing non-main worktrees
WM_SLOT=$(workmux list 2>/dev/null | tail -n +2 | grep -cv '(here)' || echo 1)
fi
echo "Auto-assigned slot $WM_SLOT"
fi
# Slot-based: predictable ports for SSH forwarding
# Slot 0 = 8000/3000, slot 1 = 8010/3010, slot 2 = 8020/3020, etc.
backend_port=$((8000 + WM_SLOT * 10))
frontend_port=$((3000 + WM_SLOT * 10))
if port_in_use "$backend_port" || port_in_use "$frontend_port"; then
echo "ERROR: Slot $WM_SLOT ports ($backend_port/$frontend_port) already in use" >&2
exit 1
fi
# Generate .env.local with port overrides
cat > .env.local <<EOF
BACKEND_PORT=$backend_port
FRONTEND_PORT=$frontend_port
REMOTE=http://localhost:$backend_port
EOF
echo "Created .env.local with ports: backend=$backend_port, frontend=$frontend_port"
# --- Allow direnv so the nix devshell activates in pane commands ---
if command -v direnv &>/dev/null && [ -f .envrc ]; then
direnv allow
echo "direnv allowed"
fi
# --- Create matching windmill-ee-private worktree ---
# Check parent directory first (sibling to worktree root), then fall back to home
parent_dir="$(cd "$(pwd)/.." && pwd)"
if [ -d "${parent_dir}/windmill-ee-private" ]; then
ee_repo="${parent_dir}/windmill-ee-private"
else
ee_repo="${HOME}/windmill-ee-private"
fi
if [ -d "$ee_repo" ]; then
branch=$(git branch --show-current 2>/dev/null || true)
wt_basename=$(basename "$(pwd)")
ee_worktree_dir="${ee_repo}__worktrees/${wt_basename}"
if [ -n "$branch" ] && [ ! -d "$ee_worktree_dir" ]; then
mkdir -p "$(dirname "$ee_worktree_dir")"
# Fetch latest so we can check out remote branches
git -C "$ee_repo" fetch --quiet 2>/dev/null || true
# Try: existing branch, then new branch from main
if git -C "$ee_repo" worktree add "$ee_worktree_dir" "$branch" 2>/dev/null; then
echo "Created EE worktree at $ee_worktree_dir (branch: $branch)"
elif git -C "$ee_repo" worktree add -b "$branch" "$ee_worktree_dir" main 2>/dev/null; then
echo "Created EE worktree at $ee_worktree_dir (new branch: $branch from main)"
else
echo "Warning: Could not create EE worktree for branch $branch"
fi
elif [ -d "$ee_worktree_dir" ]; then
echo "EE worktree already exists at $ee_worktree_dir"
fi
fi