Skip to content

Commit e6e76ba

Browse files
Villemoesgitster
authored andcommitted
help: redirect to aliased commands for "git cmd --help"
As discussed in the thread for v1 of this patch [1] [2], this changes the rules for "git foo --help" when foo is an alias. (1) When invoked as "git help foo", we continue to print the "foo is aliased to bar" message and nothing else. (2) If foo is an alias for a shell command, print "foo is aliased to !bar" as usual. (3) Otherwise, print "foo is aliased to bar" to the standard error stream, and then break the alias string into words and pretend as if "git word[0] --help" were called. Getting the man page for git-cherry-pick directly with "git cp --help" is consistent with "--help" generally providing more comprehensive help than "-h". Printing the alias definition to stderr means that in certain cases (e.g. if help.format=web or if the pager uses an alternate screen and does not clear the terminal), one has 'cp' is aliased to 'cherry-pick -n' above the prompt when one returns to the terminal/quits the pager, which is a useful reminder that using 'cp' has some flag implicitly set. There are cases where this information vanishes or gets scrolled away, but being printed to stderr, it should never hurt. [1] https://public-inbox.org/git/[email protected]/ [2] https://public-inbox.org/git/[email protected]/ Signed-off-by: Rasmus Villemoes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe8321e commit e6e76ba

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

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)

0 commit comments

Comments
 (0)