2d40cc0bb315a92368e35c15c92f955bd901c7c3

Author
Adam Lindberg <hello@alind.io>
Committer
ncanceill <nicolas.canceill@ens-cachan.org>
Date

Message

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.

Diff

 1diff --git a/lib/git.zsh b/lib/git.zsh
 2index 733492ba0a62093745699ee24a92df90bf53982d..caa7e632913251f9694b3a3b7cb357e2879e02cf 100644
 3--- a/lib/git.zsh
 4+++ b/lib/git.zsh
 5@@ -59,13 +59,6 @@ git_remote_status() {
 6     fi
 7 }
 8 
 9-# Checks if there are commits ahead from remote
10-function git_prompt_ahead() {
11-  if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
12-    echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
13-  fi
14-}
15-
16 # Gets the number of commits ahead from remote
17 function git_commits_ahead() {
18   if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
19@@ -74,6 +67,29 @@ function git_commits_ahead() {
20   fi
21 }
22 
23+# Outputs if current branch is ahead of remote
24+function git_prompt_ahead() {
25+  if [[ -n "$(command git rev-list origin/$(current_branch)..HEAD 2> /dev/null)" ]]; then
26+    echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
27+  fi
28+}
29+
30+# Outputs if current branch is behind remote
31+function git_prompt_behind() {
32+  if [[ -n "$(command git rev-list HEAD..origin/$(current_branch) 2> /dev/null)" ]]; then
33+    echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
34+  fi
35+}
36+
37+# Outputs if current branch exists on remote or not
38+function git_prompt_remote() {
39+  if [[ -n "$(command git show-ref origin/$(current_branch) 2> /dev/null)" ]]; then
40+    echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
41+  else
42+    echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
43+  fi
44+}
45+
46 # Formats prompt string for current git commit short SHA
47 function git_prompt_short_sha() {
48   SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"