Skip to content

Instantly share code, notes, and snippets.

@nicnocquee
Created December 19, 2023 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicnocquee/5fbd81eed0992dcc52eb534139c247ab to your computer and use it in GitHub Desktop.
Save nicnocquee/5fbd81eed0992dcc52eb534139c247ab to your computer and use it in GitHub Desktop.
Script to get git contribution stats
#!/bin/zsh
# Check if a repository path was provided
if [[ "$#" -ne 1 ]]; then
echo "Usage: $0 /path/to/git/repo"
exit 1
fi
# Assign the first argument to the variable 'repo_path'
repo_path="$1"
# Navigate to the specified Git repository directory
cd "$repo_path" || exit
# Optional: Specify the start date for filtering contributions
# Format: YYYY-MM-DD
# Uncomment and modify the line below to filter by date
since_date="--since=2023-01-01"
# Initialize associative arrays for author stats
typeset -A author_files
typeset -A author_insertions
typeset -A author_deletions
# Process each commit
git log --shortstat $since_date --pretty="%H|%an" | while read -r line; do
if [[ $line =~ '^[0-9a-f]{40}\|' ]]; then
current_author=${line#*|}
elif [[ $line =~ ' files changed' ]]; then
files_changed=$(echo $line | awk '{print $1}')
insertions=$(echo $line | awk '/insertions/ {print $4}')
deletions=$(echo $line | awk '/deletions/ {print $6}')
author_files[$current_author]=$((author_files[$current_author] + files_changed))
author_insertions[$current_author]=$((author_insertions[$current_author] + ${insertions:-0}))
author_deletions[$current_author]=$((author_deletions[$current_author] + ${deletions:-0}))
fi
done
# Output CSV header
echo "Author,Total Files Changed,Total Insertions,Total Deletions"
# Output the results in CSV format
for author in ${(k)author_files}; do
echo "\"$author\",${author_files[$author]},${author_insertions[$author]},${author_deletions[$author]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment