-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgit-commit-churn.sh
381 lines (327 loc) · 8.88 KB
/
git-commit-churn.sh
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env bash
PRINT_HEADER="false"
PRINT_FOOTER="true"
PRINT_TOTAL_STATS="true"
#
# Toggle printing the header (off by default)
function git_churn_toggle_header() {
if [[ "$PRINT_HEADER" == "true" ]]; then
export PRINT_HEADER="false"
else
export PRINT_HEADER="true"
fi
}
function git_churn_toggle_footer() {
if [[ "$PRINT_FOOTER" == "true" || "$1" == "true" ]]; then
export PRINT_FOOTER="false"
else
export PRINT_FOOTER="true"
fi
}
function git_churn_toggle_total_stats() {
if [[ "$PRINT_TOTAL_STATS" == "true" ]]; then
export PRINT_TOTAL_STATS="false"
else
export PRINT_TOTAL_STATS="true"
fi
}
#
# Print the header (if toggled 'true')
function __print_header() {
if [[ "$PRINT_HEADER" == "true" || "$1" == "true" ]]; then
awk 'BEGIN { printf "| %7s | %7s | %-7s | %-7s | %7s | filename/stats |\n", "files", "lines", "growth", "shrink", "net(+/-)" }'
awk 'BEGIN{ for(c=0;c<69;c++) printf "="; printf "\n"}'
fi
}
#
# Print the header (if toggled 'true')
function __print_footer() {
if [[ "$PRINT_FOOTER" == "true" || "$1" == "true" ]]; then
awk 'BEGIN { for(c=0;c<69;c++) printf "="; printf "\n"}'
awk 'BEGIN { printf "| %7s | %7s | %-7s | %-7s | %7s | filename/stats |\n", "files", "lines", "growth", "shrink", "net(+/-)" }'
fi
}
##
# Sum file and line modifications for all files from within a git project.
# Basic results are provided via:
#
# 'git log --numstat'
#
# ('git log --stat' shows growth/sthrink trends)
#
# Additional arguments may be passed in via the command line similar
# to calling 'git log' directly. For example
#
# 'git_file_churn --after="2014-06-21"'
# 'git_file_churn --after="2014-06-21 --author=dmillett'
function git_churn() {
git log --numstat "$@"| grep "^[0-9]" | awk -v stats="$PRINT_TOTAL_STATS" '{
fmods[$3]++;
adds[$3] += $1;
subtracts[$3] += $2;
lmods[$3] += ($1 + $2);
ftotal++;
ltotal += ($1 + $2);
growth += $1;
shrink += $2;
}
END {
for (f in fmods)
{
net = adds[f] - subtracts[f]
printf("| %7s | %7s | %7s | %7s | %8s | %-s |\n", fmods[f], lmods[f], adds[f], subtracts[f], net, f)
}
if (stats == "true")
{
totals = growth - shrink
printf("| %7s | %7s | %7s | %7s | %8s | Stat Totals |\n", ftotal, ltotal, growth, shrink, totals)
}
}'
}
#
# Sort file modification count ascending
function git_file_churn() {
__print_header
git_churn "$@" | sort -n --key=2
__print_footer
}
#
# Sort by line modification count per file ascending
function git_line_churn() {
__print_header
git_churn "$@" | sort -n --key=4
__print_footer
}
#
# Sort by line growth trend/count per file ascending
function git_line_growth() {
__print_header
git_churn "$@" | sort -n --key=6
__print_footer
}
#
# Sort by line shrink trend/count per file ascending
function git_line_shrink() {
__print_header
git_churn "$@" | sort -n --key=8
__print_footer
}
#
# Sort by net growth
function git_net_growth() {
__print_header
git_churn "$@" | sort -n --key=10
__print_footer
}
#
# Sort by net shrink
function git_net_shrink() {
__print_header
git_churn "$@" | sort -nr --key=10
__print_footer
}
#
# Sort by file name (regardless of count)
function git_file_sort() {
__print_header
git_churn "$@" | sort --key=12
}
#
# Print the footer (if toggled 'true')
function __print_date_header() {
if [[ "$PRINT_HEADER" == "true" || "$1" == "true" ]]; then
awk 'BEGIN { printf "| %10s | %7s | %7s | %-7s | %-7s | %7s |\n", "dates", "files", "lines", "growth", "shrink", "net(+/-)" }'
awk 'BEGIN{ for(c=0;c<65;c++) printf "="; printf "\n"}'
fi
}
#
# Print the footer (if toggled 'true')
function __print_date_footer() {
if [[ "$PRINT_FOOTER" == "true" ]]; then
awk 'BEGIN{ for(c=0;c<65;c++) printf "="; printf "\n"}'
awk 'BEGIN { printf "| %10s | %7s | %7s | %-7s | %-7s | %7s |\n", "dates", "files", "lines", "growth", "shrink", "net(+/-)" }'
fi
}
function git_churn_dates() {
git log --numstat --date=short "$@"| grep "^[0-9\|Date:]" | awk -v stats="$PRINT_TOTAL_STATS" '{
if ( $1 == "Date:" )
{
commit_date = $2
}
else
{
dmods[commit_date]++;
grow[commit_date] += $1;
shrink[commit_date] += $2;
fmods[commit_date] = fmods[commit_date] "," $3
lmods[commit_date] += ($1 + $2);
fmods[commit_date]++;
ftotal++;
ltotal += ($1 + $2);
tgrowth += $1;
tshrink += $2;
}
}
END {
for (t in dmods)
{
net = grow[t] - shrink[t]
printf("| %10s | %7s | %7s | %7s | %7s | %8s |\n", t, fmods[t], lmods[t], grow[t], shrink[t], net)
}
if (stats == "true")
{
totals = tgrowth - tshrink
#printf("| Stat Totals | %7s | %7s | %7s | %7s | %8s |\n", ftotal, ltotal, tgrowth, tshrink, totals)
}
}'
}
#
# Sort file modification count ascending
function git_date_churn() {
__print_date_header
git_churn_dates "$@" | sort -n --key=2
__print_date_footer
}
#
# Sort by line modification count per file ascending
function git_file_churn_dates() {
__print_date_header
git_churn_dates "$@" | sort -n --key=4
__print_date_footer
}
#
# Sort by line modification count per file ascending
function git_line_churn_dates() {
__print_date_header
git_churn_dates "$@" | sort -n --key=6
__print_date_footer
}
#
# Sort by line modification count per file ascending
function git_line_growth_dates() {
__print_date_header
git_churn_dates "$@" | sort -n --key=8
__print_date_footer
}
#
# Sort by line modification count per file ascending
function git_line_shrink_dates() {
__print_date_header
git_churn_dates "$@" | sort -n --key=10
__print_date_footer
}
#
# Sort by line modification count per file ascending
function git_net_growth_dates() {
__print_date_header
git_churn_dates "$@" | sort -n --key=12
__print_date_footer
}
#
# Sort by net shrink
function git_net_shrink_dates() {
__print_date_header
git_churn_dates "$@" | sort -nr --key=12
__print_date_footer
}
COMMIT_MSG_PREFIX=""
#
# Complimentary with grep=, since this only looks at the first "word"
function git_churn_commit_message_prefix() {
export COMMIT_MSG_PREFIX="$1"
}
#
# Will pick up all the text until the first white space
function git_churn_messages() {
git log --numstat "$@" | grep "^[^Author:\|Date:\|commit\|Merge]" | \
awk -v stats="$PRINT_TOTAL_STATS" -v prefix="$COMMIT_MSG_PREFIX" '{
if ($1 ~ /[^0-9\|\s]+/)
{
if (length(commit_msg) == 0 || nmbr == "true")
{
commit_msg = $1
nmbr = "false";
}
}
else if ($1 ~ /^[0-9]/)
{
nmbr = "true"
if (commit_msg != "" && commit_msg ~ prefix)
{
dmods[commit_msg]++;
grow[commit_msg] += $1;
shrink[commit_msg] += $2;
fmods[commit_msg] = fmods[commit_msg] "," $3
lmods[commit_msg] += ($1 + $2);
fmods[commit_msg]++;
ftotal++;
ltotal += ($1 + $2);
tgrowth += $1;
tshrink += $2;
}
}
}
END {
for (t in dmods)
{
net = grow[t] - shrink[t]
printf("| %20s | %7s | %7s | %7s | %7s | %8s |\n", t, fmods[t], lmods[t], grow[t], shrink[t], net)
};
if (stats == "true")
{
totals = tgrowth - tshrink
#printf("| Stat Totals | %7s | %7s | %7s | %7s | %8s |\n", ftotal, ltotal, tgrowth, tshrink, totals)
}
}'
}
#
# Print the header (if toggled 'true')
function __print_commit_msg_header() {
if [[ "$PRINT_HEADER" == "true" || "$1" == "true" ]]; then
awk 'BEGIN { printf "| %20s | %7s | %7s | %-7s | %-7s | %7s |\n", "message", "files", "lines", "growth", "shrink", "net(+/-)" }'
awk 'BEGIN{ for(c=0;c<75;c++) printf "="; printf "\n"}'
fi
}
#
# Print the footer (if toggled 'true')
function __print_commit_msg_footer() {
if [[ "$PRINT_FOOTER" == "true" ]]; then
awk 'BEGIN{ for(c=0;c<75;c++) printf "="; printf "\n"}'
awk 'BEGIN { printf "| %20s | %7s | %7s | %-7s | %-7s | %7s |\n", "message", "files", "lines", "growth", "shrink", "net(+/-)" }'
fi
}
function git_message_churn() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=2
__print_commit_msg_footer
}
function git_file_churn_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=4
__print_commit_msg_footer
}
function git_line_churn_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=6
__print_commit_msg_footer
}
function git_line_growth_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=8
__print_commit_msg_footer
}
function git_line_shrink_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=10
__print_commit_msg_footer
}
function git_net_growth_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -n --key=12
__print_commit_msg_footer
}
function git_net_shrink_messages() {
__print_commit_msg_header
git_churn_messages "$@" | sort -nr --key=12
__print_commit_msg_footer
}