Skip to content

Instantly share code, notes, and snippets.

@PiRK
Last active October 25, 2020 05:23
Show Gist options
  • Save PiRK/68029212e42af6ce0a40704d698b15a7 to your computer and use it in GitHub Desktop.
Save PiRK/68029212e42af6ce0a40704d698b15a7 to your computer and use it in GitHub Desktop.
weekly statistics on a git repository
#!/usr/bin/env bash
# Print weekly statistics on a git repository.
echo Usage: weekly_report.sh path_to_git_project iso_week_number year
gitpath=$1
week=$2
year=$3
echo Statistics for git project $1
echo Report for week $week generated on `date --iso-8601`.
# Function weekof() takes an ISO 8601 week number and a year,
# and prints the dates of the monday and sunday corresponding
# to that week.
#
# Example:
# $ weekof 1 2020
# 2019-12-30 2020-01-05
function weekof()
{
local week=$1 year=$2
local week_num_of_Jan_1 week_day_of_Jan_1
local weeks_offset
local first_Mon
local date_fmt="+%Y-%m-%d"
local monday sunday
# %W produces a 0-based week number unless January 1st is a monday.
# This is not the same definition as the ISO 8601 definition:
# "week 01 is the week with the first Thursday of the Gregorian year in it"
week_num_of_Jan_1=$(date -d $year-01-01 +%W)
iso_week_num_of_Jan_1=$(date -d $year-01-01 +%V)
week_day_of_Jan_1=$(date -d $year-01-01 +%u)
if ((week_num_of_Jan_1)); then
first_Mon=$year-01-01
else
first_Mon=$year-01-$((01 + (7 - week_day_of_Jan_1 + 1) ))
fi
if [ ${week_num_of_Jan_1} == "01" ] || [ ${iso_week_num_of_Jan_1} != "01" ]; then
# The first monday is in ISO week 1
weeks_offset=$((week - 1))
else
# The first monday is in ISO week 2
weeks_offset=$((week - 2))
fi
monday=$(date -d "$first_Mon +${weeks_offset} week" "$date_fmt")
sunday=$(date -d "$first_Mon +${weeks_offset} week + 6 day" "$date_fmt")
echo $monday $sunday
}
start="`weekof $week $year | awk '{print $1}'` 00:00:00"
end="`weekof $week $year | awk '{print $2}'` 23:59:59"
weekof $week $year
echo Start date for report: $start
echo End date for report: $end
cd $gitpath
git checkout master
git pull
git log --numstat --pretty="%H" --since="$start" --until="$end" | awk 'NF==3 {plus+=$1; minus+=$2} NF==1 {total++} END {printf("lines added: +%d\nlines deleted: -%d\ntotal commits: %d\n", plus, minus, total)}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment