Search code examples
gitshellcommit

Shell script to print Git activity by date


I'm trying to adapt this code snippet to go through a Git repo and print out by date the number of insertions, deletions and commits. My adaptation is as follows:

dates=$(git log --date=short --pretty=format:%ad | uniq)
IFS=$'\n'
echo -e "Date;Files changed;Lines added;Lines deleted;Total lines (delta);Add./Del. ratio (1:n);Commit count"

for date in $dates
do
     result=$(git log --no-merges --shortstat --since="$date" --before="$date" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "%s;%s;%s;%s;%s", files, inserted, deleted, delta, ratio }' -)
     countCommits=$(git shortlog -sn --no-merges --since="$date" --before="$date" | awk '{print $1}')
     if [[ ${result} != ';;;;' ]]
     then
        echo -e "$date;$result;$countCommits"
     fi
done

I don't however get any results. I think it's because git log considers the 'since' date to be non-inclusive (i.e. after), meaning I'm trying to get results for after (for example) 10-07-2022 and before the same date of 10-07-22 - which clearly doesn't make sense.

How do I amend the above script to output Git activity by date?


Solution

  • I think it's because git log considers the 'since' date to be non-inclusive (i.e. after), meaning I'm trying to get results for after (for example) 10-07-2022 and before the same date of 10-07-22 - which clearly doesn't make sense.

    Kinda close, this is one of Git's weirdest quirks. If you don't specify a time of day on a time spec, any time spec, it uses your current wallclock. The most easily-human-readable way I've found to fix it is to add e.g. midnight. in front of your timespecs,

    git log --no-merges --shortstat --since="00:00.$date" --before="23:59:60.$date"