-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-wt
More file actions
executable file
Β·1046 lines (902 loc) Β· 33.1 KB
/
git-wt
File metadata and controls
executable file
Β·1046 lines (902 loc) Β· 33.1 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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# git-wt - Git subcommand for worktree management
# Place this file in your PATH as 'git-wt' (executable)
# Usage: git wt <command> [args...]
# =============================================================================
# GIT WT SUBCOMMAND - Usage: git wt <command>
# =============================================================================
# Colors for output (with detection)
if [[ -t 1 ]] && command -v tput >/dev/null 2>&1 && tput colors >/dev/null 2>&1 && (( $(tput colors) >= 8 )); then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
show_help() {
echo -e "${BLUE}Git Worktree Management - Usage: git wt <command>${NC}"
echo ""
echo -e "${GREEN}π Available Commands:${NC}"
echo -e " ${BLUE}init${NC} [--template=<name>|--minimal] - Initialize .git-wt.yaml configuration"
echo -e " ${BLUE}new|add${NC} [branch-name] [base-branch] - Create new worktree"
echo -e " ${BLUE}list${NC} [-d] - List all worktrees (use -d for details)"
echo -e " ${BLUE}switch${NC} [-s|--shell] <branch-name> - Switch to worktree (optionally start shell)"
echo -e " ${BLUE}resume${NC} [-s|--shell] <branch-name> - Resume work in worktree (optionally start shell)"
echo -e " ${BLUE}remove${NC} [-f|--force] <branch-name> - Remove worktree and branch"
echo -e " ${BLUE}root${NC} - Switch to repository root"
echo -e " ${BLUE}status${NC} - Show overall status"
echo -e " ${BLUE}clean${NC} - Clean up worktrees"
echo -e " ${BLUE}task${NC} 'description' [branch] - Create worktree + start Claude task"
echo -e " ${BLUE}help${NC} - Show this help"
echo ""
echo -e "${YELLOW}π¦ Requirements:${NC}"
echo -e " ${BLUE}yq${NC} - YAML processor (required for setup/teardown actions)"
echo " Install: brew install yq"
echo " Homepage: https://github.com/mikefarah/yq"
echo ""
echo -e "${YELLOW}π Structure: All worktrees are created in .worktrees/ (auto-gitignored)${NC}"
echo -e "${YELLOW}π Integration: Works seamlessly with Claude Code slash commands${NC}"
echo ""
echo -e "${GREEN}Examples:${NC}"
echo " git wt init # Initialize configuration"
echo " git wt init --template=nodejs-simple # Use specific template"
echo " git wt new auth-feature # or: git wt add auth-feature"
echo " git wt task 'Add JWT authentication'"
echo " git wt switch auth-feature # Lightweight switch"
echo " git wt switch -s auth-feature # Switch with shell"
echo " git wt resume -s bug-fix # Resume with shell"
echo " git wt remove auth-feature # Remove worktree (safe)"
echo " git wt remove -f old-feature # Force remove worktree"
echo " git wt root # Return to main repo"
echo " git wt list"
}
ensure_git_repo() {
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo -e "${RED}β Not in a git repository${NC}"
exit 1
fi
}
get_default_branch() {
# Try to get the default branch from the remote
local default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
if [ -n "$default_branch" ]; then
echo "$default_branch"
return 0
fi
# Fallback: check which of main/master exists locally
if git show-ref --verify --quiet refs/heads/main; then
echo "main"
elif git show-ref --verify --quiet refs/heads/master; then
echo "master"
else
# Last resort: try to guess from current branch
local current_branch=$(git branch --show-current 2>/dev/null)
if [ -n "$current_branch" ]; then
echo "$current_branch"
else
# Ultimate fallback
echo "main"
fi
fi
}
_is_in_worktree_shell() {
[ "$GIT_WT_SHELL" = "1" ]
}
_exit_worktree_shell() {
if _is_in_worktree_shell; then
echo -e "${BLUE}π Exiting worktree shell and returning to repository root...${NC}"
if [ -n "$GIT_WT_ORIGINAL_DIR" ] && [ -d "$GIT_WT_ORIGINAL_DIR" ]; then
echo "π Back to: $GIT_WT_ORIGINAL_DIR"
cd "$GIT_WT_ORIGINAL_DIR"
else
# Fallback to git root if original dir is not available
local git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -n "$git_root" ]; then
cd "$git_root"
echo "π Back to: $git_root"
fi
fi
echo ""
echo -e "${GREEN}π‘ Type 'exit' to return to your original shell${NC}"
# Can't exit parent shell from script, user needs to type 'exit'
return 0
fi
return 1
}
setup_worktrees_dir() {
# Create .worktrees directory if it doesn't exist
mkdir -p .worktrees
# Add .worktrees to .gitignore if not already there
if ! grep -q "^\.worktrees/" .gitignore 2>/dev/null; then
echo ".worktrees/" >> .gitignore
echo -e "${GREEN}π Added .worktrees/ to .gitignore${NC}"
fi
}
require_config() {
if [ ! -f .git-wt.yaml ]; then
echo -e "${RED}β No .git-wt.yaml configuration found!${NC}"
echo -e "${YELLOW}π‘ Run 'git wt init' to create a configuration${NC}"
echo -e "${YELLOW}π‘ Or create .git-wt.yaml manually${NC}"
echo -e "${YELLOW}π‘ See templates/ directory for examples${NC}"
exit 1
fi
}
execute_setup_actions() {
local git_root="$1"
local worktree_path="$2"
local worktree_name="$3"
local base_branch="$4"
export GIT_ROOT="$git_root"
export WORKTREE_PATH="$worktree_path"
export WORKTREE_NAME="$worktree_name"
export BASE_BRANCH="$base_branch"
# Save current directory to restore after setup actions
pushd . > /dev/null
if ! command -v yq >/dev/null 2>&1; then
echo -e "${RED}β yq is required to run YAML-based setup actions${NC}"
echo -e "${YELLOW}π‘ Install yq: brew install yq${NC}"
echo ""
echo -e "${BLUE}π¦ yq is a lightweight YAML processor for bash${NC}"
echo " Homepage: https://github.com/mikefarah/yq"
echo ""
popd > /dev/null
exit 1
fi
local setup_count=$(yq eval '.setup | length' .git-wt.yaml 2>/dev/null)
if [ "$setup_count" = "0" ] || [ "$setup_count" = "null" ] || [ -z "$setup_count" ]; then
echo -e "${BLUE}βΉοΈ No setup actions configured${NC}"
popd > /dev/null
return 0
fi
echo -e "${BLUE}π§ Running setup actions...${NC}"
echo ""
for i in $(seq 0 $((setup_count - 1))); do
local name=$(yq eval ".setup[$i].name" .git-wt.yaml)
local desc=$(yq eval ".setup[$i].description" .git-wt.yaml)
local script=$(yq eval ".setup[$i].script" .git-wt.yaml)
echo -e "${GREEN}βΆ $desc${NC}"
# Run script in subshell so 'exit' doesn't terminate main process
(eval "$script")
local exit_code=$?
if [ $exit_code -eq 1 ]; then
echo -e "${RED}β Setup action '$name' failed${NC}"
popd > /dev/null
return 1
fi
echo ""
done
echo -e "${GREEN}β
Setup actions completed${NC}"
popd > /dev/null
return 0
}
execute_teardown_actions() {
local git_root="$1"
local worktree_path="$2"
local worktree_name="$3"
export GIT_ROOT="$git_root"
export WORKTREE_PATH="$worktree_path"
export WORKTREE_NAME="$worktree_name"
# Save current directory to restore after teardown actions
pushd . > /dev/null
if ! command -v yq >/dev/null 2>&1; then
# For teardown, we're lenient - just skip if yq not available
echo -e "${YELLOW}β οΈ yq not available, skipping teardown actions${NC}"
echo -e "${YELLOW}π‘ Install yq: brew install yq${NC}"
popd > /dev/null
return 0
fi
local teardown_count=$(yq eval '.teardown | length' .git-wt.yaml 2>/dev/null)
if [ "$teardown_count" = "0" ] || [ "$teardown_count" = "null" ] || [ -z "$teardown_count" ]; then
popd > /dev/null
return 0
fi
echo -e "${BLUE}π§ Running teardown actions...${NC}"
echo ""
for i in $(seq 0 $((teardown_count - 1))); do
local name=$(yq eval ".teardown[$i].name" .git-wt.yaml)
local desc=$(yq eval ".teardown[$i].description" .git-wt.yaml)
local script=$(yq eval ".teardown[$i].script" .git-wt.yaml)
echo -e "${YELLOW}βΆ $desc${NC}"
# Run script in subshell so 'exit' doesn't terminate main process
(eval "$script")
echo ""
done
popd > /dev/null
return 0
}
cmd_init() {
local template=""
local auto_detect=true
while [[ $# -gt 0 ]]; do
case $1 in
--template=*)
template="${1#*=}"
auto_detect=false
shift
;;
--minimal)
template="minimal"
auto_detect=false
shift
;;
-h|--help)
echo "Usage: git wt init [--template=<name>|--minimal]"
echo ""
echo "Available templates:"
echo " nodejs-full - Node.js with full setup (current git-wt behavior)"
echo " nodejs-simple - Node.js with simple npm install"
echo " python-django - Python Django projects"
echo " minimal - No setup actions"
echo ""
echo "Without --template, auto-detects project type"
return 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
return 1
;;
esac
done
ensure_git_repo
if [ -f .git-wt.yaml ]; then
echo -e "${YELLOW}β οΈ .git-wt.yaml already exists${NC}"
echo -n "Overwrite? [y/N]: "
read -n 1 -r response
echo
case $response in
[yY])
echo "Proceeding with overwrite..."
;;
*)
echo "Cancelled"
return 0
;;
esac
fi
if [ "$auto_detect" = true ]; then
if [ -f package.json ]; then
template="nodejs-full"
echo -e "${BLUE}π Detected Node.js project${NC}"
elif [ -f requirements.txt ] || [ -f setup.py ]; then
template="python-django"
echo -e "${BLUE}π Detected Python project${NC}"
else
template="minimal"
echo -e "${BLUE}π No specific project type detected, using minimal template${NC}"
fi
fi
# Get the directory where the actual script lives (follow symlinks)
local script_path="${BASH_SOURCE[0]}"
# Resolve symlink if this script is symlinked
if [ -L "$script_path" ]; then
script_path="$(readlink "$script_path")"
fi
local script_dir="$(cd "$(dirname "$script_path")" && pwd)"
local template_file="$script_dir/templates/${template}.yaml"
if [ ! -f "$template_file" ]; then
echo -e "${RED}β Template '$template' not found${NC}"
echo -e "${YELLOW}π‘ Available templates:${NC}"
ls -1 "$script_dir/templates/" 2>/dev/null | sed 's/\.yaml$//' | sed 's/^/ /'
return 1
fi
cp "$template_file" .git-wt.yaml
echo -e "${GREEN}β
Created .git-wt.yaml from '$template' template${NC}"
echo ""
echo -e "${BLUE}π Next steps:${NC}"
echo " 1. Review and customize .git-wt.yaml as needed"
echo " 2. Consider adding .git-wt.yaml to git to share with team"
echo " 3. Run 'git wt new <branch-name>' to create your first worktree"
echo ""
echo -n "Add .git-wt.yaml to git? [Y/n]: "
read -n 1 -r response
echo
case $response in
[nN])
echo "Skipped git add"
;;
*)
git add .git-wt.yaml
echo -e "${GREEN}β
Added .git-wt.yaml to git${NC}"
echo -e "${YELLOW}π‘ Don't forget to commit: git commit -m 'Add git-wt configuration'${NC}"
;;
esac
}
cmd_new() {
local branch_name=""
local base_branch=$(get_default_branch)
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: git wt new [branch-name] [base-branch]"
return 1
;;
*)
if [ -z "$branch_name" ]; then
branch_name="$1"
else
base_branch="$1"
fi
shift
;;
esac
done
# Set default branch name if not provided
if [ -z "$branch_name" ]; then
branch_name="task-$(date +%Y%m%d-%H%M%S)"
fi
local git_root=$(git rev-parse --show-toplevel)
local worktree_dir="$git_root/.worktrees/${branch_name}"
ensure_git_repo
require_config
setup_worktrees_dir
echo -e "${BLUE}π Creating new worktree...${NC}"
echo "Branch: $branch_name"
echo "Base: $base_branch"
echo "Directory: $worktree_dir"
# Create the worktree in organized structure
git worktree add -b "$branch_name" "$worktree_dir" "$base_branch"
if [ $? -eq 0 ]; then
echo -e "${GREEN}β
Worktree created successfully!${NC}"
echo -e "${BLUE}π Directory: $(realpath $worktree_dir)${NC}"
# Create a .git-wt-session file for session management
echo "branch=$branch_name" > "$worktree_dir/.git-wt-session"
echo "created=$(date -Iseconds)" >> "$worktree_dir/.git-wt-session"
echo "base_branch=$base_branch" >> "$worktree_dir/.git-wt-session"
echo ""
# Execute setup actions from YAML configuration
cd "$git_root"
if ! execute_setup_actions "$git_root" "$worktree_dir" "$branch_name" "$base_branch"; then
echo -e "${RED}β Setup actions failed${NC}"
echo -e "${YELLOW}β οΈ Worktree was created but setup failed${NC}"
echo -e "${YELLOW}π‘ You may want to remove it: git wt remove $branch_name${NC}"
return 1
fi
echo ""
echo -e "${GREEN}β
Worktree created successfully!${NC}"
echo ""
# Ask if user wants to switch to the new worktree
echo -n "Switch to the new worktree with shell? [Y/n]: "
read -n 1 -r response
echo # Add newline after single character input
case $response in
[nN][oO]|[nN])
echo -e "${BLUE}π Worktree ready for use${NC}"
echo ""
echo "Next steps:"
echo "1. cd $worktree_dir"
echo "2. Your worktree is ready to use"
echo "3. Use: claude code 'your task description'"
echo ""
echo "Or use: git wt switch -s $branch_name"
;;
*)
# Default to yes (Y or Enter or anything else)
echo -e "${BLUE}π Switching to worktree with shell...${NC}"
echo ""
# Call the existing switch function with shell option
cmd_switch -s "$branch_name"
;;
esac
else
echo -e "${RED}β Failed to create worktree${NC}"
exit 1
fi
}
cmd_list() {
local show_details=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-d|--details)
show_details=true
shift
;;
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: git wt list [-d|--details]"
return 1
;;
*)
echo -e "${RED}Unexpected argument: $1${NC}"
echo "Usage: git wt list [-d|--details]"
return 1
;;
esac
done
ensure_git_repo
if [ ! -d ".worktrees" ]; then
echo "No .worktrees directory found"
return 0
fi
if [ "$show_details" = true ]; then
# Show detailed output (original format)
echo -e "${BLUE}π Git Worktrees${NC}"
echo "========================"
local found_any=false
for worktree_dir in .worktrees/*/; do
if [ -d "$worktree_dir" ]; then
found_any=true
local branch_name=$(basename "$worktree_dir")
local relative_path="$worktree_dir"
echo ""
echo -e "${GREEN}πΏ Branch: $branch_name${NC}"
echo -e "${BLUE}π Path: $relative_path${NC}"
# Get commit info
if [ -d "$worktree_dir/.git" ] || [ -f "$worktree_dir/.git" ]; then
cd "$worktree_dir"
local commit_info=$(git log -1 --format='%h %s' 2>/dev/null || echo 'No commits')
echo "πΎ Commit: $commit_info"
# Check session info if available
if [ -f ".git-wt-session" ]; then
local created=$(grep "created=" .git-wt-session | cut -d'=' -f2)
local task=$(grep "^task=" .git-wt-session | cut -d'=' -f2-)
echo "β° Created: $created"
if [ -n "$task" ]; then
echo "π Task: $task"
fi
fi
# Check for uncommitted changes and untracked files
local git_status=$(git status --porcelain 2>/dev/null)
if [ -n "$git_status" ]; then
echo -e "${YELLOW}π Status: Has uncommitted changes${NC}"
else
echo -e "${GREEN}β
Status: Clean${NC}"
fi
cd - > /dev/null
fi
fi
done
if [ "$found_any" != true ]; then
echo "No worktrees found in .worktrees/"
fi
else
# Show simplified output (branch, status)
echo -e "${BLUE}π Git Worktrees${NC}"
echo "========================"
echo ""
local found_any=false
for worktree_dir in .worktrees/*/; do
if [ -d "$worktree_dir" ]; then
found_any=true
local branch_name=$(basename "$worktree_dir")
local status="Unknown"
local status_color=""
# Get status
if [ -d "$worktree_dir/.git" ] || [ -f "$worktree_dir/.git" ]; then
cd "$worktree_dir"
# Check for both modified and untracked files
local git_status=$(git status --porcelain 2>/dev/null)
if [ -n "$git_status" ]; then
status="Modified"
status_color="${YELLOW}"
else
status="Clean"
status_color="${GREEN}"
fi
cd - > /dev/null
fi
echo -e "${GREEN}πΏ ${branch_name}${NC} - ${status_color}${status}${NC}"
fi
done
if [ "$found_any" != true ]; then
echo "No worktrees found in .worktrees/"
fi
fi
}
cmd_switch() {
local create_shell=false
local branch_name=""
ensure_git_repo
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-s|--shell)
create_shell=true
shift
;;
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: git wt switch [-s|--shell] <branch-name>"
return 1
;;
*)
branch_name="$1"
break
;;
esac
done
if [ -z "$branch_name" ]; then
echo "Available worktrees:"
cmd_list
echo ""
echo "Usage: git wt switch [-s|--shell] <branch-name>"
return 1
fi
# Get git root before changing directories
local git_root=$(git rev-parse --show-toplevel)
local worktree_path="$git_root/.worktrees/$branch_name"
if [ -d "$worktree_path" ]; then
echo -e "${BLUE}π Switching to worktree: $branch_name${NC}"
cd "$worktree_path"
# Show current session info
if [ -f ".git-wt-session" ]; then
echo -e "${BLUE}π Session info:${NC}"
cat .git-wt-session
fi
echo ""
echo "Current directory: $(pwd)"
echo "Git status:"
git status --short
echo ""
if [ "$create_shell" = true ]; then
# Start worktree shell
echo ""
echo -e "${GREEN}Starting worktree shell. Type 'exit' or use 'git wt root' to return.${NC}"
export GIT_WT_SHELL=1
export GIT_WT_ORIGINAL_DIR="$OLDPWD"
export GIT_WT_BRANCH="$branch_name"
PS1="(wt:$branch_name) $PS1" $SHELL
else
# Just change directory
echo -e "${GREEN}β
Ready to work in worktree${NC}"
fi
else
echo -e "${RED}β Worktree '$branch_name' not found in .worktrees/${NC}"
echo "Available worktrees:"
ls -1 .worktrees/ 2>/dev/null || echo "No worktrees found"
return 1
fi
}
cmd_resume() {
local create_shell=false
local branch_name=""
ensure_git_repo
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-s|--shell)
create_shell=true
shift
;;
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: git wt resume [-s|--shell] <branch-name>"
return 1
;;
*)
branch_name="$1"
break
;;
esac
done
if [ -z "$branch_name" ]; then
echo "Available worktrees to resume:"
ls -1 .worktrees/ 2>/dev/null || echo "No worktrees found"
echo ""
echo "Usage: git wt resume [-s|--shell] <branch-name>"
return 1
fi
if [ ! -d ".worktrees/$branch_name" ]; then
echo -e "${RED}β Worktree '$branch_name' not found${NC}"
return 1
fi
# Get git root before changing directories
local git_root=$(git rev-parse --show-toplevel)
cd ".worktrees/$branch_name"
echo -e "${BLUE}π Resuming work in: $branch_name${NC}"
# Show session info if available
if [ -f ".git-wt-session" ]; then
echo -e "${BLUE}π Previous session:${NC}"
cat .git-wt-session | while IFS='=' read key value; do
case $key in
task) echo " Task: $value" ;;
created) echo " Created: $value" ;;
branch) echo " Branch: $value" ;;
base_branch) echo " Base: $value" ;;
esac
done
echo ""
fi
echo "Current status:"
git status --short
echo ""
echo "Ready to continue work in $(pwd)"
if [ "$create_shell" = true ]; then
# Start worktree shell
echo ""
echo -e "${GREEN}Starting worktree shell. Type 'exit' or use 'git wt root' to return.${NC}"
export GIT_WT_SHELL=1
export GIT_WT_ORIGINAL_DIR="$OLDPWD"
export GIT_WT_BRANCH="$branch_name"
PS1="(wt:$branch_name) $PS1" $SHELL
else
# Just change directory
echo -e "${GREEN}β
Ready to continue work in worktree${NC}"
fi
}
cmd_status() {
ensure_git_repo
echo -e "${BLUE}π Git Worktrees Status${NC}"
echo "================================"
# Show current location
echo "π Current location: $(pwd)"
if [[ "$(pwd)" == *".worktrees/"* ]]; then
echo " You are currently in a worktree"
else
echo " You are in the main repository"
fi
echo ""
# Show all worktrees with detailed status
cmd_list
echo ""
echo -e "${BLUE}π Summary:${NC}"
local total_worktrees=$(ls -1d .worktrees/*/ 2>/dev/null | wc -l)
echo " Total worktrees: $total_worktrees"
# Count worktrees with changes
local dirty_count=0
for worktree_dir in .worktrees/*/; do
if [ -d "$worktree_dir" ]; then
cd "$worktree_dir"
local git_status=$(git status --porcelain 2>/dev/null)
if [ -n "$git_status" ]; then
((dirty_count++))
fi
cd - > /dev/null
fi
done
echo " With changes: $dirty_count"
echo " Clean: $((total_worktrees - dirty_count))"
}
cmd_clean() {
ensure_git_repo
echo -e "${YELLOW}π§Ή Git worktree cleanup${NC}"
echo ""
if [ ! -d ".worktrees" ]; then
echo "No .worktrees directory found"
return 0
fi
echo "Current worktrees in .worktrees/:"
cmd_list
echo ""
# Show merged branches that can be cleaned up
local default_branch=$(get_default_branch)
echo "Branches that have been merged to $default_branch:"
git branch --merged "$default_branch" 2>/dev/null | grep -E "(task-|feature-|fix-)" | sed 's/^[ *]*//' | while read branch; do
if [ -d ".worktrees/$branch" ]; then
echo " π $branch (in .worktrees/)"
fi
done
echo ""
read -p "Enter branch name to remove (or 'auto' for all merged): " branch_name
if [ "$branch_name" = "auto" ]; then
# Auto-cleanup merged branches
local default_branch=$(get_default_branch)
git branch --merged "$default_branch" 2>/dev/null | grep -E "(task-|feature-|fix-)" | sed 's/^[ *]*//' | while read branch; do
if [ "$branch" != "main" ] && [ "$branch" != "master" ] && [ -d ".worktrees/$branch" ]; then
echo -e "${YELLOW}ποΈ Removing worktree for branch: $branch${NC}"
git worktree remove ".worktrees/$branch" 2>/dev/null || true
git branch -d "$branch" 2>/dev/null || true
fi
done
elif [ -n "$branch_name" ]; then
# Remove specific branch
if [ -d ".worktrees/$branch_name" ]; then
git worktree remove ".worktrees/$branch_name"
git branch -d "$branch_name"
echo -e "${GREEN}β
Removed worktree: $branch_name${NC}"
else
echo -e "${RED}β Branch $branch_name not found in .worktrees/${NC}"
fi
fi
}
cmd_task() {
local task_description="$1"
local branch_name="${2:-task-$(date +%Y%m%d-%H%M%S)}"
if [ -z "$task_description" ]; then
echo "Usage: git wt task 'task description' [branch-name]"
echo "Example: git wt task 'Add user authentication system' auth-feature"
exit 1
fi
ensure_git_repo
# Create the worktree
cmd_new "$branch_name"
if [ $? -eq 0 ]; then
# Switch to the new worktree
cd ".worktrees/$branch_name"
# Save task description to session file
echo "task=$task_description" >> .git-wt-session
echo "task_started=$(date -Iseconds)" >> .git-wt-session
echo -e "${BLUE}π€ Starting Claude Code task in worktree...${NC}"
echo "Task: $task_description"
echo ""
# Start Claude Code with the task
claude code "$task_description"
fi
}
cmd_remove() {
local force_remove=false
local branch_name=""
ensure_git_repo
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-f|--force)
force_remove=true
shift
;;
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: git wt remove [-f|--force] <branch-name>"
return 1
;;
*)
branch_name="$1"
break
;;
esac
done
if [ -z "$branch_name" ]; then
echo "Available worktrees to remove:"
cmd_list
echo ""
echo "Usage: git wt remove [-f|--force] <branch-name>"
return 1
fi
local git_root=$(git rev-parse --show-toplevel)
local worktree_path="$git_root/.worktrees/$branch_name"
if [ ! -d "$worktree_path" ]; then
echo -e "${RED}β Worktree '$branch_name' not found in .worktrees/${NC}"
echo "Available worktrees:"
ls -1 .worktrees/ 2>/dev/null || echo "No worktrees found"
return 1
fi
# Check if currently in the worktree we're trying to remove
if _is_in_worktree_shell && [ "$GIT_WT_BRANCH" = "$branch_name" ]; then
echo -e "${RED}β Cannot remove worktree '$branch_name' - you are currently in its shell${NC}"
echo -e "${YELLOW}π‘ Use 'git wt root' to exit the worktree shell first${NC}"
return 1
fi
# Check for uncommitted changes unless force flag is used
if [ "$force_remove" != true ]; then
cd "$worktree_path"
local git_status=$(git status --porcelain 2>/dev/null)
if [ -n "$git_status" ]; then
echo -e "${RED}β Cannot remove worktree '$branch_name' - has uncommitted changes:${NC}"
echo ""
git status --short
echo ""
echo -e "${YELLOW}π‘ Use 'git wt remove -f $branch_name' to force removal${NC}"
cd - > /dev/null
return 1
fi
cd - > /dev/null
fi
# Execute teardown actions before removing worktree
cd "$git_root"
execute_teardown_actions "$git_root" "$worktree_path" "$branch_name"
# Ensure we're back in git root and not in the worktree we're about to remove
cd "$git_root"
# Double-check we're not inside the worktree path
local current_dir=$(pwd)
if [[ "$current_dir" == "$worktree_path"* ]]; then
echo -e "${RED}β Cannot remove worktree - still inside it: $current_dir${NC}"
echo -e "${YELLOW}π‘ Changing to git root: $git_root${NC}"
cd "$git_root"
fi
echo -e "${YELLOW}ποΈ Removing worktree: $branch_name${NC}"
echo " Current directory: $(pwd)"
echo " Worktree path: $worktree_path"
# Remove the worktree
local remove_output=$(git worktree remove "$worktree_path" --force 2>&1)
local remove_exit_code=$?
if [ $remove_exit_code -eq 0 ]; then
echo -e "${GREEN}β
Worktree removed successfully${NC}"
# Delete the branch
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
if [ "$force_remove" = true ]; then
git branch -D "$branch_name" 2>/dev/null
if [ $? -eq 0 ]; then
echo -e "${GREEN}β
Branch '$branch_name' deleted (forced)${NC}"
else
echo -e "${YELLOW}β οΈ Failed to delete branch '$branch_name'${NC}"
fi
else
git branch -d "$branch_name" 2>/dev/null
if [ $? -eq 0 ]; then
echo -e "${GREEN}β
Branch '$branch_name' deleted${NC}"
else
echo -e "${YELLOW}β οΈ Branch '$branch_name' not deleted - may have unmerged commits${NC}"
echo -e "${YELLOW}π‘ Use 'git branch -D $branch_name' to force delete${NC}"
fi
fi
else
echo -e "${YELLOW}β οΈ Branch '$branch_name' not found or already deleted${NC}"
fi
echo ""
echo -e "${BLUE}π Remaining worktrees:${NC}"
cmd_list
else
echo -e "${RED}β Failed to remove worktree${NC}"
echo " Error output: $remove_output"
echo " Exit code: $remove_exit_code"
return 1
fi
}
cmd_root() {
ensure_git_repo
# Check if we're in a worktree shell and exit if so
if _exit_worktree_shell; then
return 0
fi
# Regular root switching logic
local git_root=$(git rev-parse --show-toplevel)
echo -e "${BLUE}π Switching to repository root...${NC}"
cd "$git_root"
echo "π Current directory: $(pwd)"
echo "πΏ Current branch: $(git branch --show-current)"
echo ""
# Show git status
echo "Git status:"
git status --short
echo ""
# Show available worktrees
if [ -d ".worktrees" ]; then
echo -e "${BLUE}π Available worktrees:${NC}"
ls -1 .worktrees/ 2>/dev/null || echo "No worktrees found"
echo ""
fi
echo -e "${GREEN}β
Now in repository root directory${NC}"
}