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) <avihpit@yahoo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Avi Halachmi (:avih) committed Aug 20, 2024 at 01:48 UTC fe445a1026562dfeb4fa1b0ee93bdfe3b7422251
1 file changed +16 -14
contrib/completion/git-prompt.sh
+16 -14
@@ -126,7 +126,7 @@ __git_ps1_show_upstream ()
126 case "$key" in
127 bash.showupstream)
128 GIT_PS1_SHOWUPSTREAM="$value"
129 - if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
129 + if [ -z "${GIT_PS1_SHOWUPSTREAM}" ]; then
130 p=""
131 return
132 fi
@@ -187,14 +187,14 @@ __git_ps1_show_upstream ()
187 upstream_type=${svn_upstream#/}
188 ;;
189 esac
190 - elif [[ "svn+git" = "$upstream_type" ]]; then
190 + elif [ "svn+git" = "$upstream_type" ]; then
191 upstream_type="@{upstream}"
192 fi
193 ;;
194 esac
195
196 # Find how many commits we are ahead/behind our upstream
197 - if [[ -z "$legacy" ]]; then
197 + if [ -z "$legacy" ]; then
198 count="$(git rev-list --count --left-right \
199 "$upstream_type"...HEAD 2>/dev/null)"
200 else
@@ -206,8 +206,8 @@ __git_ps1_show_upstream ()
206 for commit in $commits
207 do
208 case "$commit" in
209 - "<"*) ((behind++)) ;;
210 - *) ((ahead++)) ;;
209 + "<"*) behind=$((behind+1)) ;;
210 + *) ahead=$((ahead+1)) ;;
211 esac
212 done
213 count="$behind $ahead"
@@ -217,7 +217,7 @@ __git_ps1_show_upstream ()
217 fi
218
219 # calculate the result
220 - if [[ -z "$verbose" ]]; then
220 + if [ -z "$verbose" ]; then
221 case "$count" in
222 "") # no upstream
223 p="" ;;
@@ -243,7 +243,7 @@ __git_ps1_show_upstream ()
243 *) # diverged from upstream
244 upstream="|u+${count#* }-${count% *}" ;;
245 esac
246 - if [[ -n "$count" && -n "$name" ]]; then
246 + if [ -n "$count" ] && [ -n "$name" ]; then
247 __git_ps1_upstream_name=$(git rev-parse \
248 --abbrev-ref "$upstream_type" 2>/dev/null)
249 if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
@@ -265,7 +265,7 @@ __git_ps1_show_upstream ()
265 # their own color.
266 __git_ps1_colorize_gitstring ()
267 {
268 - if [[ -n ${ZSH_VERSION-} ]]; then
268 + if [ -n "${ZSH_VERSION-}" ]; then
269 local c_red='%F{red}'
270 local c_green='%F{green}'
271 local c_lblue='%F{blue}'
@@ -417,7 +417,7 @@ __git_ps1 ()
417 # incorrect.)
418 #
419 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
421 [ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
422
423 local repo_info rev_parse_exit_code
@@ -502,11 +502,13 @@ __git_ps1 ()
502 return $exit
503 fi
504
505 - if [[ $head == "ref: "* ]]; then
505 + case $head in
506 + "ref: "*)
507 head="${head#ref: }"
507 - else
508 + ;;
509 + *)
510 head=""
509 - fi
511 + esac
512 ;;
513 *)
514 head="$(git symbolic-ref HEAD 2>/dev/null)"
@@ -542,8 +544,8 @@ __git_ps1 ()
544 fi
545
546 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
549 conflict="|CONFLICT"
550 fi
551