#!/bin/bash

##
## git-recent
##
## list all local branches, sorted by last commit, formatted reall purdy
##

# Windows needs more basic format (#8, git-for-windows/git#865)
case $(uname -s) in
  CYGWIN*|MINGW32*|MSYS*)
    branch='%(refname:short)'
    spacer=' '
    ;;
  *)
    branch='%(color:yellow)%(refname:short)%(color:reset)'
    spacer='%(color:black) %(color:reset)'
    ;;
esac

format="\
%(HEAD) \
$branch|\
%(color:bold red)%(objectname:short)%(color:reset) \
%(color:bold green)(%(committerdate:relative))%(color:reset) \
%(color:bold blue)%(authorname)%(color:reset) \
%(color:yellow)%(upstream:track)%(color:reset)
$spacer|\
%(contents:subject)
$spacer|"

lessopts="--tabs=4 --quit-if-one-screen --RAW-CONTROL-CHARS --no-init"

git for-each-ref \
    --color=always \
    --sort=-committerdate \
    "refs/heads/"  \
    --format="$format" \
  | column -ts '|'  \
  | less "$lessopts"

# The above command:
#   for all known branches,
#   (force coloring on this, especially since it's being piped)
#   sort descending by last commit
#   show local branches (change to "" to include both local + remote branches)
#   apply the formatting template above
#   break into columns
#   use the pager only if there's not enough space
