Added support to show commits ahead, commits behind and stashes count in Git
authorEduardo Bellido Bellido <edubxb@gmail.com>
Tue, 12 Nov 2013 22:54:07 +0000 (23:54 +0100)
committerEduardo Bellido Bellido <edubxb@gmail.com>
Tue, 12 Nov 2013 22:54:07 +0000 (23:54 +0100)
through variables

themes/base.theme.bash

index 001ca65..96c7cf0 100644 (file)
@@ -70,18 +70,28 @@ function scm_prompt_info {
 }
 
 function git_prompt_vars {
-  if [[ -n $(git status -s 2> /dev/null |grep -v ^# |grep -v "working directory clean") ]]; then
+  SCM_GIT_AHEAD=''
+  SCM_GIT_BEHIND=''
+  SCM_GIT_STASH=''
+  local status="$(git status -bs --porcelain 2> /dev/null)"
+  if [[ -n "$(grep -v ^# <<< "${status}")" ]]; then
     SCM_DIRTY=1
-     SCM_STATE=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
+    SCM_STATE=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}
   else
     SCM_DIRTY=0
-     SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
+    SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}
   fi
   SCM_PREFIX=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}
   SCM_SUFFIX=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}
   local ref=$(git symbolic-ref HEAD 2> /dev/null)
   SCM_BRANCH=${ref#refs/heads/}
   SCM_CHANGE=$(git rev-parse HEAD 2>/dev/null)
+  local ahead_re='.+ahead ([0-9]+).+'
+  local behind_re='.+behind ([0-9]+).+'
+  [[ "${status}" =~ ${ahead_re} ]] && SCM_GIT_AHEAD=" ${SCM_GIT_AHEAD_CHAR}${BASH_REMATCH[1]}"
+  [[ "${status}" =~ ${behind_re} ]] && SCM_GIT_BEHIND=" ${SCM_GIT_BEHIND_CHAR}${BASH_REMATCH[1]}"
+  local stash_count="$(git stash list | wc -l)"
+  [[ "${stash_count}" -gt 0 ]] && SCM_GIT_STASH=" {${stash_count}}"
 }
 
 function svn_prompt_vars {