-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-chord.zsh
More file actions
332 lines (278 loc) · 8.79 KB
/
git-chord.zsh
File metadata and controls
332 lines (278 loc) · 8.79 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
#!/usr/bin/env zsh
# git-chord - Vim-style composable git commands
# https://github.com/socket-link/git-chord
#
# SPDX-License-Identifier: Apache-2.0
# =============================================================================
# Command Registry
# =============================================================================
# Format: CMD_CHAR -> "needs_arg:command_template"
# needs_arg: 0 = no arg, 1 = required arg, 2 = optional arg (has default)
# command_template: use {} for arg substitution
typeset -A GIT_CHORD_CMDS
GIT_CHORD_CMDS=(
# Basic operations
[a]="0:git add ."
[s]="0:git stash"
[f]="0:git fetch"
[F]="0:git pull"
[l]="0:git log --oneline -20"
[L]="0:git log"
[u]="0:git reset HEAD^ --soft"
[p]="0:git push"
[P]="0:git push --force"
# Branch operations
[x]="2:git checkout {}::main" # x = "check" (default: main)
[n]="1:git checkout -b {}" # n = "new branch"
[d]="1:git branch -D {}" # d = "delete branch"
[b]="2:git branch {}::" # b = "branch" (default: list)
[m]="1:git merge {}" # m = "merge"
# Commit operations
[c]="1:git commit -m \"{}\"" # c = "commit"
[e]="0:git commit --amend" # e = "edit last commit"
[E]="0:git commit --amend --no-edit" # E = "amend without edit"
# Rebase operations
[r]="2:git rebase {}::main" # r = "rebase" (default: main)
# Stash operations (s = "stash")
[S]="0:git stash pop" # S = "stash pop" (uppercase)
)
# Multi-character commands (parsed before single-char)
typeset -A GIT_CHORD_MULTI
GIT_CHORD_MULTI=(
[pf]="git push --force"
[pu]="git push -u origin HEAD"
[ra]="git rebase --abort"
[rc]="git rebase --continue"
[rs]="git rebase --skip"
[sa]="git stash apply"
[sl]="git stash list"
[sp]="git stash pop"
[sd]="git stash drop"
)
# Macro commands - use {branch} for the branch captured at chord start
# Format: "commands to run" where {branch} gets substituted
typeset -A GIT_CHORD_MACROS
GIT_CHORD_MACROS=(
# R = "sync Rebase" - go to main, pull, come back, rebase
[R]="x:F:x {branch}:r"
# M = "sync Merge" - go to main, pull, come back, merge main
[M]="x:F:x {branch}:m main"
# W = "Wrap up" - push and come back to main
[W]="p:x"
)
# =============================================================================
# Branch Utilities
# =============================================================================
_git_current_branch() {
git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null
}
_git_default_branch() {
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main"
}
# =============================================================================
# Chord Parser
# =============================================================================
_git_chord_parse() {
local chord="$1"
shift
local -a positional_args=("$@")
local -a commands=()
local -a args=()
local i=0
local len=${#chord}
local pos_idx=0
# Capture current branch at start for macro commands
local start_branch="$(_git_current_branch)"
while (( i < len )); do
local char="${chord:$i:1}"
local next_char="${chord:$((i+1)):1}"
local two_char="${char}${next_char}"
# Check for macro commands first
if [[ -n "${GIT_CHORD_MACROS[$char]}" ]]; then
local macro="${GIT_CHORD_MACROS[$char]}"
macro="${macro//\{branch\}/$start_branch}"
echo "⚡ Macro $char → expanding for branch '$start_branch'" >&2
# Split on ":" without losing spaces inside parts
local -a macro_parts=("${(s/:/)macro}")
for part in "${macro_parts[@]}"; do
local macro_cmd="${part%% *}"
local macro_arg="${part#* }"
[[ "$macro_cmd" == "$macro_arg" ]] && macro_arg=""
commands+=("$macro_cmd")
args+=("$macro_arg")
done
(( i++ ))
continue
fi
# Check for multi-character commands
if [[ -n "${GIT_CHORD_MULTI[$two_char]}" ]]; then
commands+=("$two_char")
args+=("")
(( i += 2 ))
continue
fi
# Check for single-character command
if [[ -n "${GIT_CHORD_CMDS[$char]}" ]]; then
commands+=("$char")
(( i++ ))
# Check for quoted argument
if [[ "${chord:$i:1}" == '"' ]]; then
(( i++ ))
local arg=""
while (( i < len )) && [[ "${chord:$i:1}" != '"' ]]; do
arg+="${chord:$i:1}"
(( i++ ))
done
(( i++ ))
args+=("$arg")
else
local spec="${GIT_CHORD_CMDS[$char]}"
local needs_arg="${spec%%:*}"
if (( needs_arg > 0 )) && (( pos_idx < ${#positional_args[@]} )); then
args+=("${positional_args[$((pos_idx+1))]}")
(( pos_idx++ ))
else
args+=("")
fi
fi
else
echo "✗ Unknown command: $char" >&2
return 1
fi
done
# Execute commands in sequence
local idx=1
for cmd in "${commands[@]}"; do
local arg="${args[$idx]}"
if ! _git_chord_exec "$cmd" "$arg"; then
echo "✗ Command failed: $cmd" >&2
return 1
fi
(( idx++ ))
done
}
_git_chord_exec() {
local cmd="$1"
local arg="$2"
local template=""
# Check multi-char commands first
if [[ -n "${GIT_CHORD_MULTI[$cmd]}" ]]; then
template="${GIT_CHORD_MULTI[$cmd]}"
echo "→ $template" >&2
eval "$template"
return $?
fi
# Single-char command
local spec="${GIT_CHORD_CMDS[$cmd]}"
local needs_arg="${spec%%:*}"
template="${spec#*:}"
# Handle optional args with defaults (format: "template::default")
local default=""
if [[ "$template" == *"::"* ]]; then
default="${template##*::}"
template="${template%%::*}"
fi
# Apply argument or default
if [[ -n "$arg" ]]; then
template="${template//\{\}/$arg}"
elif [[ -n "$default" ]]; then
template="${template//\{\}/$default}"
elif (( needs_arg == 1 )); then
echo "✗ Command '$cmd' requires an argument" >&2
return 1
else
template="${template//\{\}/}"
fi
template="${template%% }"
echo "→ $template" >&2
eval "$template"
}
# =============================================================================
# Main Entry Point
# =============================================================================
g() {
if [[ $# -eq 0 ]]; then
git status
return
fi
local chord="$1"
shift
_git_chord_parse "$chord" "$@"
}
# =============================================================================
# Convenience Aliases (backward compatibility)
# =============================================================================
alias ga='g a'
alias gc='g c'
alias gx='g x'
alias gn='g n'
alias gu='g u'
alias gf='g f'
alias gF='g F'
alias gl='g l'
alias gs='g s'
alias gS='g S'
alias gr='g r'
alias gra='g ra'
alias grc='g rc'
alias grs='g rs'
alias gp='g p'
alias gpf='g pf'
alias gpu='g pu'
alias gd='g d'
alias gb='g b'
alias gsa='g sa'
alias gsl='g sl'
alias gsp='g sp'
alias gsd='g sd'
alias gm='g m'
alias ge='g e'
alias gE='g E'
# =============================================================================
# Help
# =============================================================================
ghelp() {
cat << 'EOF'
git-chord - Vim-style composable git commands
USAGE
g <chord> [args...]
SYNTAX
g acp "Fix bug" Positional args (no spaces in chord)
g x"branch"ac"message"p Inline quoted args
COMMANDS
a add . s status
c commit -m "<msg>" p push
P push --force f fetch
F pull l log --oneline
L log (full) u uncommit (reset --soft)
x checkout [branch] (default: main)
n checkout -b <branch> (new)
d branch -D <branch> (delete)
b branch [arg] (no arg = list)
m merge <branch>
e commit --amend (edit)
E commit --amend --no-edit
r rebase [branch] (default: main)
s stash
S stash pop
MULTI-CHAR
pf push --force pu push -u origin HEAD
ra rebase --abort rc rebase --continue
rs rebase --skip
sa stash apply sl stash list
sp stash pop sd stash drop
MACROS (capture current branch)
R Sync-Rebase: x → F → x {branch} → r
M Sync-Merge: x → F → x {branch} → m main
W Wrap-up: p → x
EXAMPLES
g git status
g acp "Fix bug" add, commit, push
g x checkout main
g xF checkout main, pull
g n"feature"ac"WIP" new branch, add, commit
g sxFS stash, checkout main, pull, pop
g ac"Done"R commit, sync-rebase from main
g aE add, amend (no edit)
EOF
}