-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-graph.sh
More file actions
executable file
·79 lines (65 loc) · 2.21 KB
/
Copy pathgit-graph.sh
File metadata and controls
executable file
·79 lines (65 loc) · 2.21 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
#!/bin/sh
set -eu
IFS=$(printf ' \n\t')
# Compatibility check
command -v git >/dev/null || ( printf 'git not found\n' >&2 && exit 1 )
command -v awk >/dev/null || ( printf 'awk not found\n' >&2 && exit 1 )
command -v sort >/dev/null || ( printf 'sort not found\n' >&2 && exit 1 )
command -v head >/dev/null || ( printf 'head not found\n' >&2 && exit 1 )
# Context check
git rev-parse >/dev/null 2>&1 || ( printf 'not a git repository\n' >&2 && exit 1 )
# Calculate viewport width
terminalWidth=$(tput cols 2>/dev/null || printf '80')
width="${GIT_GRAPH_WIDTH:-$((terminalWidth - 1))}"
# Calculate maximum width of the graph
graphWidth=$(git log --graph --max-count=500 --pretty=format:'' ${1+"$@"} |
awk '{ print length }' |
sort -n -r -u |
head -n 1)
# Threshold for removing left indentation
# Prevents breaking columns layout when using '--stat' or similar options
graphWidthThreshold="${GIT_GRAPH_GRAPH_WIDTH_THRESHOLD:-30}"
if [ ${graphWidthThreshold} -lt ${graphWidth} ]; then
graphWidth='0'
fi
# Calculate short hash width
hashWidth=$(git rev-parse --short HEAD | awk '{ print length }')
# Configure column widths
authorWidth="${GIT_GRAPH_AUTHOR_WIDTH:-12}"
dateWidth="${GIT_GRAPH_DATE_WIDTH:-12}"
# Options
dateFormat="${GIT_GRAPH_DATE_FORMAT:-relative}"
# Setup format strings
hash="%>|($((graphWidth + hashWidth)))%C(blue)%h%C(auto)"
decorate="% D"
message="%<|($((width - authorWidth - dateWidth - 2)),trunc)% s"
author="%C(blue)%<(${authorWidth},trunc)% an%C(auto)"
date="%C(green)%>|(${width},trunc)% ad%C(auto)"
format="${hash}${decorate}${message}${author}${date}"
# Add '\n' at the end of the line when using options like: --stat, --patch, etc.
multiline=false
for arg in ${1+"$@"}; do
case "${arg}" in
'--compact-summary'|\
'--name-only'|\
'--name-status'|\
'--numstat'|\
'--patch'|'-p'|'-u'|\
'--shortstat'|\
'--stat'|\
'--summary')
multiline=true
break ;;
*)
continue ;;
esac
done
if ${multiline}; then
format="${format}%n"
fi
# Run git log with the calculated format
LANG=C.UTF-8 git log --graph --color \
--pretty=format:"${format}" \
--date="${dateFormat}" \
${1+"$@"}
printf '\n'