-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathfzgit
More file actions
executable file
·360 lines (318 loc) · 8.43 KB
/
fzgit
File metadata and controls
executable file
·360 lines (318 loc) · 8.43 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
#!/usr/bin/env bash
declare -A aliases
declare -A helptext
declare -r c_reset=$(tput sgr0)
declare -r c_red=$(tput setaf 1)
declare -r c_green=$(tput setaf 2)
declare -r c_yellow=$(tput setaf 3)
declare -r c_grey=$(tput setaf 8)
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
if (($# > 0)); then
err "$@"
fi
exit 1
}
export COLUMNS
has() {
local v c
if [[ $1 = '-v' ]]; then
v=1
shift
fi
for c; do
c="${c%% *}"
if ! command -v "$c" &>/dev/null; then
((v > 0)) && err "$c not found"
return 1
fi
done
}
select_from() {
local cmd='command -v'
for a in "$@"; do
case "$a" in
-c)
cmd="$2"
shift 2
;;
esac
done
for c in "$@"; do
if $cmd "${c%% *}" &>/dev/null; then
echo "$c"
return 0
fi
done
return 1
}
aliases[-h]=help
aliases[--help]=help
helptext[help]='show this help'
subcmd_help() {
local formattedhelptext
formattedhelptext=$(for c in "${subcmds_avail[@]}"; do
printf " %s\n %s\n" "$c" "${helptext[$c]}"
done)
LESS=-FEXR less <<-HELP
$0 <COMMAND>
${formattedhelptext}
HELP
}
basedir=$(basename $(git rev-parse --show-toplevel))
# TODO: customization?
branch_prefix=' '
separator=' ❯ '
diffprg=$(select_from 'delta -w ${FZF_PREVIEW_COLUMNS:-$COLUMNS}' 'bat --color=always -p' diff-so-fancy diff-highlight colordiff cat)
fzf() {
local prompt
if [[ $1 == --prompt=* ]]; then
prompt="${1##*=}${separator}"
shift
fi
branch=$(__current_branch)
opts=(--prompt="${basedir}${branch_prefix}${branch}${separator}${prompt}")
command fzf $FZGIT_FZF_OPTS \
--inline-info \
--ansi \
"$@"
}
__current_branch() {
git name-rev --name-only HEAD
}
helptext[diff]='Show changes between commits, commit and working tree, etc'
subcmd_diff() {
local listcmd="diff_histogram $*"
local diffcmd="git diff $*"
local statcmd="git diff --shortstat $*"
while :; do
watcher
curl -s --unix-socket /tmp/fzgit.sock http -d "reload($listcmd)+change-header($($statcmd))"
done &
listener_pid=$!
SHELL=bash fzf --prompt='diff $*' \
-d│ \
--listen=/tmp/fzgit.sock \
--header="$($statcmd)" \
--bind="start:reload:$listcmd" \
--nth=2.. \
--preview="$diffcmd -- {1} | $diffprg" \
--bind='ctrl-p:toggle-preview' \
--bind="enter:execute:$diffcmd -- {1} | $diffprg | less -R" \
--preview-window=70%,down
}
helptext[rebase]='Reapply commits on top of another base tip'
subcmd_rebase() {
if (($# > 0)); then
git rebase "$@"
return
fi
local commit
commit=$(
git log \
--color=always \
--pretty=format:$'%h\t%C(dim yellow)%ar%C(reset)\t%s' "$@" |
fzf --prompt='rebase' \
--preview='git show --color=always {1}' \
--accept-nth=1
)
[[ -z $commit ]] && return
git rebase -i "$commit"
}
helptext[add]='Add file contents to the index'
subcmd_add() {
if (($# > 0)); then
git add "$@"
return
fi
# TODO: split and make patches smaller
local listcmd="git diff --color=always | perl -pwe 's/^(\e\[[0-9;]*m)*(?=diff)/\0/m'"
while :; do
watcher
curl -s --unix-socket /tmp/fzgit.sock http -d "reload($listcmd)"
done &
listener_pid=$!
local diff
diff=$(SHELL=bash fzf \
--prompt='add' \
--disabled \
--read0 \
--listen=/tmp/fzgit.sock \
--bind="start:reload:$listcmd" \
--header='enter:stage | s:split patch' \
--expect=s \
--preview='git diff --staged | git apply --stat; git diff --staged --color=always')
[[ -z $diff ]] && return
if [[ $diff = s* ]]; then
split_patch <<<"${diff:2}"
else
# elif [[ $diff = a* ]]; then
patch="${diff:2}"
git apply --check --cached <<<"$patch" &&
git apply --cached <<<"$patch"
fi
subcmd_add
}
split_patch() {
local incomingpatch patches patch
read -r -d $'\0' incomingpatch
mapfile -d $'\0' -t patches < <(
perl -pwe 's/^(\e\[[0-9;]*m)*(?=@@)/\0/m' <<<"$incomingpatch"
)
bat --color=always --plain <<<"$incomingpatch" |
perl -pwe 's/(\e\[[0-9;]*m)*(?=@@)/\0/m' |
fzf \
--header-lines=1 \
--disabled \
--multi \
--read0 \
--bind="enter:execute(printf -v diff '%s' '${patches[0]}' {+}; git apply --cached <<< \$diff)+abort" \
--preview="printf -v diff '%s' '${patches[0]}' {+}; git apply --stat <<< \$diff; bat --color=always --plain <<< \"\$diff\""
}
helptext[log]='Show commit history'
subcmd_log() {
git log --color=always --pretty=format:$'%h\t%C(dim yellow)%ar%C(reset)\t%s' "$@" |
fzf --prompt='log' \
--multi \
--track \
--header='enter:show ctrl-d:diff ctrl-p:print' \
--bind="enter:execute:git show {1} | $diffprg | less -R" \
--bind='ctrl-d:execute:fzgit diff {1}' \
--bind='ctrl-p:accept' \
--preview-window=80%,bottom \
--preview="git show {1} | $diffprg" |
awk '{print $1}'
}
helptext[status]='Show the working tree status'
subcmd_status() {
local preview='git diff --color=always {2..}'
fzf --prompt='status' \
--nth=2.. \
--bind='start:reload:git -c color.status=always status -s' \
--preview="$preview" \
--bind='enter:execute:git add -p {2..}'
}
helptext[checkout]='Checkout a branch or paths to the working tree'
subcmd_checkout() { # {{{
local branch header
if (($# > 0)); then
git checkout "$@"
return
fi
branch=$(__current_branch)
{
git branch --all --color -vv
git tag
} |
fzf --prompt='checkout' \
--header="$header" \
--bind=enter:execute:"git checkout \$(perl -pe 's/^\*?\s*(remotes\/[^\/]*\/)?([^ ]+).*/\2/' <<< {})"
}
helptext['stash-ls']='List the stash entries that you currently have'
subcmd_stash-ls() {
if (($# > 0)); then
git stash "$@"
return
fi
local header='use ctrl-d to show a diff or ctrl-b to create a new branch'
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf --prompt='stash' \
--no-sort \
--header="$header" \
--bind='ctrl-d:execute: git diff {1} --color=always | less -R' \
--bind='ctrl-b:execute(git stash branch "stash-{1}" {1})+abort' \
--preview='git diff --color {1}'
}
watcher() {
local files=("$(git rev-parse --show-toplevel)/.git/index")
while IFS= read -r f; do
[[ -e "$f" ]] && files+=("$f")
done < <(git ls-files)
inotifywait -q -e modify,attrib,close_write,move,create,delete "${files[@]}" &>/dev/null
}
export -f watcher
diff_histogram() {
git diff --numstat "$@" | perl -e '
use strict;
use warnings;
my $green = `tput setaf 2`;
my $red = `tput setaf 1`;
my $reset = `tput sgr0`;
chomp($green, $red, $reset);
my (@files, @adds, @dels, @totals);
my $max = 0;
while (<>) {
my ($added, $deleted, $file) = split /\t/, $_, 3;
chomp $file;
# Skip binary files (git numstat outputs - for binary)
next if $added eq "-" || $deleted eq "-";
my $total = $added + $deleted;
$max = $total if $total > $max;
push @files, $file;
push @adds, $added;
push @dels, $deleted;
push @totals, $total;
}
my $maxlen = 0;
for (@files) {
$maxlen = length($_) if length($_) > $maxlen;
}
my $maxdigits = length($max);
for my $i (0 .. $#files) {
printf " %-*s │ ", $maxlen, $files[$i];
printf "%*d ", $maxdigits, $totals[$i];
if ($max > 0) {
my $graphwidth = 50;
my $bars = int($totals[$i] * $graphwidth / $max);
$bars = 1 if $bars == 0 && $totals[$i] > 0;
my $plus = int($adds[$i] * $bars / $totals[$i]);
my $minus = $bars - $plus;
print $green;
print "+" x $plus;
print $red;
print "-" x $minus;
print $reset;
}
print "\n";
}
'
}
export -f diff_histogram
cleanup() {
if [[ -n $listener_pid ]] && kill -0 "$listener_pid" 2>/dev/null; then
kill "$listener_pid" 2>/dev/null
wait "$listener_pid" 2>/dev/null
listener_pid=''
fi
}
trap cleanup EXIT INT TERM
has -v fzf perl git || die
mapfile -t subcmds_avail < <(compgen -A function | awk '/^subcmd_/ { sub(/^subcmd_/, "", $0); print }')
nocmd() {
local cmd
cmd=$(for c in "${subcmds_avail[@]}"; do
printf "%s\t%s\n" "$c" "${helptext[$c]}"
done)
cmd=$(column -t -s $'\t' <<<"$cmd" | fzf | awk '{print $1}')
if [[ -n $cmd ]]; then
subcmd_$cmd
else
exit 1
fi
}
if (($# < 1)); then
nocmd
exit 1
elif has "subcmd_$1"; then
subcmd="subcmd_$1"
shift
"$subcmd" "$@"
elif [[ -v aliases[$1] ]]; then
subcmd=subcmd_${aliases[$1]}
shift
"$subcmd" "$@"
else
exec git "$@"
fi