Skip to content

Instantly share code, notes, and snippets.

@e3krisztian
Created December 8, 2020 07:49
Show Gist options
  • Save e3krisztian/2ee4b8c1b4aba5db5178fd8b75508c8b to your computer and use it in GitHub Desktop.
Save e3krisztian/2ee4b8c1b4aba5db5178fd8b75508c8b to your computer and use it in GitHub Desktop.
fzf-git-hash
#!/bin/bash
KEEP_ONLY_GIT_HASH='sed "s/^[*|\\\\/ ]*\\([^ ]*\\) .*/\\1/"'
GIT_LOG=(
git log
--color=always
--graph
--pretty=format:'%Creset%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
--abbrev-commit
--date=relative
)
FZF=(
fzf
--layout=reverse
--no-sort
--ansi
--height=60%
--min-height=30
--preview='hash="$(echo {} | '"$KEEP_ONLY_GIT_HASH"')"; [ -z "$hash" ] || git show --color "$hash"'
)
"${GIT_LOG[@]}" | "${FZF[@]}" | eval "$KEEP_ONLY_GIT_HASH"
exit $?
# bash readline adapter:
_get_fzf_git_hash() {
local hash="$(bash fzf-git-hash.sh)"
# insert hash at cursor
READLINE_LINE="${READLINE_LINE:0:${READLINE_POINT}}${hash}${READLINE_LINE:${READLINE_POINT}}"
# move cursor to end of inserted hash
READLINE_POINT=$((READLINE_POINT + ${#hash}))
}
# bind hash selection to alt-h
bind -x '"\eh":_get_fzf_git_hash'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment