-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzsh-fzf-git-worktree.zsh
More file actions
273 lines (243 loc) · 7.48 KB
/
zsh-fzf-git-worktree.zsh
File metadata and controls
273 lines (243 loc) · 7.48 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
#!/usr/bin/env zsh
fzf-git-worktree() {
local CWD=${PWD}
local CWD_PARENT=${CWD:h}
local CWD_NAME=${CWD:t}
make_temp_dir() {
mktemp -d -t "fzf-git-worktree-$(date +%Y%m%d-%H%M%S)"
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}"
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
cwd_is_git_repo() {
[[ -d "$CWD/.git" ]] && git rev-parse --is-inside-work-tree >/dev/null 2>&1
}
usage() {
cat <<HEREDOC
Usage:
fzf-git-worktree interactive switcher
fzf-git-worktree i, init setup "fzf-git-worktree" in cwd
fzf-git-worktree add <n> create new work tree and switch to it
fzf-git-worktree rm, remove <n> remove work tree
fzf-git-worktree ls, list list work trees
fzf-git-worktree help print usage
HEREDOC
}
prepare() {
if ! cwd_is_git_repo && [[ -d "$CWD/main" ]]; then
cd main || return 1
CWD=${PWD}
CWD_PARENT=${CWD:h}
CWD_NAME=${CWD:t}
fi
git worktree prune
}
handle_init() {
if cwd_is_git_repo; then
if [[ "$CWD_NAME" == "main" ]]; then
echo "FATAL: cannot init in a directory called 'main'"
return 1
fi
local TMP=$(make_temp_dir)
mv ./* "$TMP"
mkdir main
mv "$TMP"/* main
cd main || return 1
else
echo "FATAL: not in a git repository's root directory"
return 1
fi
}
handle_list() {
git worktree list | sed -E 's/^(.*\/([^[:space:]]* ))/\1 \2/g' | awk '{printf "%-10s %s\n", $2, $4}'
}
handle_switch() {
local SELECTION=$(git worktree list | \
sed -E 's/^(.*\/([^[:space:]]* ))/\1 \2/g' | \
fzf --with-nth=2,4 \
--preview-window=right:50% \
--ansi \
--preview="echo '📦 Branch:' && \
git -C {1} branch --show-current && \
echo && \
echo '📝 Changed files:' && \
git -C {1} status --porcelain | head -10 && \
echo && \
echo '📚 Recent commits:' && \
git -C {1} log --oneline --decorate -10" )
if [[ -z "$SELECTION" ]]; then
return 0
fi
local DIR=${SELECTION%% *}
cd "$DIR"
}
handle_add() {
local NAME=$2
if [[ -z "$NAME" ]]; then
echo -n "Enter new worktree name: "
read NAME
if [[ -z "$NAME" ]]; then
echo "FATAL: worktree name cannot be empty"
return 1
fi
fi
local CURRENT_BRANCH=$(git branch --show-current)
local NEW_DIR
# Check if a branch with the given name exists locally
if git branch --list "$NAME" | grep -q .; then
# Local branch exists, try to check it out
if git worktree add "../$NAME" "$NAME" 2>/dev/null; then
NEW_DIR=${CWD_PARENT}/${NAME}
else
# Branch is already checked out somewhere else
echo "Branch '$NAME' is already checked out"
return 1
fi
else
# Check if branch exists on remote
local REMOTE_BRANCH=$(git branch -r | grep -E "origin/${NAME}$" | head -1)
# Trim whitespace using parameter expansion
REMOTE_BRANCH="${REMOTE_BRANCH#"${REMOTE_BRANCH%%[![:space:]]*}"}"
REMOTE_BRANCH="${REMOTE_BRANCH%"${REMOTE_BRANCH##*[![:space:]]}"}"
if [[ -n "$REMOTE_BRANCH" ]]; then
# Remote branch exists, create worktree from it
echo "Creating worktree from remote branch: $REMOTE_BRANCH"
if git worktree add "../$NAME" "$REMOTE_BRANCH" 2>/dev/null; then
NEW_DIR=${CWD_PARENT}/${NAME}
git -C "$NEW_DIR" switch "$NAME"
else
echo "Failed to create worktree from remote branch"
return 1
fi
else
# Branch doesn't exist anywhere, create it
echo "Creating new branch: $NAME"
git worktree add -b "$NAME" "../$NAME" "$CURRENT_BRANCH"
NEW_DIR=${CWD_PARENT}/${NAME}
fi
fi
if [[ -f "$CWD_PARENT/hook.sh" ]]; then
bash "$CWD_PARENT/hook.sh" "$NEW_DIR"
fi
cd "$NEW_DIR"
}
handle_new() {
echo "DEPRECATED: 'new' is deprecated. Use 'add' instead."
return 1
}
handle_remove() {
local FORCE=""
local ALL=false
local NAME=""
# Parse arguments
shift # Remove the 'rm' or 'remove' command
while [[ $# -gt 0 ]]; do
case $1 in
-f|--force)
FORCE="-f"
shift
;;
--all)
ALL=true
shift
;;
*)
NAME=$1
shift
;;
esac
done
# Handle --all flag
if [[ "$ALL" == true ]]; then
# Get main worktree path
local MAIN_WORKTREE=$(git worktree list | head -1 | awk '{print $1}')
cd "$MAIN_WORKTREE"
# Get all worktrees except main
local WORKTREES=$(git worktree list | tail -n +2)
if [[ -z "$WORKTREES" ]]; then
echo "No worktrees to remove (only main exists)"
return 0
fi
echo "Removing all worktrees except main..."
while IFS= read -r line; do
local WORKTREE_PATH=$(echo "$line" | awk '{print $1}')
local WORKTREE_NAME=$(basename "$WORKTREE_PATH")
local BRANCH_NAME=$(echo "$line" | awk '{print $3}' | tr -d '[]')
echo "Removing worktree: $WORKTREE_NAME"
git worktree remove $FORCE "$WORKTREE_PATH"
# If worktree removal succeeded and we have a branch name, delete the branch
if [[ $? -eq 0 ]] && [[ -n "$BRANCH_NAME" ]]; then
git branch -D "$BRANCH_NAME" 2>/dev/null
fi
done <<< "$WORKTREES"
return 0
fi
# Handle single worktree removal
if [[ -z "$NAME" ]]; then
local SELECTION=$(git worktree list | \
sed -E 's/^(.*\/([^[:space:]]* ))/\1 \2/g' | \
fzf --with-nth=2,4 \
--header="Select worktree to remove:" \
--ansi \
--preview-window=right:50% \
--preview="echo '📦 Branch:' && \
git -C {1} branch --show-current && \
echo && \
echo '📝 Changed files:' && \
git -C {1} status --porcelain | head -10 && \
echo && \
echo '📚 Recent commits:' && \
git -C {1} log --oneline --decorate -10" )
if [[ -z "$SELECTION" ]]; then
return 0
fi
NAME=${SELECTION%% *}
fi
# Get the branch name before removing the worktree
local WORKTREE_PATH
if [[ "$NAME" =~ ^/ ]]; then
# NAME is already a full path
WORKTREE_PATH="$NAME"
else
# NAME is relative, find the full path
WORKTREE_PATH=$(git worktree list | grep -E "/$NAME " | awk '{print $1}')
fi
local BRANCH_NAME=$(git -C "$WORKTREE_PATH" branch --show-current 2>/dev/null)
# Always cd to the main worktree before removing
local MAIN_WORKTREE=$(git worktree list | head -1 | awk '{print $1}')
cd "$MAIN_WORKTREE"
git worktree remove $FORCE "$NAME"
# If worktree removal succeeded and we have a branch name, delete the branch
if [[ $? -eq 0 ]] && [[ -n "$BRANCH_NAME" ]]; then
git branch -D "$BRANCH_NAME"
fi
}
prepare
local COMMAND=$1
case "$COMMAND" in
"")
handle_switch
;;
i|init)
handle_init
;;
ls|list)
handle_list
;;
add)
handle_add "$@"
;;
new)
handle_new
;;
rm|remove)
handle_remove "$@"
;;
help|--help|-h|*)
usage
;;
esac
}