#!/bin/bash
# See https://stackoverflow.com/questions/25484745/combine-cloc-with-git-blame
# Thanks for the awesome script I adapted here!

LIST_OF_GIT_FILES="$cacheDir/$repo-gitfiles.txt"
GIT_BLAME_COMBINED_RESULTS="$cacheDir/$repo-git-blame.txt"
OUTPUT="$cacheDir/$repo-git-blame-output.txt"
SUMMARY="$statsDir/$repo-summary.csv"

git ls-files > "$LIST_OF_GIT_FILES"
while read p; do
  git blame -f $p >> "$GIT_BLAME_COMBINED_RESULTS"
done < $LIST_OF_GIT_FILES
awk -F ' ' '{print $2 "," $3}' "$GIT_BLAME_COMBINED_RESULTS" | tr -d '(' | awk -F ',' '{n = split($1, a, "."); print a[n] "," $2}' > $OUTPUT
sort $OUTPUT | uniq -c | sort -n | awk -F ' ' '{print $2 "," $1}' | sort > "$SUMMARY"

rm $GIT_BLAME_COMBINED_RESULTS
rm $LIST_OF_GIT_FILES
rm $OUTPUT