Skip to content

Instantly share code, notes, and snippets.

@dariotilgner
Last active August 11, 2023 04:27
Show Gist options
  • Save dariotilgner/3f2123afee87bf4b296bf7ad9bca662a to your computer and use it in GitHub Desktop.
Save dariotilgner/3f2123afee87bf4b296bf7ad9bca662a to your computer and use it in GitHub Desktop.
Git Commit Stats
#!/bin/bash
FILTER_VALUE=${1:-0}
SUSPICIOUS_THRESHOLD=${2:-20}
GITHUB_REPO_URL="${GITHUB_REPO_URL:-https://github.com/DUMMY-USER/DUMMY-REPO}"
display_help() {
echo "Usage: $0 [FILTER_VALUE] [SUSPICIOUS_THRESHOLD]"
echo
echo "List commits and display changes in terms of lines added and lines removed."
echo " FILTER_VALUE Display only commits with more than this number of changes. Default is 0."
echo " SUSPICIOUS_THRESHOLD Mark commits with more than this number of changes as suspicious. Default is 20."
echo
echo "Environment variables:"
echo " GITHUB_REPO_URL Set the base URL for the commits. If not set, a dummy URL is used."
exit 1
}
if [[ "$1" == "--help" ]]; then
display_help
fi
git pull
# Execute git log to get each commit and the number of lines added/removed
git log HEAD --numstat --pretty=format:"Commit: %h %ad" --date=format:'%Y-%m-%d %H:%M:%S' | awk -v filter="$FILTER_VALUE" -v suspicious_threshold="$SUSPICIOUS_THRESHOLD" -v repo_url="$GITHUB_REPO_URL" -v alert_emoji="🚨" '
BEGIN {
# ANSI color codes
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
RESET="\033[0m"
print "Listing commits with more than", filter, "line changes:"
}
/^Commit:/ {
commit=$2
date=$3" "$4
added=0
deleted=0
next
}
{
added+=$1
deleted+=$2
}
!NF {
if (added+deleted > filter) {
suspicious = (added + deleted > suspicious_threshold) ? YELLOW alert_emoji " suspicious" RESET : " "
print "Commit:", commit, "- Date:", date, "- Added:", GREEN "+" added RESET, "- Deleted:", RED "-" deleted RESET, "- Link:", repo_url "/commit/" commit, "- Status:", suspicious
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment