-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree-manager.bash
More file actions
417 lines (369 loc) · 13.4 KB
/
worktree-manager.bash
File metadata and controls
417 lines (369 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env bash
# Multi-project worktree manager - Bash version
#
# ASSUMPTIONS & SETUP:
# - Works from any git repository in the filesystem
# - Worktrees will be created in: <project-parent-dir>/.worktrees/<project-name>/<branch>
# - New branches will be named: <your-username>/<feature-name>
#
# DIRECTORY STRUCTURE EXAMPLE:
# /path/to/projects/
# ├── my-app/ (main git repo)
# ├── another-project/ (main git repo)
# └── .worktrees/
# ├── my-app/
# │ ├── feature-x/ (worktree)
# │ └── bugfix-y/ (worktree)
# └── another-project/
# └── new-feature/ (worktree)
#
# INSTALLATION:
# 1. Source this file in your .bashrc:
# source /path/to/worktree-manager.bash
#
# 2. Add bash completion (see bottom of file for setup instructions)
#
# 3. Restart your terminal or run: source ~/.bashrc
#
# USAGE:
# wt <worktree> # cd to worktree (creates if needed) - uses current repo
# wt <worktree> <command> # run command in worktree
# wt --list # list all worktrees for current repo
# wt --list-all # list all worktrees for all sibling repos
# wt --rm <worktree> # remove worktree
# wt --project <project> <worktree> # operate on a specific project
#
# EXAMPLES:
# wt feature-x # cd to feature-x worktree
# wt feature-x code . # open VS Code in worktree
# wt feature-x git status # git status in worktree
# wt --project myapp feature-x # work with myapp's feature-x worktree
# Find git root directory
_find_git_root() {
local dir="$1"
[[ -z "$dir" ]] && dir="$PWD"
while [[ "$dir" != "/" ]]; do
if [[ -d "$dir/.git" ]]; then
echo "$dir"
return 0
fi
dir="$(dirname "$dir")"
done
return 1
}
# Multi-project worktree manager
wt() {
# Handle special flags first
if [[ "$1" == "--list" ]]; then
local git_root
git_root=$(_find_git_root)
if [[ $? -ne 0 ]]; then
echo "Error: Not in a git repository"
return 1
fi
local project_name
project_name=$(basename "$git_root")
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_dir="$parent_dir/.worktrees/$project_name"
echo "=== Worktrees for $project_name ==="
if [[ -d "$worktrees_dir" ]]; then
for wt in "$worktrees_dir"/*; do
if [[ -d "$wt" ]]; then
echo " • $(basename "$wt")"
fi
done
else
echo " No worktrees found"
fi
return 0
elif [[ "$1" == "--list-all" ]]; then
local git_root
git_root=$(_find_git_root)
if [[ $? -ne 0 ]]; then
echo "Error: Not in a git repository"
return 1
fi
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_base="$parent_dir/.worktrees"
if [[ ! -d "$worktrees_base" ]]; then
echo "No worktrees directory found"
return 0
fi
echo "=== All Worktrees ==="
for project in "$worktrees_base"/*; do
if [[ -d "$project" ]]; then
local project_name
project_name=$(basename "$project")
echo ""
echo "[$project_name]"
for wt in "$project"/*; do
if [[ -d "$wt" ]]; then
echo " • $(basename "$wt")"
fi
done
fi
done
return 0
elif [[ "$1" == "--rm" ]]; then
shift
local worktree="$1"
if [[ -z "$worktree" ]]; then
echo "Usage: wt --rm <worktree>"
return 1
fi
local git_root
git_root=$(_find_git_root)
if [[ $? -ne 0 ]]; then
echo "Error: Not in a git repository"
return 1
fi
local project_name
project_name=$(basename "$git_root")
local parent_dir
parent_dir=$(dirname "$git_root")
local wt_path="$parent_dir/.worktrees/$project_name/$worktree"
if [[ ! -d "$wt_path" ]]; then
echo "Worktree not found: $wt_path"
return 1
fi
(cd "$git_root" && git worktree remove "$wt_path")
return $?
elif [[ "$1" == "--project" ]]; then
shift
local project="$1"
local worktree="$2"
shift 2
local command=("$@")
if [[ -z "$project" || -z "$worktree" ]]; then
echo "Usage: wt --project <project> <worktree> [command...]"
return 1
fi
# Find the project directory
local git_root
git_root=$(_find_git_root)
if [[ $? -ne 0 ]]; then
echo "Error: Not in a git repository"
return 1
fi
local parent_dir
parent_dir=$(dirname "$git_root")
local project_dir="$parent_dir/$project"
if [[ ! -d "$project_dir" ]] || [[ ! -d "$project_dir/.git" ]]; then
echo "Project not found: $project_dir"
return 1
fi
# Handle worktree operations for the specified project
local worktrees_dir="$parent_dir/.worktrees/$project"
local wt_path="$worktrees_dir/$worktree"
# Create worktree if it doesn't exist
if [[ ! -d "$wt_path" ]]; then
echo "Creating new worktree: $worktree for project $project"
# Ensure worktrees directory exists
mkdir -p "$worktrees_dir"
# Determine branch name (use current username prefix if available)
local branch_name
if [[ -n "$USER" ]]; then
branch_name="$USER/$worktree"
else
branch_name="$worktree"
fi
# Create the worktree
(cd "$project_dir" && git worktree add "$wt_path" -b "$branch_name") || {
echo "Failed to create worktree"
return 1
}
fi
# Execute command or cd to worktree
if [[ ${#command[@]} -eq 0 ]]; then
cd "$wt_path"
else
local old_pwd="$PWD"
cd "$wt_path"
"${command[@]}"
local exit_code=$?
cd "$old_pwd"
return $exit_code
fi
return 0
fi
# Normal usage: w <worktree> [command...]
local worktree="$1"
shift
local command=("$@")
if [[ -z "$worktree" ]]; then
cat <<EOF
Usage:
wt <worktree> [command...] # Work with worktree in current repo
wt --list # List worktrees for current repo
wt --list-all # List all worktrees for sibling repos
wt --rm <worktree> # Remove worktree
wt --project <project> <worktree> # Work with specific project's worktree
EOF
return 1
fi
# Find git root of current directory
local git_root
git_root=$(_find_git_root)
if [[ $? -ne 0 ]]; then
echo "Error: Not in a git repository"
return 1
fi
local project_name
project_name=$(basename "$git_root")
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_dir="$parent_dir/.worktrees/$project_name"
local wt_path="$worktrees_dir/$worktree"
# If worktree doesn't exist, create it
if [[ ! -d "$wt_path" ]]; then
echo "Creating new worktree: $worktree"
# Ensure worktrees directory exists
mkdir -p "$worktrees_dir"
# Determine branch name (use current username prefix if available)
local branch_name
if [[ -n "$USER" ]]; then
branch_name="$USER/$worktree"
else
branch_name="$worktree"
fi
# Create the worktree
(cd "$git_root" && git worktree add "$wt_path" -b "$branch_name") || {
echo "Failed to create worktree"
return 1
}
fi
# Execute based on number of arguments
if [[ ${#command[@]} -eq 0 ]]; then
# No command specified - just cd to the worktree
cd "$wt_path"
else
# Command specified - run it in the worktree without cd'ing
local old_pwd="$PWD"
cd "$wt_path"
"${command[@]}"
local exit_code=$?
cd "$old_pwd"
return $exit_code
fi
}
# Bash completion for the wt function
_wt_completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# First argument - could be a flag or worktree name
if [[ ${COMP_CWORD} -eq 1 ]]; then
opts="--list --list-all --rm --project"
# Also add existing worktrees for current project
local git_root
git_root=$(_find_git_root 2>/dev/null)
if [[ $? -eq 0 ]]; then
local project_name
project_name=$(basename "$git_root")
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_dir="$parent_dir/.worktrees/$project_name"
if [[ -d "$worktrees_dir" ]]; then
for wt in "$worktrees_dir"/*; do
if [[ -d "$wt" ]]; then
opts="$opts $(basename "$wt")"
fi
done
fi
fi
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
# Handle flag-specific completions
case "${COMP_WORDS[1]}" in
--rm)
if [[ ${COMP_CWORD} -eq 2 ]]; then
# Complete with existing worktrees
local git_root
git_root=$(_find_git_root 2>/dev/null)
if [[ $? -eq 0 ]]; then
local project_name
project_name=$(basename "$git_root")
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_dir="$parent_dir/.worktrees/$project_name"
if [[ -d "$worktrees_dir" ]]; then
local worktrees=""
for wt in "$worktrees_dir"/*; do
if [[ -d "$wt" ]]; then
worktrees="$worktrees $(basename "$wt")"
fi
done
COMPREPLY=( $(compgen -W "${worktrees}" -- ${cur}) )
fi
fi
fi
;;
--project)
if [[ ${COMP_CWORD} -eq 2 ]]; then
# Complete with sibling projects
local git_root
git_root=$(_find_git_root 2>/dev/null)
if [[ $? -eq 0 ]]; then
local parent_dir
parent_dir=$(dirname "$git_root")
local projects=""
for proj in "$parent_dir"/*; do
if [[ -d "$proj/.git" ]]; then
projects="$projects $(basename "$proj")"
fi
done
COMPREPLY=( $(compgen -W "${projects}" -- ${cur}) )
fi
elif [[ ${COMP_CWORD} -eq 3 ]]; then
# Complete with worktrees for the specified project
local project="${COMP_WORDS[2]}"
local git_root
git_root=$(_find_git_root 2>/dev/null)
if [[ $? -eq 0 ]]; then
local parent_dir
parent_dir=$(dirname "$git_root")
local worktrees_dir="$parent_dir/.worktrees/$project"
if [[ -d "$worktrees_dir" ]]; then
local worktrees=""
for wt in "$worktrees_dir"/*; do
if [[ -d "$wt" ]]; then
worktrees="$worktrees $(basename "$wt")"
fi
done
COMPREPLY=( $(compgen -W "${worktrees}" -- ${cur}) )
fi
fi
fi
;;
*)
# For worktree names followed by commands, use default command completion
if [[ ${COMP_CWORD} -eq 2 ]]; then
COMPREPLY=( $(compgen -c -- ${cur}) )
fi
;;
esac
}
# Register bash completion
complete -F _wt_completion wt
# Installation help
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
cat <<EOF
Worktree Manager - Bash Version
To install, add this line to your ~/.bashrc:
source $(realpath "${BASH_SOURCE[0]}")
Then restart your terminal or run:
source ~/.bashrc
The 'wt' command will then be available with tab completion.
Usage examples:
wt feature-x # Create/switch to feature-x worktree
wt feature-x git status # Run git status in worktree
wt --list # List current project's worktrees
wt --list-all # List all sibling projects' worktrees
wt --rm feature-x # Remove feature-x worktree
wt --project myapp feature-y # Work with myapp's feature-y worktree
EOF
fi