git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jeff King <peff@peff.net>, git@vger.kernel.org
Subject: Re: [PATCH 2/4] contrib/subtree: stop using `-o` to test for number of args
Date: Fri, 10 Nov 2023 11:18:49 +0100	[thread overview]
Message-ID: <ZU4DiTQbKyuuT55k@tanuki> (raw)
In-Reply-To: <xmqq8r76zg1j.fsf@gitster.g>

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

On Fri, Nov 10, 2023 at 08:02:32AM +0900, Junio C Hamano wrote:
> Jeff King <peff@peff.net> writes:
> 
> >>  # Usage: process_subtree_split_trailer SPLIT_HASH MAIN_HASH [REPOSITORY]
> >>  process_subtree_split_trailer () {
> >> -	assert test $# = 2 -o $# = 3
> >> +	assert test $# -ge 2
> >> +	assert test $# -le 3
> >
> > It took me a minute to figure out why we were swapping "=" for "-ge". It
> > is because we want to logical-OR the two conditions, but "assert"
> > requires that we test one at a time. I think that is probably worth
> > explaining in the commit message.
> 
> I wish we could write something like
> 
> 	assert test $# -ge 2 && test $# -le 3
> 
> (and I'd allow double quoting the whole thing after assert if
> needed) but we cannot do so without tweaking the implementation of
> assert.
> 
> >
> >> @@ -916,7 +919,7 @@ cmd_split () {
> >>  	if test $# -eq 0
> >>  	then
> >>  		rev=$(git rev-parse HEAD)
> >> -	elif test $# -eq 1 -o $# -eq 2
> >> +	elif test $# -eq 1 || test $# -eq 2
> >
> > OK, this one is a straight-forward use of "||".
> 
> Yes, but why not consistently use the range notation like the
> earlier one here, or below?

I opted to go for the "obvious" conversion, if there was one easily
available, to make the diff easier to read. We could of course use a
range notation like this:

 		rev=$(git rev-parse HEAD)
-	elif test $# -eq 1 || test $# -eq 2
+	elif test $# -ge 1 && test $# -le 2
 	then
 		rev=$(git rev-parse -q --verify "$1^{commit}") ||
 			die "fatal: '$1' does not refer to a commit"

Or :

 		rev=$(git rev-parse HEAD)
-	elif test $# -eq 1 || test $# -eq 2
+	elif ! { test $# -lt 1 || test $# -gt 2; }
 	then
 		rev=$(git rev-parse -q --verify "$1^{commit}") ||
 			die "fatal: '$1' does not refer to a commit"

But both of these are not consistent with the other cases due to the
negation here, and both of them are harder to read in my opinion. So
I'm not sure whether we gain anything by trying to make this a bit more
consistent with the other conversions.

Patrick

> 	elif test $# -ge 1 && test $# -le 2
> 
> >>  cmd_merge () {
> >> -	test $# -eq 1 -o $# -eq 2 ||
> >> +	if test $# -lt 1 || test $# -gt 2
> >> ...
> > (I am OK with either, it just took me a minute to verify that your
> > conversion was correct. But that is a one-time issue now while
> > reviewing, and I think the code is readable going forward).
> 
> Yeah, the end result looks good.
> 
> Thanks, both.

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

  reply	other threads:[~2023-11-10 10:18 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 [this message]
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   ` [PATCH v2 3/4] contrib/subtree: convert subtree type check to use case statement Patrick Steinhardt
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=ZU4DiTQbKyuuT55k@tanuki \
    --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).