Skip to content

Instantly share code, notes, and snippets.

@mfurquimdev
Last active December 7, 2023 21:33
Show Gist options
  • Save mfurquimdev/e048ac7f654eacb259d1208754dbdbb4 to your computer and use it in GitHub Desktop.
Save mfurquimdev/e048ac7f654eacb259d1208754dbdbb4 to your computer and use it in GitHub Desktop.
Check how many commits, files, and lines of code an author has changed

Git LOC

Define the AUTHOR_NAME as an environment variable before calling the script:

$ AUTHOR_NAME="João" ./git_loc.sh

The outputs will be separated into three files:

  • João.csv: File containing insertions, deletions, and file path
  • commits.list: File containing one commit id per line
  • files.list: File containing one modified file per line

To ignore specific file patterns, use multiple words.

$ AUTHOR_NAME="João" git_loc.sh yml py

The above command will ignore any file containing yml and py on the name.

#!/bin/bash
if [ "${AUTHOR_NAME}" == "" ]; then
AUTHOR_NAME="Mateus Furquim"
fi
OUTPUT_FILE="${AUTHOR_NAME// /_}.csv"
TMP_FILE="${OUTPUT_FILE%.csv}.tmp"
COMMITS_FILE="commits.list"
FILES_FILE="files.list"
IGNORE_FILES=("$@")
if [ "${#IGNORE_FILES[@]}" == 0 ]; then
IGNORE_FILES=("lock" "csv" "pkl" "txt" "html" "json" "dashboard")
fi
rm -f "${OUTPUT_FILE}"
rm -f "${TMP_FILE}"
rm -f "${COMMITS_FILE}"
rm -f "${FILES_FILE}"
function join_by {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
IGNORE_PATTERN="$(join_by '|' "${IGNORE_FILES[@]}")"
for GIT_ID in $(git rev-list --all --author="${AUTHOR_NAME}" --full-history); do
git diff-tree -t --numstat --ignore-cr-at-eol --ignore-space-at-eol --ignore-space-change --ignore-all-space --ignore-blank-lines "${GIT_ID}" |
awk -v COMMITS_FILE="${COMMITS_FILE}" 'NR==1{print $0 >> COMMITS_FILE; next}{print $0}' |
rg --invert-match "${IGNORE_PATTERN}" |
rg --regexp='^([0-9]*)\t([0-9]*)\t(.*)$' --replace='$1,$2,$3' --only-matching >>"${TMP_FILE}"
done
echo "ins,del,file" >"${OUTPUT_FILE}"
sort --unique --version-sort "${TMP_FILE}" >>"${OUTPUT_FILE}"
cut -f 3 -d ',' "${OUTPUT_FILE}" | sort --unique >"${FILES_FILE}"
rm -f "${TMP_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment