Skip to content

Instantly share code, notes, and snippets.

@kaimingguo
Created November 20, 2019 08:56
Show Gist options
  • Save kaimingguo/f56a6977326390a8345fbff299ba5314 to your computer and use it in GitHub Desktop.
Save kaimingguo/f56a6977326390a8345fbff299ba5314 to your computer and use it in GitHub Desktop.
# add aliases to use git-fzf method
alias git-fzf 'set arg1 = `echo \!:1* | awk '"'"'{ print $1 }'"'"'`; \\
bash -c "source ~/.config/bash/plugins/git-fzf.bash && __git_fzf_bash::$arg1" ; \\
'
#!/usr/bin/env bash
# usage:
# - in bash: ./git-fzf [args]
# - add tcsh alias: git-fzf [args]
__git_fzf_bash::info() { printf "%b[info]%b %s\n" '\e[0;32m' '\e[0m' "$@" >&2; }
__git_fzf_bash::warn() { printf "%b[warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
__git_fzf_bash::in_repo() { git rev-parse --is-inside-work-tree >/dev/null; }
hash fzf &> /dev/null || { __git_fzf_bash::warn "FZF command required"; exit 2; }
git_fzf_pager=$(git config core.pager || echo 'cat')
__git_fzf_bash::log() {
__git_fzf_bash::in_repo || return 1
local cmd
local opts
cmd="echo {} | grep -Eo '[a-f0-9]+' | head -1 | xargs -I% git show --color=always % $* | $git_fzf_pager"
opts="
$GIT_FZF_DEFAULT_OPTS
+s +m --tiebreak=index --preview=\"$cmd\"
--bind=\"enter:execute($cmd | LESS='-R' less)\"
--bind=\"ctrl-y:execute-silent(echo {} | grep -Eo '[a-f0-9]+' | lead -1 | tr -d '\n' | pbcopy)\"
"
eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $*" |
FZF_DEFAULT_OPTS="$opts" fzf
}
__git_fzf_bash::diff() {
__git_fzf_bash::in_repo || return 1
}
__git_fzf_bash::usage() {
__git_fzf_bash::in_repo || return 1
}
git_fzf() {
local args=${1:-}
if [[ -z $args ]]; then
__git_fzf_bash::usage
return
fi
type __git_fzf_bash::$args &>/dev/null && __git_fzf_bash::$args || __git_fzf_bash::usage
}
GIT_FZF_DEFAULT_OPTS="
$FZF_DEFAULT_OPS
--ansi
--height='80%'
--preview-window='right:60%'
$GIT_FZF_DEFAULT_OPTS
"
git_fzf "$1"
@kaimingguo
Copy link
Author

Idea from: forgit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment