Skip to content

Commit 7752999

Browse files
committed
Merge branch 'rv/alias-help'
"git cmd --help" when "cmd" is aliased used to only say "cmd is aliased to ...". Now it shows that to the standard error stream and runs "git $cmd --help" where $cmd is the first word of the alias expansion. This could be misleading for those who alias a command with options (e.g. with "[alias] cpn = cherry-pick -n", "git cpn --help" would show the manual of "cherry-pick", and the reader would not be told to pay close attention to the part that describes the "--no-commit" option until closing the pager that showed the contents of the manual, if the pager is configured to restore the original screen, or would not be told at all, if the pager simply makes the message on the standard error scroll away. * rv/alias-help: git-help.txt: document "git help cmd" vs "git cmd --help" for aliases git.c: handle_alias: prepend alias info when first argument is -h help: redirect to aliased commands for "git cmd --help"
2 parents d1f96fd + 9121047 commit 7752999

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

Documentation/git-help.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ guide is brought up. The 'man' program is used by default for this
2929
purpose, but this can be overridden by other options or configuration
3030
variables.
3131

32+
If an alias is given, git shows the definition of the alias on
33+
standard output. To get the manual page for the aliased command, use
34+
`git COMMAND --help`.
35+
3236
Note that `git --help ...` is identical to `git help ...` because the
3337
former is internally converted into the latter.
3438

builtin/help.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,37 @@ static const char *check_git_cmd(const char* cmd)
415415

416416
alias = alias_lookup(cmd);
417417
if (alias) {
418-
printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
419-
free(alias);
420-
exit(0);
418+
const char **argv;
419+
int count;
420+
421+
/*
422+
* handle_builtin() in git.c rewrites "git cmd --help"
423+
* to "git help --exclude-guides cmd", so we can use
424+
* exclude_guides to distinguish "git cmd --help" from
425+
* "git help cmd". In the latter case, or if cmd is an
426+
* alias for a shell command, just print the alias
427+
* definition.
428+
*/
429+
if (!exclude_guides || alias[0] == '!') {
430+
printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
431+
free(alias);
432+
exit(0);
433+
}
434+
/*
435+
* Otherwise, we pretend that the command was "git
436+
* word0 --help". We use split_cmdline() to get the
437+
* first word of the alias, to ensure that we use the
438+
* same rules as when the alias is actually
439+
* used. split_cmdline() modifies alias in-place.
440+
*/
441+
fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
442+
count = split_cmdline(alias, &argv);
443+
if (count < 0)
444+
die(_("bad alias.%s string: %s"), cmd,
445+
split_cmdline_strerror(count));
446+
free(argv);
447+
UNLEAK(alias);
448+
return alias;
421449
}
422450

423451
if (exclude_guides)

git.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ static int handle_alias(int *argcp, const char ***argv)
318318
alias_command = (*argv)[0];
319319
alias_string = alias_lookup(alias_command);
320320
if (alias_string) {
321+
if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
322+
fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
323+
alias_command, alias_string);
321324
if (alias_string[0] == '!') {
322325
struct child_process child = CHILD_PROCESS_INIT;
323326
int nongit_ok;

0 commit comments

Comments
 (0)