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

Re: [PATCH] git: show alias info only with lone -h

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 25, 2025, 23:52 UTC
Message-ID
<xmqqqzy33k1j.fsf@gitster.g>
In-Reply-To
<a35dc2bf-015c-472d-9528-6763f7aac180@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 11 quoted lines
> Builtin commands show usage information on stdout if called with -h as
> their only option, usage.c::show_usage_if_asked() makes sure of that.
>
> Aliases show alias information on stderr if called with -h as the first
> option since a9a60b94cc (git.c: handle_alias: prepend alias info when
> first argument is -h, 2018-10-09).  This is surprising when using
> aliases for commands that take -h as a normal argument among others,
> like git grep.
>
> Tighten the condition and show the alias information only if -h is the
> only option given, to be consistent with builtins.

Hmph. This certainly is an obvious improvement, especially when the alias does not redirect to our commands, e.g. "git g" is an alias to the system supplied (likely to be GNU) grep:

   $ git -c alias.g='!grep' g -h frotz *.c

which gives us hits in our sources as expected. But without arguments, we get the obvious plus something we may not want to see:

   $ git -c alias.g='!grep' g -h
   'g' is aliased to '!grep'
   Usage: grep [OPTION]... PATTERNS [FILE]...
   Try 'grep --help' for more information.

I wonder if we should not run the underlying/aliased command after showing the alias expansion? With "grep" we got lucky that a sole "-h" means the same thing as it means to us (i.e. short help), but if it is not one of our commands that would not generally hold. It is risky to blindly run the external command with a single "-h" as an argument.

When it redirects to our commands, it is less risky as we aim to make all our commands honor a single "-h" via t0450.

   $ git -c alias.c=checkout c -h
   'c' is aliased to 'checkout'
   usage: git checkout [<options>] <branch>
      or: git checkout [<options>] [<branch>] -- <file>...
       -b <branch>           create and checkout a new branch
       -B <branch>           create/reset and checkout a branch
   ...
       --pathspec-from-file <file>
                             read pathspec from file
       --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character

But then, it may not be such a good idea to pay attention to "do we have extra '-h'?" when alias expands to our commands, e.g.

   $ git -c alias.c=checkout c -h main
   usage: git checkout [<options>] <branch>
      or: git checkout [<options>] [<branch>] -- <file>...
       -b <branch>           create and checkout a new branch
       -B <branch>           create/reset and checkout a branch
   ...
       --pathspec-from-file <file>
                             read pathspec from file
       --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character

We get the same short-help, without what alias expansion caused this mess, and without any indication that we lost 'main' on the command line.

So, I dunno.
Previous: René ScharfeNext: Jeff King
Message 3 of 9 in “-h prints alias information even for grep”
  1. Kevin BrodskyJul 25, 2025
  2. git: show alias info only with lone -hRené Scharfe, Jul 25, 2025
  3. Junio C HamanoJul 25, 2025
  4. Jeff KingJul 26, 2025
  5. D. Ben KnobleJul 26, 2025
  6. D. Ben KnobleJul 26, 2025
  7. Jeff KingJul 29, 2025
  8. Junio C HamanoJul 28, 2025
  9. Jeff KingJul 29, 2025

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.