Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created November 18, 2025 23:20
Show Gist options
  • Select an option

  • Save jmcph4/063597e6548e8c27da4a7556b90774b8 to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/063597e6548e8c27da4a7556b90774b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
echo "Usage: attrib [COMMIT_HASH] [AUTHOR_NAME]"
echo
echo "Defaults:"
echo " COMMIT_HASH = git rev-parse HEAD"
echo " AUTHOR_NAME = jmcph4"
exit 0
fi
commit="${1:-$(git rev-parse HEAD)}"
user="${2:-jmcph4}"
tmpfile="$(mktemp)"
# List only blob paths (skip submodules/gitlinks), NUL-delimited
git ls-tree -r -z "$commit" \
| while IFS=$'\t' read -r -d '' meta path; do
# meta is like: "100644 blob <sha>"
# we only want entries where type == blob
set -- $meta
type=$2
if [ "$type" = "blob" ]; then
printf '%s\0' "$path"
fi
done \
| xargs -0 -n1 -P"$(nproc)" git blame --line-porcelain "$commit" -- \
| sed -n 's/^author //p' > "$tmpfile"
total=$(wc -l < "$tmpfile")
mine=$(grep -Fx -c "$user" "$tmpfile")
rm "$tmpfile"
awk -v m="$mine" -v t="$total" 'BEGIN{ if (t==0) print 0; else printf "%.2f%%\n", 100*m/t }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment