-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-lg
executable file
·72 lines (62 loc) · 1.85 KB
/
git-lg
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
#!/bin/bash -e
# Git script for printing beautiful logs.
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
echo "usage: ${0##*/} [-h|--help] [ARGS]" 1>&2
echo "Forwards any additional ARGS to the git log command." 1>&2
exit 1
fi
HASH='%C(red)%h%C(reset)'
DATE='%C(green)(%cr)%C(reset)'
AUTHOR='%C(bold blue)<%an>%C(reset)'
REFS='%C(yellow)%d%C(reset)'
MESSAGE='%s'
TERM_WIDTH="$(tput cols)"
MAX_HASH_WIDTH="$((TERM_WIDTH / 4))"
make_format() {
local delim="$1"
echo "$HASH$delim$DATE$delim$AUTHOR$delim$REFS $MESSAGE"
}
make_log() {
local format="$1"
shift
git log \
--date=relative \
--graph \
--pretty="tformat:$format" \
"$@"
}
max_hash_width() {
make_log "$HASH" "$@" | awk '{ if (length > L) { L = length } }END{ print L }'
}
git_pretty_log() {
local columnize
if [[ "$(max_hash_width "$@")" -le $MAX_HASH_WIDTH ]]; then
columnize=true
else
columnize=false
fi
# 1. Replace (2 years, 5 months) with (2 years)
# 2. Replace (2 years ago) with (2 years)
# 3. Color merge commits specially
# 4. Page only if we're asked to (GIT_NO_PAGER)
# 5. Page only if needed
if [[ $columnize == true ]]; then
make_log "$(make_format '}')" "$@"
else
make_log "$(make_format ' ')" "$@"
fi |
sed -Ee 's/([[:digit:]]+ [[:alpha:]]+), [[:digit:]]+ months? ago/\1/' |
sed -Ee 's/([[:digit:]]+ [[:alpha:]]+) ago/\1/' |
sed -Ee "s/(Merge (branch|remote-tracking branch|pull request) .*$)/$(printf '\033[31m')\\1$(printf '\033[0m')/" |
if [[ $columnize == true ]]; then
column -ts '}'
else
cat
fi |
if [ -n "$GIT_NO_PAGER" ]; then
cat
else
less --quit-if-one-screen --no-init --RAW-CONTROL-CHARS --chop-long-lines
fi
}
git_pretty_log "$@"