From 2d40cc0bb315a92368e35c15c92f955bd901c7c3 Mon Sep 17 00:00:00 2001 From: Adam Lindberg Date: Wed, 16 Apr 2014 10:34:45 +0200 Subject: [PATCH] Add git_prompt_behind and git_prompt_exists This adds two new theme functions for git: * `git_prompt_behind` works in a identical fashion to `git_prompt_ahead` and will output a format variable (`ZSH_THEME_GIT_PROMPT_BEHIND`) if the branch is behind. * `git_prompt_remote` will output one format variable if the branch exists on remote (`ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS`), and another if the branch is unpublished (`ZSH_THEME_GIT_PROMPT_REMOTE_MISSING`). The old `git_prompt_ahead` has been changed. Using git log is subject to formatting in .gitconfig, which can be overridden and will break this function. Relying on rev-list is much more stable. --- lib/git.zsh | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/git.zsh b/lib/git.zsh index 733492ba..caa7e632 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -59,13 +59,6 @@ git_remote_status() { fi } -# Checks if there are commits ahead from remote -function git_prompt_ahead() { - if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then - echo "$ZSH_THEME_GIT_PROMPT_AHEAD" - fi -} - # Gets the number of commits ahead from remote function git_commits_ahead() { if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then @@ -74,6 +67,29 @@ function git_commits_ahead() { fi } +# Outputs if current branch is ahead of remote +function git_prompt_ahead() { + if [[ -n "$(command git rev-list origin/$(current_branch)..HEAD 2> /dev/null)" ]]; then + echo "$ZSH_THEME_GIT_PROMPT_AHEAD" + fi +} + +# Outputs if current branch is behind remote +function git_prompt_behind() { + if [[ -n "$(command git rev-list HEAD..origin/$(current_branch) 2> /dev/null)" ]]; then + echo "$ZSH_THEME_GIT_PROMPT_BEHIND" + fi +} + +# Outputs if current branch exists on remote or not +function git_prompt_remote() { + if [[ -n "$(command git show-ref origin/$(current_branch) 2> /dev/null)" ]]; then + echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS" + else + echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING" + fi +} + # Formats prompt string for current git commit short SHA function git_prompt_short_sha() { SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"