bash: offer to show (un)staged changes
Add a bit of code to __git_ps1 that lets it append '*' to the branch name if there are any unstaged changes, and '+' if there are any staged changes. Since this is a rather expensive operation and will force a lot of data into the cache whenever you first enter a repository, you have to enable it manually by setting GIT_PS1_SHOWDIRTYSTATE to a nonempty value. The configuration variable bash.showDirtyState can then be used to disable it again for some repositories. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Acked-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Rast committed
Feb 3, 2009 at 10:20 UTC
738a94a9f645a5f63fbccaab76d5e43a97e53d78
1 file changed
+24
-2
contrib/completion/git-completion.bash
+24
-2
@@ -34,6 +34,12 @@
34
# are currently in a git repository. The %s token will be
35
# the name of the current branch.
36
#
37
+# In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
38
+# value, unstaged (*) and staged (+) changes will be shown next
39
+# to the branch name. You can configure this per-repository
40
+# with the bash.showDirtyState variable, which defaults to true
41
+# once GIT_PS1_SHOWDIRTYSTATE is enabled.
42
+#
43
# To submit patches:
44
#
45
# *) Read Documentation/SubmittingPatches
@@ -116,10 +122,26 @@ __git_ps1 ()
122
fi
123
fi
124
125
+ local w
126
+ local i
127
+
128
+ if test -n "$GIT_PS1_SHOWDIRTYSTATE"; then
129
+ if test "$(git config --bool bash.showDirtyState)" != "false"; then
130
+ git diff --no-ext-diff --ignore-submodules \
131
+ --quiet --exit-code || w="*"
132
+ if git rev-parse --quiet --verify HEAD >/dev/null; then
133
+ git diff-index --cached --quiet \
134
+ --ignore-submodules HEAD -- || i="+"
135
+ else
136
+ i="#"
137
+ fi
138
+ fi
139
+ fi
140
+
141
if [ -n "${1-}" ]; then
120
- printf "$1" "${b##refs/heads/}$r"
142
+ printf "$1" "${b##refs/heads/}$w$i$r"
143
else
122
- printf " (%s)" "${b##refs/heads/}$r"
144
+ printf " (%s)" "${b##refs/heads/}$w$i$r"
145
fi
146
fi
147
}