From d2e08436da58a4d8f4faa921b89440530355472a Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Fri, 7 Jun 2024 12:25:20 -0700 Subject: [PATCH] standalone and checkout works great now --- git-recent-with-fzf-and-diff | 55 ++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/git-recent-with-fzf-and-diff b/git-recent-with-fzf-and-diff index b2441f4..2d7cd05 100755 --- a/git-recent-with-fzf-and-diff +++ b/git-recent-with-fzf-and-diff @@ -1,33 +1,52 @@ #!/bin/bash -# git recent but with a FZF preview of the branch diff. -# run this somewhere: -# bind \cb git-recent-with-fzf-and-diff -# then do -# git checkout CTRL-B - +# +# git recent upgraded. lists unique commits to that branch. and the branch diff. +# +# run it: +# git recent-with-fzf-and-diff # or alias it +# then within it +# `ctrl-o` to show the diff of that branch against the origin/main (or origin/master) +# `enter` to git checkout that branch +# branches_format="%(color:yellow)%(refname:short)%(color:reset)" commits_format="%C(red bold)%h %C(bold blue)%an %C(bold green)%ad %Creset%s" -# This relies on my own git aliases. see .gitconfig -stat_cmd="git diff --color=always --stat origin/$(git mainormaster)...{}" -diffcommits_cmd="git log --date=human --color=always --format='$commits_format' --no-merges origin/$(git mainormaster)..{1}" -diffbranch_cmd="git diffbranch-that {} | delta" +mainormaster() { + git branch --format '%(refname:short)' --sort=-committerdate --list master main | head -n1 +} + +stat_cmd="git diff --color=always --stat origin/$(mainormaster)...{}" +diffcommits_cmd="git log --date=human --color=always --format='$commits_format' --no-merges origin/$(mainormaster)..{1}" +diffbranch_cmd="git diff origin/$(mainormaster)...{} | delta" # fzf basic inspiration: # - https://github.com/junegunn/fzf/wiki/Examples#git, https://github.com/junegunn/fzf/wiki/Examples-(fish)#git # Hardcore inspiration: # - https://github.com/junegunn/fzf-git.sh -git for-each-ref --color=always --sort=-committerdate "refs/heads/" --format="$branches_format" \ - | fzf-tmux --ansi -p80%,60% -- \ - --layout=reverse --multi --height=90% --min-height=20 \ - --border-label-pos=2 --border-label '🌲 Branches' --border \ - --no-hscroll --no-multi \ - --preview-window='right,70%,border-left,border-rounded' --preview="$diffcommits_cmd" --preview-label="Commits unique to branch" \ - --header $'CTRL-O (branch diff)\n\n' \ - --bind "ctrl-o:execute:$diffbranch_cmd" +_browse_branches() { + local preview_cmd="$diffcommits_cmd" # Default preview: commit log + + git for-each-ref --color=always --sort=-committerdate "refs/heads/" --format="$branches_format" \ + | fzf-tmux --ansi -p80%,60% -- \ + --layout=reverse --multi --height=90% --min-height=20 \ + --border-label-pos=2 --border-label '🌲 Branches' --border \ + --no-hscroll --no-multi \ + --preview-window='right,70%,border-left,border-rounded' --preview="$diffcommits_cmd" --preview-label="Commits unique to branch" \ + --header $'CTRL-O (show branch diff)\nENTER (checkout)\n' \ + --bind 'preview-scroll-up:preview-up+preview-up+preview-up' \ + --bind 'preview-scroll-down:preview-down+preview-down+preview-down' \ + --bind "ctrl-o:preview:$diffbranch_cmd" +} + + +chosen_branch="$(_browse_branches)" +if [[ -n "$chosen_branch" ]]; then + echo git checkout "$chosen_branch" + git checkout "$chosen_branch" +fi