homebrew-cask-versions/developer/bin/project_stats
Roland Walker bc650b8079 devscript: quote/expansion nit
for clarity. I think this is special-cased anyway
2014-01-31 12:33:29 -05:00

135 lines
4.5 KiB
Bash
Executable file

#!/bin/bash
#
# project_stats
#
# stats on project/release from git database
#
set -e # exit on any uncaught error
set +o histexpand # don't expand history expressions
shopt -s nocasematch # case-insensitive regular expressions
warn () {
local message="$@"
if ! [[ $message =~ "\n"$ ]]; then
message="${message}\n"
fi
printf "$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 branch="$(git rev-parse --abbrev-ref HEAD)"
local wanted_branch='master'
if [[ -n "$1" ]]; then
wanted_branch="$1"
fi
if ! [[ "$branch" = "$wanted_branch" ]]; then
warn "\nWARNING: you are running from branch '$branch', not '$wanted_branch'\n\n"
fi
}
verify_git_object () {
if ! git rev-parse --verify "$1" > /dev/null 2>/dev/null; then
die "\nERROR: No such commit object: $1"
fi
}
_homebrew_cask_project_stats () {
local initial_commit="5a0d1d5556e3f963a0d34da46e16ecffa59ea2fc"
local start_object="$initial_commit"
local end_object="HEAD"
# these paths relative to project root
local cask_paths="Casks"
local code_paths="bin developer lib test brew-cask.rb Rakefile Gemfile Gemfile.lock"
local doc_paths="LICENSE *.md"
cd_to_project_root
warn_if_off_branch 'master'
if [[ "$1" = 'release' ]]; then
start_object="$(git describe --tags --abbrev=0)"
elif [[ -n "$1" ]]; then
start_object="$1"
fi
verify_git_object "$start_object"
printf "Unique contributors"
if ! [[ "$start_object" = "$initial_commit" ]]; then
printf " since $start_object"
fi
printf "\n"
local git_log_cmd="git log --no-merges --format='%ae' ${start_object}..${end_object}"
local cask_authors="$($git_log_cmd -- $cask_paths | /usr/bin/sort | /usr/bin/uniq | /usr/bin/wc -l)"
printf " Casks\t$cask_authors\n"
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$alltime_contribs\n"
printf "New contributors since $start_object\t$new_contribs\n"
fi
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 "$new_casks Casks added ($updated_casks updated) by $cask_authors contributors since $start_object\n"
fi
printf "Total current Casks in HEAD\t"
/usr/bin/find $cask_paths -name '*.rb' | /usr/bin/wc -l
}
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
_homebrew_cask_project_stats "${@}"
#