Skip to content

Commit 6a0ab4d

Browse files
authored
Add worktree script (#35593)
Intended to be used directly and/or from skills in an agent. Usage is `./scripts/worktree.sh [--compiler] [--claude] <name>`. The script: * Checks that ./worktrees is in gitignore * Checks the named worktree does not exist yet * Creates the named worktree in ./worktrees/ * Installs deps * cds into the worktree (optionally the compiler dir if `--compiler`) * optionally runs claude in the worktree if `--claude`
1 parent 03ee29d commit 6a0ab4d

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ chrome-user-data
2424
*.swp
2525
*.swo
2626
/tmp
27+
/worktrees
2728

2829
packages/react-devtools-core/dist
2930
packages/react-devtools-extensions/chrome/build

scripts/worktree.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set -eo pipefail
8+
9+
# --- Configuration ---
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
12+
WORKTREE_CREATED=false
13+
WORKTREE_PATH=""
14+
15+
# --- Usage ---
16+
usage() {
17+
echo "Usage: $0 <name> [--claude] [--compiler]"
18+
echo ""
19+
echo "Creates a new git worktree with dependencies installed."
20+
echo ""
21+
echo "Arguments:"
22+
echo " <name> Name for the worktree (also used as branch name)"
23+
echo ""
24+
echo "Options:"
25+
echo " --claude Launch Claude Code after setup"
26+
echo " --compiler When used with --claude, launch Claude in the compiler directory"
27+
exit 1
28+
}
29+
30+
# --- Error handling ---
31+
error() {
32+
echo "Error: $1" >&2
33+
exit 1
34+
}
35+
36+
# --- Cleanup on error ---
37+
cleanup() {
38+
if [[ "$WORKTREE_CREATED" == "true" && -n "$WORKTREE_PATH" ]]; then
39+
echo "Cleaning up: removing worktree at $WORKTREE_PATH..." >&2
40+
git worktree remove "$WORKTREE_PATH" --force 2>/dev/null || true
41+
fi
42+
}
43+
44+
trap cleanup ERR
45+
46+
# --- Argument parsing ---
47+
NAME=""
48+
USE_CLAUDE=false
49+
USE_COMPILER=false
50+
51+
while [[ $# -gt 0 ]]; do
52+
case "$1" in
53+
--claude)
54+
USE_CLAUDE=true
55+
shift
56+
;;
57+
--compiler)
58+
USE_COMPILER=true
59+
shift
60+
;;
61+
-h|--help)
62+
usage
63+
;;
64+
-*)
65+
error "Unknown option: $1"
66+
;;
67+
*)
68+
if [[ -z "$NAME" ]]; then
69+
NAME="$1"
70+
else
71+
error "Unexpected argument: $1"
72+
fi
73+
shift
74+
;;
75+
esac
76+
done
77+
78+
if [[ -z "$NAME" ]]; then
79+
usage
80+
fi
81+
82+
# --- Check .gitignore ---
83+
if ! grep -qE '^/?worktrees/?$' "$REPO_ROOT/.gitignore" 2>/dev/null; then
84+
error "'worktrees' is not in .gitignore. Add it before creating worktrees."
85+
fi
86+
87+
# --- Check if worktree already exists ---
88+
if git worktree list | grep -q "\[$NAME\]"; then
89+
error "Worktree with branch name '$NAME' already exists"
90+
fi
91+
92+
# --- Set up worktree path ---
93+
WORKTREES_DIR="$REPO_ROOT/worktrees"
94+
WORKTREE_PATH="$WORKTREES_DIR/$NAME"
95+
96+
if [[ -d "$WORKTREE_PATH" ]]; then
97+
error "Directory already exists at $WORKTREE_PATH"
98+
fi
99+
100+
# --- Create worktree ---
101+
mkdir -p "$WORKTREES_DIR"
102+
echo "Creating worktree '$NAME' at $WORKTREE_PATH..."
103+
git worktree add "$WORKTREE_PATH" -b "$NAME"
104+
WORKTREE_CREATED=true
105+
106+
# --- Install dependencies ---
107+
echo "Installing compiler dependencies..."
108+
(cd "$WORKTREE_PATH/compiler" && yarn install)
109+
110+
echo "Installing root dependencies..."
111+
(cd "$WORKTREE_PATH" && yarn install)
112+
113+
echo "Worktree '$NAME' created successfully at $WORKTREE_PATH"
114+
115+
# --- Launch Claude (optional) ---
116+
if [[ "$USE_CLAUDE" == "true" ]]; then
117+
if [[ "$USE_COMPILER" == "true" ]]; then
118+
echo "Launching Claude in compiler directory..."
119+
cd "$WORKTREE_PATH/compiler"
120+
else
121+
echo "Launching Claude in worktree root..."
122+
cd "$WORKTREE_PATH"
123+
fi
124+
exec claude
125+
fi

0 commit comments

Comments
 (0)