Skip to content

Instantly share code, notes, and snippets.

@zoolu-got-rhythm
Created November 21, 2023 17:49
Show Gist options
  • Save zoolu-got-rhythm/42720e19fc11fd112911d9b758018061 to your computer and use it in GitHub Desktop.
Save zoolu-got-rhythm/42720e19fc11fd112911d9b758018061 to your computer and use it in GitHub Desktop.
count number of commits for each author in a git repository
#!/opt/homebrew/Cellar/bash/5.2.21/bin/bash
# echo "running in shell: $(ps -p $$)"
# Check if a directory argument was provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Check if the provided argument is a directory
if [ ! -d "$1" ]; then
echo "Error: '$1' is not a valid directory."
exit 1
fi
# Perform operations on the provided directory
directory="$1"
absolute_path=$(realpath "$directory")
git_directory="$directory/.git"
if [ ! -d "$git_directory" ]; then
echo "'$absolute_path' is not a Git repository."
exit 1
fi
echo "Absolute path of '$directory' is: $absolute_path"
cd "$absolute_path"
# Initialize an associative array to store commit counts per author
declare -A commit_counts
# Iterate through all commits and count them per author
while read author; do
((commit_counts["$author"]++))
done < <(git log --format='%aN')
# Print the commit counts for each author
for author in "${!commit_counts[@]}"; do
echo "Author: $author, Commits: ${commit_counts[$author]}"
done
cd -
@zoolu-got-rhythm
Copy link
Author

some shell compatability issues with 'declare' keyword, most shell's don't seem to like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment