Skip to content

Instantly share code, notes, and snippets.

@gabesoft
Last active May 18, 2022 01:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabesoft/b6e5e959c4cb11ed257d41edb07d47cb to your computer and use it in GitHub Desktop.
Save gabesoft/b6e5e959c4cb11ed257d41edb07d47cb to your computer and use it in GitHub Desktop.
Fish function for browsing the git commit using fzf and diff-so-fancy
function gbr --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 % | diff-so-fancy | 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 --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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment