-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcount-commits
More file actions
executable file
·253 lines (236 loc) · 5.7 KB
/
count-commits
File metadata and controls
executable file
·253 lines (236 loc) · 5.7 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
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
#!/bin/bash
# SPDX-FileCopyrightText: 2017 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command awk find git && exit 3
#
# Functions
#
get_count() {
local dir
local cmd
dir=${1%/*}
# Git will traverse the entire from HEAD to the first commit where the
# committer or the author date falls outside the range. This can caused
# unexpected behavior for merge / rebase / fixup commits, as the committer
# and author dates will likely not match.
cmd="git -C ${dir@Q} rev-list --count --branches"
if [ -n "${AUTHOR}" ] || [ -n "${EMAIL}" ]; then
cmd="${cmd} --author=^${AUTHOR:-.*}\ \<${EMAIL:-.*}\>$"
fi
if [ -n "${SINCE}" ]; then
cmd="${cmd} --since=$(date -d "${SINCE}" +%s)"
fi
if [ -n "${UNTIL}" ]; then
cmd="${cmd} --until=$(date -d "${UNTIL}" +%s)"
fi
eval "${cmd}"
}
export -f get_count
print_usage() {
show_header "Usage: count-commits"
echo
show_listitem " -a|--author <author>"
show_listitem " -e|--email <email>"
show_listitem " -s|--since <time>"
show_listitem " -u|--until <time>"
show_listitem " -h|--help print (this) help message"
echo
show_listitem "<author> = commit author"
show_listitem "<email> = committer/author email address"
show_listitem \
"<time> = string (e.g. 1week, 2years, now), passed to 'date' from coreutils"
}
parse_date() {
local string_array
local string_array_part2
local item
local item2
local digits
local suffix
local prefix
digits=
suffix=
prefix=
IFS=' ' read -r -a string_array <<< "${1}"
for item in "${string_array[@]}"; do
IFS='-' read -r -a string_array_part2 <<< "${item}"
for item2 in "${string_array_part2[@]}"; do
case "${item2}" in
one | 1)
digits=${digits}+1
;;
two | 2)
digits=${digits}+2
;;
three | 3)
digits=${digits}+3
;;
four | 4)
digits=${digits}+4
;;
five | 5)
digits=${digits}+5
;;
six | 6)
digits=${digits}+6
;;
seven | 7)
digits=${digits}+7
;;
eight | 8)
digits=${digits}+8
;;
nine | 9)
digits=${digits}+9
;;
ten | 10)
digits=${digits}+10
;;
eleven | 11)
digits=${digits}+11
;;
twelve | 12)
digits=${digits}+12
;;
thirteen | 13)
digits=${digits}+13
;;
fourteen | 14)
digits=${digits}+14
;;
fifteen | 15)
digits=${digits}+15
;;
sixteen | 16)
digits=${digits}+16
;;
seventeen | 17)
digits=${digits}+17
;;
eighteen | 18)
digits=${digits}+18
;;
nineteen | 19)
digits=${digits}+19
;;
twenty | 20)
digits=${digits}+20
;;
thirty | 30)
digits=${digits}+30
;;
forty | 40)
digits=${digits}+40
;;
fifty | 50)
digits=${digits}+50
;;
sixty | 60)
digits=${digits}+60
;;
seventy | 70)
digits=${digits}+70
;;
eighty | 80)
digits=${digits}+80
;;
ninety | 90)
digits=${digits}+90
;;
hundred | 100)
digits="${digits}*100"
;;
thousand | 1000 | 1,000)
digits="${digits}*1000"
;;
million | 1000000 | 1,000,000)
digits="${digits}*1000000"
;;
ago)
prefix="-"
;;
*)
suffix="${suffix:+${suffix} }${item2}"
;;
esac
done
done
[ -n "${digits}" ] && digits="$((digits))"
echo "${prefix}${digits:+${digits} }${suffix}"
}
#
# Main
#
SINCE=
UNTIL=
AUTHOR=
EMAIL=
OPTIONS=ha:d:e:s:u:
LONGOPTIONS=help,author:,depth:,email:,since:,until:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-a | --author)
AUTHOR=${2}
shift 2
;;
-e | --email)
EMAIL=${2}
shift 2
;;
-d | --depth)
DEPTH=${2}
shift 2
;;
-s | --since)
SINCE="$(parse_date "${2}")"
shift 2
;;
-u | --until)
UNTIL="$(parse_date "${2}")"
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: option ${1@Q} unrecognized. Exiting."
exit 3
;;
esac
done
export AUTHOR
export EMAIL
export SINCE
export UNTIL
projectdir="${projectdir:-${HOME}}"
PROJECTDIR=("${@:-${projectdir}}")
if [ ${#} -eq 1 ] && git -C "${1}" log > /dev/null 2>&1; then
get_count "${1}" | awk '{s+=$1}END{print s}'
else
find -L "${PROJECTDIR[@]}" ${DEPTH:+-maxdepth ${DEPTH}} -type d -name ".git" \
-exec bash -c 'get_count "${1}"' bash {} \; | awk '{s+=$1}END{print s}'
fi