-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-worktree-utils.sh
More file actions
484 lines (430 loc) · 15.8 KB
/
git-worktree-utils.sh
File metadata and controls
484 lines (430 loc) · 15.8 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
#!/usr/bin/env bash
# Git Worktree Utils - Core Functions
# https://github.com/yourusername/git-worktree-utils
# shellcheck disable=SC2016 # Ignore backtick warnings for command substitution
# Load configuration (XDG aware with legacy fallback)
GWT_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
GWT_CONFIG_DIR="${GWT_CONFIG_HOME}/git-worktree-utils"
GWT_CONFIG_FILE="${GWT_CONFIG_DIR}/config"
# Legacy fallback to ~/.config if XDG location missing
if [[ ! -f "$GWT_CONFIG_FILE" ]]; then
LEGACY_CONFIG_DIR="${HOME}/.config/git-worktree-utils"
if [[ -f "${LEGACY_CONFIG_DIR}/config" ]]; then
GWT_CONFIG_DIR="$LEGACY_CONFIG_DIR"
GWT_CONFIG_FILE="${LEGACY_CONFIG_DIR}/config"
fi
fi
# Default configuration
GWT_DIR_PATTERN="{base}-{branch}"
GWT_AUTO_PRUNE=true
GWT_CONFIRM_DELETE=true
GWT_CLEANUP_PATTERNS="*-feature* *-hotfix* *-release* *-review* *-epic*"
# Load user config if exists
if [[ -f "$GWT_CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$GWT_CONFIG_FILE"
fi
# Color codes for output (can be disabled via GWT_USE_COLOR)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Helper function to print colored output
_gwt_print() {
local color=$1
shift
if [[ "${GWT_USE_COLOR:-true}" != "true" ]]; then
printf '%s\n' "$*"
else
printf '%b\n' "${color}$*${NC}"
fi
}
# Helper function to parse worktree paths from --porcelain output
_gwt_get_worktree_paths() {
git worktree list --porcelain | awk '/^worktree / {print $2}'
}
# Helper: list recent entries in a directory (by mtime)
_gwt_list_recent() {
local dir="$1"
local n="${2:-3}"
case "$(uname -s)" in
Darwin)
while IFS= read -r -d '' p; do
stat -f "%m %N" "$p" 2>/dev/null || true
done < <(find "$dir" -mindepth 1 -maxdepth 1 -print0 2>/dev/null) \
| sort -nr | head -n "$n" | cut -d' ' -f2- | sed 's/^/ /'
;;
Linux)
while IFS= read -r -d '' p; do
stat -c "%Y %n" "$p" 2>/dev/null || true
done < <(find "$dir" -mindepth 1 -maxdepth 1 -print0 2>/dev/null) \
| sort -nr | head -n "$n" | cut -d' ' -f2- | sed 's/^/ /'
;;
*)
# Fallback: unsorted list via find (no ls parsing)
find "$dir" -mindepth 1 -maxdepth 1 -print 2>/dev/null | head -n "$n" | sed 's/^/ /'
;;
esac
}
# Main worktree function
gwt() {
# Handle --help flag first, before any other checks
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
gwthelp gwt
return 0
fi
# If no argument, list worktrees (doesn't require git repo check if we just want to show nothing)
if [[ -z "$1" ]]; then
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
_gwt_print "$RED" "✗ Not in a git repository"
return 1
fi
_gwt_print "$GREEN" "Current worktrees:"
git worktree list
return 0
fi
# Check if we're in a git repository for branch operations
if ! git rev-parse --git-dir > /dev/null 2>&1; then
_gwt_print "$RED" "✗ Not in a git repository"
return 1
fi
local branch="$1"
# Sanitize branch name for directory (replace / with -)
local safe_name
safe_name=$(echo "$1" | tr '/' '-' | tr ' ' '-' | tr ':' '-')
# Get base directory name from repository root
local base_dir repo_root
repo_root=$(git rev-parse --show-toplevel)
base_dir=$(basename "$repo_root")
# Construct target directory path using pattern
local dir_name
dir_name="${GWT_DIR_PATTERN//\{base\}/$base_dir}"
dir_name="${dir_name//\{branch\}/$safe_name}"
local target_dir="../${dir_name}"
local abs_target
abs_target=$(cd ..; pwd)/${dir_name}
# Auto-prune if enabled
if [[ "$GWT_AUTO_PRUNE" == "true" ]]; then
git worktree prune 2>/dev/null
fi
# Check if worktree already exists
if _gwt_get_worktree_paths | grep -Fxq "$abs_target"; then
# Worktree exists, check if directory exists
if [[ -d "$target_dir" ]]; then
_gwt_print "$GREEN" "✓ Worktree already set up at $target_dir"
cd "$target_dir" || return 1
return
else
_gwt_print "$YELLOW" "⚠️ Worktree registered but directory missing. Repairing..."
git worktree prune
fi
fi
# Check if directory exists without worktree
if [[ -d "$target_dir" ]]; then
_gwt_print "$YELLOW" "⚠️ Directory exists without worktree: $target_dir"
echo "Recent entries:"
_gwt_list_recent "$target_dir" 3
if [[ "$GWT_CONFIRM_DELETE" == "true" ]]; then
read -p "Delete and recreate? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
_gwt_print "$RED" "✗ Aborted"
return 1
fi
fi
rm -rf "$target_dir"
_gwt_print "$GREEN" "✓ Removed old directory"
fi
# Determine if branch exists and create worktree accordingly
if git show-ref --verify --quiet "refs/heads/$branch"; then
_gwt_print "$GREEN" "→ Using existing local branch: $branch"
if git worktree add "$target_dir" "$branch" 2>/dev/null; then
cd "$target_dir" || return 1
_gwt_print "$GREEN" "✓ Switched to $target_dir"
# If submodules exist, initialize them
if [[ -f .gitmodules ]]; then
_gwt_print "$YELLOW" "→ Initializing submodules..."
git submodule update --init --recursive
fi
return
fi
elif git show-ref --verify --quiet "refs/remotes/origin/$branch"; then
_gwt_print "$GREEN" "→ Checking out remote branch: origin/$branch"
git fetch origin "$branch" 2>/dev/null
if git worktree add -b "$branch" "$target_dir" "origin/$branch" 2>/dev/null; then
cd "$target_dir" || return 1
_gwt_print "$GREEN" "✓ Switched to $target_dir"
if [[ -f .gitmodules ]]; then
_gwt_print "$YELLOW" "→ Initializing submodules..."
git submodule update --init --recursive
fi
return
fi
else
_gwt_print "$GREEN" "→ Creating new branch: $branch"
if git worktree add -b "$branch" "$target_dir" 2>/dev/null; then
cd "$target_dir" || return 1
_gwt_print "$GREEN" "✓ Switched to $target_dir"
if [[ -f .gitmodules ]]; then
_gwt_print "$YELLOW" "→ Initializing submodules..."
git submodule update --init --recursive
fi
return
fi
fi
# If we reached here, primary path failed; try alternative approach
_gwt_print "$RED" "✗ Failed to create worktree"
_gwt_print "$YELLOW" "Trying alternative approach..."
if git worktree add "$target_dir" "$branch" 2>/dev/null; then
cd "$target_dir" || return 1
_gwt_print "$GREEN" "✓ Switched to $target_dir"
else
_gwt_print "$RED" "✗ Could not create worktree for branch: $branch"
return 1
fi
}
# Cleanup function for orphaned directories and broken worktrees
gwtclean() {
# Handle --help flag
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
gwthelp gwtclean
return
fi
_gwt_print "$GREEN" "=== Git Worktree Cleanup ==="
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
_gwt_print "$RED" "✗ Not in a git repository"
return 1
fi
# Prune broken worktree references
_gwt_print "$YELLOW" "→ Pruning broken worktree references..."
git worktree prune -v
# Get base directory name from repository root
local base_dir repo_root
repo_root=$(git rev-parse --show-toplevel)
base_dir=$(basename "$repo_root")
# Find orphaned directories
_gwt_print "$YELLOW" "→ Searching for orphaned directories..."
local orphans=()
local patterns
# Convert space-separated patterns to array
read -ra patterns <<< "$GWT_CLEANUP_PATTERNS"
# Search for directories matching patterns
for pattern in "${patterns[@]}"; do
# Replace * with base directory in pattern if needed
local search_pattern="${pattern/\*/${base_dir}}"
# Use find to locate directories, handling spaces properly
while IFS= read -r -d '' dir; do
if [[ -d "$dir" ]]; then
local abs_dir
abs_dir=$(cd "$dir" 2>/dev/null && pwd)
if [[ -n "$abs_dir" ]] && ! _gwt_get_worktree_paths | grep -Fxq "$abs_dir"; then
orphans+=("$dir")
fi
fi
done < <(find .. -maxdepth 1 -type d -name "$search_pattern" -print0 2>/dev/null)
done
# Remove duplicates from orphans array
local unique_orphans=()
local seen=()
for orphan in "${orphans[@]}"; do
local skip=false
for s in "${seen[@]}"; do
if [[ "$s" == "$orphan" ]]; then
skip=true
break
fi
done
if [[ "$skip" == false ]]; then
seen+=("$orphan")
unique_orphans+=("$orphan")
fi
done
if [[ ${#unique_orphans[@]} -gt 0 ]]; then
_gwt_print "$YELLOW" ""
_gwt_print "$YELLOW" "Found ${#unique_orphans[@]} orphaned directories:"
# Show disk usage for each orphan
for orphan in "${unique_orphans[@]}"; do
local size
size=$(du -sh "$orphan" 2>/dev/null | cut -f1)
echo " $orphan (${size:-unknown size})"
done
if [[ "$GWT_CONFIRM_DELETE" == "true" ]]; then
echo
read -p "Delete all orphaned directories? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
for orphan in "${unique_orphans[@]}"; do
rm -rf "$orphan"
_gwt_print "$GREEN" " ✓ Removed $orphan"
done
_gwt_print "$GREEN" "✓ Cleaned up ${#unique_orphans[@]} directories"
else
_gwt_print "$YELLOW" "→ Skipped cleanup"
fi
else
# Auto-delete if confirmation disabled
for orphan in "${unique_orphans[@]}"; do
rm -rf "$orphan"
done
_gwt_print "$GREEN" "✓ Cleaned up ${#unique_orphans[@]} directories"
fi
else
_gwt_print "$GREEN" "✓ No orphaned directories found"
fi
# Show current active worktrees
echo
_gwt_print "$GREEN" "Active worktrees:"
git worktree list
}
# List worktrees with additional information
gwtlist() {
# Handle --help flag
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
gwthelp gwtlist
return
fi
if ! git rev-parse --git-dir > /dev/null 2>&1; then
_gwt_print "$RED" "✗ Not in a git repository"
return 1
fi
_gwt_print "$GREEN" "=== Git Worktrees ==="
# Parse porcelain output to handle spaces/newlines robustly
local path="" branch="" locked="" prunable=""
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ -z "$line" ]]; then
if [[ -n "$path" ]]; then
if [[ -d "$path" ]]; then
local last_commit modified
last_commit=$(cd "$path" && git log -1 --format="%h %s" 2>/dev/null || echo "no commits")
modified=$(cd "$path" && git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
echo "$path"
if [[ -n "$branch" ]]; then
echo " Branch: $branch"
else
echo " Branch: (detached)"
fi
echo " Last commit: $last_commit"
if [[ "$modified" -gt 0 ]]; then
_gwt_print "$YELLOW" " Modified files: $modified"
fi
if [[ -n "$locked" ]]; then
_gwt_print "$YELLOW" " Locked"
fi
if [[ -n "$prunable" ]]; then
_gwt_print "$YELLOW" " Prunable"
fi
else
echo "$path"
_gwt_print "$RED" " [MISSING DIRECTORY]"
fi
echo
fi
path=""; branch=""; locked=""; prunable=""
continue
fi
case "$line" in
worktree\ *)
path=${line#worktree }
;;
branch\ *)
branch=${line#branch }
if [[ "$branch" == refs/heads/* ]]; then
branch=${branch#refs/heads/}
elif [[ "$branch" == refs/remotes/* ]]; then
branch=${branch##*/}
fi
;;
locked*)
locked=1
;;
prunable*)
prunable=1
;;
esac
done < <(git worktree list --porcelain)
# Flush last block if not already
if [[ -n "$path" ]]; then
if [[ -d "$path" ]]; then
local last_commit modified
last_commit=$(cd "$path" && git log -1 --format="%h %s" 2>/dev/null || echo "no commits")
modified=$(cd "$path" && git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
echo "$path"
if [[ -n "$branch" ]]; then
echo " Branch: $branch"
else
echo " Branch: (detached)"
fi
echo " Last commit: $last_commit"
if [[ "$modified" -gt 0 ]]; then
_gwt_print "$YELLOW" " Modified files: $modified"
fi
if [[ -n "$locked" ]]; then
_gwt_print "$YELLOW" " Locked"
fi
if [[ -n "$prunable" ]]; then
_gwt_print "$YELLOW" " Prunable"
fi
else
echo "$path"
_gwt_print "$RED" " [MISSING DIRECTORY]"
fi
echo
fi
}
# Quick switch between worktrees
gwts() {
# Handle --help flag
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
gwthelp gwts
return
fi
if ! git rev-parse --git-dir > /dev/null 2>&1; then
_gwt_print "$RED" "✗ Not in a git repository"
return 1
fi
# Get list of worktrees
local worktrees=()
while IFS= read -r line; do
if [[ -n "$line" ]]; then
worktrees+=("$line")
fi
done < <(_gwt_get_worktree_paths)
if [[ ${#worktrees[@]} -eq 0 ]]; then
_gwt_print "$YELLOW" "No worktrees found"
return 1
fi
# Display menu
_gwt_print "$GREEN" "Select worktree to switch to:"
local i=1
for wt in "${worktrees[@]}"; do
local current=""
if [[ "$wt" == "$(pwd)" ]]; then
current=" [CURRENT]"
_gwt_print "$GREEN" " $i) $wt$current"
else
echo " $i) $wt$current"
fi
((i++))
done
# Read selection
echo
read -p "Enter number (1-${#worktrees[@]}): " -r selection
echo
# Validate selection
if [[ "$selection" =~ ^[0-9]+$ ]] && [[ "$selection" -ge 1 ]] && [[ "$selection" -le ${#worktrees[@]} ]]; then
local target="${worktrees[$((selection-1))]}"
cd "$target" || return 1
_gwt_print "$GREEN" "✓ Switched to $target"
else
_gwt_print "$RED" "✗ Invalid selection"
return 1
fi
}
# Load help functions from separate file
_GWT_SCRIPT_DIR="${BASH_SOURCE[0]%/*}"
if [[ -f "${_GWT_SCRIPT_DIR}/git-worktree-utils-help.sh" ]]; then
# shellcheck source=git-worktree-utils-help.sh
source "${_GWT_SCRIPT_DIR}/git-worktree-utils-help.sh"
fi