├── README.md └── code-age.sh /README.md: -------------------------------------------------------------------------------- 1 | # code-age 2 | 3 | This is a script that calculates the average age of your code lines using `git blame` and math. 4 | 5 | ## Installation 6 | 7 | Clone this repo somewhere on your computer: 8 | 9 | ``` 10 | git clone git@github.com:draperunner/code-age.git 11 | ``` 12 | 13 | Create a symbolic link to the `code-age.sh` to some bin folder that's on your path. 14 | 15 | ``` 16 | cd /usr/local/bin 17 | ln -s /path/to/projects/code-age/code-age.sh code-age 18 | ``` 19 | 20 | Now you can run `code-age` anywhere you'd like. Keep it updated by running `git pull` in the repo. 21 | -------------------------------------------------------------------------------- /code-age.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [[ ! -d .git ]]; then 4 | echo "This is not a git directory!" 5 | exit 1 6 | fi 7 | 8 | NOW=$(date +"%s") 9 | SEC_IN_DAY=86400 10 | DAYS_IN_YEAR=365 11 | 12 | TOTAL_AGE_SEC=0 13 | TOTAL_LINES=0 14 | 15 | CURRENT_FILE=1 16 | TOTAL_FILES=$(git ls-files | wc -l) 17 | 18 | echo "Calculating code age ..." 19 | 20 | for FILE in $(git ls-files); do 21 | PERC=$((100 * CURRENT_FILE / TOTAL_FILES)) 22 | echo -e "\e[1A\e[K$PERC %\t$FILE" 23 | 24 | for LINE in $(git blame "$FILE" --date=unix -c | awk -v FS='\t' '{ print $3 }'); do 25 | AGE_SEC=$((NOW - LINE)) 26 | TOTAL_AGE_SEC=$((TOTAL_AGE_SEC + AGE_SEC)) 27 | TOTAL_LINES=$((TOTAL_LINES + 1)) 28 | done 29 | 30 | CURRENT_FILE=$((CURRENT_FILE + 1)) 31 | done 32 | 33 | AVG_AGE_SEC=$((TOTAL_AGE_SEC / TOTAL_LINES)) 34 | AVG_AGE_DAYS=$((AVG_AGE_SEC / SEC_IN_DAY)) 35 | AVG_AGE_YEARS=$((AVG_AGE_DAYS / DAYS_IN_YEAR)) 36 | 37 | if [ $AVG_AGE_YEARS == 0 ]; then 38 | echo "$AVG_AGE_DAYS days" 39 | else 40 | DAYS_REMAINDER="$((AVG_AGE_DAYS - $((AVG_AGE_YEARS * DAYS_IN_YEAR))))" 41 | echo "$AVG_AGE_YEARS years, $DAYS_REMAINDER days" 42 | fi 43 | --------------------------------------------------------------------------------