Skip to content

Instantly share code, notes, and snippets.

@tichopad
Created April 17, 2025 13:06
Show Gist options
  • Save tichopad/2f6e70a1ab3eb9ed40a5fa2806c89c17 to your computer and use it in GitHub Desktop.
Save tichopad/2f6e70a1ab3eb9ed40a5fa2806c89c17 to your computer and use it in GitHub Desktop.
Script to find top 5 files an author spent most time in based on git history.
#!/bin/bash
# Script to find top 5 files an author spent most time in based on git history
# Usage: ./top_files.sh -a "Author Name" -s "2024-01-01" -e "2025-04-10" -b "master" -n 5
# Default values
START_DATE="2024-01-01"
END_DATE="2025-04-10"
BRANCH="master"
AUTHOR=""
NUM_FILES=5
# Parse command line arguments
while getopts "a:s:e:b:n:h" opt; do
case $opt in
a) AUTHOR="$OPTARG" ;;
s) START_DATE="$OPTARG" ;;
e) END_DATE="$OPTARG" ;;
b) BRANCH="$OPTARG" ;;
n) NUM_FILES="$OPTARG" ;;
h)
echo "Usage: $0 -a \"Author Name\" [-s start_date] [-e end_date] [-b branch] [-n num_files]"
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 " -n: Number of top files to display (default: 5)"
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] [-n num_files]"
exit 1
fi
# Validate num_files is a positive integer
if ! [[ "$NUM_FILES" =~ ^[1-9][0-9]*$ ]]; then
echo "Error: Number of files must be a positive integer"
exit 1
fi
echo "Finding top $NUM_FILES files \"$AUTHOR\" spent most time in on branch $BRANCH from $START_DATE to $END_DATE..."
# Get all commits by the author in the given timeframe
git log --author="$AUTHOR" --since="$START_DATE" --until="$END_DATE" $BRANCH --name-only --pretty=format:"%H" |
grep -v "^[0-9a-f]\{40\}$" |
grep -v "^$" |
sort |
uniq -c |
sort -rn |
head -n $NUM_FILES |
awk '{
printf("%d. %-60s %d commits\n", NR, $2, $1)
}'
# Handle empty result
if [ $? -ne 0 ] || [ -z "$(git log --author="$AUTHOR" --since="$START_DATE" --until="$END_DATE" $BRANCH --pretty=format:%H | head -n 1)" ]; then
echo "No commits found for the specified author and time period."
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment