Skip to content

Instantly share code, notes, and snippets.

@strix
Created September 16, 2020 20:59
Show Gist options
  • Select an option

  • Save strix/47db85aa4e9403a253428b2cec38fd7c to your computer and use it in GitHub Desktop.

Select an option

Save strix/47db85aa4e9403a253428b2cec38fd7c to your computer and use it in GitHub Desktop.
Better git log and git stash fuzzy searching
# Based off examples from https://gist.github.com/junegunn/f4fca918e937e6bf5bad
# git log show with fzf
gli () {
# param validation
if [[ ! `git log -n 1 $@ | head -n 1` ]] ;then
return
fi
# filter by file string
local filter
# param existed, git log for file if existed
if [ -n $@ ] && [ -f $@ ]; then
filter="-- $@"
fi
# git command
local gitlog=(
git log
--graph --color=always
--abbrev=7
--format='%C(auto)%h %an %C(blue)%s %C(yellow)%cr'
$@
)
# fzf command
local fzf=(
fzf
--ansi --no-sort --reverse --tiebreak=index
--preview "f() { set -- \$(echo -- \$@ | grep -o '[a-f0-9]\{7\}'); [ \$# -eq 0 ] || git show --color=always \$1 $filter; }; f {}"
--bind "j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up,ctrl-q:abort,ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % $filter | less -R') << 'FZF-EOF'
{}
FZF-EOF"
--preview-window=right:60%
)
# piping them
$gitlog | $fzf
}
# git stash show with fzf
gls () {
git stash list --color=always --format="%C(yellow)%C(bold)%gd%C(reset) %s %C(black)%C(bold)%cr" | \
fzf --ansi --no-sort --reverse --tiebreak=index --preview \
'f() { set -- $(echo -- "$@" | grep -o "stash@{[0-9]\+}"); [ $# -eq 0 ] || git stash show -p --color=always $1 ; }; f {}' \
--bind "j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up,ctrl-q:abort,ctrl-m:execute:
(grep -o 'stash@{[0-9]\+}' | head -1 |
xargs -I % sh -c 'git stash show -p --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF" --preview-window=right:60%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment