-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-gl
executable file
·319 lines (276 loc) · 9.97 KB
/
git-gl
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
set -o noclobber
## Print the (first) selected line
SINGLE_SELECTION="echo {}"
## Print all selected lines, one per line
MULTI_SELECTION="printf '%s\n' {+}"
## Filter the output of git-short-log to just the hashes
filter-short-hash() {
sed -En 's#^[/\*| ]*\s*(\<[a-f0-9]{7,}\>).*#\1#p'
}
## Expand shortened hashes into full hashes
expand-full-hash() {
xargs git rev-parse | sed 's/[[:space:]]*$//'
}
strip-trailing-whitespace() {
sed 's/[[:space:]]*$//'
}
git-show-pretty() {
git show --submodule --color=always --pretty=fuller "$@"
}
## A (dumber?) xargs that works with functions
#
# If multiple arguments are passed, the first argument is the name of the function to call, and the
# rest are passed to xargs
#
# TODO: Split out into separate script because this is so useful
xargs-function() {
local function="$1"
shift
export -f "${function?}"
xargs "$@" -I% bash -c "$function %"
}
## Determine if the given string is a valid Git object (revision, reference, etc)
git-object-exists() {
local -r object="$1"
git rev-parse -q --verify "$object" >/dev/null
}
## Get the default branch for the origin remote
get-default-branch() {
# TODO: Support multiple remotes?
local -r remote="${GIT_REMOTE:-origin}"
set +o errexit
local default_branch
default_branch="$(git rev-parse --abbrev-ref "$remote/HEAD" 2>/dev/null)"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
echo "${default_branch#"$remote/"}"
fi
set -o errexit
}
## Get the upstream branch for the given reference, if it exists
get-upstream-branch() {
local -r branch="$1"
git rev-parse --abbrev-ref --symbolic-full-name --quiet --verify "$branch@{upstream}" 2>/dev/null || true
}
## Determines if $query is too distant from $base for some definition of "distant" and "too"
branch-too-distant() {
local -r base="$1"
local -r query="$2"
if [[ -z "$base" ]] || [[ -z "$query" ]]; then
return 0
fi
local ratio
ratio="$(git rev-list --left-right --count "$base...$query")"
local behind
behind="$(echo "$ratio" | awk '{ print $1 }')"
local ahead
ahead="$(echo "$ratio" | awk '{ print $2 }')"
local -r lines="${FZF_PREVIEW_LINES:-${FZF_LINES:-${LINES:-$(tput lines)}}}"
local -r limit="$((lines / 2))"
[[ $behind -gt "$limit" ]] || [[ $ahead -gt "$limit" ]]
}
## A nicely formatted git log
git-short-log() {
git log \
--graph \
--color=always \
--format="%C(auto)%h%d %s %C(dim)%an, %ar" \
--decorate=short \
"$@"
}
## An interactive decorator around git-short-log
fzf-git-log() {
local -r rebase="$1"
shift
local fzf_args=(
# Show a preview of each commit with 'git show | delta'
--preview-label="[ Commit ]"
--preview="
$(declare -f filter-short-hash git-show-pretty xargs-function)
$SINGLE_SELECTION |
filter-short-hash |
xargs-function git-show-pretty |
DELTA_FEATURES='line-numbers' delta --width \$FZF_PREVIEW_COLUMNS
"
# Selecting a commit with <enter> will open it in a pager
--bind="enter:execute(
$(declare -f filter-short-hash git-show-pretty xargs-function)
$MULTI_SELECTION |
filter-short-hash |
xargs-function git-show-pretty |
delta --width \$FZF_COLUMNS --paging=always |
LESS=R less
)"
# Copy the selected commits hashes to the clipboard
--bind="ctrl-h:execute(
$(declare -f filter-short-hash expand-full-hash strip-trailing-whitespace)
$MULTI_SELECTION |
filter-short-hash |
expand-full-hash |
tr '\n' ' ' |
strip-trailing-whitespace |
clip
)"
# Copy the selected commit *messages* to the clipboard
--bind="ctrl-g:execute(
$(declare -f filter-short-hash)
$MULTI_SELECTION |
filter-short-hash |
xargs git show -s --format=%B |
clip
)"
# Checkout the selected commit
--bind="ctrl-y:become(
$(declare -f filter-short-hash)
$SINGLE_SELECTION |
filter-short-hash |
xargs git checkout
)"
)
local fzf_header="<ctrl-h> copy hash <ctrl-g> copy message <ctrl-y> checkout"
if [[ "$rebase" = "true" ]]; then
fzf_header="$fzf_header <ctrl-r> rebase"
fzf_args+=(
# Interactively rebase the current branch onto the selected commit
#
# When multile commits are selected, mark each of the selected commits (except for the
# rebase target) as commits to be 'edit'ed.
--bind="ctrl-r:execute(
$(declare -f filter-short-hash)
if [[ \$FZF_SELECT_COUNT -gt 1 ]]; then
# fzf --multi selection can select in arbitrary order. So we take the selected
# commits, topo-sort them, and grab the last one as the rebase target.
GIT_GL_REBASE_TARGET=\"\$(
$MULTI_SELECTION | filter-short-hash | xargs git rev-list --topo-order --no-walk | tail -1 | xargs git rev-parse --short
)\"
# Build a Vim startup command to transform the selected commits 'pick' command
# to 'edit'. But skip the rebase target, because it's less likely that editing
# it automatically is desired.
#
# This doesn't do what you want if there's only one hash selected, so special
# case on FZF_SELECT_COUNT above
GIT_GL_CAPTURE_GROUP=\"\$(
$MULTI_SELECTION | filter-short-hash | grep -v \$GIT_GL_REBASE_TARGET | tr '\n' '|'
)\"
GIT_GL_REPLACEMENT_COMMAND=\"%s/\\v^pick (\${GIT_GL_CAPTURE_GROUP%|})/edit \\1/\"
GIT_EDITOR=\"vim -c \\\"\$GIT_GL_REPLACEMENT_COMMAND\\\"\" git rebase --interactive \$GIT_GL_REBASE_TARGET~
else
$SINGLE_SELECTION | filter-short-hash | xargs -oI % git rebase --interactive %~
fi
)+abort"
)
else
fzf_header="$fzf_header <ctrl-p> cherry pick"
fzf_args+=(
# Cherry pick the selected commits (in the order that they were selected)
--bind="ctrl-p:execute(
$(declare -f filter-short-hash)
$MULTI_SELECTION |
filter-short-hash |
xargs -o git cherry-pick -x
)+abort"
)
fi
if ! git-short-log "$@" |
fzf \
--ansi \
--no-sort \
--reverse \
--exit-0 \
--multi \
--header-first \
--header="$fzf_header" \
"${fzf_args[@]}"; then
# fzf returns 130 when user exits with ctrl-c or similar
if [[ $? -eq 130 ]]; then
true
fi
fi
}
## Interactively browse the git log
main() {
local use_fzf="true"
local heuristics="true"
local rebase="true"
local args=()
for arg in "$@"; do
if [[ "$arg" = "--no-fzf" ]]; then
use_fzf="false"
elif [[ "$arg" = "--no-heuristics" ]]; then
heuristics="false"
else
args+=("$arg")
fi
done
# Determine which, if any, of the arguments are git objects
local git_refs=()
for arg in "${args[@]}"; do
if git-object-exists "$arg"; then
git_refs+=("$arg")
rebase="false"
# Branch heuristics don't make sense when looking at the log for a file path
elif [[ -e "$arg" ]]; then
heuristics="false"
rebase="false"
fi
done
# If there were no arguments passed, we don't want to override showing 'git log HEAD' by adding
# extra branches in there.
if [[ "$heuristics" = "true" ]] && [[ ${#git_refs[@]} -eq 0 ]]; then
local current_ref
current_ref="$(git branch --show-current)"
if [[ -n "$current_ref" ]]; then
git_refs+=("$current_ref")
args+=("$current_ref")
else
git_refs+=(HEAD)
args+=(HEAD)
fi
fi
# Use distance heuristics to pick appropriate additional branches to show (upstream branches,
# default branch, etc)
if [[ "$heuristics" = "true" ]]; then
local extra_refs=()
local default_branch
default_branch="$(get-default-branch)"
local default_branch_too_distant="false"
local default_branch_already_listed="false"
for ref in "${git_refs[@]}"; do
local upstream
upstream="$(get-upstream-branch "$ref")"
if [[ -n "$upstream" ]]; then
if ! branch-too-distant "$ref" "$upstream"; then
extra_refs+=("$upstream")
fi
fi
# We only want to show the default branch if it's close enough to _all_ branches given
if branch-too-distant "$ref" "$default_branch"; then
default_branch_too_distant="true"
fi
if [[ "$ref" = "$default_branch" ]]; then
default_branch_already_listed="true"
fi
done
if [[ "$default_branch_too_distant" = "false" ]] && [[ "$default_branch_already_listed" = "false" ]]; then
if [[ -n "$default_branch" ]]; then
extra_refs+=("$default_branch")
fi
local upstream
upstream="$(get-upstream-branch "$default_branch")"
if [[ -n "$upstream" ]]; then
extra_refs+=("$upstream")
fi
fi
args+=("${extra_refs[@]}")
fi
if [[ "$use_fzf" = "true" ]]; then
fzf-git-log "$rebase" "${args[@]}"
else
git-short-log "${args[@]}"
fi
}
main "$@"