#!/bin/sh -e
#
# git-browse - git commit browser with fzf
# dependencies: delta | fzf

_gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
_viewGitLogLine="${_gitLogLineToHash} \
  | xargs -I % sh -c 'git show --color=always % | delta'"
_viewGitLogLineUnfancy="${_gitLogLineToHash} | xargs -I % sh -c 'git show %'"

git log --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an' "$@" \
  | fzf --no-sort --reverse --tiebreak=index --no-multi \
    --ansi --preview="${_viewGitLogLine}" \
    --header='enter: view, ctrl-e: edit, ctrl-y: copy hash' \
    --bind="enter:execute:${_viewGitLogLine} | less -R" \
    --bind="ctrl-e:execute:${_viewGitLogLineUnfancy} | v -" \
    --bind="ctrl-y:execute:${_gitLogLineToHash} | pbcopy" \
    --bind='?:toggle-preview' \
    --preview-window='right,50%'
