forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_stats
executable file
·236 lines (195 loc) · 7.61 KB
/
project_stats
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
#!/bin/bash
#
# project_stats
#
# stats on project/release from git database
#
###
### settings
###
set -e # exit on any uncaught error
set +o histexpand # don't expand history expressions
shopt -s nocasematch # case-insensitive regular expressions
###
### configurable global variables
###
# these paths relative to project root
declare -a cask_paths=(Casks)
declare -a code_paths=(bin developer lib spec test brew-cask.rb Rakefile Gemfile Gemfile.lock .travis.yml .gitignore)
declare -a doc_paths=(doc LICENSE "*.md")
end_object="HEAD"
###
### global variables
###
cask_authors=''
# prefer GNU xargs
xargs="$(/usr/bin/which gxargs || printf '/usr/bin/xargs')"
###
### functions
###
warn () {
local message="$@"
message="${message//\\t/$'\011'}"
message="${message//\\n/$'\012'}"
message="${message%"${message##*[![:space:]]}"}"
printf "%s\n" "$message" 1>&2
}
die () {
warn "$@"
exit 1
}
cd_to_project_root () {
local script_dir="$(/usr/bin/dirname "$0")"
cd "$script_dir"
local git_root="$(git rev-parse --show-toplevel)"
if [[ -z "$git_root" ]]; then
die "ERROR: Could not find git project root"
fi
cd "$git_root"
}
warn_if_off_branch () {
local wanted_branch='master'
if [[ -n "$1" ]]; then
wanted_branch="$1"
fi
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
if ! [[ "$current_branch" = "$wanted_branch" ]]; then
warn "\nWARNING: you are running from branch '$current_branch', not '$wanted_branch'\n\n"
fi
}
verify_git_object () {
local object="$1"
if ! git rev-parse --verify "$object" -- >/dev/null 2>&1; then
die "\nERROR: No such commit object: '$object'\n\n"
fi
}
print_contributor_stats () {
local start_object="$1"
local initial_commit="$2"
printf "====================\n"
printf "Contributors\n"
printf "====================\n"
local -a git_log_cmd=("git" "log" "--no-merges" "--format='%ae'" "${start_object}..${end_object}")
printf "Unique contributors"
if ! [[ "$start_object" = "$initial_commit" ]]; then
printf " since %s" "${start_object#v}"
fi
printf "\n"
cask_authors="$("${git_log_cmd[@]}" -- "${cask_paths[@]}" | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
printf " Casks\t%s\n" "$cask_authors"
printf " code\t"
"${git_log_cmd[@]}" -- "${code_paths[@]}" | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l
printf " docs\t"
"${git_log_cmd[@]}" -- "${doc_paths[@]}" | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l
printf " any\t"
"${git_log_cmd[@]}" -- . | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l
if ! [[ "$start_object" = "$initial_commit" ]]; then
local alltime_contribs="$(git log --no-merges --format='%ae' "${initial_commit}".."${end_object}" -- . | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
local prior_contribs="$(git log --no-merges --format='%ae' "${initial_commit}".."${start_object}" -- . | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
# arithmetic removes whitespace
((alltime_contribs += 0))
((new_contribs = alltime_contribs - prior_contribs))
printf "\nAll-time contributors\t%s\n" "$alltime_contribs"
printf "New contributors since %s\t%s\n" "${start_object#v}" "$new_contribs"
fi
printf "\n"
}
print_commit_stats () {
local start_object="$1"
local initial_commit="$2"
printf "====================\n"
printf "Commits\n"
printf "====================\n"
local -a git_log_cmd=("git" "log" "--no-merges" "--format='%ae'" "${start_object}..${end_object}")
printf "Commit count"
if ! [[ "$start_object" = "$initial_commit" ]]; then
printf " since %s" "${start_object#v}"
fi
printf "\n"
printf " Casks\t"
"${git_log_cmd[@]}" -- "${cask_paths[@]}" | /usr/bin/wc -l
printf " code\t"
"${git_log_cmd[@]}" -- "${code_paths[@]}" | /usr/bin/wc -l
printf " docs\t"
"${git_log_cmd[@]}" -- "${doc_paths[@]}" | /usr/bin/wc -l
printf " any\t"
"${git_log_cmd[@]}" -- . | /usr/bin/wc -l
if ! [[ "$start_object" = "$initial_commit" ]]; then
printf "\nAll-time commits\t"
git log --no-merges --format='%ae' "${initial_commit}".."${end_object}" -- . | /usr/bin/wc -l
fi
printf "\n"
}
print_doc_stats () {
local start_object="$1"
local initial_commit="$2"
printf "====================\n"
printf "Docs\n"
printf "====================\n"
local -a git_log_cmd=("git" "log" "--no-merges" "--format='%ae'" "${start_object}..${end_object}")
printf "Doc contributors"
if ! [[ "$start_object" = "$initial_commit" ]]; then
printf " since %s" "${start_object#v}"
fi
printf "\n "
"${git_log_cmd[@]}" -- "${doc_paths[@]}" | /usr/bin/sort | /usr/bin/uniq | \
/usr/bin/egrep -v $'^\'(paul\\.t\\.hinze@gmail\\.com|fanquake@users\\.noreply\\.github\\.com|fanquake@gmail\\.com|leoj3n@gmail\\.com|nano@fdp\\.io|nanoid\\.xd@gmail\\.com|walker@pobox\\.com|info@vitorgalvao\\.com|calebcenter@live\\.com|ndr@qef\\.io|josh@joshbutts\\.com|goxberry@gmail\\.com|radek\\.simko@gmail\\.com|federicobond@gmail\\.com)\'$' | \
"$xargs" | /usr/bin/perl -pe 's{ }{, }g' # '
printf "\n"
}
print_cask_stats () {
local start_object="$1"
local initial_commit="$2"
printf "====================\n"
printf "Casks\n"
printf "====================\n"
if ! [[ "$start_object" = "$initial_commit" ]]; then
local new_casks="$(git diff --name-status "$start_object" "$end_object" -- "${cask_paths[@]}" | /usr/bin/grep '^A.*\.rb' | cut -f2 | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
local deleted_casks="$(git diff --name-status "$start_object" "$end_object" -- "${cask_paths[@]}" | /usr/bin/grep '^D.*\.rb' | cut -f2 | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
local updated_casks="$(git diff --name-status "$start_object" "$end_object" -- "${cask_paths[@]}" | /usr/bin/grep '^M.*\.rb' | cut -f2 | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
# arithmetic removes whitespace
((cask_authors += 0))
((deleted_casks += 0))
((new_casks -= deleted_casks))
((updated_casks += 0))
printf "%s Casks added (%s updated) by %s contributors since %s\n" "$new_casks" "$updated_casks" "$cask_authors" "${start_object#v}"
fi
printf "Total current Casks in HEAD\t"
/usr/bin/find "${cask_paths[@]}" -name '*.rb' | /usr/bin/wc -l
printf "\n"
}
###
### main
###
_project_stats () {
local arg_object="$1"
cd_to_project_root
warn_if_off_branch 'master'
local initial_commit="$(git log --pretty=format:%H -- | /usr/bin/tail -1)"
verify_git_object "$initial_commit"
local start_object="$initial_commit"
if [[ "$arg_object" = 'release' ]]; then
start_object="$(./developer/bin/get_release_tag)"
elif [[ -n "$arg_object" ]]; then
start_object="$arg_object"
fi
verify_git_object "$start_object"
print_contributor_stats "$start_object" "$initial_commit"
print_commit_stats "$start_object" "$initial_commit"
print_doc_stats "$start_object" "$initial_commit"
print_cask_stats "$start_object" "$initial_commit"
}
# process args
if [[ $1 =~ ^-+h(elp)?$ ]]; then
printf "project_stats [ <commit-object> ]
With optional single argument, (eg a tag or commit-hash)
show statistics since that commit object.
Use the special argument 'release' to calculate since the
most recent tag (usually the same as the last release).
Without argument, show statistics since first commit.
"
exit
fi
# dispatch main
_project_stats "${@}"
#