Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created June 23, 2020 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/dd7efaa5352b31e8e2997779d407712b to your computer and use it in GitHub Desktop.
Save zekroTJA/dd7efaa5352b31e8e2997779d407712b to your computer and use it in GitHub Desktop.
Script to walk through a git repository commit by commit and count the lines of code.
#!/bin/bash
REPO_URL=$1
BRANCH=$2
TMP_DIR="./.tmp"
CLOC_EXCLUDED_DIRS="node_modules,vendor"
CLOC_EXCLUDED_LANGS="JSON,Markdown,YAML,SVG,SQL,Dockerfile,make"
OUT_FILE="./results.csv"
which cloc > /dev/null 2>&1 || {
echo "This script uses cloc to count lines of code. Please install cloc to continue."
echo "https://github.com/AlDanial/cloc"
exit
}
which jq > /dev/null 2>&1 || {
echo "This script uses jq to analyze the json output from cloc. Please install jq to continue."
echo "https://github.com/stedolan/jq"
exit
}
[ -z $REPO_URL ] && {
echo "Please specify the repository URL as first parameter."
exit 1
}
[ -z $BRNACH ] && {
BRANCH="master"
}
# ---------------------------------
[ -d $TMP_DIR ] && rm -rf $TMP_DIR
git clone --branch $BRANCH $REPO_URL $TMP_DIR
echo "COMMIT_N,COMMIT_HASH,CODE_LINES,COMMENT_LINES,FILES" > $OUT_FILE
N_COMMITS=$(git -C $TMP_DIR rev-list origin/$BRANCH --count)
I=1
for COMMIT in $(git -C $TMP_DIR rev-list origin/$BRANCH --reverse); do
git -C $TMP_DIR checkout $COMMIT > /dev/null 2>&1
echo "[$I/$N_COMMITS] $COMMIT"
CLOC_RES=$(cloc $TMP_DIR \
--exclude-dir=$CLOC_EXCLUDED_DIRS \
--exclude-lang=$CLOC_EXCLUDED_LANGS \
--json | jq '.SUM')
N_CODE=$(echo $CLOC_RES | jq ".code")
N_COMMENT=$(echo $CLOC_RES | jq ".comment")
N_FILES=$(echo $CLOC_RES | jq ".nFiles")
echo "$I,$COMMIT,$N_CODE,$N_COMMENT,$N_FILES" >> $OUT_FILE
I=$(( $I + 1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment