From 027fcccc9256aaf63e0d1b812069a1a0880f46fa Mon Sep 17 00:00:00 2001 From: Ted Vessenes Date: Tue, 31 Jul 2012 13:27:40 -0400 Subject: [PATCH 1/3] Make git use sha when branch name is missing. --- lib/git.zsh | 3 ++- plugins/git/git.plugin.zsh | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/git.zsh b/lib/git.zsh index fb4ad8ca6..af8b153d9 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -1,6 +1,7 @@ # get the name of the branch we are on function git_prompt_info() { - ref=$(git symbolic-ref HEAD 2> /dev/null) || return + ref=$(git symbolic-ref HEAD 2> /dev/null) || \ + ref=$(git rev-parse --short HEAD 2> /dev/null) || return echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX" } diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index d3d3f702a..14237cebe 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -50,13 +50,14 @@ alias gsd='git svn dcommit' # Usage example: git pull origin $(current_branch) # function current_branch() { - ref=$(git symbolic-ref HEAD 2> /dev/null) || return + ref=$(git symbolic-ref HEAD 2> /dev/null) || \ + ref=$(git rev-parse --short HEAD 2> /dev/null) || return echo ${ref#refs/heads/} } function current_repository() { - - ref=$(git symbolic-ref HEAD 2> /dev/null) || return + ref=$(git symbolic-ref HEAD 2> /dev/null) || \ + ref=$(git rev-parse --short HEAD 2> /dev/null) || return echo $(git remote -v | cut -d':' -f 2) } From f8a602e6f9ffd39520ca1da21bbd117e45daefb0 Mon Sep 17 00:00:00 2001 From: Michele Campeotto Date: Wed, 1 Aug 2012 09:38:47 -0700 Subject: [PATCH 2/3] git functions cleanup * moved current_branch implementation in lib/git.zsh and renamed to git_current_branch * renamed parse_git_dirty to git_parse_dirty, still backwards compatible --- lib/git.zsh | 18 ++++++++++++------ plugins/git/git.plugin.zsh | 6 +----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/git.zsh b/lib/git.zsh index af8b153d9..d2cdde037 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -1,13 +1,18 @@ # get the name of the branch we are on -function git_prompt_info() { +function git_current_branch() { ref=$(git symbolic-ref HEAD 2> /dev/null) || \ ref=$(git rev-parse --short HEAD 2> /dev/null) || return - echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX" + echo ${ref#refs/heads/} } +# Branch and dirty flag +function git_prompt_info() { + ref=$(git symbolic-ref HEAD 2> /dev/null) || return + echo "$ZSH_THEME_GIT_PROMPT_PREFIX$(git_current_branch)$(git_parse_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX" +} # Checks if working tree is dirty -parse_git_dirty() { +function git_parse_dirty() { local SUBMODULE_SYNTAX='' if [[ $POST_1_7_2_GIT -gt 0 ]]; then SUBMODULE_SYNTAX="--ignore-submodules=dirty" @@ -18,11 +23,12 @@ parse_git_dirty() { echo "$ZSH_THEME_GIT_PROMPT_CLEAN" fi } - +# For backwards compatibility +function parse_git_dirty() { git_parse_dirty } # Checks if there are commits ahead from remote function git_prompt_ahead() { - if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then + if $(echo "$(git log origin/$(git_current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then echo "$ZSH_THEME_GIT_PROMPT_AHEAD" fi } @@ -38,7 +44,7 @@ function git_prompt_long_sha() { } # Get the status of the working tree -git_prompt_status() { +function git_prompt_status() { INDEX=$(git status --porcelain 2> /dev/null) STATUS="" if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index 14237cebe..e0540ef81 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -49,11 +49,7 @@ alias gsd='git svn dcommit' # Will return the current branch name # Usage example: git pull origin $(current_branch) # -function current_branch() { - ref=$(git symbolic-ref HEAD 2> /dev/null) || \ - ref=$(git rev-parse --short HEAD 2> /dev/null) || return - echo ${ref#refs/heads/} -} +function current_branch() { git_current_branch } function current_repository() { ref=$(git symbolic-ref HEAD 2> /dev/null) || \ From cf9a16efad17a95334ebc731ebc2228ad05a0607 Mon Sep 17 00:00:00 2001 From: Michele Campeotto Date: Wed, 1 Aug 2012 14:47:55 -0700 Subject: [PATCH 3/3] added git_prompt_behind use status for git_prompt_ahead instead of log --- lib/git.zsh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/git.zsh b/lib/git.zsh index d2cdde037..0193d2775 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -28,11 +28,19 @@ function parse_git_dirty() { git_parse_dirty } # Checks if there are commits ahead from remote function git_prompt_ahead() { - if $(echo "$(git log origin/$(git_current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then + if $(git status --porcelain --branch 2> /dev/null | grep '^## .*ahead' &> /dev/null); then echo "$ZSH_THEME_GIT_PROMPT_AHEAD" fi } +# Checks if there are commits behind from remote +function git_prompt_behind() { + if $(git status --porcelain --branch 2> /dev/null | grep '^## .*behind' &> /dev/null); then + echo $ZSH_THEME_GIT_PROMPT_BEHIND + fi +} + + # Formats prompt string for current git commit short SHA function git_prompt_short_sha() { SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"