a3c2a2f6e8ff9db508070a905de056346386ef4f

Author
Caio Romão <caioromao@gmail.com>
Committer
Caio Romão <caioromao@gmail.com>
Date

Message

Add branch status support to git_prompt_status

This patch makes git_prompt_status support three new status variables:

 - ZSH_THEME_GIT_PROMPT_AHEAD
 - ZSH_THEME_GIT_PROMPT_BEHIND
 - ZSH_THEME_GIT_PROMPT_DIVERGED

With these extra variables it's easy to see (1) if you have commits in
your local branch that weren't pushed to the remote (AHEAD), (2) if
there are commits in the remote that you haven't merged/rebased yet
(BEHIND) or (3) if you have local unpushed commits AND the remote has
some commits you haven't merged yet (DIVERGED).

Refer to the first line displayed on `git status -b --porcelain`.

An example setup in a .zsh-theme file would be:

ZSH_THEME_GIT_PROMPT_AHEAD="↑"
ZSH_THEME_GIT_PROMPT_BEHIND="↓"
ZSH_THEME_GIT_PROMPT_DIVERGED="↕"

Diff

 1diff --git a/lib/git.zsh b/lib/git.zsh
 2index fb4ad8ca64ffbf7bcfd34ca82b1acbe8947d36bf..bca8bdc967dfedf2c9134f038b8085a753f479b0 100644
 3--- a/lib/git.zsh
 4+++ b/lib/git.zsh
 5@@ -38,7 +38,7 @@ function git_prompt_long_sha() {
 6 
 7 # Get the status of the working tree
 8 git_prompt_status() {
 9-  INDEX=$(git status --porcelain 2> /dev/null)
10+  INDEX=$(git status --porcelain -b 2> /dev/null)
11   STATUS=""
12   if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
13     STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
14@@ -66,6 +66,15 @@ git_prompt_status() {
15   if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
16     STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
17   fi
18+  if $(echo "$INDEX" | grep '^## .*ahead' &> /dev/null); then
19+    STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
20+  fi
21+  if $(echo "$INDEX" | grep '^## .*behind' &> /dev/null); then
22+    STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
23+  fi
24+  if $(echo "$INDEX" | grep '^## .*diverged' &> /dev/null); then
25+    STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
26+  fi
27   echo $STATUS
28 }
29