Skip to content

Commit a688a3d

Browse files
authored
worktree script improvements (facebook#35603)
A few small improvements: * Use `<root>/.worktrees` as the directory for worktrees so its hidden by default in finder/ls * Generate names with a timestamp, and allow auto-generating a name so that you can just call eg `./scripts/worktree.sh --compiler --claude` and get a random name
1 parent 2c8725f commit a688a3d

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ chrome-user-data
2424
*.swp
2525
*.swo
2626
/tmp
27-
/worktrees
27+
/.worktrees
2828
.claude/*.local.*
2929

3030
packages/react-devtools-core/dist

scripts/worktree.sh

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ WORKTREE_PATH=""
1414

1515
# --- Usage ---
1616
usage() {
17-
echo "Usage: $0 <name> [--claude] [--compiler]"
17+
echo "Usage: $0 [name] [--claude] [--compiler]"
1818
echo ""
1919
echo "Creates a new git worktree with dependencies installed."
2020
echo ""
2121
echo "Arguments:"
22-
echo " <name> Name for the worktree (also used as branch name)"
22+
echo " [name] Name for the worktree (auto-generated if not provided)"
2323
echo ""
2424
echo "Options:"
2525
echo " --claude Launch Claude Code after setup"
@@ -75,13 +75,42 @@ while [[ $# -gt 0 ]]; do
7575
esac
7676
done
7777

78+
# Generate worktree name with timestamp prefix
79+
TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)
80+
7881
if [[ -z "$NAME" ]]; then
79-
usage
82+
# Word lists for random name generation
83+
ADJECTIVES=(
84+
"quick" "bright" "calm" "dark" "eager" "fair" "gentle" "happy" "idle" "jolly"
85+
"keen" "lively" "merry" "noble" "orange" "proud" "quiet" "rapid" "silent" "tall"
86+
"unique" "vivid" "warm" "young" "zealous" "ancient" "bold" "clever" "daring" "elegant"
87+
"fancy" "grand" "humble" "icy" "jagged" "kind" "loud" "magic" "narrow" "odd"
88+
"plain" "quaint" "rough" "sharp" "tender" "ultra" "vast" "wild" "xeric" "youthful"
89+
"zesty" "agile" "brave" "crisp" "deep"
90+
)
91+
NOUNS=(
92+
"apple" "bear" "cloud" "dragon" "eagle" "flame" "garden" "hill" "island" "jungle"
93+
"kettle" "lemon" "mountain" "night" "ocean" "panda" "quartz" "river" "storm" "tiger"
94+
"umbrella" "valley" "whale" "xenon" "yarn" "zebra" "anchor" "bridge" "canyon" "desert"
95+
"ember" "forest" "glacier" "harbor" "igloo" "jewel" "knight" "lantern" "meadow" "nebula"
96+
"oasis" "phoenix" "quest" "rocket" "shadow" "thunder" "unity" "vortex" "willow" "xylophone"
97+
"yonder" "zenith" "aurora" "beacon" "coral"
98+
)
99+
100+
# Generate random name: worktree-yyyy-mm-dd-hh-mm-ss-<adjective>-<noun>
101+
RANDOM_ADJ=${ADJECTIVES[$RANDOM % ${#ADJECTIVES[@]}]}
102+
RANDOM_NOUN=${NOUNS[$RANDOM % ${#NOUNS[@]}]}
103+
NAME="worktree-${TIMESTAMP}-${RANDOM_ADJ}-${RANDOM_NOUN}"
104+
echo "Auto-generated worktree name: $NAME"
105+
else
106+
# Use provided name: worktree-yyyy-mm-dd-hh-mm-ss-<name>
107+
NAME="worktree-${TIMESTAMP}-${NAME}"
108+
echo "Worktree name: $NAME"
80109
fi
81110

82111
# --- 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."
112+
if ! grep -qE '^/?\.worktrees/?$' "$REPO_ROOT/.gitignore" 2>/dev/null; then
113+
error "'.worktrees' is not in .gitignore. Add it before creating worktrees."
85114
fi
86115

87116
# --- Check if worktree already exists ---
@@ -90,7 +119,7 @@ if git worktree list | grep -q "\[$NAME\]"; then
90119
fi
91120

92121
# --- Set up worktree path ---
93-
WORKTREES_DIR="$REPO_ROOT/worktrees"
122+
WORKTREES_DIR="$REPO_ROOT/.worktrees"
94123
WORKTREE_PATH="$WORKTREES_DIR/$NAME"
95124

96125
if [[ -d "$WORKTREE_PATH" ]]; then

0 commit comments

Comments
 (0)