Skip to content

Instantly share code, notes, and snippets.

@b-Tomas
Created August 11, 2025 18:55
Show Gist options
  • Save b-Tomas/e294c95ac9e93c31d5c821ca840f5b83 to your computer and use it in GitHub Desktop.
Save b-Tomas/e294c95ac9e93c31d5c821ca840f5b83 to your computer and use it in GitHub Desktop.
Count lines by author within a git repo or a subdirectory of it
#!/bin/bash
# Shows the number of lines authored by each author in the current repo or the specified subdirectory
lines-by-author() {
if ! command -v parallel >/dev/null 2>&1; then
echo "GNU parallel is not installed. Please install it to run this script."
exit 1
fi
# Default to the repo root if no path is provided
repo_root=$(git rev-parse --show-toplevel) || exit 1
if [ -z "$1" ]; then
path=$repo_root
else
path=$1
fi
# Print the directory of path relative to the repo root
rel_path=$(realpath --relative-to="$repo_root" "$path")
echo "Counting lines by author in $rel_path"
# list tracked files under $path, blame in parallel, tally authors
git ls-files -z -- "$path" |
parallel -0 --no-notice -j "$(nproc)" \
'git blame --line-porcelain {} 2>/dev/null' |
awk '
/^author / {
author = substr($0, 8)
counts[author]++
}
END {
for (a in counts)
printf "%d %s\n", counts[a], a
}
' |
sort -rn
}
lines-by-author "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment