-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwt
More file actions
executable file
·774 lines (673 loc) · 23 KB
/
Copy pathwt
File metadata and controls
executable file
·774 lines (673 loc) · 23 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#!/bin/bash
# git worktree management
set -e
# Version information
WT_VERSION="0.0.0-beta"
# Get current directory name (e.g., ws.wakumo.ai)
DIR_NAME=$(basename "$(pwd)")
# Default worktree directory
TMP_DIR="${HOME}/.wt/worktrees"
# ============================================================================
# Helper Functions
# ============================================================================
# Sanitize branch name for directory use (replace / and other invalid chars with _)
sanitize_name() {
echo "$1" | sed 's/[\\/\\:*?"<>|]/_/g'
}
# Get worktree directory path
get_worktree_dir() {
local name="$1"
local safe_name
safe_name=$(sanitize_name "$name")
echo "${TMP_DIR}/${DIR_NAME}-${safe_name}"
}
# Get symlink path
get_symlink_path() {
local name="$1"
local safe_name
safe_name=$(sanitize_name "$name")
echo ".wt/worktrees/${safe_name}"
}
# Initialize .wt directory structure
init_wt_directory() {
# Create .wt directory and .gitignore if they don't exist
if [ ! -d ".wt" ]; then
mkdir -p .wt
cat >.wt/.gitignore <<'EOF'
worktrees
*.local
EOF
fi
# Create .wt/setup template if it doesn't exist
if [ ! -f ".wt/setup" ]; then
cat >.wt/setup <<'EOF'
#! /bin/bash
# wt - Git Worktree Management Tool
# https://github.com/tumf/wt
# $ROOT_WORKTREE_PATH is path to the base repository (source tree)
if [ -f .wt/setup.local ]; then
source .wt/setup.local
fi
EOF
fi
# Create .wt/AGENTS.md documentation if it doesn't exist
if [ ! -f ".wt/AGENTS.md" ]; then
cat >.wt/AGENTS.md <<'EOF'
# .wt Directory
## Overview
The `.wt` directory is the configuration and state directory for the **wt** (worktree) tool, a git worktree management system. This directory contains setup scripts, symlinks to worktrees, and local configuration files.
## Purpose
The `wt` tool simplifies git worktree management by:
- Creating organized worktrees in a centralized location (`~/.wt/worktrees`)
- Maintaining symlinks in `.wt/worktrees/` for easy access
- Running setup scripts automatically when worktrees are created
- Supporting custom per-worktree and per-project configurations
## Directory Structure
```
.wt/
├── AGENTS.md # This file - documentation for AI agents
├── .gitignore # Git ignore rules for this directory
├── setup # Setup script template (executed in new worktrees)
├── setup.local # Local setup overrides (gitignored)
└── worktrees/ # Symlinks to actual worktrees (gitignored)
├── feature-a -> ~/.wt/worktrees/project-name-feature-a
├── bugfix-b -> ~/.wt/worktrees/project-name-bugfix-b
└── ...
```
## Files
### `.gitignore`
Prevents committing:
- `worktrees/` - Symlinks to worktrees (machine-specific)
- `*.local` - Local configuration files (user-specific)
### `setup`
Template setup script that runs when a new worktree is created. This script:
- Receives `$ROOT_WORKTREE_PATH` environment variable pointing to the base repository
- Can be customized per-project
- Sources `setup.local` if it exists for user-specific overrides
Common use cases:
- Installing dependencies (`npm install`, `bundle install`, etc.)
- Setting up development environment
- Creating configuration files
- Running initial builds
### `setup.local`
User-specific setup overrides (not committed to git). Create this file to add:
- Local environment variables
- User-specific development tools
- Machine-specific configuration
## How wt Uses This Directory
1. **Initialization**: When you run `wt add/go/run` for the first time, the directory structure is created automatically
2. **Worktree Creation**: New worktrees are created in `~/.wt/worktrees/{project-name}-{branch-name}`
3. **Symlink Creation**: A symlink is created in `.wt/worktrees/{branch-name}` pointing to the actual worktree
4. **Setup Execution**: The `setup` script runs in the context of the new worktree with `$ROOT_WORKTREE_PATH` set
## Best Practices
### For Project Maintainers
1. **Customize setup script**: Edit `.wt/setup` to include project-specific initialization
2. **Document requirements**: Add comments in `setup` explaining what happens
3. **Keep it fast**: Setup runs on every new worktree, so optimize for speed
4. **Make it idempotent**: Ensure setup can run multiple times safely
### For Contributors
1. **Create setup.local**: Add your personal setup steps without modifying the committed `setup` file
2. **Don't commit worktrees directory**: It's already gitignored
3. **Use relative paths**: When possible, use `$ROOT_WORKTREE_PATH` for paths to the main repository
## Example setup Script
```bash
#!/bin/bash
# wt - Git Worktree Management Tool
# https://github.com/tumf/wt
# $ROOT_WORKTREE_PATH is path to the base repository (source tree)
# Source local overrides first
if [ -f .wt/setup.local ]; then
source .wt/setup.local
fi
# Install dependencies (use cache from root if available)
if [ -d "$ROOT_WORKTREE_PATH/node_modules" ]; then
echo "Linking node_modules from root worktree..."
ln -sf "$ROOT_WORKTREE_PATH/node_modules" .
else
echo "Installing dependencies..."
npm install
fi
# Copy environment file if it doesn't exist
if [ ! -f .env ] && [ -f "$ROOT_WORKTREE_PATH/.env" ]; then
cp "$ROOT_WORKTREE_PATH/.env" .env
fi
# Run initial build
npm run build
```
## Example setup.local
```bash
#!/bin/bash
# User-specific setup (not committed)
# Set custom environment variables
export DEBUG=true
export LOG_LEVEL=verbose
# Use custom package manager
alias npm='pnpm'
# Skip builds for faster setup
export SKIP_BUILD=true
```
## Integration with AI Agents
When working with this project, AI agents should:
1. **Respect the structure**: Don't modify `.wt/worktrees/` manually
2. **Update setup thoughtfully**: Changes to `setup` affect all new worktrees
3. **Suggest setup.local**: For user-specific customizations
4. **Use wt commands**: Prefer `wt add/go/run` over manual worktree commands
5. **Document changes**: Update this file if adding new conventions
## Related Documentation
- Main tool: `/wt` (bash script)
- Repository: https://github.com/tumf/wt
- Git worktrees: https://git-scm.com/docs/git-worktree
EOF
fi
mkdir -p .wt/worktrees
}
# Create or add to existing worktree
create_worktree() {
local name="$1"
local worktree_dir="$2"
# Check if branch exists
if git branch --list "$name" | grep -q "$name"; then
echo "Branch '$name' already exists. Adding worktree without -b flag."
git worktree add "$worktree_dir" "$name"
else
echo "Creating new branch '$name' and adding worktree."
git worktree add "$worktree_dir" -b "$name"
fi
}
# Create symlink to worktree
create_symlink() {
local worktree_dir="$1"
local symlink_path="$2"
local force="${3:-false}"
if [ -e "$symlink_path" ]; then
if [ "$force" = true ]; then
echo "Symlink already exists at $symlink_path, removing old symlink"
rm "$symlink_path"
else
echo "Symlink already exists at $symlink_path"
exit 1
fi
fi
ln -s "$worktree_dir" "$symlink_path"
echo "Created symlink $symlink_path -> $worktree_dir"
}
# Run setup script if it exists
run_setup_script() {
if [ -f ".wt/setup" ]; then
echo "Running setup script..."
ROOT_WORKTREE_PATH="$(pwd)" bash ".wt/setup"
echo "Setup script completed"
fi
}
# Function to run setup script in worktree if it's a new worktree
run_setup_if_new() {
local worktree_dir="$1"
local is_new="$2"
local root_path="$3"
# Only run setup for newly created worktrees
if [ "$is_new" = "true" ]; then
# Run setup script if it exists in worktree
if [ -f "$worktree_dir/.wt/setup" ]; then
echo "Running setup script in worktree..."
(cd "$worktree_dir" && ROOT_WORKTREE_PATH="$root_path" bash .wt/setup)
echo "Setup script completed"
fi
fi
}
# Check if worktree exists and is valid
worktree_exists() {
local worktree_dir="$1"
if git worktree list --porcelain | grep -q "worktree $worktree_dir"; then
# Check if it's prunable (directory doesn't exist)
if [ ! -d "$worktree_dir" ]; then
return 1 # Exists but is prunable
fi
return 0 # Exists and is valid
fi
return 1 # Does not exist
}
case "$1" in
add)
if [ -z "$2" ]; then
echo "Usage: $0 add <name>"
exit 1
fi
NAME="$2"
WORKTREE_DIR=$(get_worktree_dir "$NAME")
SYMLINK_PATH=$(get_symlink_path "$NAME")
# Initialize .wt directory structure
init_wt_directory
# Check if worktree already exists
if git worktree list --porcelain | grep -q "worktree $WORKTREE_DIR"; then
echo "Worktree already exists at $WORKTREE_DIR"
exit 1
fi
# Create worktree
create_worktree "$NAME" "$WORKTREE_DIR"
# Create symlink
create_symlink "$WORKTREE_DIR" "$SYMLINK_PATH"
echo "Created worktree at $WORKTREE_DIR"
# Run setup script if it exists in worktree
ROOT_PATH="$(pwd)"
run_setup_if_new "$WORKTREE_DIR" "true" "$ROOT_PATH"
;;
remove | rm)
FORCE_FLAG=""
NAME=""
# Parse arguments
while [ $# -gt 1 ]; do
case "$2" in
-f | --force)
FORCE_FLAG="-f"
shift
;;
*)
if [ -z "$NAME" ]; then
NAME="$2"
fi
shift
;;
esac
done
if [ -z "$NAME" ]; then
echo "Usage: $0 remove [-f|--force] <name>"
exit 1
fi
SYMLINK_PATH=$(get_symlink_path "$NAME")
# Debug: show what we're checking
if [ ! -e "$SYMLINK_PATH" ]; then
# Check if it's a broken symlink
if [ -L "$SYMLINK_PATH" ]; then
echo "Found broken symlink at $SYMLINK_PATH"
rm "$SYMLINK_PATH"
echo "Removed broken symlink"
exit 0
else
echo "Worktree '$NAME' not found at $SYMLINK_PATH"
exit 1
fi
fi
# Get actual worktree path
ACTUAL_WORKTREE_DIR=$(readlink -f "$SYMLINK_PATH" 2>/dev/null)
# If symlink exists but worktree directory doesn't exist, just remove the symlink
if [ -z "$ACTUAL_WORKTREE_DIR" ] || [ ! -e "$ACTUAL_WORKTREE_DIR" ]; then
echo "Worktree directory not found, removing orphaned symlink"
rm "$SYMLINK_PATH"
echo "Removed orphaned symlink $SYMLINK_PATH"
exit 0
fi
# Remove worktree using git
echo "Removing worktree at $ACTUAL_WORKTREE_DIR"
if [ -n "$FORCE_FLAG" ]; then
if ! git worktree remove -f "$ACTUAL_WORKTREE_DIR"; then
echo "Failed to remove worktree"
exit 1
fi
else
if ! git worktree remove "$ACTUAL_WORKTREE_DIR"; then
echo "Failed to remove worktree (may contain modified files)"
echo "Use --force flag to force removal"
exit 1
fi
fi
# Verify worktree directory is gone
if [ -e "$ACTUAL_WORKTREE_DIR" ]; then
echo "Worktree still exists at $ACTUAL_WORKTREE_DIR"
echo "Worktree not removed completely"
exit 1
fi
# Remove symlink since worktree removal was successful
# Use -L to check for symlink existence (works even if broken)
if [ -L "$SYMLINK_PATH" ] || [ -e "$SYMLINK_PATH" ]; then
rm "$SYMLINK_PATH"
echo "Removed symlink $SYMLINK_PATH"
fi
echo "Worktree removed successfully"
;;
list)
git worktree list
;;
go)
if [ -z "$2" ]; then
echo "Usage: $0 go <name>"
exit 1
fi
NAME="$2"
WORKTREE_DIR=$(get_worktree_dir "$NAME")
SYMLINK_PATH=$(get_symlink_path "$NAME")
# Initialize .wt directory structure
init_wt_directory
# Check if worktree already exists
WORKTREE_EXISTS=false
IS_PRUNABLE=false
if git worktree list --porcelain | grep -q "worktree $WORKTREE_DIR"; then
WORKTREE_EXISTS=true
# Check if it's prunable (directory doesn't exist)
if [ ! -d "$WORKTREE_DIR" ]; then
IS_PRUNABLE=true
echo "Worktree '$NAME' is marked as prunable, removing stale entry"
git worktree remove "$WORKTREE_DIR" 2>/dev/null || true
git worktree prune 2>/dev/null || true
else
echo "Worktree '$NAME' exists, navigating to it"
fi
fi
# If worktree doesn't exist or was prunable, create it
if [ "$WORKTREE_EXISTS" = false ] || [ "$IS_PRUNABLE" = true ]; then
echo "Worktree '$NAME' does not exist, creating it first"
# Create worktree
create_worktree "$NAME" "$WORKTREE_DIR"
# Create symlink (force=true to remove old symlink if exists)
create_symlink "$WORKTREE_DIR" "$SYMLINK_PATH" true
echo "Created worktree at $WORKTREE_DIR"
# Run setup script if it exists in worktree (only for newly created worktrees)
ROOT_PATH="$(pwd)"
run_setup_if_new "$WORKTREE_DIR" "true" "$ROOT_PATH"
else
# Worktree exists and is not prunable, just ensure symlink exists
if [ ! -e "$SYMLINK_PATH" ]; then
ln -s "$WORKTREE_DIR" "$SYMLINK_PATH"
echo "Created symlink $SYMLINK_PATH -> $WORKTREE_DIR"
fi
fi
# Change to worktree directory
echo "Changing to worktree directory: $WORKTREE_DIR"
cd "$WORKTREE_DIR" && $SHELL
;;
run)
if [ -z "$2" ]; then
echo "Usage: $0 run <name> [--] <command>"
exit 1
fi
NAME="$2"
# Extract command arguments
# If -- delimiter exists, use everything after it
# Otherwise, use everything from 3rd argument onwards
if [ "$3" = "--" ]; then
if [ -z "$4" ]; then
echo "Usage: $0 run <name> -- <command>"
exit 1
fi
COMMAND_ARGS="${*:4}"
else
if [ -z "$3" ]; then
echo "Usage: $0 run <name> [--] <command>"
exit 1
fi
COMMAND_ARGS="${*:3}"
fi
WORKTREE_DIR=$(get_worktree_dir "$NAME")
SYMLINK_PATH=$(get_symlink_path "$NAME")
# Initialize .wt directory structure
init_wt_directory
# Check if worktree already exists
WORKTREE_EXISTS=false
IS_PRUNABLE=false
if git worktree list --porcelain | grep -q "worktree $WORKTREE_DIR"; then
WORKTREE_EXISTS=true
# Check if it's prunable (directory doesn't exist)
if [ ! -d "$WORKTREE_DIR" ]; then
IS_PRUNABLE=true
echo "Worktree '$NAME' is marked as prunable, removing stale entry"
git worktree remove "$WORKTREE_DIR" 2>/dev/null || true
git worktree prune 2>/dev/null || true
else
echo "Worktree '$NAME' exists, running command in it"
fi
fi
# If worktree doesn't exist or was prunable, create it
if [ "$WORKTREE_EXISTS" = false ] || [ "$IS_PRUNABLE" = true ]; then
echo "Worktree '$NAME' does not exist, creating it first"
# Create worktree
create_worktree "$NAME" "$WORKTREE_DIR"
# Create symlink
create_symlink "$WORKTREE_DIR" "$SYMLINK_PATH"
echo "Created worktree at $WORKTREE_DIR"
# Run setup script if it exists in worktree (only for newly created worktrees)
ROOT_PATH="$(pwd)"
run_setup_if_new "$WORKTREE_DIR" "true" "$ROOT_PATH"
fi
# Store current directory and execute command in worktree
CURRENT_DIR="$(pwd)"
echo "Running command in worktree '$NAME': $COMMAND_ARGS"
cd "$WORKTREE_DIR"
eval "$COMMAND_ARGS"
EXIT_CODE=$?
cd "$CURRENT_DIR"
exit $EXIT_CODE
;;
version | --version | -v)
echo "wt version $WT_VERSION"
;;
complations)
echo "Did you mean 'completions'?" >&2
echo "Usage: $0 completions <bash|zsh|fish|powershell>" >&2
exit 1
;;
completions)
SHELL_NAME="$2"
if [ -z "$SHELL_NAME" ]; then
echo "Usage: $0 completions <bash|zsh|fish|powershell>"
echo
echo "Install instructions:"
echo "- bash: wt completions bash > ~/.local/share/bash-completion/completions/wt (or /etc/bash_completion.d/wt)"
echo "- zsh: wt completions zsh > ~/.zsh/completions/_wt; echo 'fpath+=(~/.zsh/completions)' >> ~/.zshrc; autoload -Uz compinit && compinit"
echo "- fish: wt completions fish > ~/.config/fish/completions/wt.fish"
echo "- powershell: wt completions powershell | Out-String | Invoke-Expression (or save to your profile)"
exit 1
fi
case "$SHELL_NAME" in
bash)
cat <<'EOF'
# bash completion for wt
_wt()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
local subs="add remove rm list go run version completions"
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(compgen -W "$subs" -- "$cur") )
return 0
fi
case "${COMP_WORDS[1]}" in
remove|rm)
if [[ "$cur" == -* ]]; then
COMPREPLY=( $(compgen -W "--force -f" -- "$cur") )
else
local names
# Find git repository root
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
# Try to get names from .wt/worktrees directory first
if [ -n "$git_root" ] && [ -d "$git_root/.wt/worktrees" ]; then
names=$(command ls -1 "$git_root/.wt/worktrees" 2>/dev/null | sort -u)
fi
# Fallback to git worktree list
if [ -z "$names" ]; then
names=$(git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | sort -u)
fi
COMPREPLY=( $(compgen -W "$names" -- "$cur") )
fi
;;
go|add|run)
local names
# Find git repository root
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
# Try to get names from .wt/worktrees directory first
if [ -n "$git_root" ] && [ -d "$git_root/.wt/worktrees" ]; then
names=$(command ls -1 "$git_root/.wt/worktrees" 2>/dev/null | sort -u)
fi
# Fallback to git worktree list
if [ -z "$names" ]; then
names=$(git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | sort -u)
fi
COMPREPLY=( $(compgen -W "$names" -- "$cur") )
;;
completions)
COMPREPLY=( $(compgen -W "bash zsh fish powershell" -- "$cur") )
;;
*) ;;
esac
}
complete -F _wt wt ./wt
EOF
;;
zsh)
cat <<'EOF'
#compdef wt ./wt
local -a names
_wt_get_worktrees() {
# Find git repository root
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
# Try to get names from .wt/worktrees directory first
if [[ -n "$git_root" ]] && [[ -d "$git_root/.wt/worktrees" ]]; then
names=(${(f)"$(command ls -1 "$git_root/.wt/worktrees" 2>/dev/null)"})
fi
# Fallback to git worktree list
if [[ ${#names} -eq 0 ]]; then
names=(${(f)"$(git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | sort -u)"})
fi
}
if [[ $CURRENT -eq 2 ]]; then
# Completing subcommand (first argument)
local -a subcommands
subcommands=(
'add:Add a worktree'
'remove:Remove a worktree'
'rm:Alias of remove'
'list:List worktrees'
'go:Create or navigate to worktree'
'run:Run command in worktree'
'version:Show version'
'completions:Generate shell completion script'
)
_describe 'command' subcommands
elif [[ $CURRENT -eq 3 ]]; then
# Completing argument to subcommand
case $words[2] in
add|go|remove|rm|run)
_wt_get_worktrees
_describe 'worktree' names
;;
completions)
_values 'shell' bash zsh fish powershell
;;
esac
fi
EOF
;;
fish)
cat <<'EOF'
# fish completion for wt
complete -c wt -f
complete -c wt -n '__fish_use_subcommand' -a add -d 'Add a worktree'
complete -c wt -n '__fish_use_subcommand' -a remove -d 'Remove a worktree'
complete -c wt -n '__fish_use_subcommand' -a rm -d 'Alias of remove'
complete -c wt -n '__fish_use_subcommand' -a list -d 'List worktrees'
complete -c wt -n '__fish_use_subcommand' -a go -d 'Create or navigate to worktree'
complete -c wt -n '__fish_use_subcommand' -a run -d 'Run command in worktree'
complete -c wt -n '__fish_use_subcommand' -a version -d 'Show version'
complete -c wt -n '__fish_use_subcommand' -a completions -d 'Generate shell completion script'
# remove flags
complete -c wt -n '__fish_seen_subcommand_from remove' -s f -l force -d 'Force removal'
complete -c wt -n '__fish_seen_subcommand_from rm' -s f -l force -d 'Force removal'
# worktree names for relevant subcommands
# Define completion function for worktree names
function __wt_get_names
# Find git repository root
set -l git_root (git rev-parse --show-toplevel 2>/dev/null)
set -l names
# Try to get names from .wt/worktrees directory first
if test -n "$git_root" && test -d "$git_root/.wt/worktrees"
set names (command ls -1 "$git_root/.wt/worktrees" 2>/dev/null | sort -u)
end
# Fallback to git worktree list
if test -z "$names"
set names (git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | sort -u)
end
printf '%s\n' $names
end
complete -c wt -n '__fish_seen_subcommand_from go' -a '(__wt_get_names)'
complete -c wt -n '__fish_seen_subcommand_from add' -a '(__wt_get_names)'
complete -c wt -n '__fish_seen_subcommand_from remove' -a '(__wt_get_names)'
complete -c wt -n '__fish_seen_subcommand_from rm' -a '(__wt_get_names)'
complete -c wt -n '__fish_seen_subcommand_from run' -a '(__wt_get_names)'
# shells for completions
complete -c wt -n '__fish_seen_subcommand_from completions' -a 'bash zsh fish powershell'
EOF
;;
powershell | pwsh | ps)
cat <<'EOF'
# PowerShell argument completer for wt
Register-ArgumentCompleter -CommandName wt -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$subs = @('add','remove','rm','list','go','version','completions')
$tokens = [System.Management.Automation.PSParser]::Tokenize($commandAst.ToString(), [ref]$null)
$args = @($tokens | Where-Object { $_.Type -in 'CommandArgument','Parameter' } | ForEach-Object Value)
if ($args.Count -lt 1) {
$subs | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
return
}
$sub = $args[0]
switch ($sub) {
'completions' {
@('bash','zsh','fish','powershell') | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
}
'remove' { goto names }
'rm' { goto names }
'go' { goto names }
'add' { goto names }
default { }
}
return
:names
$names = @()
try {
$output = git worktree list 2>$null
foreach ($line in $output) {
if ($line -match '\[([^\]]+)\]') {
$names += $matches[1]
}
}
} catch {}
if ($names.Count -eq 0) {
try { $names = Get-ChildItem -Name '.wt/worktrees' -ErrorAction Stop } catch {}
}
$names | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
}
EOF
;;
*)
echo "Unsupported shell: $SHELL_NAME"
echo "Supported: bash, zsh, fish, powershell"
exit 1
;;
esac
;;
"" | *)
# No subcommand provided, show help
echo "wt - Git Worktree Management Tool v$WT_VERSION"
echo ""
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " add <name> Add a new worktree"
echo " remove <name> Remove a worktree"
echo " list List all worktrees"
echo " go <name> Create worktree if needed and navigate to it"
echo " run <name> [--] <command> Run command in worktree (creates if needed)"
echo " version Show version information"
echo ""
echo "Examples:"
echo " $0 add feature-branch"
echo " $0 go feature-branch"
echo " $0 run feature-branch git status"
echo " $0 run feature-branch -- git stash pop"
echo ""
git worktree list
;;
esac