git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/4] contrib/subtree: stop using `-o` to test for number of args

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 9, 2023, 23:02 UTC
Message-ID
<xmqq8r76zg1j.fsf@gitster.g>
In-Reply-To
<20231109185515.GD2711684@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 10 quoted lines
>>  # 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.

Show 9 quoted lines
>
>> @@ -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?

	elif test $# -ge 1 && test $# -le 2
Show 7 quoted lines
>>  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.
Previous: Jeff KingNext: Patrick Steinhardt
Message 9 of 26 in “Replace use of `test <expr> -o/a <expr>`”
  1. 0/4 Replace use of `test <expr> -o/a <expr>`Patrick Steinhardt, Nov 9, 2023
  2. 1/4 global: convert trivial usages of `test <expr> -a/-o <expr>`Patrick Steinhardt, Nov 9, 2023
  3. Junio C HamanoNov 9, 2023
  4. Jeff KingNov 9, 2023
  5. Junio C HamanoNov 9, 2023
  6. Patrick SteinhardtNov 10, 2023
  7. 2/4 contrib/subtree: stop using `-o` to test for number of argsPatrick Steinhardt, Nov 9, 2023
  8. Jeff KingNov 9, 2023
  9. Junio C HamanoNov 9, 2023
  10. Patrick SteinhardtNov 10, 2023
  11. Junio C HamanoNov 10, 2023
  12. Patrick SteinhardtNov 10, 2023
  13. 3/4 contrib/subtree: convert subtree type check to use case statementPatrick Steinhardt, Nov 9, 2023
  14. Jeff KingNov 9, 2023
  15. 4/4 Makefile: stop using `test -o` when unlinking duplicate executablesPatrick Steinhardt, Nov 9, 2023
  16. 0/4 Replace use of `test <expr> -o/a <expr>`Patrick Steinhardt, Nov 10, 2023
  17. 1/4 global: convert trivial usages of `test <expr> -a/-o <expr>`Patrick Steinhardt, Nov 10, 2023
  18. Jeff KingNov 10, 2023
  19. Junio C HamanoNov 11, 2023
  20. Junio C HamanoNov 11, 2023
  21. Patrick SteinhardtNov 13, 2023
  22. Junio C HamanoNov 11, 2023
  23. 2/4 contrib/subtree: stop using `-o` to test for number of argsPatrick Steinhardt, Nov 10, 2023
  24. 3/4 contrib/subtree: convert subtree type check to use case statementPatrick Steinhardt, Nov 10, 2023
  25. 4/4 Makefile: stop using `test -o` when unlinking duplicate executablesPatrick Steinhardt, Nov 10, 2023
  26. Jeff KingNov 10, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.