Skip to content

Instantly share code, notes, and snippets.

@pirobtumen
Last active February 18, 2023 10:06
Show Gist options
  • Save pirobtumen/b374857b72861a45bdd34578525fca05 to your computer and use it in GitHub Desktop.
Save pirobtumen/b374857b72861a45bdd34578525fca05 to your computer and use it in GitHub Desktop.
count project code lines over time with git
#!/bin/bash
# Copyright 2023 Alberto Sola (pirobtumen)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ----------------------------------------------------
# count-lines.sh
#
# simple script to count project source code lines
# over time in a git respository.
#
# output: data.csv [date, line count]
#
# WARNING: any changes not commited will be lost!
#
# https://www.pirobits.com/post/contar-el-numero-de-lineas-de-codigo-de-un-proyecto-con-git-y-bash
#
# ----------------------------------------------------
# ----------------------------------------------------
# Configure this variables for your project
# ----------------------------------------------------
# path to your project folder
PROJECT_DIR=~/Projects/your-project
# folder names to count lines: "src tests"
FOLDERS="."
# number of commits to explore
ITERATIONS=8
# git branch to use
GIT_BRANCH=main
# ----------------------------------------------------
CURRENT_DIR=$PWD
cd $PROJECT_DIR
pwd
git checkout $GIT_BRANCH
git pull origin $GIT_BRANCH
echo "count,date" > $CURRENT_DIR/data.csv
for (( i=1; i<=$ITERATIONS; i++ ))
do
echo -n `git ls-files $FOLDERS | xargs wc -l | tail -1 | awk '{ print $1 }'`, >> $CURRENT_DIR/data.csv
git show -s --format=%ci >> $CURRENT_DIR/data.csv
git checkout HEAD~1 &> /dev/null
done
git checkout $GIT_BRANCH
cd $CURRENT_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment