forked from alexandre-mbm/gitgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitgraph
executable file
·295 lines (262 loc) · 8.26 KB
/
gitgraph
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
#!/bin/bash
#
# git-graph: a repo visualizer
# Copyright (C) 2013 Ed <ed.youwish.com>
# Copyright (C) 2014 Alexandre Magno <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# Global vars (I know, evil!)
#
MAX=100
ALL=""
CLEAN=0
DEEP_CLEAN=0
USE_DIA=0
HIGH=0
ARGS=""
QUIET=0
###############################################################################
#
OUT_SVG="out.svg"
OUT_PNG="out.png"
TMP_HIGH="/tmp/$$-tmp.high"
TMP_DOT="/tmp/$$-tmp.dot"
TMP_SVG="/tmp/$$-$OUT_SVG"
TMP_PNG="/tmp/$$-$OUT_PNG"
function check_dependencies()
{
NOT_FOUND=""
while (($#))
do
if ! which $1 > /dev/null 2>&1
then
NOT_FOUND="${NOT_FOUND} $1"
fi
shift
done
if [[ "$NOT_FOUND" != "" ]]
then
for iNotFound in ${NOT_FOUND}
do
echo "DEPENDENCY NOT MET: $iNotFound"
done
printf "\nPlease install the missing dependencies.\n"
return 1
fi
}
###############################################################################
#
function process_args()
{
while [[ $# > 0 && "${1:0:1}" == "-" ]]
do
if [[ "$1" == "-a" ]]
then
# Capture the log for all, not just the current branch.
#
ALL="--all"
elif [[ "$1" == "-c" ]]
then
# Simplify graph by aggregating nodes with only one child.
#
CLEAN=1
elif [[ "$1" == "-cc" ]]
then
# Simplify "same as -c", but make sure nodes with more than one
# parent don't get aggregated with others. (kinda of a sluggish
# operation).
#
CLEAN=1
DEEP_CLEAN=1
elif [[ "$1" == "-d" ]]
then
# Output as svg (not png)
#
USE_DIA=1
elif [[ "$1" == "-q" ]]
then
# Quiet.
#
QUIET=1
elif [[ "$1" == "-high" ]]
then
# Highlight the left-parent history of a given branch/tag/hash.
#
HIGH=1
shift
git log --first-parent --pretty=format:"%H" $1 > "$TMP_HIGH"
fi
shift
done
if (($# > 0)) &&
[[ "$(echo $1 | tr -d \"[0-9]\")" == "" ]]
then
# If final option is numerical, this is the maximum number of lines to
# extract from the log.
#
MAX=$1
shift
fi
# Pass the rest of the command line arguments to 'git log'.
#
ARGS=$*
}
###############################################################################
#
function create_dot_graph()
{
all_shas=$(git log $ALL --pretty=format:"%H" $ARGS | head -n $MAX)
all_shas_with_parents=$(git log $ALL --pretty=format:"%H %P" $ARGS | head -n $MAX)
count=1
all_tag_branch_shas=""
echo "digraph graphname {"
{
# Add branches and tags
#
IFS=$'\n'
for line in `git branch -a | sed -e 's/^..//' -e 's/ .*//'`
do
IFS=$' '
record=($(git log --pretty=format:"B$count%H %H \"Branch: $line\"" $line | head -n 1))
((count++))
if echo $all_shas | grep -q ${record[1]} > /dev/null
then
# Only add branches that reference something else on our graph
#
echo ${record[*]}
all_tag_branch_shas="$all_tag_branch_shas ${record[1]}"
fi
done
IFS=$'\n'
for line in `git tag`
do
IFS=$' '
record=($(git log --pretty=format:"T$count%H %H \"Tag: $line\"" $line | head -n 1))
((count++))
if echo $all_shas | grep -q ${record[1]} > /dev/null
then
# Only add tags that reference something else on our graph
#
echo ${record[*]}
all_tag_branch_shas="$all_tag_branch_shas ${record[1]}"
fi
done
# Get log entries.
#
clean_head=""
clean_next=""
git log $ALL --topo-order --pretty=format:"%H %P \"%cd: %s\"" --date=short $ARGS | head -n $MAX | while read cLine
do
# Cleanup
#
shas=($(echo $cLine | cut -d'"' -f1))
# IF... not doing any cleaning or
# sha is not the expected one or sha is merge commit or
# sha is referenced by tag/branch or
# (deep_clean enabled and sha has more than 1 child)
#
if [[ "$CLEAN" != "1" ]] ||
[[ "${shas[0]}" != "$clean_next" || ${#shas[*]} > 2 ]] ||
echo $all_tag_branch_shas | grep -q ${shas[0]} ||
[[ "$DEEP_CLEAN" == "1" && $(echo $all_shas_with_parents | grep -c ${shas[0]}) > 2 ]]
then
# If start-point or is merge point... continue as normal,
# but make sure to close any open loops.
#
if [[ "$clean_head" != "" &&
"$clean_next" != "" &&
"$clean_next" != "$clean_head" ]]
then
echo "$clean_head $clean_next \"...\""
fi
clean_next=""
clean_head=${shas[1]}
echo $cLine
fi
clean_next=${shas[1]}
done
# Adding trailing newline (echo) because "git log" doesn't provide one,
# and this causes the while statement to leave out the last line.
#
echo
} | grep . | while read line
do
shas=($(echo $line | cut -d'"' -f1))
label=$(echo $line | cut -d'"' -f2)
if [[ "${shas:0:1}" == "B" ]]
then
echo " \"${shas[0]:0:5}\" [style=\"filled\"];"
echo " \"${shas[0]:0:5}\" [fillcolor=\"green\"];"
echo " \"${shas[0]:0:5}\" [label=\"${label:0:50}\"];"
elif [[ "${shas:0:1}" == "T" ]]
then
echo " \"${shas[0]:0:5}\" [style=\"filled\"];"
echo " \"${shas[0]:0:5}\" [fillcolor=\"yellow\"];"
echo " \"${shas[0]:0:5}\" [label=\"${label:0:50}\"];"
else
echo " \"${shas[0]:0:5}\" [label=\"${shas[0]:0:5}\n${label:0:50}\"];"
fi
#######################################################################
# Highlight nodes.
#
if [[ "$HIGH" == "1" ]] &&
grep -q ^${shas[0]:0:5} "$TMP_HIGH" 2>/dev/null
then
echo " \"${shas[0]:0:5}\" [style=\"filled\"];"
echo " \"${shas[0]:0:5}\" [fillcolor=\"pink\"];"
rm "$TMP_HIGH"
fi
for ((i=1;i<${#shas[*]};++i))
do
echo " \"${shas[0]:0:5}\" -> \"${shas[$i]:0:5}\";"
done
done
echo "}"
}
function main()
{
if ! check_dependencies dot git sed cut
then
return 1
fi
process_args $*
create_dot_graph > "$TMP_DOT"
# Invoke "dot" and show graph
#
if [[ "$USE_DIA" == "1" ]]
then
dot -Tsvg -o "$TMP_SVG" "$TMP_DOT"
if [[ "$QUIET" != "1" ]]
then
! check_dependencies dia && return 1
dia "$TMP_SVG"
rm "$TMP_SVG"
else
mv "$TMP_SVG" "./$OUT_SVG"
fi
else
dot -Tpng -o "$TMP_PNG" "$TMP_DOT"
if [[ "$QUIET" != "1" ]]
then
! check_dependencies eog && return 1
eog "$TMP_PNG"
rm "$TMP_PNG"
else
mv "$TMP_PNG" "./$OUT_PNG"
fi
fi
rm "$TMP_DOT"
}
main $*