Skip to content

Instantly share code, notes, and snippets.

@hchbaw
Created March 2, 2011 14:24
Show Gist options
  • Save hchbaw/851005 to your computer and use it in GitHub Desktop.
Save hchbaw/851005 to your computer and use it in GitHub Desktop.
# zaw.zsh source for 'git show-branch'.
# Thank you very much, nakamuray!
# https://github.com/nakamuray/zaw
zaw-src-git-showbranch () {
git rev-parse -q --is-inside-work-tree >/dev/null || return 1
#local b="${orig_lbuffer}${orig_rbuffer}"
local b="$BUFFER"
local z="$(git rev-parse --git-dir)""/zaw-src-git-showbranch"
{
zaw-src-git-showbranch~ "$b" "$z" >| ${z}.tmp || return 1
} always {
mv -f ${z}.tmp ${z}
}
: ${(A)candidates::=${(@f)"$(<"${z}")"}}
[[ -n "$b" ]] || {
BUFFER=$candidates[1]
(( $+functions[_zsh_highlight] )) && _zsh_highlight
}
shift candidates
actions=(zaw-callback-git-showbranch)
act_descriptions=("setup edit buffer")
options+=(-m)
}
zaw-src-git-showbranch~ () {
local gb="$1"
local zf="$2"
setopt localoptions extendedglob
case "$gb" in
([[:space:]]#'git show-branch '*)
echo "$gb"; "${(z)gb}"; return $?
;;
([[:space:]]##)
echo " "; git show-branch; return $?
;;
esac
[[ -s "$zf" ]] && { "$0" "$(head -n1 $zf)" "$zf"; return $? }
return -1
}
zaw-callback-git-showbranch () {
setopt localoptions extendedglob
local -a commitish
local line=; for line in ${@[@]}; do
zaw-callback-git-showbranch-accum "$line" "" commitish
done
zaw-callback-git-showbranch~ "${commitish[@]}"
}
zaw-callback-git-showbranch-accum () {
local b="$1"
local a="$2"
local p="$3"
local -a match mbegin mend
[[ "$b" == (#b)(*)\[(*)\]* ]] &&
{ "$0" "$match[1]" "$match[2]" "$p" ; return $? }
[[ -n "$a" ]] && {
local -a tmp; : ${(A)tmp::=${(PA)p}}
tmp+="$a"; : ${(PA)p::=${(A)tmp}}; return $?
}
return -1
}
zaw-callback-git-showbranch~ () {
BUFFER="noglob zargs -- $@ -- git"
((CURSOR=$#BUFFER))
}
zaw-register-src -n git-showbranch zaw-src-git-showbranch
#print -z 'git show-branch master next pu'
#zaw-src-git-showbranch~ "" .git/zaw-src-git-showbranch ; echo $?
@hchbaw
Copy link
Author

hchbaw commented Mar 2, 2011

Hello, git show-branch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment