completion: Better __git_ps1 support when not in working directory
If .git/HEAD is not readable, __git_ps1 does nothing. If --is-in-git-dir, __git_ps1 returns " (GIT_DIR!)" as a cautionary note. The previous behavior would show the branch name (and would optionally attempt to determine the dirtyState of the directory, which was impossible because a "git diff" was used). If --is-in-work-tree, __git_ps1 returns the branch name. Additionally, if showDirtyState is on, the dirty state is displayed. Signed-off-by: Ted Pavlic <ted@tedpavlic.com> Acked-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ted Pavlic committed
Feb 11, 2009 at 13:03 UTC
e5dd864adfeb8b0176b31a132e972d7f7beff32a
1 file changed
+22
-14
contrib/completion/git-completion.bash
+22
-14
@@ -108,7 +108,9 @@ __git_ps1 ()
108
fi
109
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
110
if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
111
- b="$(cut -c1-7 "$g/HEAD")..."
111
+ if [ -r "$g/HEAD" ]; then
112
+ b="$(cut -c1-7 "$g/HEAD")..."
113
+ fi
114
fi
115
fi
116
fi
@@ -116,23 +118,29 @@ __git_ps1 ()
118
local w
119
local i
120
119
- if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
120
- if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
121
- git diff --no-ext-diff --ignore-submodules \
122
- --quiet --exit-code || w="*"
123
- if git rev-parse --quiet --verify HEAD >/dev/null; then
124
- git diff-index --cached --quiet \
125
- --ignore-submodules HEAD -- || i="+"
126
- else
127
- i="#"
121
+ if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
122
+ b="GIT_DIR!"
123
+ elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
124
+ if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
125
+ if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
126
+ git diff --no-ext-diff --ignore-submodules \
127
+ --quiet --exit-code || w="*"
128
+ if git rev-parse --quiet --verify HEAD >/dev/null; then
129
+ git diff-index --cached --quiet \
130
+ --ignore-submodules HEAD -- || i="+"
131
+ else
132
+ i="#"
133
+ fi
134
fi
135
fi
136
fi
137
132
- if [ -n "${1-}" ]; then
133
- printf "$1" "${b##refs/heads/}$w$i$r"
134
- else
135
- printf " (%s)" "${b##refs/heads/}$w$i$r"
138
+ if [ -n "$b" ]; then
139
+ if [ -n "${1-}" ]; then
140
+ printf "$1" "${b##refs/heads/}$w$i$r"
141
+ else
142
+ printf " (%s)" "${b##refs/heads/}$w$i$r"
143
+ fi
144
fi
145
fi
146
}