-
Notifications
You must be signed in to change notification settings - Fork 31
/
fzgit
executable file
·157 lines (138 loc) · 3.68 KB
/
fzgit
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
#!/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
}
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
}
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
}
fzf() {
local prompt
if [[ $1 == --prompt=* ]]; then
prompt="${1##*=}>"
shift
fi
branch=$(git rev-parse --abbrev-ref HEAD)
opts=( --prompt="fzgit${branch}>${prompt} " )
[[ -v FZGIT_OPTIONS ]] && opts=( $FZGIT_OPTIONS )
command fzf "${opts[@]}" \
--inline-info \
--ansi \
--no-clear \
"$@"
}
helptext[diff]='Show changes between commits, commit and working tree, etc'
subcmd_diff() {
if (( $# > 0 )); then git diff "$@"; return; fi
fzf \
--bind='start:reload:git -c color.status=always status -su' \
--nth=2.. \
--preview='git diff --color=always {2..} | delta -w $FZF_PREVIEW_COLUMNS' \
--bind='enter:execute:git diff --color=always {2..}' \
--preview-window=70%
}
helptext[add]='Add file contents to the index'
subcmd_add() {
if (( $# > 0 )); then git add "$@"; return; fi
local diff
diff=$(git diff --color=always | perl -pwe 's/^(\e\[[0-9;]*m)*(?=diff)/\0/m' | fzf --read0)
[[ -z $diff ]] && return
git apply --check --cached <<< "$diff" &&
git apply --cached <<< "$diff" &&
git apply --stat <<< "$diff"
subcmd_add
}
helptext[status]='Show the working tree status'
subcmd_status() {
local preview='git diff --color=always {2..}'
fzf \
--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 list response key branch header
if (( $# > 0 )); then git checkout "$@"; return; fi
{
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}'
}
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 "$c\t${help}\t${helptext[$c]}\n"
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