# fshow - git commit browser (enter for show, ctrl-d for diff, ` toggles sort) | |
fshow() { | |
local out shas sha q k | |
while out=$( | |
git log --graph --color=always \ | |
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" | | |
fzf --ansi --multi --no-sort --reverse --query="$q" \ | |
--print-query --expect=ctrl-d --toggle-sort=\`); do | |
q=$(head -1 <<< "$out") | |
k=$(head -2 <<< "$out" | tail -1) | |
shas=$(sed '1,2d;s/^[^a-z0-9]*//;/^$/d' <<< "$out" | awk '{print $1}') | |
[ -z "$shas" ] && continue | |
if [ "$k" = ctrl-d ]; then | |
git diff --color=always $shas | less -R | |
else | |
for sha in $shas; do | |
git show --color=always $sha | less -R | |
done | |
fi | |
done | |
} |
Perhaps someone should turn this into a zsh plugin?
@melkster Actually there is a project called forgit, which does something similar based on fzf and is available as a zsh plugin.
@carlfriedrich Interesting, I'll check it out!
@melkster FYI I edited my post above with an updated version, adding a git-fuzzy-diff
command which shows a diff for each changed file separately.
I think one could plug this into forgit, if it would allow for customizing the preview command, which is not possible at the moment. I created an issue for that. If you are interested in that as well, maybe you could add a comment there to show that I'm not the only person who might want this. :-)
@Frederick888 I'll check it out later. My use-case is actually different , I'm looking to use fzf to find commit to fixup :).
git fixup <command>
I would then go to fzf and look for commit based on the title, enter would return hash.
How did you hook it up at the end? a fuzzy-find for fixup sounds awesome!
Thanks everybody for sharing your versions. All of this was so helpful!
After spending some hours of optimizing, I'd like to throw my version on the table as well. It adds the diff stats in the preview window, using a dimmed presentation:
When entering a commit, another fzf instance opens up containing a list of files changed in that commit, with the preview window showing the diff for that file:
Entering a file shows the diff on that file with its complete context.
The
git-fuzzy-diff
function detects whetherdiff-so-fancy
is installed and, if so, uses it.Furthermore I tried to split and structure the code a bit in order to not have just one huge intransparent command. I started off from @victorbrca's version and now it looks like this:
In my
gitconfig
I have set up aliases for it: