contrib/subtree: stop using `-o` to test for number of args

Functions in git-subtree.sh all assert that they are being passed the correct number of arguments. In cases where we accept a variable number of arguments we assert this via a single call to `test` with `-o`, which is discouraged by our coding guidelines. Convert these cases to stop doing so. This requires us to decompose assertions of the style `assert test $# = 2 -o $# = 3` into two calls because we have no easy way to logically chain statements passed to the assert function. 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 88983946fa00ebea8b346acab46e19bceeed427d
1 file changed +11 -5
contrib/subtree/git-subtree.sh
+11 -5
@@ -373,7 +373,8 @@ try_remove_previous () {
373
374 # Usage: process_subtree_split_trailer SPLIT_HASH MAIN_HASH [REPOSITORY]
375 process_subtree_split_trailer () {
376 - assert test $# = 2 -o $# = 3
376 + assert test $# -ge 2
377 + assert test $# -le 3
378 b="$1"
379 sq="$2"
380 repository=""
@@ -402,7 +403,8 @@ process_subtree_split_trailer () {
403
404 # Usage: find_latest_squash DIR [REPOSITORY]
405 find_latest_squash () {
405 - assert test $# = 1 -o $# = 2
406 + assert test $# -ge 1
407 + assert test $# -le 2
408 dir="$1"
409 repository=""
410 if test "$#" = 2
@@ -455,7 +457,8 @@ find_latest_squash () {
457
458 # Usage: find_existing_splits DIR REV [REPOSITORY]
459 find_existing_splits () {
458 - assert test $# = 2 -o $# = 3
460 + assert test $# -ge 2
461 + assert test $# -le 3
462 debug "Looking for prior splits..."
463 local indent=$(($indent + 1))
464
@@ -916,7 +919,7 @@ cmd_split () {
919 if test $# -eq 0
920 then
921 rev=$(git rev-parse HEAD)
919 - elif test $# -eq 1 -o $# -eq 2
922 + elif test $# -eq 1 || test $# -eq 2
923 then
924 rev=$(git rev-parse -q --verify "$1^{commit}") ||
925 die "fatal: '$1' does not refer to a commit"
@@ -1006,8 +1009,11 @@ cmd_split () {
1009
1010 # Usage: cmd_merge REV [REPOSITORY]
1011 cmd_merge () {
1009 - test $# -eq 1 -o $# -eq 2 ||
1012 + if test $# -lt 1 || test $# -gt 2
1013 + then
1014 die "fatal: you must provide exactly one revision, and optionally a repository. Got: '$*'"
1015 + fi
1016 +
1017 rev=$(git rev-parse -q --verify "$1^{commit}") ||
1018 die "fatal: '$1' does not refer to a commit"
1019 repository=""