47c04d921ebb3fe4916eb131ae11557650aa1ae3

Author
Arnaud Thimel <thimel@codelutin.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(git-prompt): display stash count in prompt

Diff

 1diff --git a/plugins/git-prompt/git-prompt.plugin.zsh b/plugins/git-prompt/git-prompt.plugin.zsh
 2index 43a327ab059fb65964192eed2cdc3db05483387a..a4c95247136b28d21be6854ce2858b1c80ddc71e 100644
 3--- a/plugins/git-prompt/git-prompt.plugin.zsh
 4+++ b/plugins/git-prompt/git-prompt.plugin.zsh
 5@@ -40,6 +40,8 @@ function update_current_git_vars() {
 6     GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
 7     GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
 8     GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
 9+    GIT_STASHED=$__CURRENT_GIT_STATUS[8]
10+    GIT_CLEAN=$__CURRENT_GIT_STATUS[9]
11 }
12 
13 git_super_status() {
14@@ -65,7 +67,10 @@ git_super_status() {
15       if [ "$GIT_UNTRACKED" -ne "0" ]; then
16           STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED$GIT_UNTRACKED%{${reset_color}%}"
17       fi
18-      if [ "$GIT_CHANGED" -eq "0" ] && [ "$GIT_CONFLICTS" -eq "0" ] && [ "$GIT_STAGED" -eq "0" ] && [ "$GIT_UNTRACKED" -eq "0" ]; then
19+      if [ "$GIT_STASHED" -ne "0" ]; then
20+          STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STASHED$GIT_STASHED%{${reset_color}%}"
21+      fi
22+      if [ "$GIT_CLEAN" -eq "1" ]; then
23           STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
24       fi
25       STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
26@@ -84,6 +89,7 @@ ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
27 ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
28 ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
29 ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[cyan]%}%{…%G%}"
30+ZSH_THEME_GIT_PROMPT_STASHED="%{$fg_bold[blue]%}%{⚑%G%}"
31 ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
32 
33 # Set the prompt.
34diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py
35index bf3173614bda4872aa458379f1301354aca9e491..786274a71bb667d11ce10e5cec7284ead16f8e18 100644
36--- a/plugins/git-prompt/gitstatus.py
37+++ b/plugins/git-prompt/gitstatus.py
38@@ -23,6 +23,18 @@ def get_tagname_or_hash():
39         return hash_
40     return None
41 
42+# Re-use method from https://github.com/magicmonty/bash-git-prompt to get stashs count
43+def get_stash():
44+    cmd = Popen(['git', 'rev-parse', '--git-dir'], stdout=PIPE, stderr=PIPE)
45+    so, se = cmd.communicate()
46+    stash_file = '%s%s' % (so.decode('utf-8').rstrip(), '/logs/refs/stash')
47+
48+    try:
49+        with open(stash_file) as f:
50+            return sum(1 for _ in f)
51+    except IOError:
52+        return 0
53+
54 
55 # `git status --porcelain --branch` can collect all information
56 # branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
57@@ -68,6 +80,12 @@ for st in status:
58         elif st[0] != ' ':
59             staged.append(st)
60 
61+stashed = get_stash()
62+if not changed and not staged and not conflicts and not untracked and not stashed:
63+    clean = 1
64+else:
65+    clean = 0
66+
67 out = ' '.join([
68     branch,
69     str(ahead),
70@@ -76,5 +94,7 @@ out = ' '.join([
71     str(len(conflicts)),
72     str(len(changed)),
73     str(len(untracked)),
74+    str(stashed),
75+    str(clean)
76 ])
77 print(out, end='')