Skip to content

Instantly share code, notes, and snippets.

@kevsmith
Created December 30, 2019 17:06
Show Gist options
  • Save kevsmith/5623365e109e480de62d8d494df48498 to your computer and use it in GitHub Desktop.
Save kevsmith/5623365e109e480de62d8d494df48498 to your computer and use it in GitHub Desktop.
Retrieve historical LOC within a Git repo
> git history-cloc 6
2019-06-30 20f3e2cf16972f9fa1a20e7e936ce9009442bad3 45280
2019-07-31 3eeea800e5230e77de8bba90cf7e5c84dee549fe 47312
2019-08-31 b765ca0d2477b25efd2720fc064162f7cafbcc59 47442
2019-09-30 bcca0795db6d46839021e8d2c51a6f1902a9cc09 48977
2019-10-31 513d85eccdf7131dcfbecd827859414c98e18348 45672
2019-11-30 e6c775cff9359322b0db232dd028cd253c9fd3c0 47877
>
#!/bin/bash
scriptname=$(basename ${0})
os=$(uname -s)
function usage {
printf "\n%s [months]" ${scriptname}
printf "\nPrints lines of code at the end of the last N months.\n"
printf "Defaults to last 3 months. Maximum is 12 months.\n"
}
function past_date {
current="${1}"
interval="${2}"
result="$(date -j -v -${interval}m -f %m/%d/%Y ${current} +%m/%d/%Y)"
echo "$(date -j -v -1d -f %m/%d/%Y ${result} +%Y-%m-%d)"
}
function run_cloc {
commit=${1}
# Find top 5 most popular file extensions
exts="$(find . -type f -not -ipath "./.git/*" | sed -n 's/..*\.//p' | sort | uniq -c | sort -r | head -n5 | awk '{print $2}' | tr '\n' ',')"
${cloc} --git --quiet --hide-rate --skip-uniqueness --include-ext="${exts}" ${commit} 2>&1 | grep -v recursion | grep -o -E 'SUM:([0-9 ])+' | tr -su ' ' ',' | cut -d , -f5
}
cloc=$(which cloc)
if [ "${cloc}" == "" ]; then
if [ "${os}" == "Darwin" ]; then
pkgmgr="brew install"
else
pkgmgr="[apt-get install|pacman -S]"
fi
printf "Please install 'cloc'. Ex: \"%s cloc\"\n" "${pkgmgr}"
usage
exit 1
fi
count=""
if [ $# -gt 0 ]; then
case "${1}" in
"-h")
usage
exit 0
;;
"--help")
usage
exit 0
;;
"0")
count="3"
;;
"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"10"|"11"|"12")
count="${1}"
;;
*)
usage
exit 1
esac
fi
if [ "${count}" == "" ]; then
count="3"
fi
count=$(expr ${count} - 1)
current=$(date +%m/1/%Y)
dates=""
while [ ${count} -gt -1 ]
do
if [ "${dates}" == "" ]; then
dates="$(past_date ${current} ${count})"
else
dates="${dates} $(past_date ${current} ${count})"
fi
count=$(expr ${count} - 1)
done
for d in ${dates}
do
commit=$(git rev-list --until=\"${d}\" HEAD | head -n1)
clocs="$(run_cloc ${commit})"
printf "%s\t%s\t%d\n" ${d} ${commit} ${clocs}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment