Created
January 21, 2025 01:59
-
-
Save refraction-ray/ef52b3301a5c86a3e6a114ca01c6945c to your computer and use it in GitHub Desktop.
This script can summarize contributions of each author on given filename extension (defaults .py)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# the script is fully created by Google Gemini | |
# Function to display usage information | |
usage() { | |
echo "Usage: $0 [file_extension]" | |
echo " [file_extension] - Optional file extension to filter by (default: .py)" | |
exit 1 | |
} | |
# Default file extension | |
file_extension=".py" | |
# Check if an argument is provided and is not an option | |
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then | |
file_extension="$1" | |
shift # Shift arguments to remove the processed file extension | |
fi | |
# Check for any unexpected additional arguments | |
if [[ $# -gt 0 ]]; then | |
echo "Error: Invalid argument(s) provided." | |
usage | |
fi | |
# Get a sorted list of unique author names | |
author_names=$(git log --pretty=format:"%an" | sort -u) | |
# Check if there are any authors | |
if [[ -z "$author_names" ]]; then | |
echo "No authors found in the commit history." | |
exit 0 | |
fi | |
# Initialize total counters | |
total_added_lines=0 | |
total_removed_lines=0 | |
total_changed_lines=0 | |
# Create a temporary file to store author stats | |
stats_file=$(mktemp) | |
# Iterate through each author to get their individual stats and accumulate total stats | |
while IFS= read -r author_name; do | |
# Get numstat output for this author, for the specified file extension | |
stats=$(git log --author="$author_name" --pretty=tformat: --numstat --textconv -- "*$file_extension") | |
# Check if git log command was successful | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Failed to retrieve git log for author '$author_name'." | |
continue | |
fi | |
# Initialize counters for the author | |
added_lines=0 | |
removed_lines=0 | |
# Process the numstat output | |
while IFS=$'\t' read -r added removed file; do | |
if [[ "$file" == *"$file_extension" ]]; then | |
if [[ -n "$added" ]]; then | |
added_lines=$((added_lines + added)) | |
fi | |
if [[ -n "$removed" ]]; then | |
removed_lines=$((removed_lines + removed)) | |
fi | |
fi | |
done <<< "$stats" | |
# Store author stats in the temporary file | |
echo "$author_name,$added_lines,$removed_lines" >> "$stats_file" | |
# Accumulate total stats | |
total_added_lines=$((total_added_lines + added_lines)) | |
total_removed_lines=$((total_removed_lines + removed_lines)) | |
total_changed_lines=$((total_added_lines + removed_lines)) | |
done <<< "$author_names" | |
# Check if there are any changes | |
if [[ $total_changed_lines -eq 0 ]]; then | |
echo "No changes found for files with extension '$file_extension' in the commit history." | |
rm "$stats_file" # Clean up temporary file | |
exit 0 | |
fi | |
# Iterate through each author to print stats and calculate percentage | |
while IFS=',' read -r author_name added_lines removed_lines; do | |
changed_lines=$((added_lines + removed_lines)) | |
# Calculate percentage | |
if (( total_changed_lines > 0 )); then | |
percentage=$(awk "BEGIN {printf \"%.2f\", ($changed_lines*100/$total_changed_lines)}") | |
else | |
percentage=0 | |
fi | |
# Print the results for the author | |
echo "Author: $author_name" | |
echo " Added lines: $added_lines" | |
echo " Removed lines: $removed_lines" | |
echo " Total changed lines: $changed_lines" | |
echo " Contribution Percentage: $percentage%" | |
echo "" | |
done < "$stats_file" | |
# Print overall total stats | |
echo "Overall Total Added Lines: $total_added_lines" | |
echo "Overall Total Removed Lines: $total_removed_lines" | |
echo "Overall Total Changed Lines: $total_changed_lines" | |
# Clean up temporary file | |
rm "$stats_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment