Skip to content

Commit fe445a1

Browse files
avihgitster
authored andcommitted
git-prompt: replace [[...]] with standard code
The existing [[...]] tests were either already valid as standard [...] tests, or only required minimal retouch: Notes: - [[...]] doesn't do field splitting and glob expansion, so $var or $(cmd...) don't need quoting, but [... does need quotes. - [[ X == Y ]] when Y is a string is same as [ X = Y ], but if Y is a pattern, then we need: case X in Y)... ; esac . - [[ ... && ... ]] was replaced with [ ... ] && [ ... ] . - [[ -o <zsh-option> ]] requires [[...]], so put it in "eval" and only eval it in zsh, so other shells would not abort on syntax error (posix says [[ has unspecified results, shells allowed to reject it) - ((x++)) was changed into x=$((x+1)) (yeah, not [[...]] ...) Shells which accepted the previous forms: - bash, zsh, ksh93, mksh, openbsd sh, pdksh. Shells which didn't, and now can process it: - dash, free/net bsd sh, busybox-ash, Schily Bourne sh, yash. Signed-off-by: Avi Halachmi (:avih) <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f2e264e commit fe445a1

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

contrib/completion/git-prompt.sh

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ __git_ps1_show_upstream ()
126126
case "$key" in
127127
bash.showupstream)
128128
GIT_PS1_SHOWUPSTREAM="$value"
129-
if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
129+
if [ -z "${GIT_PS1_SHOWUPSTREAM}" ]; then
130130
p=""
131131
return
132132
fi
@@ -187,14 +187,14 @@ __git_ps1_show_upstream ()
187187
upstream_type=${svn_upstream#/}
188188
;;
189189
esac
190-
elif [[ "svn+git" = "$upstream_type" ]]; then
190+
elif [ "svn+git" = "$upstream_type" ]; then
191191
upstream_type="@{upstream}"
192192
fi
193193
;;
194194
esac
195195

196196
# Find how many commits we are ahead/behind our upstream
197-
if [[ -z "$legacy" ]]; then
197+
if [ -z "$legacy" ]; then
198198
count="$(git rev-list --count --left-right \
199199
"$upstream_type"...HEAD 2>/dev/null)"
200200
else
@@ -206,8 +206,8 @@ __git_ps1_show_upstream ()
206206
for commit in $commits
207207
do
208208
case "$commit" in
209-
"<"*) ((behind++)) ;;
210-
*) ((ahead++)) ;;
209+
"<"*) behind=$((behind+1)) ;;
210+
*) ahead=$((ahead+1)) ;;
211211
esac
212212
done
213213
count="$behind $ahead"
@@ -217,7 +217,7 @@ __git_ps1_show_upstream ()
217217
fi
218218

219219
# calculate the result
220-
if [[ -z "$verbose" ]]; then
220+
if [ -z "$verbose" ]; then
221221
case "$count" in
222222
"") # no upstream
223223
p="" ;;
@@ -243,7 +243,7 @@ __git_ps1_show_upstream ()
243243
*) # diverged from upstream
244244
upstream="|u+${count#* }-${count% *}" ;;
245245
esac
246-
if [[ -n "$count" && -n "$name" ]]; then
246+
if [ -n "$count" ] && [ -n "$name" ]; then
247247
__git_ps1_upstream_name=$(git rev-parse \
248248
--abbrev-ref "$upstream_type" 2>/dev/null)
249249
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
@@ -265,7 +265,7 @@ __git_ps1_show_upstream ()
265265
# their own color.
266266
__git_ps1_colorize_gitstring ()
267267
{
268-
if [[ -n ${ZSH_VERSION-} ]]; then
268+
if [ -n "${ZSH_VERSION-}" ]; then
269269
local c_red='%F{red}'
270270
local c_green='%F{green}'
271271
local c_lblue='%F{blue}'
@@ -417,7 +417,7 @@ __git_ps1 ()
417417
# incorrect.)
418418
#
419419
local ps1_expanded=yes
420-
[ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
420+
[ -z "${ZSH_VERSION-}" ] || eval '[[ -o PROMPT_SUBST ]]' || ps1_expanded=no
421421
[ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
422422

423423
local repo_info rev_parse_exit_code
@@ -502,11 +502,13 @@ __git_ps1 ()
502502
return $exit
503503
fi
504504

505-
if [[ $head == "ref: "* ]]; then
505+
case $head in
506+
"ref: "*)
506507
head="${head#ref: }"
507-
else
508+
;;
509+
*)
508510
head=""
509-
fi
511+
esac
510512
;;
511513
*)
512514
head="$(git symbolic-ref HEAD 2>/dev/null)"
@@ -542,8 +544,8 @@ __git_ps1 ()
542544
fi
543545

544546
local conflict="" # state indicator for unresolved conflicts
545-
if [[ "${GIT_PS1_SHOWCONFLICTSTATE-}" == "yes" ]] &&
546-
[[ $(git ls-files --unmerged 2>/dev/null) ]]; then
547+
if [ "${GIT_PS1_SHOWCONFLICTSTATE-}" = "yes" ] &&
548+
[ "$(git ls-files --unmerged 2>/dev/null)" ]; then
547549
conflict="|CONFLICT"
548550
fi
549551

0 commit comments

Comments
 (0)