From f485e58d5876c7c0efffba361513203bf40fe440 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Thu, 9 Apr 2026 12:17:12 +0100 Subject: [PATCH] Add git hooks for automatic worktree setup When a new worktree is created, the post-checkout hook copies .env from the main worktree and runs pnpm install so the worktree is ready to use immediately. The hooks directory is configured automatically via the prepare script (guarded to dev-only). Also gitignores .claude/worktrees/. Co-Authored-By: Claude Opus 4.6 --- .githooks/post-checkout | 27 +++++++++++++++++++++++++++ .gitignore | 1 + package.json | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100755 .githooks/post-checkout diff --git a/.githooks/post-checkout b/.githooks/post-checkout new file mode 100755 index 000000000..ffb0f0d08 --- /dev/null +++ b/.githooks/post-checkout @@ -0,0 +1,27 @@ +#!/bin/sh +# +# post-checkout hook: set up worktrees after creation +# Copies .env from the main worktree and runs pnpm install. +# +# Arguments: $1=prev HEAD, $2=new HEAD, $3=branch flag (1=branch checkout, 0=file checkout) + +# Only run on branch checkouts +[ "$3" = "1" ] || exit 0 + +# Detect if this is a worktree (not the main working tree) +main_worktree="$(git worktree list --porcelain | head -1 | sed 's/^worktree //')" +current_worktree="$(git rev-parse --show-toplevel)" + +[ "$main_worktree" != "$current_worktree" ] || exit 0 + +# Copy .env from the main worktree if it exists and we don't already have one +if [ -f "$main_worktree/.env" ] && [ ! -f "$current_worktree/.env" ]; then + cp "$main_worktree/.env" "$current_worktree/.env" + echo "post-checkout: copied .env from main worktree" +fi + +# Run pnpm install if node_modules doesn't exist +if [ ! -d "$current_worktree/node_modules" ]; then + echo "post-checkout: running pnpm install..." + cd "$current_worktree" && pnpm install +fi diff --git a/.gitignore b/.gitignore index 54a0ea42a..685108a87 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ tsconfig.tsbuildinfo # Claude local settings .claude/*.local.json +.claude/worktrees/ # Playwright logs playwright-report/ diff --git a/package.json b/package.json index 354a9cea1..a486cd48c 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "test:coverage:report": "vitest run --coverage --reporter=html", "test:coverage:check": "vitest run --coverage", "prepack": "pnpm run build && ([ -f node_modules/.bin/oclif ] && node_modules/.bin/oclif manifest || echo 'Skipping oclif commands in production')", - "prepare": "pnpm run build && CI=true ABLY_INTERACTIVE=false oclif manifest", + "prepare": "[ -d .git ] && [ -d .githooks ] && git config core.hooksPath .githooks || true; pnpm run build && CI=true ABLY_INTERACTIVE=false oclif manifest", "postinstall": "[ \"$CI\" = \"true\" ] || (test -f ./dist/scripts/postinstall-welcome.js && node ./dist/scripts/postinstall-welcome.js || echo \"Skipping welcome script (not found)\")", "preversion": "pnpm run prepare", "version": "oclif readme && git add README.md",