#!/bin/sh -e
#
# git-bvv - enhanced `git branch -vv`. custom git command => git bvv
# for use in git config as subcommand
#
#   list branches sorted from most to least recent (by last commit date)
#   also display more details per each branch (last commit date and author)
#
# ---
# source: https://github.com/bluz71/dotfiles

_widest_branch_name=$(git branch --format='%(refname:short)' "$@" \
  | awk '{ print length() }' | sort -r -n | head -1)
_width=$((_widest_branch_name + 3))

_fmt="%(HEAD) \
%(align:${_width})%(color:green)%(refname:short)%(end) \
%(color:yellow)%(objectname:short)\
%(if)%(upstream)%(then) %(color:red)(%(upstream:short)%(color:reset)\
%(if)%(upstream:track)%(then): %(upstream:track,nobracket)\
%(end)%(color:red))\
%(end)\
%(color:reset) - \
%(color:green bold)(%(committerdate:relative)) \
%(color:reset)%(contents:subject) %(color:blue)[%(authorname)]%(color:reset)"

git branch -vv --sort=-committerdate --format="${_fmt}" "$@"
