[zsh] add function `git_current_branch`

This commit is contained in:
Dmytro Meleshko 2019-10-30 18:32:43 +02:00
parent a084fce534
commit 8f36c52cd8
2 changed files with 16 additions and 1 deletions

View File

@ -61,3 +61,18 @@ else
fi
eval "clipcopy(){$copy_cmd;};clippaste(){$paste_cmd;}"
unset copy_cmd paste_cmd
# for compatibility with Oh-My-Zsh plugins
# Source: https://github.com/robbyrussell/oh-my-zsh/blob/5911aea46c71a2bcc6e7c92e5bebebf77b962233/lib/git.zsh#L58-L71
git_current_branch() {
if [[ "$(command git rev-parse --is-inside-work-tree)" != true ]]; then
return 1
fi
local ref
ref="$(
command git symbolic-ref --quiet HEAD 2> /dev/null ||
command git rev-parse --short HEAD 2> /dev/null
)" || return
echo "${ref#refs/heads/}"
}

View File

@ -42,7 +42,7 @@ prompt_vcs_info() {
fi
local branch="(no branches)" line
git branch | while IFS= read -r line; do
command git branch | while IFS= read -r line; do
# find a line which starts with `* `, it contains the current branch name
if [[ "$line" == "* "* ]]; then
# remove the `* ` prefix