#!/bin/bash
#
# Log output:
#
# * 51c333e    (12 days)    <Gary Bernhardt>   add vim-eunuch
#
# The time massaging regexes start with ^[^<]* because that ensures that they
# only operate before the first "<". That "<" will be the beginning of the
# author name, ensuring that we don't destroy anything in the commit message
# that looks like time.

HASH="%C(auto)%h%Creset"
RELATIVE_TIME="%Cgreen(%ar)%Creset"
AUTHOR="%C(bold blue)<%an>%Creset"
REFS="%C(auto)%d%Creset"
SUBJECT="%s"

# ASCII Field Separator
# FS=$'\x1d'
# FORMAT="$HASH$FS$RELATIVE_TIME$FS$AUTHOR$FS$REFS $SUBJECT"
FORMAT="$HASH}$RELATIVE_TIME}$AUTHOR}$REFS $SUBJECT"

show_git_head() {
  pretty_git_log -1
  git show --patch --pretty="tformat:"
}

pretty_git_log() {
  git log --color=always --graph --pretty="tformat:${FORMAT}" $* |
    # Replace (2 years ago) with (2 years)
    sed -Ee 's/(^[^<]*) ago\)/\1)/' |
    # Replace (2 years, 5 months) with (2 years)
    sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?\)/\1)/' |
    # Line columns up based on } delimiter
    # column -s "$FS" -t |
    column -s "}" -t |
    # Page only if we need to
    less -FXRS
}
