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

Commands using -h as an option don't work consistently

From
KDKevin Day <toasty@dragondata.com>
Date
May 29, 2024, 22:03 UTC
Message-ID
<52819526-4C6F-418C-8B8B-A4D5C7E371EA@dragondata.com>
Because of a bug in parse-options.c, any command that has a '-h' option will sometimes display the usage page instead of executing. For example, ls-remote has two options:
    -t, --[no-]tags       limit to tags
    -h, --[no-]heads      limit to heads

git ls-remote --heads #works git ls-remote --tags #works git ls-remote -t #works git ls-remote -t -h #works git ls-remote -h #shows the help page

This is because of these lines in parse-options.c:

/* lone -h asks for help */ if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h")) goto show_usage;

This is being executed before it looks to see if there actually is a -h option. So if a program has a -h option, and that's the ONLY parameter you pass, the usage page gets displayed incorrectly. This appears to affect ls-remote, show-ref (it's not documented, but show-ref accepts -h as an alias for --heads) and grep.
I fixed this by moving the lone -h check lower down, which fixed everything. Except now lots and lots of tests are failing because many of them assume you can always pass -h to get the usage page, and now you can't for some commands. I don't think this actually breaks grep because you need to pass at least one more option other than -h to use it, but tests for it are still failing after fixing this bug because it's now showing the man page instead of the expected short usage page because it's erroring out at a different place.
The specific tests that are failing are t1502-rev-parse-parseopt.sh, t0012-help.sh and t0450-txt-doc-vs-help.sh all of which are trying to use -h on commands that have repurposed it.
The options I see:
1) Fix -h handling and add ignores and fixes where possible to the failing tests and try to not use -h as an option for anything new.
2) Change -h to -H or something, but this breaks backwards compatibility
3) Fix it so that -h works if a command uses it, and additionally make a new global option -? or --usage or something that always shows the usage page and change tests to use that, while leaving -h sometimes showing usage and sometimes executing the option to preserve as much backward compatibility as possible.
I'm happy to do the work and submit it, but this is looking more like a policy decision than just a bug now.
-- Kevin
Next: Junio C Hamano
Message 1 of 5 in “Commands using -h as an option don't work consistently”
  1. Kevin DayMay 29, 2024
  2. Junio C HamanoMay 29, 2024
  3. Kevin DayMay 29, 2024
  4. Junio C HamanoMay 30, 2024
  5. Junio C HamanoJun 3, 2024

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.