Skip to content

Commit 738a94a

Browse files
trastgitster
authored andcommitted
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 <[email protected]> Acked-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e656fc9 commit 738a94a

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

contrib/completion/git-completion.bash

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
# are currently in a git repository. The %s token will be
3535
# the name of the current branch.
3636
#
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+
#
3743
# To submit patches:
3844
#
3945
# *) Read Documentation/SubmittingPatches
@@ -116,10 +122,26 @@ __git_ps1 ()
116122
fi
117123
fi
118124

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+
119141
if [ -n "${1-}" ]; then
120-
printf "$1" "${b##refs/heads/}$r"
142+
printf "$1" "${b##refs/heads/}$w$i$r"
121143
else
122-
printf " (%s)" "${b##refs/heads/}$r"
144+
printf " (%s)" "${b##refs/heads/}$w$i$r"
123145
fi
124146
fi
125147
}

0 commit comments

Comments
 (0)