#!/bin/sh # Runs on the HOST (devcontainer `initializeCommand`) before the container starts. # # The main checkout stays wherever it was cloned. Worktrees live in one dedicated # sibling folder named after it, so they never collide with other projects: # # ~/code// <- main checkout (owns .git) # ~/code/-worktrees/ # / <- git worktrees # .shared/ <- pnpm store, Nx cache, Claude and gh config, plans # ~/code/.claude-shared/ <- Claude settings.json and CLAUDE.md, shared with other repos # # All three folders are bind-mounted into the container at the SAME absolute paths as on the # host, so git's absolute worktree links resolve on both sides. This writes those paths # to `.devcontainer/.env`, which docker compose reads for variable substitution. set -eu cd "$(dirname "$0")/.." worktree="$(pwd -P)" common_dir="$(git rev-parse --path-format=absolute --git-common-dir)" main_checkout="$(dirname "$common_dir")" worktrees_dir="${main_checkout}-worktrees" shared="$worktrees_dir/.shared" if [ "$worktree" != "$main_checkout" ] && [ "$(dirname "$worktree")" != "$worktrees_dir" ]; then echo "error: worktree $worktree must live in $worktrees_dir" >&2 echo " create worktrees with: scripts/worktree new " >&2 exit 1 fi mkdir -p "$shared/nx-cache" "$shared/nx-cache-dagger" # One-time migration of state that used to live inside the main checkout if [ ! -e "$shared/pnpm-store" ] && [ -d "$main_checkout/.pnpm-store" ]; then echo "Moving $main_checkout/.pnpm-store -> $shared/pnpm-store" mv "$main_checkout/.pnpm-store" "$shared/pnpm-store" fi if [ ! -e "$shared/claude-config" ] && [ -d "$main_checkout/.claude-config" ]; then # Copy rather than move: a running session may still be using the old location echo "Copying $main_checkout/.claude-config -> $shared/claude-config" cp -R "$main_checkout/.claude-config" "$shared/claude-config" fi mkdir -p "$shared/pnpm-store" "$shared/pnpm-cache" "$shared/go-build" "$shared/go-mod" "$shared/claude-config" "$shared/gh-config" # Claude settings and instructions shared with other repos' devcontainers: the config dir's settings.json and # CLAUDE.md are absolute links into a folder beside the main checkout, mounted at the same path. Memory (`projects/`) # stays per repo. A repo's own file moves there the first time; if both exist, nothing is linked until it's merged. claude_shared="$(dirname "$main_checkout")/.claude-shared" mkdir -p "$claude_shared" for file in settings.json CLAUDE.md; do repo_file="$shared/claude-config/$file" shared_file="$claude_shared/$file" if [ -L "$repo_file" ]; then continue fi if [ -e "$repo_file" ]; then if [ -e "$shared_file" ]; then echo "warning: $repo_file isn't shared, as $shared_file exists too." >&2 echo " Merge it into the shared one by hand and delete it; the next start links it." >&2 continue fi echo "Moving $repo_file -> $shared_file" mv "$repo_file" "$shared_file" elif [ ! -e "$shared_file" ]; then if [ "$file" = settings.json ]; then printf '{}\n' > "$shared_file" else : > "$shared_file" fi fi ln -s "$shared_file" "$repo_file" done # Plans shared by every worktree, kept out of git: `plans` is a gitignored symlink mkdir -p "$shared/plans" if [ ! -e plans ] && [ ! -L plans ]; then ln -s "$shared/plans" plans fi # Namespace the compose project by repo so same-named worktrees in other repos don't clash project="$(basename "$main_checkout")" if [ "$worktree" != "$main_checkout" ]; then project="${project}_$(basename "$worktree")" fi project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9_-' '-')" # DNS labels for this worktree's URL, `https://..localhost` . .devcontainer/names.sh # Yours to set in `.devcontainer/.env`, kept when the file is rewritten (tools/local-ingress/README.md#tunnels) tunnel_host="$(sed -n 's/^TUNNEL_HOST="\(.*\)"$/\1/p' .devcontainer/.env 2> /dev/null || true)" { printf 'MAIN_CHECKOUT="%s"\n' "$main_checkout" printf 'WORKTREES_DIR="%s"\n' "$worktrees_dir" printf 'CLAUDE_SHARED="%s"\n' "$claude_shared" printf 'COMPOSE_PROJECT_NAME="%s"\n' "$project" printf 'REPO_NAME="%s"\n' "$repo_name" printf 'WORKTREE_NAME="%s"\n' "$worktree_name" if [ -n "$tunnel_host" ]; then printf 'TUNNEL_HOST="%s"\n' "$tunnel_host" fi } > .devcontainer/.env # The machine's shared HTTPS ingress (tools/local-ingress). Never blocks the container from starting: without it, # only the URL above doesn't load. The network must exist either way, as the container joins it. if ! sh tools/local-ingress/ensure; then echo "warning: local-ingress didn't start, so https://$worktree_name.$repo_name.localhost won't load." >&2 echo " See tools/local-ingress/README.md, then run tools/local-ingress/ensure." >&2 docker network inspect local-ingress > /dev/null 2>&1 || docker network create local-ingress > /dev/null 2>&1 || true fi