fish: config: Add helper functions for fzf + git

We can now use fzf to easily checkout a branch, commit or view logs.

Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
This commit is contained in:
Sanchayan Maity 2020-05-19 17:36:02 +05:30
parent df18cc7ec4
commit 61b3302a18

View file

@ -220,3 +220,38 @@ alias vdm="nvim (git diff master --name-only --diff-filter=ACMR)"
alias vdu="nvim (git diff --name-only --diff-filter=U | uniq)"
# Add unmerged files
alias vdua="git add (git diff --name-only --diff-filter=U)"
# Git fzf helper functions
# https://github.com/junegunn/fzf/wiki/Examples-(fish)
function fco -d "Fuzzy-find and checkout a branch"
git branch --all | grep -v HEAD | string trim | fzf | read -l result; and git checkout "$result"
end
function fcoc -d "Fuzzy-find and checkout a commit"
git log --pretty=oneline --abbrev-commit --reverse | fzf --tac +s -e | awk '{print $1;}' | read -l result; and git checkout "$result"
end
# https://gist.github.com/gabesoft/b6e5e959c4cb11ed257d41edb07d47cb
# Modify to use --exact flag for fzf and delta as pager
function fbr --description "Git browse commits"
set -l log_line_to_hash "echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
set -l view_commit "$log_line_to_hash | xargs -I % sh -c 'git show --color=always % | delta | less -R'"
set -l copy_commit_hash "$log_line_to_hash | xclip"
set -l git_checkout "$log_line_to_hash | xargs -I % sh -c 'git checkout %'"
set -l open_cmd "open"
if test (uname) = Linux
set open_cmd "xdg-open"
end
set github_open "$log_line_to_hash | xargs -I % sh -c '$open_cmd https://github.\$(git config remote.origin.url | cut -f2 -d. | tr \':\' /)/commit/%'"
git log --color=always --format='%C(auto)%h%d %s %C(green)%C(bold)%cr% C(blue)%an' | \
fzf --exact --no-sort --reverse --tiebreak=index --no-multi --ansi \
--preview="$view_commit" \
--header="ENTER to view, CTRL-Y to copy hash, CTRL-O to open on GitHub, CTRL-X to checkout, CTRL-C to exit" \
--bind "enter:execute:$view_commit" \
--bind "ctrl-y:execute:$copy_commit_hash" \
--bind "ctrl-x:execute:$git_checkout" \
--bind "ctrl-o:execute:$github_open"
end