-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree-me
More file actions
executable file
·264 lines (229 loc) · 7.99 KB
/
tree-me
File metadata and controls
executable file
·264 lines (229 loc) · 7.99 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
#!/bin/bash
# Minimal git worktree helper - leverages git's native capabilities
# Usage: tree-me <command> [options]
#
# For auto-cd and tab completion, add this to your ~/.bashrc or ~/.zshrc:
# source <(tree-me shellenv)
#
# This enables:
# - Automatic cd to worktree after checkout/create/pr commands
# - Tab completion for commands and branch names
# copied from https://github.com/haacked/dotfiles/blob/main/bin/tree-me
set -e
# Worktree organization: ~/dev/worktrees/<repo>/<branch>
WORKTREE_ROOT="${WORKTREE_ROOT:-$HOME/dev/worktrees}"
# Show help if no arguments
show_help() {
cat << 'EOF'
Usage: tree-me <command> [options]
Git-like worktree management with organized directory structure.
Commands:
checkout, co <branch> Checkout existing branch in new worktree
create <branch> [base] Create new branch in worktree (default: main/master)
pr <number|url> Checkout GitHub PR in worktree (uses gh)
list, ls List all worktrees
remove, rm <branch> Remove a worktree
prune Remove worktree administrative files
shellenv Output shell function for auto-cd (source this)
Examples:
tree-me checkout feature-branch
tree-me create my-feature
tree-me create my-feature develop
tree-me pr 123
tree-me pr https://github.com/org/repo/pull/123
tree-me list
tree-me remove old-branch
tree-me prune
Setup auto-cd:
Add to ~/.bashrc or ~/.zshrc:
source <(tree-me shellenv)
Worktrees are organized at: $WORKTREE_ROOT/<repo>/<branch>
Set WORKTREE_ROOT to customize the location (default: ~/dev/worktrees)
EOF
}
if [ $# -eq 0 ]; then
show_help
exit 0
fi
# Get repo name from git
get_repo_name() {
basename "$(git remote get-url origin 2>/dev/null || git rev-parse --show-toplevel)" .git
}
# Get default base branch
get_default_base() {
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo main
}
# Extract PR number from URL or number
get_pr_number() {
local input="$1"
if [[ "$input" =~ ^https://github.com/.*/pull/([0-9]+) ]]; then
echo "${BASH_REMATCH[1]}"
elif [[ "$input" =~ ^[0-9]+$ ]]; then
echo "$input"
else
echo "Error: Invalid PR number or URL: $input" >&2
exit 1
fi
}
repo=$(get_repo_name)
command="$1"
shift
case "$command" in
shellenv)
cat << 'EOF'
tree-me() {
local output
output=$(command tree-me "$@")
local exit_code=$?
echo "$output"
if [ $exit_code -eq 0 ]; then
local cd_path=$(echo "$output" | grep "^TREE_ME_CD:" | cut -d: -f2-)
[ -n "$cd_path" ] && cd "$cd_path"
fi
return $exit_code
}
# Bash completion
if [ -n "$BASH_VERSION" ]; then
_tree_me_complete() {
local cur prev commands
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="checkout co create pr list ls remove rm prune help shellenv"
# Complete commands if first argument
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
return 0
fi
# Complete branch names for checkout/remove/rm
case "$prev" in
checkout|co|remove|rm)
local branches
branches=$(git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | tail -n +2)
COMPREPLY=( $(compgen -W "$branches" -- "$cur") )
return 0
;;
esac
}
complete -F _tree_me_complete tree-me
fi
# Zsh completion
if [ -n "$ZSH_VERSION" ]; then
_tree_me_complete_zsh() {
local -a commands branches
commands=(
'checkout:Checkout existing branch in new worktree'
'co:Checkout existing branch in new worktree'
'create:Create new branch in worktree'
'pr:Checkout GitHub PR in worktree'
'list:List all worktrees'
'ls:List all worktrees'
'remove:Remove a worktree'
'rm:Remove a worktree'
'prune:Remove worktree administrative files'
'help:Show help'
'shellenv:Output shell function for auto-cd'
)
if (( CURRENT == 2 )); then
_describe 'command' commands
elif (( CURRENT == 3 )); then
case "$words[2]" in
checkout|co|remove|rm)
branches=(${(f)"$(git worktree list 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p' | tail -n +1)"})
_describe 'branch' branches
;;
esac
fi
}
compdef _tree_me_complete_zsh tree-me
fi
EOF
;;
checkout|co)
branch="${1:?Branch name required. Usage: tree-me checkout <branch>}"
path="$WORKTREE_ROOT/$repo/$branch"
# Check if worktree already exists
if git worktree list | grep -q "\[$branch\]"; then
existing=$(git worktree list | grep "\[$branch\]" | awk '{print $1}')
echo "✓ Worktree already exists: $existing"
echo "TREE_ME_CD:$existing"
exit 0
fi
# Check if branch exists
if git show-ref --verify --quiet "refs/heads/$branch" || \
git show-ref --verify --quiet "refs/remotes/origin/$branch"; then
git worktree add "$path" "$branch"
echo "✓ Worktree created at: $path"
echo "TREE_ME_CD:$path"
else
echo "Error: Branch '$branch' does not exist" >&2
echo "Use 'tree-me create $branch' to create a new branch" >&2
exit 1
fi
;;
create)
branch="${1:?Branch name required. Usage: tree-me create <branch> [base-branch]}"
base="${2:-$(get_default_base)}"
path="$WORKTREE_ROOT/$repo/$branch"
# Check if worktree already exists
if git worktree list | grep -q "\[$branch\]"; then
existing=$(git worktree list | grep "\[$branch\]" | awk '{print $1}')
echo "✓ Worktree already exists: $existing"
echo "TREE_ME_CD:$existing"
exit 0
fi
# Create new branch
git worktree add "$path" -b "$branch" "$base"
echo "✓ Worktree created at: $path"
echo "TREE_ME_CD:$path"
;;
pr)
input="${1:?PR number or URL required. Usage: tree-me pr <number|url>}"
pr_number=$(get_pr_number "$input")
# Check if gh is installed
if ! command -v gh >/dev/null 2>&1; then
echo "Error: 'gh' CLI not found. Install it from https://cli.github.com" >&2
exit 1
fi
# Use gh to checkout PR in a worktree
branch="pr-$pr_number"
path="$WORKTREE_ROOT/$repo/$branch"
# Check if worktree already exists
if git worktree list | grep -q "\[$branch\]"; then
existing=$(git worktree list | grep "\[$branch\]" | awk '{print $1}')
echo "✓ Worktree already exists: $existing"
echo "TREE_ME_CD:$existing"
exit 0
fi
# Fetch the PR and create worktree
git fetch origin "pull/$pr_number/head:$branch" 2>/dev/null || true
git worktree add "$path" "$branch"
echo "✓ PR #$pr_number checked out at: $path"
echo "TREE_ME_CD:$path"
;;
list|ls)
git worktree list
;;
remove|rm)
branch="${1:?Branch name required. Usage: tree-me remove <branch>}"
existing=$(git worktree list | grep "\[$branch\]" | awk '{print $1}')
if [ -z "$existing" ]; then
echo "Error: No worktree found for branch: $branch" >&2
exit 1
fi
git worktree remove "$existing"
echo "✓ Removed worktree: $existing"
;;
prune)
git worktree prune
echo "✓ Pruned stale worktree administrative files"
;;
help|--help|-h)
show_help
;;
*)
echo "Error: Unknown command '$command'" >&2
echo "Run 'tree-me help' for usage information" >&2
exit 1
;;
esac