Skip to content

Instantly share code, notes, and snippets.

@tichopad
Created April 11, 2025 10:11
Show Gist options
  • Save tichopad/214ebf50e7ef1dae6a9107c4f9d194eb to your computer and use it in GitHub Desktop.
Save tichopad/214ebf50e7ef1dae6a9107c4f9d194eb to your computer and use it in GitHub Desktop.
Script to count total lines changed by a specific author in git history in a given time frame.
#!/bin/bash
# Script to count total lines changed by a specific author in git history
# Usage: ./count_lines_changed.sh -a "Author Name" -s "2024-01-01" -e "2025-04-10" -b "master"
# Default values
START_DATE="2024-01-01"
END_DATE="2025-04-10"
BRANCH="master"
AUTHOR=""
# Parse command line arguments
while getopts "a:s:e:b:h" opt; do
case $opt in
a) AUTHOR="$OPTARG" ;;
s) START_DATE="$OPTARG" ;;
e) END_DATE="$OPTARG" ;;
b) BRANCH="$OPTARG" ;;
h)
echo "Usage: $0 -a \"Author Name\" [-s start_date] [-e end_date] [-b branch]"
echo " -a: Author name (required)"
echo " -s: Start date (default: 2024-01-01)"
echo " -e: End date (default: 2025-04-10)"
echo " -b: Branch name (default: master)"
echo " -h: Show this help message"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Check if author is provided
if [ -z "$AUTHOR" ]; then
echo "Error: Author name is required"
echo "Usage: $0 -a \"Author Name\" [-s start_date] [-e end_date] [-b branch]"
exit 1
fi
echo "Counting lines changed by \"$AUTHOR\" on branch $BRANCH from $START_DATE to $END_DATE..."
# Get line changes statistics using the same precise command as git log
STATS=$(git log --author="$AUTHOR" --since="$START_DATE" --until="$END_DATE" $BRANCH --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { print add, subs, add + subs }')
ADDED=$(echo $STATS | cut -d' ' -f1)
REMOVED=$(echo $STATS | cut -d' ' -f2)
TOTAL=$(echo $STATS | cut -d' ' -f3)
# Handle empty result
if [ -z "$TOTAL" ]; then
echo "No changes found for the specified author and time period."
exit 0
fi
echo "Lines added: $ADDED"
echo "Lines removed: $REMOVED"
echo "Total changes: $TOTAL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment