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

Re: [PATCH 1/4] global: convert trivial usages of `test <expr> -a/-o <expr>`

From
Jeff King <peff@peff.net>
Date
Nov 9, 2023, 18:48 UTC
Message-ID
<20231109184843.GC2711684@coredump.intra.peff.net>
In-Reply-To
<xmqqpm0jyx02.fsf@gitster.g>
On Thu, Nov 09, 2023 at 08:41:33PM +0900, Junio C Hamano wrote:
Show 9 quoted lines
> > -elif test -d ${GIT_DIR:-.git} -o -f .git &&
> > +elif ( test -d ${GIT_DIR:-.git} || test -f .git ) &&
> 
> I do not think this is strictly necessary.
> 
> Because the command line parser of "test" comes from left, notices
> "-d" and takes the next one to check if it is a directory.  There is
> no value in ${GIT_DIR} can make "test -d ${GIT_DIR} -o ..." fail the
> same way as the problem Peff pointed out during the discussion.

I think this is one of the ambiguous cases. If $GIT_DIR is "=", then "test" cannot tell if you meant:

  var1=-d
  var2=-o
  test "$var1" = "$var2" ...
or:
  var1="="
  test -d "$var1" -o ...
With bash, for example:
  $ test -d /tmp -o -f .git; echo $?
  0
  $ test -d = -o -f .git; echo $?
  bash: test: syntax error: `-f' unexpected
  2

Without "-o", it uses the number of arguments to disambiguate (though of course the lack of quotes around $GIT_DIR is another potential problem here).

And I think the same is true of the other cases below using "-z", "-n", and so on.

But IMHO it is worth getting rid of all -o/-a regardless. Even non-ambiguous cases make reasoning about the code harder, and we don't want to encourage people to think they're OK to use.

> I do not need a subshell for grouping, either.  Plain {} should do
> (but you may need a LF or semicolon after the statement)..
This I definitely agree with. :)
-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 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.