git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Jeff King <peff@peff.net>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 3/4] contrib/subtree: convert subtree type check to use case statement
Date: Fri, 10 Nov 2023 11:01:24 +0100	[thread overview]
Message-ID: <761cde1b341e22b20063ce3baa6b70d80aa05168.1699609940.git.ps@pks.im> (raw)
In-Reply-To: <cover.1699609940.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 1227 bytes --]

The `subtree_for_commit ()` helper function asserts that the subtree
identified by its parameters are either a commit or tree. This is done
via the `-o` parameter of test, which is discouraged.

Refactor the code to instead use a switch statement over the type.
Despite being aligned with our coding guidelines, the resulting code is
arguably also easier to read.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 contrib/subtree/git-subtree.sh | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 8af0a81ba3f..3028029ac2d 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -641,10 +641,16 @@ subtree_for_commit () {
 	while read mode type tree name
 	do
 		assert test "$name" = "$dir"
-		assert test "$type" = "tree" -o "$type" = "commit"
-		test "$type" = "commit" && continue  # ignore submodules
-		echo $tree
-		break
+
+		case "$type" in
+		commit)
+			continue;; # ignore submodules
+		tree)
+			echo $tree
+			break;;
+		*)
+			die "fatal: tree entry is of type ${type}, expected tree or commit";;
+		esac
 	done || exit $?
 }
 
-- 
2.42.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2023-11-10 10:01 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 10:53 [PATCH 0/4] Replace use of `test <expr> -o/a <expr>` Patrick Steinhardt
2023-11-09 10:53 ` [PATCH 1/4] global: convert trivial usages of `test <expr> -a/-o <expr>` Patrick Steinhardt
2023-11-09 11:41   ` Junio C Hamano
2023-11-09 18:48     ` Jeff King
2023-11-09 22:56       ` Junio C Hamano
2023-11-10 10:18       ` Patrick Steinhardt
2023-11-09 10:53 ` [PATCH 2/4] contrib/subtree: stop using `-o` to test for number of args Patrick Steinhardt
2023-11-09 18:55   ` Jeff King
2023-11-09 23:02     ` Junio C Hamano
2023-11-10 10:18       ` Patrick Steinhardt
2023-11-10 23:27         ` Junio C Hamano
2023-11-10 10:18     ` Patrick Steinhardt
2023-11-09 10:53 ` [PATCH 3/4] contrib/subtree: convert subtree type check to use case statement Patrick Steinhardt
2023-11-09 18:56   ` Jeff King
2023-11-09 10:53 ` [PATCH 4/4] Makefile: stop using `test -o` when unlinking duplicate executables Patrick Steinhardt
2023-11-10 10:01 ` [PATCH v2 0/4] Replace use of `test <expr> -o/a <expr>` Patrick Steinhardt
2023-11-10 10:01   ` [PATCH v2 1/4] global: convert trivial usages of `test <expr> -a/-o <expr>` Patrick Steinhardt
2023-11-10 21:44     ` Jeff King
2023-11-11  0:14       ` Junio C Hamano
2023-11-11  0:20         ` Junio C Hamano
2023-11-13  7:12           ` Patrick Steinhardt
2023-11-11  0:12     ` Junio C Hamano
2023-11-10 10:01   ` [PATCH v2 2/4] contrib/subtree: stop using `-o` to test for number of args Patrick Steinhardt
2023-11-10 10:01   ` Patrick Steinhardt [this message]
2023-11-10 10:01   ` [PATCH v2 4/4] Makefile: stop using `test -o` when unlinking duplicate executables Patrick Steinhardt
2023-11-10 21:46   ` [PATCH v2 0/4] Replace use of `test <expr> -o/a <expr>` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=761cde1b341e22b20063ce3baa6b70d80aa05168.1699609940.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).