unixorn/git-extra-commands

View on GitHub
bin/git-commit-browser

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash
#
# Browse git commits. From https://github.com/junegunn/fzf/wiki/examples

set -o pipefail
if [[ -n "$DEBUG" ]]; then
  set -x
fi

function fail() {
  printf '%s\n' "$1" >&2  ## Send message to stderr. Exclude >&2 if you don't want it that way.
  exit "${2-1}"  ## Return a code specified by $2 or 1 by default.
}

function has() {
  which "$@" > /dev/null 2>&1
}

# fshow - git commit browser
fshow() {
  git log --graph --color=always \
      --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
  fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
      --bind "ctrl-m:execute:
                (grep -o '[a-f0-9]\{7\}' | head -1 |
                xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
                {}
FZF-EOF"
}

if has fzf; then
  fshow "$@"
else
  myname=$(basename "$0")
  fail "$myname requires fzf, but can't find it in your PATH."
fi