Skip to content

Instantly share code, notes, and snippets.

@augustohp
Last active April 24, 2019 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save augustohp/2234a685faa8d89cc70a5dd4f8f35536 to your computer and use it in GitHub Desktop.
Save augustohp/2234a685faa8d89cc70a5dd4f8f35536 to your computer and use it in GitHub Desktop.
How good is a commit? The bigger the commit message, more thought was put into that commit
#!/bin/sh
CSV_OUTPUT=0
_main()
{
header_printed=""
for commit in $(repository_commits)
do
changed_lines=$(diff_stat_of $commit)
lines_explaining=$(lines_from_commit $commit)
ratio=$(ratio_between $lines_explaining $changed_lines)
if [ "$CSV_OUTPUT" == "1" ]
then
if [ "$header_printed" != "yes" ]
then
echo "commit,ratio,commit lines,changes"
header_printed="yes"
fi
echo "${commit},${ratio},${lines_explaining},${changed_lines}"
else
echo "$commit ratio of explanation is $ratio% ($lines_explaining lines / $changed_lines changes)"
fi
done
}
repository_commits()
{
if [ ! -d "${PWD}/.git" ]
then
_error "Not inside a Git repository. Type $0 -h"
fi
git log --oneline | awk '{print $1}'
}
diff_stat_of()
{
commit=$(echo_when_not_empty $@)
diff=$(git diff --stat $commit^ 2> /dev/null)
if [ $? -ne 0 ]
then
echo 1 # first commit has no diff
fi
echo $diff | tail -n1 | awk -F , '{print $2}' | sed 's/[^0-9]//g'
}
lines_from_commit()
{
commit=$(echo_when_not_empty $@)
git show --format="%s%n%n%b" --no-patch $commit | wc -l | sed 's/ //g'
}
# Usage: ratio_between <commit message lines> <commit additions>
ratio_between()
{
message_lines=$1
diff_additions=$2
# http://stackoverflow.com/a/20562313/447920
bc_program=$(cat <<-EOT
define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x };
scale=2;
b=(${message_lines}/${diff_additions})*100;
print trunc(b);
EOT
)
echo $bc_program | bc
}
# Usage: _error "Some warning"
_error () { echo "Error: $@" 1>&2; exit 2; }
# Usage: assert_not_empty <subject> [error message]
echo_when_not_empty() { test -z "$1" && _error "${error_msg:-'Argument is empty.'}" || echo $1; }
while getopts ":hvc" option
do
case $option in
v)
echo "$0 version 1.0.0"
exit 0
;;
c)
CSV_OUTPUT=1
;;
h)
cat <<-EOT
Usage: $0 [options]
Prints the ratio between the lines in commit messages and changes
made in the source code.
Options:
-v Displays version information.
-h This friendly help message.
-c Machine friendly output, CSV style.
Send bugs or suggestions to Augusto Pascutti <augusto.hp@gmail.com>
EOT
exit 0;
esac
done
_main
#vim: ft=shell noet ts=4 sw=4:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment