-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash_aliases
More file actions
347 lines (297 loc) · 11.1 KB
/
Copy pathbash_aliases
File metadata and controls
347 lines (297 loc) · 11.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
#!/usr/bin/env bash
# =======================================================================
# Git worktrees enable multiple working directories for a single
# repository, allowing parallel work on different branches.
#
# This powerful feature lets you:
# - Work on multiple features simultaneously without branch switching
# - Enable multiple Agentic AI sessions without interference
# - Review pull requests while keeping your main work untouched
# - Test different versions of code in separate directories
# - Maintain isolated development environments per feature
#
# Copyright (c) 2025 Schuberg Philis B.V.
# SPDX-License-Identifier: Apache-2.0
# =======================================================================
# =======================================================================
# General Git Aliases
# =======================================================================
# push back active branch
alias pb='git push origin `git rev-parse --abbrev-ref HEAD` -f'
# pull down active branch
alias pd='git pull origin `git rev-parse --abbrev-ref HEAD` -f'
# activate local virtualenv
alias venv='source .*/bin/activate'
# =======================================================================
# Git Worktree Aliases
# also see aliases
# venv=source .*/bin/activate
# make env # a standard job to run virtualenv .package
# =======================================================================
# Helper function to get main worktree path
_get_main_worktree() {
git worktree list | head -1 | awk '{print $1}'
}
# Helper function to get worktree root (handles being in subdirectories)
_get_worktree_root() {
git rev-parse --show-toplevel 2>/dev/null
}
# gw: Git Worktree - Create new worktree
# Usage: gw feat/authentication
# gw bugfix/issue-123
alias gw='_git_worktree_create'
_git_worktree_create() {
# WORKTREE_BASE: Base directory for all worktrees (relative to repo root)
BASE=$(basename "$(_get_main_worktree)")
DIRNAME=$(dirname "$(_get_main_worktree)")
WORKTREE_BASE="$DIRNAME/$BASE-worktree"
if [ -z "$1" ]; then
echo "Usage: gw <branch-name>"
echo "Example: gw feat/authentication"
echo " gw spike/authentication"
echo " gw fix/authentication"
return 1
fi
local branch_name="$1"
local worktree_name=$(echo "$branch_name" | sed 's/\//-/g') # Replace / with -
local worktree_path="${WORKTREE_BASE}/${worktree_name}"
echo "Creating worktree: $worktree_path"
echo "Branch: $branch_name"
# Create worktree
if git worktree add "$worktree_path" -b "$branch_name"; then
echo "✅ Worktree created successfully!"
echo "To move there: wm $worktree_name"
echo "To setup: cd $worktree_path && make env && venv" # virtualenv .env && source .venv/bin/activate
else
echo "❌ Failed to create worktree"
return 1
fi
}
# gwc: Git Worktree Create and setup - Create worktree + auto-setup environment
# Usage: gwc feat/authentication
alias gwc='_git_worktree_create_and_setup'
_git_worktree_create_and_setup() {
# WORKTREE_BASE: Base directory for all worktrees (relative to repo root)
BASE=$(basename "$(_get_main_worktree)")
DIRNAME=$(dirname "$(_get_main_worktree)")
WORKTREE_BASE="$DIRNAME/$BASE-worktree"
if [ -z "$1" ]; then
echo "Usage: gwc <branch-name>"
echo "Example: gwc feat/authentication"
echo "This creates the worktree AND runs 'make env && venv'" # virtualenv .env && source .venv/bin/activate
return 1
fi
local branch_name="$1"
local worktree_name=$(echo "$branch_name" | sed 's/\//-/g')
local worktree_path="${WORKTREE_BASE}/${worktree_name}"
echo "🌳 Creating worktree: $worktree_path"
echo "📋 Branch: $branch_name"
# Create worktree
if git worktree add "$worktree_path" -b "$branch_name"; then
echo "✅ Worktree created!"
# Move to worktree
cd "$worktree_path" || return 1
echo "📂 Changed to: $(pwd)"
# Setup environment
echo "🔧 Running 'make env'..."
if make env; then
echo "✅ Environment setup complete!"
echo ""
echo "🎯 Ready to work! Run 'venv' to activate environment"
echo ""
# Try to activate (may not work in some shells, but worth trying)
source .*/bin/activate 2>/dev/null && echo "✅ Virtual environment activated!" || echo "Run: venv"
else
echo "❌ 'make env' failed - please check errors above"
return 1
fi
else
echo "❌ Failed to create worktree"
return 1
fi
}
# wm: Worktree Move - Move to a worktree
# Usage: wm feat-authentication
# wm bugfix-issue-123
alias wm='_worktree_move'
_worktree_move() {
# WORKTREE_BASE: Base directory for all worktrees (relative to repo root)
BASE=$(basename "$(_get_main_worktree)")
DIRNAME=$(dirname "$(_get_main_worktree)")
WORKTREE_BASE="$DIRNAME/$BASE-worktree"
if [ -z "$1" ]; then
echo "Usage: wm <worktree-name>"
_list_worktrees
return 1
fi
local worktree_name=$(echo "$1" | sed 's/\//-/g')
local worktree_path="${WORKTREE_BASE}/${worktree_name}"
if [ -d "$worktree_path" ]; then
cd "$worktree_path" || return 1
echo "📂 Moved to: $(pwd)"
echo "🌳 Branch: $(git branch --show-current)"
# Check if .virtual_env exists
local venv_dir=$(ls -d .[^.]*/bin/python 2>/dev/null | head -n1 | cut -d'/' -f1)
if [ -d "$venv_dir" ]; then
echo "✅ Virtual environment exists ($venv_dir/)"
else
echo "⚠️ No virtual environment found - create local env"
fi
else
echo "❌ Worktree not found: $worktree_path"
_list_worktrees
return 1
fi
}
# lw: List Worktrees - Show all worktrees with status
alias lw='_list_worktrees'
_list_worktrees() {
echo "Git Worktrees:"
echo "=============="
git worktree list | while IFS= read -r line; do
local worktree_path=$(echo "$line" | awk '{print $1}')
local branch=$(echo "$line" | awk '{print $3}' | tr -d '[]')
# Check if this is the current worktree
local current_path=$(git rev-parse --show-toplevel 2>/dev/null)
local marker=""
if [ "$worktree_path" = "$current_path" ]; then
marker="👉 "
fi
venv_dir=""
for potential_venv in "$worktree_path"/.*; do
if [ -d "$potential_venv" ] && [ "$(basename "$potential_venv")" != "." ] && [ "$(basename "$potential_venv")" != ".." ]; then
if [ -f "$potential_venv/bin/python" ]; then
venv_dir=$(basename "$potential_venv")
break
fi
fi
done
local env_status=""
if [ -n "$venv_dir" ]; then
env_status="[✅ $venv_dir]"
else
env_status="[❌ no env]"
fi
echo "${marker}${worktree_path}"
echo " Branch: ${branch} ${env_status}"
echo ""
done
echo ""
echo "Use: wm <name> to move to worktree"
echo "Use: bw to return to main"
}
# bw: Back to main Worktree
alias bw='_back_to_main'
_back_to_main() {
local main_path=$(_get_main_worktree)
if [ -z "$main_path" ]; then
echo "❌ Could not find main worktree"
return 1
fi
cd "$main_path" || return 1
echo "📂 Back to main: $(pwd)"
echo "🌳 Branch: $(git branch --show-current)"
# Check if .virtual_env exists
local venv_dir=$(ls -d .[^.]*/bin/python 2>/dev/null | head -n1 | cut -d'/' -f1)
if [ -n "$venv_dir" ]; then
echo "✅ Virtual environment exists ($venv_dir/)"
else
echo "⚠️ No virtual environment found - run 'make env'" # virtualenv .env
fi
}
# rw: Remove Worktree
# Usage: rw feat-authentication
alias rw='_remove_worktree'
_remove_worktree() {
# WORKTREE_BASE: Base directory for all worktrees (relative to repo root)
BASE=$(basename "$(_get_main_worktree)")
WORKTREE_BASE="../$BASE-worktree"
if [ -z "$1" ]; then
echo "Usage: rw <worktree-name>"
echo "Available worktrees:"
lw
return 1
fi
local worktree_name=$(echo "$1" | sed 's/\//-/g') # Replace / with -
local worktree_path="${WORKTREE_BASE}/${worktree_name}"
# Verify worktree exists
if ! git worktree list | grep -q "$worktree_path"; then
echo "❌ Worktree not found: $worktree_path"
echo "Available worktrees:"
lw
return 1
fi
# Confirm removal
echo "⚠️ About to remove worktree:"
echo " Path: $worktree_path"
echo " Branch: $(git worktree list | grep "$worktree_path" | awk '{print $3}' | tr -d '[]')"
echo ""
read -p "Continue? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if git worktree remove "$worktree_path"; then
git branch -D $1
echo "✅ Worktree removed: $worktree_path"
else
echo "❌ Failed to remove worktree"
echo "Try: git worktree remove --force $worktree_path"
return 1
fi
else
echo "❌ Cancelled"
fi
}
# pw: Print current Worktree path
alias pw='_print_worktree'
_print_worktree() {
local worktree_path=$(_get_worktree_root)
if [ -z "$worktree_path" ]; then
echo "❌ Not in a git repository"
return 1
fi
local branch=$(git branch --show-current)
local is_main=$(git worktree list | head -1 | grep -q "$worktree_path" && echo "yes" || echo "no")
echo "Current Worktree:"
echo "================="
echo "Path: $worktree_path"
echo "Branch: $branch"
echo "Main: $is_main"
# Check if .virtual_env exists in this worktree
local venv_dir=$(ls -d $worktree_path/.[^.]*/bin/python 2>/dev/null | head -n1 | cut -d'/' -f2)
if [ -n $venv_dir ]; then
echo "Env: ✅ $venv_dir exists"
if [ -n "$VIRTUAL_ENV" ]; then
echo "Active: ✅ $(basename $VIRTUAL_ENV)"
else
echo "Active: ❌ (run: venv)" # alias venv=source .*/bin/activate
fi
else
echo "Env: ❌ No .venv (run: virtualenv .PACKAGE)"
fi
}
# cw: Cleanup removed Worktrees
alias cw='_cleanup_worktrees'
_cleanup_worktrees() {
echo "Pruning removed worktrees..."
git worktree prune -v
echo "✅ Cleanup complete"
}
# Combined alias for quick workflow
alias gww='_git_worktree_workflow'
_git_worktree_workflow() {
echo "Git Worktree Quick Workflow:"
echo "============================"
echo "1. gw <branch> - Create worktree"
echo "2. gwc <branch> - Create + setup (make dev + venv)"
echo "3. wm <name> - Move to worktree"
echo "4. lw - List all worktrees"
echo "5. bw - Back to main"
echo "6. rw <name> - Remove worktree"
echo "7. pw - Print current worktree info"
echo "8. cw - Cleanup pruned worktrees"
echo ""
lw
}
# =======================================================================
# End Git Worktree Aliases
# =======================================================================