-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-brv
executable file
·73 lines (61 loc) · 2.17 KB
/
git-brv
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
#!/usr/bin/env bash
# A sorted prettier branch -vv
# Based on https://github.com/tj/git-extras/blob/master/bin/git-brv
set -eEu -o pipefail
IFS=$'\n\t'
PS4='+\t '
error_handler() { echo "Error: Line ${1} exited with status ${2}"; }
trap 'error_handler ${LINENO} $?' ERR
[[ "${TRACE:-0}" == "1" ]] && set -x
if ! (( BASH_VERSINFO[0] > 4 ||
BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 2 )); then
printf >&2 'This script requires bash 4.2 or newer\n'
exit 1
fi
if [[ -t 1 ]]; then
shopt -s checkwinsize
COLUMNS=$(tput cols)
color_branch_local=$(git config --get-color color.branch.local normal)
color_branch_current=$(git config --get-color color.branch.current green)
color_diff_commit=$(git config --get-color color.diff.commit yellow)
color_branch_upstream=$(git config --get-color color.branch.upstream blue)
reset=$(tput sgr0)
fi
declare -A upstream date hash message
eval "$(
git for-each-ref --format='upstream[%(refname:short)]=%(upstream:short)' \
--shell 'refs/heads/**'
)"
for b in "${!upstream[@]}"; do
blen=${#b} bwidth=0 ulen=${#upstream[$b]} uwidth=0
bwidth=$(( blen > bwidth ? blen : bwidth ))
uwidth=$(( ulen > uwidth ? ulen : uwidth ))
IFS=/ read -r 'date[$b]' 'hash[$b]' 'message[$b]' < <(
git log --no-walk=unsorted --format=%ct/%h/%s "$b" --
)
hlen=${#hash[$b]} hwidth=0
hwidth=$(( hlen > hwidth ? hlen : hwidth ))
done
mapfile -t ordered < <(
for b in "${!date[@]}"; do
printf '%d\t%s\n' "${date[$b]}" "$b"
done | sort -rn | cut -f2-
)
current=$(git symbolic-ref -q --short HEAD)
for b in "${ordered[@]}"; do
branch_color=$color_branch_local
if [[ $b = "$current" ]]; then
branch_color=$color_branch_current
fi
if [[ -t 1 ]]; then
msg=${message[$b]:0:COLUMNS-bwidth-uwidth-hwidth-14}
else
msg=${message[$b]}
fi
printf '%(%Y-%m-%d)T %s%*s%s %s%*s%s %s%*s%s %s\n' \
"${date[$b]}" \
"$branch_color" "-$bwidth" "$b" "$reset" \
"$color_branch_upstream" "-$uwidth" "${upstream[$b]}" "$reset" \
"$color_diff_commit" "-$hwidth" "${hash[$b]}" "$reset" \
"$msg"
done