#!/usr/bin/env sh
# Show a summary of the commits, authors, and files in a Git repo
#
# Requires ~/bin/transpose
#
# Omit file stats with `-f` for a substantially faster processing time for
# large repositories. Can also summarize subsets of just files or paths by
# passing those as additional `git log` arguments.
#
# Usage:
#
#     git summary
#     git summary -f
#     git summary -- ./path/to/file

show_files=1
while getopts hf opt; do
    case $opt in
    h) awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
       exit ;;
    f) unset show_files;;
    esac
done
shift $(( OPTIND - 1 ))

test -t 1 && show_progress=1

# %x09 is a tab character
git log --no-renames ${show_files:+--numstat} \
        --format='XXX%x09%h%x09%an%x09%ae%x09%cn%x09%ce%x09%cD%x09%cr' "$@" \
    | awk -v show_progress="$show_progress" '
    function alen(a, i, c) { for (i in a) c += 1; return c; }
    function isnum(x){ return (x == x + 0) }
    function max(n, m) { return n >= m ? n : m; }

    BEGIN { FS="\t"; OFS="\t" }

    /^$/ { next }

    /^XXX/ {
        sha=$2
        author=$3 " <" $4 ">"
        committer=$5 " <" $6 ">"
        reldate=$8

        split($7, cal_segs, " ")

        weekday=cal_segs[1]
        weekday=substr(weekday, 1, length(weekday) - 1)

        day=cal_segs[2]
        month=cal_segs[3]
        year=cal_segs[4]

        hour=cal_segs[5]
        hour=substr(hour, 1, index(hour, ":") - 1)

        tz=cal_segs[6]

        auth_date=month ", " day " " year

        commit_count += 1
        author_list[author] += 1
        committer_list[committer] += 1
        active_days[auth_date] = 1
        top_hours[hour] += 1
        top_weekdays[weekday] += 1

        if (show_progress) {
            printf("\033[2K\033[1GCommits processed: %s", commit_count)
        }
    }

    NR == 1 { last_date=auth_date; last_reldate=reldate; last_sha=sha }

    !/^XXX/ {
        added=isnum($1) ? $1 : 0
        removed=isnum($2) ? $2 : 0
        filename=$3

        file_list[filename] += 1
        lines_changed_count += added - removed
    }

    END {
        # No Git output? (Probably an error.)
        if (NR == 0) { exit 1 }

        if (show_progress) { printf("\033[2K\033[1G") }

        commit_str = "%s:\t%s - %s (%s)\n"
        printf(commit_str commit_str "\n",
            "First Commit", sha, auth_date, reldate,
            "Last Commit", last_sha, last_date, last_reldate)

        summary_cmd = "transpose"
        print "Commits", commit_count | summary_cmd
        print "Files", alen(file_list) | summary_cmd
        print "Lines", lines_changed_count | summary_cmd
        print "Authors", alen(author_list) | summary_cmd
        print "Committers", alen(committer_list) | summary_cmd
        print "Active Days", alen(active_days) | summary_cmd
        close(summary_cmd)

        printf("\nTop days:\n\n")
        weekday_cmd = "sort -n -r | cut -f2,3 | transpose"
        for (h in top_weekdays) {
            wd = top_weekdays[h]
            printf("%s\t%s\t%.1f%%\n", wd, h, (wd / commit_count) * 100 ) \
            | weekday_cmd
        }
        close(weekday_cmd)

        printf("\nTop hours:\n\n")
        hour_cmd = "sort -n -r | cut -f2,3 | head -12 | transpose"
        for (i in top_hours) {
            hr = top_hours[i]
            printf("%s\t%s\t%.0f%%\n", hr, i, (hr / commit_count) * 100 ) \
            | hour_cmd
        }
        close(hour_cmd)

        printf("\nTop authors:\n\n")
        cmd = "sort -n -r | head -5"
        for (j in author_list) {
            printf("%s\t%*.1f%%\t%s\n",
                author_list[j],
                5, (author_list[j] / commit_count) * 100,
                j) | cmd
        }
        close(cmd)

        printf("\nTop committers:\n\n")
        cmd = "sort -n -r | head -5"
        for (l in committer_list) {
            printf("%s\t%*.1f%%\t%s\n",
                committer_list[l],
                5, (committer_list[l] / commit_count) * 100,
                l) | cmd
        }
        close(cmd)

        if (alen(file_list)) {
            cmd = "sort -n -r | head -5"
            printf("\nTop files:\n\n")
            for (k in file_list) {
                print file_list[k], k | cmd
            }
            close(cmd)
        }
    }
    '
