global: convert trivial usages of `test <expr> -a/-o <expr>`

Our coding guidelines say to not use `test` with `-a` and `-o` because it can easily lead to bugs. Convert trivial cases where we still use these to instead instead concatenate multiple invocations of `test` via `&&` and `||`, respectively. While not all of the converted instances can cause ambiguity, it is worth getting rid of all of them regardless: - It becomes easier to reason about the code as we do not have to argue why one use of `-a`/`-o` is okay while another one isn't. - We don't encourage people to use these expressions. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 10, 2023 at 11:01 UTC 13420028e5b119d27277d521f4d267212c5ca21c
6 files changed +11 -10
GIT-VERSION-GEN
+1 -1
@@ -11,7 +11,7 @@ LF='
11 if test -f version
12 then
13 VN=$(cat version) || VN="$DEF_VER"
14 -elif test -d ${GIT_DIR:-.git} -o -f .git &&
14 +elif { test -d "${GIT_DIR:-.git}" || test -f .git; } &&
15 VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
16 case "$VN" in
17 *$LF*) (exit 1) ;;
configure.ac
+1 -1
@@ -94,7 +94,7 @@ AC_DEFUN([GIT_PARSE_WITH_SET_MAKE_VAR],
94 [AC_ARG_WITH([$1],
95 [AS_HELP_STRING([--with-$1=VALUE], $3)],
96 if test -n "$withval"; then
97 - if test "$withval" = "yes" -o "$withval" = "no"; then
97 + if test "$withval" = "yes" || test "$withval" = "no"; then
98 AC_MSG_WARN([You likely do not want either 'yes' or 'no' as]
99 [a value for $1 ($2). Maybe you do...?])
100 fi
contrib/subtree/git-subtree.sh
+2 -2
@@ -489,13 +489,13 @@ find_existing_splits () {
489 ;;
490 END)
491 debug "Main is: '$main'"
492 - if test -z "$main" -a -n "$sub"
492 + if test -z "$main" && test -n "$sub"
493 then
494 # squash commits refer to a subtree
495 debug " Squash: $sq from $sub"
496 cache_set "$sq" "$sub"
497 fi
498 - if test -n "$main" -a -n "$sub"
498 + if test -n "$main" && test -n "$sub"
499 then
500 debug " Prior: $main -> $sub"
501 cache_set $main $sub
t/perf/perf-lib.sh
+1 -1
@@ -31,7 +31,7 @@ unset GIT_CONFIG_NOSYSTEM
31 GIT_CONFIG_SYSTEM="$TEST_DIRECTORY/perf/config"
32 export GIT_CONFIG_SYSTEM
33
34 -if test -n "$GIT_TEST_INSTALLED" -a -z "$PERF_SET_GIT_TEST_INSTALLED"
34 +if test -n "$GIT_TEST_INSTALLED" && test -z "$PERF_SET_GIT_TEST_INSTALLED"
35 then
36 error "Do not use GIT_TEST_INSTALLED with the perf tests.
37
t/perf/run
+5 -4
@@ -91,10 +91,10 @@ set_git_test_installed () {
91 run_dirs_helper () {
92 mydir=${1%/}
93 shift
94 - while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
94 + while test $# -gt 0 && test "$1" != -- && test ! -f "$1"; do
95 shift
96 done
97 - if test $# -gt 0 -a "$1" = --; then
97 + if test $# -gt 0 && test "$1" = --; then
98 shift
99 fi
100
@@ -124,7 +124,7 @@ run_dirs_helper () {
124 }
125
126 run_dirs () {
127 - while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
127 + while test $# -gt 0 && test "$1" != -- && test ! -f "$1"; do
128 run_dirs_helper "$@"
129 shift
130 done
@@ -180,7 +180,8 @@ run_subsection () {
180 GIT_PERF_AGGREGATING_LATER=t
181 export GIT_PERF_AGGREGATING_LATER
182
183 - if test $# = 0 -o "$1" = -- -o -f "$1"; then
183 + if test $# = 0 || test "$1" = -- || test -f "$1"
184 + then
185 set -- . "$@"
186 fi
187
t/valgrind/valgrind.sh
+1 -1
@@ -23,7 +23,7 @@ memcheck)
23 VALGRIND_MAJOR=$(expr "$VALGRIND_VERSION" : '[^0-9]*\([0-9]*\)')
24 VALGRIND_MINOR=$(expr "$VALGRIND_VERSION" : '[^0-9]*[0-9]*\.\([0-9]*\)')
25 test 3 -gt "$VALGRIND_MAJOR" ||
26 - test 3 -eq "$VALGRIND_MAJOR" -a 4 -gt "$VALGRIND_MINOR" ||
26 + { test 3 -eq "$VALGRIND_MAJOR" && test 4 -gt "$VALGRIND_MINOR"; } ||
27 TOOL_OPTIONS="$TOOL_OPTIONS --track-origins=yes"
28 ;;
29 *)