-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree-manager.sh
More file actions
executable file
·162 lines (139 loc) · 4.32 KB
/
worktree-manager.sh
File metadata and controls
executable file
·162 lines (139 loc) · 4.32 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
#!/bin/bash
# Git Worktree Manager Script
# This script helps manage Git worktrees for the project
PROJECT_NAME="test-workspace"
BASE_DIR="$(pwd)/worktrees"
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
function print_help() {
echo "Git Worktree Manager"
echo "==================="
echo ""
echo "Usage: ./worktree-manager.sh [command] [args]"
echo ""
echo "Commands:"
echo " list - List all worktrees"
echo " create <branch-name> - Create a new worktree for a branch"
echo " remove <branch-name> - Remove a worktree"
echo " switch <branch-name> - Open worktree in Cursor"
echo " status - Show status of all worktrees"
echo " clean - Remove all worktrees (except main)"
echo ""
}
function list_worktrees() {
echo -e "${BLUE}Current worktrees:${NC}"
git worktree list
}
function create_worktree() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide a branch name${NC}"
return 1
fi
# Create worktrees directory if it doesn't exist
mkdir -p "${BASE_DIR}"
local branch_name=$1
local worktree_path="${BASE_DIR}/${branch_name}"
echo -e "${GREEN}Creating worktree for branch: ${branch_name}${NC}"
git worktree add "${worktree_path}" -b "${branch_name}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Worktree created at: ${worktree_path}${NC}"
echo -e "${BLUE}To open in Cursor, run: cursor ${worktree_path}${NC}"
else
echo -e "${RED}✗ Failed to create worktree${NC}"
fi
}
function remove_worktree() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide a branch name${NC}"
return 1
fi
local branch_name=$1
local worktree_path="${BASE_DIR}/${branch_name}"
echo -e "${GREEN}Removing worktree: ${worktree_path}${NC}"
git worktree remove "${worktree_path}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Worktree removed${NC}"
else
echo -e "${RED}✗ Failed to remove worktree${NC}"
fi
}
function switch_worktree() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide a branch name${NC}"
return 1
fi
local branch_name=$1
local worktree_path="${BASE_DIR}/${branch_name}"
if [ -d "${worktree_path}" ]; then
echo -e "${GREEN}Opening ${worktree_path} in Cursor...${NC}"
cursor "${worktree_path}"
else
echo -e "${RED}Error: Worktree not found at ${worktree_path}${NC}"
fi
}
function show_status() {
echo -e "${BLUE}Worktree Status:${NC}"
echo "================"
# Get all worktrees
while IFS= read -r line; do
worktree_path=$(echo "$line" | awk '{print $1}')
commit_hash=$(echo "$line" | awk '{print $2}')
branch_name=$(echo "$line" | awk '{print $3}' | tr -d '[]')
echo -e "\n${GREEN}Branch: ${branch_name}${NC}"
echo "Path: ${worktree_path}"
echo "Commit: ${commit_hash}"
# Show git status for each worktree
if [ -d "${worktree_path}" ]; then
cd "${worktree_path}"
status=$(git status --porcelain)
if [ -z "$status" ]; then
echo "Status: Clean"
else
echo "Status: Has uncommitted changes"
echo "$status" | head -5
fi
cd - > /dev/null
fi
done < <(git worktree list)
}
function clean_worktrees() {
echo -e "${RED}Warning: This will remove all worktrees except main!${NC}"
read -p "Are you sure? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git worktree list | grep -v '\[main\]' | awk '{print $1}' | while read -r path; do
echo -e "${GREEN}Removing: ${path}${NC}"
git worktree remove "${path}"
done
echo -e "${GREEN}✓ Cleanup complete${NC}"
else
echo "Cancelled"
fi
}
# Main script logic
case "$1" in
list)
list_worktrees
;;
create)
create_worktree "$2"
;;
remove)
remove_worktree "$2"
;;
switch)
switch_worktree "$2"
;;
status)
show_status
;;
clean)
clean_worktrees
;;
*)
print_help
;;
esac