threads / patch / 49428

patchhelp: allow redirecting to help for aliased command

Subject: [PATCH] help: allow redirecting to help for aliased command

## tl;dr

43 messages between Sep 26, 2018 and Oct 12, 2018. Diffs are folded; open one to read it.

replies: 42people: 5as markdown or json

Rasmus Villemoes· Sep 26, 2018, 10:26 UTC · lore

I often use 'git <cmd> --help' as a quick way to get the documentation for a command. However, I've also trained my muscle memory to use my aliases (cp=cherry-pick, co=checkout etc.), which means that I often end up doing

  git cp --help

to which git correctly informs me that cp is an alias for cherry-pick. However, I already knew that, and what I really wanted was the man page for the cherry-pick command.

This introduces a help.followAlias config option that transparently redirects to (the first word of) the alias text (provided of course it is not a shell command), similar to the option for autocorrect of misspelled commands.

The documentation in config.txt could probably be improved. Also, I mimicked the autocorrect case in that the "Continuing to ..." text goes to stderr, but because of that, I also print the "is aliased to" text to stderr, which is different from the current behaviour of using stdout. I'm not sure what the most correct thing is, but I assume --help is mostly used interactively with stdout and stderr pointing at the same place.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 Documentation/config.txt | 10 ++++++++++
 builtin/help.c           | 36 +++++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 3 deletions(-)
Show changes to 2 files +43 −3

Documentation/config.txt, builtin/help.c

diff --git a/Documentation/config.txt b/Documentation/config.txt
index ad0f4510c3..8a1fc8064e 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2105,6 +2105,16 @@ help.autoCorrect::
 	value is 0 - the command will be just shown but not executed.
 	This is the default.
 
+help.followAlias::
+	When requesting help for an alias, git prints a line of the
+	form "'<alias>' is aliased to '<string>'". If this option is
+	set to a positive integer, git proceeds to show the help for
+	the first word of <string> after the given number of
+	deciseconds. If the value of this option is negative, the
+	redirect happens immediately. If the value is 0 (which is the
+	default), or <string> begins with an exclamation point, no
+	redirect takes place.
+
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
 	and URLs are supported. HTML pages will be prefixed with this path when
diff --git a/builtin/help.c b/builtin/help.c
index 8d4f6dd301..ef1c3f0916 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -34,6 +34,7 @@ enum help_format {
 };
 
 static const char *html_path;
+static int follow_alias;
 
 static int show_all = 0;
 static int show_guides = 0;
@@ -273,6 +274,10 @@ static int git_help_config(const char *var, const char *value, void *cb)
 		html_path = xstrdup(value);
 		return 0;
 	}
+	if (!strcmp(var, "help.followalias")) {
+		follow_alias = git_config_int(var, value);
+		return 0;
+	}
 	if (!strcmp(var, "man.viewer")) {
 		if (!value)
 			return config_error_nonbool(var);
@@ -415,9 +420,34 @@ static const char *check_git_cmd(const char* cmd)
 
 	alias = alias_lookup(cmd);
 	if (alias) {
-		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
-		free(alias);
-		exit(0);
+		const char **argv;
+		int count;
+
+		if (!follow_alias || alias[0] == '!') {
+			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
+			free(alias);
+			exit(0);
+		}
+		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
+
+		/*
+		 * We use split_cmdline() to get the first word of the
+		 * alias, to ensure that we use the same rules as when
+		 * the alias is actually used. split_cmdline()
+		 * modifies alias in-place.
+		 */
+		count = split_cmdline(alias, &argv);
+		if (count < 0)
+			die("Bad alias.%s string: %s", cmd,
+			    split_cmdline_strerror(count));
+
+		if (follow_alias > 0) {
+			fprintf_ln(stderr,
+				   _("Continuing to help for %s in %0.1f seconds."),
+				   alias, follow_alias/10.0);
+			sleep_millisec(follow_alias * 100);
+		}
+		return alias;
 	}
 
 	if (exclude_guides)
-- 
2.16.4
Taylor Blau· Sep 26, 2018, 14:37 UTC · re: Rasmus Villemoes · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 12:26:36PM +0200, Rasmus Villemoes wrote:
Show 10 quoted lines
> I often use 'git <cmd> --help' as a quick way to get the documentation
> for a command. However, I've also trained my muscle memory to use my
> aliases (cp=cherry-pick, co=checkout etc.), which means that I often end
> up doing
>
>   git cp --help
>
> to which git correctly informs me that cp is an alias for
> cherry-pick. However, I already knew that, and what I really wanted was
> the man page for the cherry-pick command.

Neat. I have many of those such aliases myself, and have always wanted something like this. Thanks for taking the time to put such a patch together :-).

> This introduces a help.followAlias config option that transparently
> redirects to (the first word of) the alias text (provided of course it
> is not a shell command), similar to the option for autocorrect of
> misspelled commands.

Good. I was curious if you were going to introduce a convention along the lines of, "If the alias begins with a '!', then pass "--help" to it and it must respond appropriately." I'm glad that you didn't take that approach.

Show 26 quoted lines
> The documentation in config.txt could probably be improved. Also, I
> mimicked the autocorrect case in that the "Continuing to ..." text goes
> to stderr, but because of that, I also print the "is aliased to" text to
> stderr, which is different from the current behaviour of using
> stdout. I'm not sure what the most correct thing is, but I assume --help
> is mostly used interactively with stdout and stderr pointing at the same
> place.
>
> Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
> ---
>  Documentation/config.txt | 10 ++++++++++
>  builtin/help.c           | 36 +++++++++++++++++++++++++++++++++---
>  2 files changed, 43 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index ad0f4510c3..8a1fc8064e 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -2105,6 +2105,16 @@ help.autoCorrect::
>  	value is 0 - the command will be just shown but not executed.
>  	This is the default.
>
> +help.followAlias::
> +	When requesting help for an alias, git prints a line of the
> +	form "'<alias>' is aliased to '<string>'". If this option is
> +	set to a positive integer, git proceeds to show the help for

With regard to "set to a positive integer", I'm not sure why this is the way that it is. I see below you used 'git_config_int()', but I think that 'git_config_bool()' would be more appropriate.

The later understands strings like "yes", "on" or "true", which I think is more of what I would expect from a configuration setting such as this.

Show 5 quoted lines
> +	the first word of <string> after the given number of
> +	deciseconds. If the value of this option is negative, the
> +	redirect happens immediately. If the value is 0 (which is the
> +	default), or <string> begins with an exclamation point, no
> +	redirect takes place.

It was unclear to my originlly why this was given as a configuration knob, but my understanding after reading the patch is that this is to do _additional_ things besides printing what is aliased to what.

Could you perhaps note this in the documentation?
Show 23 quoted lines
>  help.htmlPath::
>  	Specify the path where the HTML documentation resides. File system paths
>  	and URLs are supported. HTML pages will be prefixed with this path when
> diff --git a/builtin/help.c b/builtin/help.c
> index 8d4f6dd301..ef1c3f0916 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -34,6 +34,7 @@ enum help_format {
>  };
>
>  static const char *html_path;
> +static int follow_alias;
>
>  static int show_all = 0;
>  static int show_guides = 0;
> @@ -273,6 +274,10 @@ static int git_help_config(const char *var, const char *value, void *cb)
>  		html_path = xstrdup(value);
>  		return 0;
>  	}
> +	if (!strcmp(var, "help.followalias")) {
> +		follow_alias = git_config_int(var, value);
> +		return 0;
> +	}

Good. I think in modern Git, we'd prefer to write this as a series of `else if`'s, but this matches the style of the surrounding code. I think that you could optionally clean up this style as a preparatory commit, but ultimately I don't think it's worth a reroll on its own.

Show 19 quoted lines
>  	if (!strcmp(var, "man.viewer")) {
>  		if (!value)
>  			return config_error_nonbool(var);
> @@ -415,9 +420,34 @@ static const char *check_git_cmd(const char* cmd)
>
>  	alias = alias_lookup(cmd);
>  	if (alias) {
> -		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> -		free(alias);
> -		exit(0);
> +		const char **argv;
> +		int count;
> +
> +		if (!follow_alias || alias[0] == '!') {
> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> +			free(alias);
> +			exit(0);
> +		}
> +		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);

OK, I think that this is a sensible decision: print to STDERR when that's not the main purpose of what're doing (e.g., we're going to follow the alias momentarily), and STDOUT when it's the only thing we're doing.

Potentially we could call 'fprintf_ln()' only once, and track an `int fd` at the top of this block.

Show 11 quoted lines
> +
> +		/*
> +		 * We use split_cmdline() to get the first word of the
> +		 * alias, to ensure that we use the same rules as when
> +		 * the alias is actually used. split_cmdline()
> +		 * modifies alias in-place.
> +		 */
> +		count = split_cmdline(alias, &argv);
> +		if (count < 0)
> +			die("Bad alias.%s string: %s", cmd,
> +			    split_cmdline_strerror(count));
Please wrap this in _() so that translators can translate it.
Show 7 quoted lines
> +		if (follow_alias > 0) {
> +			fprintf_ln(stderr,
> +				   _("Continuing to help for %s in %0.1f seconds."),
> +				   alias, follow_alias/10.0);
> +			sleep_millisec(follow_alias * 100);
> +		}
> +		return alias;

I'm not sure that this notification is necessary, but I'll defer to the judgement of others on this one.

Thanks, Taylor

Duy Nguyen· Sep 26, 2018, 15:19 UTC · re: Taylor Blau · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 4:42 PM Taylor Blau <me@ttaylorr.com> wrote:
Show 13 quoted lines
> > +
> > +             /*
> > +              * We use split_cmdline() to get the first word of the
> > +              * alias, to ensure that we use the same rules as when
> > +              * the alias is actually used. split_cmdline()
> > +              * modifies alias in-place.
> > +              */
> > +             count = split_cmdline(alias, &argv);
> > +             if (count < 0)
> > +                     die("Bad alias.%s string: %s", cmd,
> > +                         split_cmdline_strerror(count));
>
> Please wrap this in _() so that translators can translate it.

Yes! And another nit. die(), error(), warning()... usually start the message with a lowercase letter because we already start the sentence with a prefix, like

fatal: bad alias.blah blah
-- 
Duy
Rasmus Villemoes· Sep 28, 2018, 07:44 UTC · re: Duy Nguyen · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On 2018-09-26 17:19, Duy Nguyen wrote:
Show 21 quoted lines
> On Wed, Sep 26, 2018 at 4:42 PM Taylor Blau <me@ttaylorr.com> wrote:
>>> +
>>> +             /*
>>> +              * We use split_cmdline() to get the first word of the
>>> +              * alias, to ensure that we use the same rules as when
>>> +              * the alias is actually used. split_cmdline()
>>> +              * modifies alias in-place.
>>> +              */
>>> +             count = split_cmdline(alias, &argv);
>>> +             if (count < 0)
>>> +                     die("Bad alias.%s string: %s", cmd,
>>> +                         split_cmdline_strerror(count));
>>
>> Please wrap this in _() so that translators can translate it.
> 
> Yes! And another nit. die(), error(), warning()... usually start the
> message with a lowercase letter because we already start the sentence
> with a prefix, like
> 
> fatal: bad alias.blah blah
> 
I'll keep these points in mind, but this was pure copy-paste from git.c.
Rasmus
Junio C Hamano· Sep 26, 2018, 15:30 UTC · re: Taylor Blau · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Taylor Blau <me@ttaylorr.com> writes:
Show 12 quoted lines
>> +help.followAlias::
>> +	When requesting help for an alias, git prints a line of the
>> +	form "'<alias>' is aliased to '<string>'". If this option is
>> +	set to a positive integer, git proceeds to show the help for
>
> With regard to "set to a positive integer", I'm not sure why this is the
> way that it is. I see below you used 'git_config_int()', but I think
> that 'git_config_bool()' would be more appropriate.
>
> The later understands strings like "yes", "on" or "true", which I think
> is more of what I would expect from a configuration setting such as
> this.

That is, as you read in the next paragraph, because it gives the number of deciseconds to show a prompt before showing the manpage.

Not that I think this configuration is a good idea (see my review).
Show 11 quoted lines
>> +	the first word of <string> after the given number of
>> +	deciseconds. If the value of this option is negative, the
>> +	redirect happens immediately. If the value is 0 (which is the
>> +	default), or <string> begins with an exclamation point, no
>> +	redirect takes place.
>
> It was unclear to my originlly why this was given as a configuration
> knob, but my understanding after reading the patch is that this is to do
> _additional_ things besides printing what is aliased to what.
>
> Could you perhaps note this in the documentation?

It may be that the description for the "execute the likely typoed command" configuration is poorly written and this merely copied the badness from it. Over there the prompt gives a chance to ^C out, which serves useful purpose, and if that is not documented, we should.

On the other hand, I'd rather see this prompt in the new code removed, because I do not think the prompt given in the new code here is all that useful.

Show 21 quoted lines
>> @@ -415,9 +420,34 @@ static const char *check_git_cmd(const char* cmd)
>>
>>  	alias = alias_lookup(cmd);
>>  	if (alias) {
>> -		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
>> -		free(alias);
>> -		exit(0);
>> +		const char **argv;
>> +		int count;
>> +
>> +		if (!follow_alias || alias[0] == '!') {
>> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
>> +			free(alias);
>> +			exit(0);
>> +		}
>> +		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
>
> OK, I think that this is a sensible decision: print to STDERR when
> that's not the main purpose of what're doing (e.g., we're going to
> follow the alias momentarily), and STDOUT when it's the only thing we're
> doing.
> Potentially we could call 'fprintf_ln()' only once, and track an `int
> fd` at the top of this block.
I actually think this should always give the output to standard output.
Show 24 quoted lines
>> +
>> +		/*
>> +		 * We use split_cmdline() to get the first word of the
>> +		 * alias, to ensure that we use the same rules as when
>> +		 * the alias is actually used. split_cmdline()
>> +		 * modifies alias in-place.
>> +		 */
>> +		count = split_cmdline(alias, &argv);
>> +		if (count < 0)
>> +			die("Bad alias.%s string: %s", cmd,
>> +			    split_cmdline_strerror(count));
>
> Please wrap this in _() so that translators can translate it.
>
>> +		if (follow_alias > 0) {
>> +			fprintf_ln(stderr,
>> +				   _("Continuing to help for %s in %0.1f seconds."),
>> +				   alias, follow_alias/10.0);
>> +			sleep_millisec(follow_alias * 100);
>> +		}
>> +		return alias;
>
> I'm not sure that this notification is necessary, but I'll defer to the
> judgement of others on this one.

I didn't bother to check the original but this is mimicking an existing code that lets configuration to be set to num-deciseconds to pause and give chance to ^C out, and also allows it to be set to negative to immediately go ahead. follow-alias at this point cannot be zero in the codeflow, but it still can be negative.

Taylor Blau· Sep 26, 2018, 18:09 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 08:30:32AM -0700, Junio C Hamano wrote:
Show 98 quoted lines
> Taylor Blau <me@ttaylorr.com> writes:
>
> >> +help.followAlias::
> >> +	When requesting help for an alias, git prints a line of the
> >> +	form "'<alias>' is aliased to '<string>'". If this option is
> >> +	set to a positive integer, git proceeds to show the help for
> >
> > With regard to "set to a positive integer", I'm not sure why this is the
> > way that it is. I see below you used 'git_config_int()', but I think
> > that 'git_config_bool()' would be more appropriate.
> >
> > The later understands strings like "yes", "on" or "true", which I think
> > is more of what I would expect from a configuration setting such as
> > this.
>
> That is, as you read in the next paragraph, because it gives the
> number of deciseconds to show a prompt before showing the manpage.
>
> Not that I think this configuration is a good idea (see my review).
>
> >> +	the first word of <string> after the given number of
> >> +	deciseconds. If the value of this option is negative, the
> >> +	redirect happens immediately. If the value is 0 (which is the
> >> +	default), or <string> begins with an exclamation point, no
> >> +	redirect takes place.
> >
> > It was unclear to my originlly why this was given as a configuration
> > knob, but my understanding after reading the patch is that this is to do
> > _additional_ things besides printing what is aliased to what.
> >
> > Could you perhaps note this in the documentation?
>
> It may be that the description for the "execute the likely typoed
> command" configuration is poorly written and this merely copied the
> badness from it.  Over there the prompt gives a chance to ^C out,
> which serves useful purpose, and if that is not documented, we should.
>
> On the other hand, I'd rather see this prompt in the new code
> removed, because I do not think the prompt given in the new code
> here is all that useful.
>
> >> @@ -415,9 +420,34 @@ static const char *check_git_cmd(const char* cmd)
> >>
> >>  	alias = alias_lookup(cmd);
> >>  	if (alias) {
> >> -		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> >> -		free(alias);
> >> -		exit(0);
> >> +		const char **argv;
> >> +		int count;
> >> +
> >> +		if (!follow_alias || alias[0] == '!') {
> >> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> >> +			free(alias);
> >> +			exit(0);
> >> +		}
> >> +		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
> >
> > OK, I think that this is a sensible decision: print to STDERR when
> > that's not the main purpose of what're doing (e.g., we're going to
> > follow the alias momentarily), and STDOUT when it's the only thing we're
> > doing.
>
> > Potentially we could call 'fprintf_ln()' only once, and track an `int
> > fd` at the top of this block.
>
> I actually think this should always give the output to standard output.
>
> >> +
> >> +		/*
> >> +		 * We use split_cmdline() to get the first word of the
> >> +		 * alias, to ensure that we use the same rules as when
> >> +		 * the alias is actually used. split_cmdline()
> >> +		 * modifies alias in-place.
> >> +		 */
> >> +		count = split_cmdline(alias, &argv);
> >> +		if (count < 0)
> >> +			die("Bad alias.%s string: %s", cmd,
> >> +			    split_cmdline_strerror(count));
> >
> > Please wrap this in _() so that translators can translate it.
> >
> >> +		if (follow_alias > 0) {
> >> +			fprintf_ln(stderr,
> >> +				   _("Continuing to help for %s in %0.1f seconds."),
> >> +				   alias, follow_alias/10.0);
> >> +			sleep_millisec(follow_alias * 100);
> >> +		}
> >> +		return alias;
> >
> > I'm not sure that this notification is necessary, but I'll defer to the
> > judgement of others on this one.
>
> I didn't bother to check the original but this is mimicking an
> existing code that lets configuration to be set to num-deciseconds
> to pause and give chance to ^C out, and also allows it to be set to
> negative to immediately go ahead.  follow-alias at this point cannot
> be zero in the codeflow, but it still can be negative.

I think that this is the most compelling argument _for_ the configuration that you are not in favor of. I understood your previous review as "I know that 'git cp' is a synonym of 'git cherry-pick', but I want to use 'git co --help' for when I don't remember what 'git co' is a synonym of."

This pause (though I'm a little surprised by it when reviewing the code), I think strikes a good balance between the two, i.e., that you can get help for whatever it is aliased to, and see what that alias is.

Thanks, Taylor

Junio C Hamano· Sep 26, 2018, 18:20 UTC · re: Taylor Blau · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Taylor Blau <me@ttaylorr.com> writes:
> This pause (though I'm a little surprised by it when reviewing the
> code), I think strikes a good balance between the two, i.e., that you
> can get help for whatever it is aliased to, and see what that alias is.

And I need to react to it within subsecond with ^C when I want a compact answer to "what is this aliased to"? No, thanks.

Junio C Hamano· Sep 26, 2018, 15:16 UTC · re: Rasmus Villemoes · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
Show 15 quoted lines
> I often use 'git <cmd> --help' as a quick way to get the documentation
> for a command. However, I've also trained my muscle memory to use my
> aliases (cp=cherry-pick, co=checkout etc.), which means that I often end
> up doing
>
>   git cp --help
>
> to which git correctly informs me that cp is an alias for
> cherry-pick. However, I already knew that, and what I really wanted was
> the man page for the cherry-pick command.
>
> This introduces a help.followAlias config option that transparently
> redirects to (the first word of) the alias text (provided of course it
> is not a shell command), similar to the option for autocorrect of
> misspelled commands.

While I do agree with you that it would sometimes be very handy if "git cp --help" behaved identically to "git cherry-pick --help" just like "git cp -h" behaves identically to "git cherry-pick -h" when you have "[alias] cp = cherry-pick", I do not think help.followAlias configuration is a good idea. I may know, perhaps because I use it all the time, by heart that "cp" is aliased to "cherry-pick" and want "git cp --help" to directly give me the manpage, but I may not remember if "co" was commit or checkout and want to be concisely told that it is aliased to checkout without seeing the full manpage. Which means you'd want some way to command line override anyway, and having to say "git -c help.followAlias=false cp --help" is not a great solution.

If we expect users to use "git cp --help" a lot more often than "git help cp" (or the other way around), one way to give a nicer experience may be to unconditionally make "git cp --help" to directly show the manpage of cherry-pick, while keeping "git help cp" to never do that. Then those who want to remember what "co" is aliased to can ask "git help co".

Show 18 quoted lines
> +		/*
> +		 * We use split_cmdline() to get the first word of the
> +		 * alias, to ensure that we use the same rules as when
> +		 * the alias is actually used. split_cmdline()
> +		 * modifies alias in-place.
> +		 */
> +		count = split_cmdline(alias, &argv);
> +		if (count < 0)
> +			die("Bad alias.%s string: %s", cmd,
> +			    split_cmdline_strerror(count));
> +
> +		if (follow_alias > 0) {
> +			fprintf_ln(stderr,
> +				   _("Continuing to help for %s in %0.1f seconds."),
> +				   alias, follow_alias/10.0);
> +			sleep_millisec(follow_alias * 100);
> +		}
> +		return alias;

If you have "[alias] cp = cherry-pick -n", split_cmdline discards "-n" and the follow-alias prompt does not even tell you that it did so, and you get "git help cherry-pick". This code somehow expects you to know to jump to the section that describes the "--no-commit" option. I do not think that is a reasonable expectation.

When you have "[alias] cp = cherry-pick -n", "git cp --help" should not do "git help cherry-pick". Only a single word that exactly matches a git command should get this treatment.

>  	}
>  
>  	if (exclude_guides)
Taylor Blau· Sep 26, 2018, 18:12 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 08:16:36AM -0700, Junio C Hamano wrote:
Show 30 quoted lines
> Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
>
> > I often use 'git <cmd> --help' as a quick way to get the documentation
> > for a command. However, I've also trained my muscle memory to use my
> > aliases (cp=cherry-pick, co=checkout etc.), which means that I often end
> > up doing
> >
> >   git cp --help
> >
> > to which git correctly informs me that cp is an alias for
> > cherry-pick. However, I already knew that, and what I really wanted was
> > the man page for the cherry-pick command.
> >
> > This introduces a help.followAlias config option that transparently
> > redirects to (the first word of) the alias text (provided of course it
> > is not a shell command), similar to the option for autocorrect of
> > misspelled commands.
>
> While I do agree with you that it would sometimes be very handy if
> "git cp --help" behaved identically to "git cherry-pick --help" just
> like "git cp -h" behaves identically to "git cherry-pick -h" when
> you have "[alias] cp = cherry-pick", I do not think help.followAlias
> configuration is a good idea.  I may know, perhaps because I use it
> all the time, by heart that "cp" is aliased to "cherry-pick" and
> want "git cp --help" to directly give me the manpage, but I may not
> remember if "co" was commit or checkout and want to be concisely
> told that it is aliased to checkout without seeing the full manpage.
> Which means you'd want some way to command line override anyway, and
> having to say "git -c help.followAlias=false cp --help" is not a
> great solution.

I think I responded partially to this hunk in another thread, but I think I can add some additional information here where it is more relevant.

One approach to take when digesting this is that 'git co --help' shows you the manual page for 'git-checkout(1)' (or whatever you have it aliased to), so that answers the question for the caller who has a terminal open.

In the case where you are scripting (and want to know what 'git co' means for programmatic usage), I think that there are two options. One, which you note above, is the 'git -c help.followAlias=false ...' approach, which I don't think is so bad for callers, given the tradeoff.

Another way to go is 'git config alias.co', which should provide the same answer. I think that either would be fine.

Perhaps we could assume that 'help.followAlias' is false when it is unset, _and_ isatty(2) says that we aren't a TTY. Otherwise, since I feel that this is a good feature that should be the new default, we can assume it's set to true.

Thanks, Taylor

Rasmus Villemoes· Sep 28, 2018, 07:53 UTC · re: Taylor Blau · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On 2018-09-26 20:12, Taylor Blau wrote:
Show 8 quoted lines
> 
> In the case where you are scripting (and want to know what 'git co'
> means for programmatic usage), I think that there are two options. One,
> which you note above, is the 'git -c help.followAlias=false ...'
> approach, which I don't think is so bad for callers, given the tradeoff.
> 
> Another way to go is 'git config alias.co', which should provide the
> same answer. I think that either would be fine.

The latter seems much more robust, since that will also tell you precisely whether co is an alias at all, and you don't have to parse -h/--help output (stripping out the 'is aliased to...' stuff, which might be complicated by i18n etc. etc.). So I don't think we should worry too much about scripted use of -h/--help.

Rasmus
Jeff King· Sep 26, 2018, 18:49 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 08:16:36AM -0700, Junio C Hamano wrote:
Show 24 quoted lines
> > This introduces a help.followAlias config option that transparently
> > redirects to (the first word of) the alias text (provided of course it
> > is not a shell command), similar to the option for autocorrect of
> > misspelled commands.
> 
> While I do agree with you that it would sometimes be very handy if
> "git cp --help" behaved identically to "git cherry-pick --help" just
> like "git cp -h" behaves identically to "git cherry-pick -h" when
> you have "[alias] cp = cherry-pick", I do not think help.followAlias
> configuration is a good idea.  I may know, perhaps because I use it
> all the time, by heart that "cp" is aliased to "cherry-pick" and
> want "git cp --help" to directly give me the manpage, but I may not
> remember if "co" was commit or checkout and want to be concisely
> told that it is aliased to checkout without seeing the full manpage.
> Which means you'd want some way to command line override anyway, and
> having to say "git -c help.followAlias=false cp --help" is not a
> great solution.
> 
> If we expect users to use "git cp --help" a lot more often than "git
> help cp" (or the other way around), one way to give a nicer experience
> may be to unconditionally make "git cp --help" to directly show the
> manpage of cherry-pick, while keeping "git help cp" to never do
> that.  Then those who want to remember what "co" is aliased to can
> ask "git help co".

I like that direction much better. I also wondered if we could leverage the "-h" versus "--help" distinction. The problem with printing the alias definition along with "--help" is that the latter will start a pager that obliterates what we wrote before (and hence all of this delay trickery).

But for "-h" we generally expect the command to output a usage message.
So what if the rules were:
  - "git help cp" shows "cp is an alias for cherry-pick" (as it does
    now)
  - "git cp -h" shows "cp is an alias for cherry-pick", followed by
    actually running "cherry-pick -h", which will show the usage
    message. For a single-word command that does very little, since the
    usage message starts with "cherry-pick". But if your alias is
    actually "cp = cherry-pick -n", then it _is_ telling you extra
    information. And this could even work with "!" aliases: we define
    it, and then it is up to the alias to handle "-h" sensibly.
  - "git cp --help" opens the manpage for cherry-pick. We don't bother
    with the alias definition, as it's available through other means
    (and thus we skip the obliteration/timing thing totally).
    This really only works for non-! aliases. Those would continue to
    show the alias definition.
Show 9 quoted lines
> If you have "[alias] cp = cherry-pick -n", split_cmdline discards
> "-n" and the follow-alias prompt does not even tell you that it did
> so, and you get "git help cherry-pick".  This code somehow expects
> you to know to jump to the section that describes the "--no-commit"
> option.  I do not think that is a reasonable expectation.
> 
> When you have "[alias] cp = cherry-pick -n", "git cp --help" should
> not do "git help cherry-pick".  Only a single word that exactly
> matches a git command should get this treatment.

I'm not sure I agree. A plausible scenario (under the rules I gave above) is:

  $ git cp -h
  'cp' is aliased to 'cherry-pick -n'
  usage: git cherry-pick ...
  $ git cp --help

I.e., you already know the "-n" part, and now you want to dig further. Of course one could just type "git cherry-pick --help" since you also know that, too. But by that rationale, one could already do:

  $ git help cp
  $ git help cherry-pick
without this patch at all.
-Peff
Junio C Hamano· Sep 26, 2018, 19:31 UTC · re: Jeff King · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Jeff King <peff@peff.net> writes:
Show 10 quoted lines
>> When you have "[alias] cp = cherry-pick -n", "git cp --help" should
>> not do "git help cherry-pick".  Only a single word that exactly
>> matches a git command should get this treatment.
>
> I'm not sure I agree. A plausible scenario (under the rules I gave
> above) is:
>
>   $ git cp -h
>   'cp' is aliased to 'cherry-pick -n'
>   usage: git cherry-pick ...

With that additional rule, I can buy "it is fine for 'git cp --help' to completely ignore -n and behave as if 'git help cherry-pick' was given", I think. People already expect "git cp --help" to give the alias expansion, so to them any change will be a regression any way we cut it---but I think this is the least bad approach.

>   $ git cp --help
>
> I.e., you already know the "-n" part, and now you want to dig further.

One very good thing about the "make '--help' go directly to the manpage, while teaching '-h' to report also alias expansion" is that people already expect "-h" is more concise than "--help". The current output from "git cp --help" violates that expectation, and the change you suggest rectifies it.

> Of course one could just type "git cherry-pick --help" since you also
> know that, too.

Yeah, but that is not an argument. The user aliased cp because cherry-pick was quite a mouthful and do not want to type "git cherry-pick --help" in the first place.

Rasmus Villemoes· Sep 28, 2018, 08:18 UTC · re: Jeff King · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On 2018-09-26 20:49, Jeff King wrote:
Show 22 quoted lines
> On Wed, Sep 26, 2018 at 08:16:36AM -0700, Junio C Hamano wrote:
> 
>>
>> If we expect users to use "git cp --help" a lot more often than "git
>> help cp" (or the other way around), one way to give a nicer experience
>> may be to unconditionally make "git cp --help" to directly show the
>> manpage of cherry-pick, while keeping "git help cp" to never do
>> that.  Then those who want to remember what "co" is aliased to can
>> ask "git help co".
> 
> I like that direction much better. I also wondered if we could leverage
> the "-h" versus "--help" distinction. The problem with printing the
> alias definition along with "--help" is that the latter will start a
> pager that obliterates what we wrote before (and hence all of this delay
> trickery).
> 
> But for "-h" we generally expect the command to output a usage message.
> 
> So what if the rules were:
> 
>   - "git help cp" shows "cp is an alias for cherry-pick" (as it does
>     now)
Sounds good.
Show 6 quoted lines
>   - "git cp -h" shows "cp is an alias for cherry-pick", followed by
>     actually running "cherry-pick -h", which will show the usage
>     message. For a single-word command that does very little, since the
>     usage message starts with "cherry-pick". But if your alias is
>     actually "cp = cherry-pick -n", then it _is_ telling you extra
>     information.

Funny, I never noticed this difference, and that '-h' for an alias would actually give more information than '--help'. I sort-of knew that -h would give the synopsis, so I guess I've just gotten used to always use --help, and just noticed that for aliases that doesn't provide much help.

Adding the 'is an alias for' info to -h sounds quite sensible.
And this could even work with "!" aliases: we define
>     it, and then it is up to the alias to handle "-h" sensibly.

I'd be nervous about doing this, though, especially if we introduce this without a new opt-in config option (which seems to be the direction the discussion is taking). There are lots of commands that don't respond with a help message to -h, or that only recognize -h as the first word, or... There are really too many ways this could cause headaches.

But, now that I test it, it seems that we already let the alias handle -h (and any other following words, with --help as the first word special-cased). So what you're suggesting is (correct me if I'm wrong) to _also_ intercept -h as the first word, and then print the alias info, in addition to spawning the alias with the entire argv as usual. The alias info would probably need to go to stderr in this case.

>   - "git cp --help" opens the manpage for cherry-pick. We don't bother
>     with the alias definition, as it's available through other means
>     (and thus we skip the obliteration/timing thing totally).

It sounds like you suggest doing this unconditionally, and without any opt-in via config option or a short wait? That would certainly work for me. It is, in fact, how I expect 'git cp --help' to work, until I get reminded that it does not... Also, as Junio noted, is consistent with --help generally providing more information than -h - except that one loses the 'is an alias for' part for --help.

>     This really only works for non-! aliases. Those would continue to
>     show the alias definition.
Yes.

Thanks, Rasmus

Jeff King· Sep 29, 2018, 08:21 UTC · re: Rasmus Villemoes · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Fri, Sep 28, 2018 at 10:18:05AM +0200, Rasmus Villemoes wrote:
Show 14 quoted lines
> >     it, and then it is up to the alias to handle "-h" sensibly.
> 
> I'd be nervous about doing this, though, especially if we introduce this
> without a new opt-in config option (which seems to be the direction the
> discussion is taking). There are lots of commands that don't respond
> with a help message to -h, or that only recognize -h as the first word,
> or... There are really too many ways this could cause headaches.
> 
> But, now that I test it, it seems that we already let the alias handle
> -h (and any other following words, with --help as the first word
> special-cased). So what you're suggesting is (correct me if I'm wrong)
> to _also_ intercept -h as the first word, and then print the alias info,
> in addition to spawning the alias with the entire argv as usual. The
> alias info would probably need to go to stderr in this case.

Right, I'm proposing only to add the extra message and then continue as usual.

It is a little funny, I guess, if you have a script which doesn't respond to "-h", because you'd get our "foo is aliased to git-bar" message to stderr, followed by who-knows-what. But as long as it's to stderr (and not stdout), I think it's not likely to _break_ anything.

Show 10 quoted lines
> >   - "git cp --help" opens the manpage for cherry-pick. We don't bother
> >     with the alias definition, as it's available through other means
> >     (and thus we skip the obliteration/timing thing totally).
> 
> It sounds like you suggest doing this unconditionally, and without any
> opt-in via config option or a short wait? That would certainly work for
> me. It is, in fact, how I expect 'git cp --help' to work, until I get
> reminded that it does not... Also, as Junio noted, is consistent with
> --help generally providing more information than -h - except that one
> loses the 'is an alias for' part for --help.
Yes, I'd suggest doing it always. No config, no wait.
-Peff
Junio C Hamano· Sep 29, 2018, 17:39 UTC · re: Jeff King · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Jeff King <peff@peff.net> writes:
Show 20 quoted lines
> Right, I'm proposing only to add the extra message and then continue as
> usual.
>
> It is a little funny, I guess, if you have a script which doesn't
> respond to "-h", because you'd get our "foo is aliased to git-bar"
> message to stderr, followed by who-knows-what. But as long as it's to
> stderr (and not stdout), I think it's not likely to _break_ anything.
>
>> >   - "git cp --help" opens the manpage for cherry-pick. We don't bother
>> >     with the alias definition, as it's available through other means
>> >     (and thus we skip the obliteration/timing thing totally).
>> 
>> It sounds like you suggest doing this unconditionally, and without any
>> opt-in via config option or a short wait? That would certainly work for
>> me. It is, in fact, how I expect 'git cp --help' to work, until I get
>> reminded that it does not... Also, as Junio noted, is consistent with
>> --help generally providing more information than -h - except that one
>> loses the 'is an alias for' part for --help.
>
> Yes, I'd suggest doing it always. No config, no wait.

While I do think your suggestion is the best among various ones floated in the thread, I just realized there is one potential glitch even with that approach.

Suppose "git foo" is aliased to a command "git bar".

The best case is when "git bar -h" knows that it is asked to give us a short usage. We get "foo is aliased to bar" followed by the short usage for "bar" and everything is visible above the shell prompt after all that happens.

The second best case is when "git bar" simply does not support "-h" but actively notices an unknown option on the command line to give the usage message. We see "foo is aliased to bar" followed by "-h is an unknown option; supported options are ..." and everything is visible above the shell prompt after all that happens.

The worst case is when "git bar" supports or ignores "-h" and produces reams of output. Sending the "aliased to" message to the standard error means that it is scrolled out when the output is done, or lost even when "git foo -h | less" attempts to let the reader read before the early part of the output scrolls away.

Even the first two "better" cases share the same glitch if the "foo is aliased to bar" goes to the standard error output. Parse-options enabled commands tend to show a long "-h" output that you would need to say "git grep -h | less", losing the "aliased to" message.

At least it seems to me an improvement to use standard output, instead of standard error, for the alias information.

In practice, however, what the command that "git foo" is aliased to does when given "-h" is probably unknown (because the user is asking what "git foo" is in the first place), so perhaps I am worried too much. When the user does not know if the usage text comes to the standard output or to the standard error, and if the usage text is very long or not, they probably would learn quickly that the safest thing to do is to

	$ git unknown-command -h >&2 | less

And at that point, it does not matter which between the standard output and the standard error streams we write "unknown-command is aliased to ...".

So I dunno.
Jeff King· Sep 30, 2018, 04:27 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Sat, Sep 29, 2018 at 10:39:54AM -0700, Junio C Hamano wrote:
Show 12 quoted lines
> Suppose "git foo" is aliased to a command "git bar".
> 
> The best case is when "git bar -h" knows that it is asked to give us
> a short usage.  We get "foo is aliased to bar" followed by the short
> usage for "bar" and everything is visible above the shell prompt
> after all that happens.
> 
> The second best case is when "git bar" simply does not support "-h"
> but actively notices an unknown option on the command line to give
> the usage message.  We see "foo is aliased to bar" followed by "-h
> is an unknown option; supported options are ..." and everything is
> visible above the shell prompt after all that happens.
Right, these are the ones we hope for.
Show 5 quoted lines
> The worst case is when "git bar" supports or ignores "-h" and
> produces reams of output.  Sending the "aliased to" message to the
> standard error means that it is scrolled out when the output is
> done, or lost even when "git foo -h | less" attempts to let the
> reader read before the early part of the output scrolls away.
This is the "who-knows-what" case I meant here:
>> It is a little funny, I guess, if you have a script which doesn't
>> respond to "-h", because you'd get our "foo is aliased to git-bar"
>> message to stderr, followed by who-knows-what. But as long as it's to
>> stderr (and not stdout), I think it's not likely to _break_ anything.

And I think this has to be stderr. We're polluting the output of the aliased command with our extra message, so we have two choices:

  1. Pollute stderr, and risk copious stdout (or a pager) scrolling it
     off the screen.
  2. Pollute stdout, at which point our message may be confused as part
     of the actual output of the command (and that may not even be
     immediately noticed if it is passed through a shell pipeline or
     into a file).

Choice (2) seems like a regression to me. Choice (1) is unfortunate in some cases, but is no worse than today's behavior.

(Obviously I'm not including choices like not running the sub-command at all, but I think that would be even worse).

Show 7 quoted lines
> Even the first two "better" cases share the same glitch if the "foo
> is aliased to bar" goes to the standard error output.  Parse-options
> enabled commands tend to show a long "-h" output that you would need
> to say "git grep -h | less", losing the "aliased to" message.
> 
> At least it seems to me an improvement to use standard output,
> instead of standard error, for the alias information.
...so I'd disagree with this.
Show 13 quoted lines
> In practice, however, what the command that "git foo" is aliased to
> does when given "-h" is probably unknown (because the user is asking
> what "git foo" is in the first place), so perhaps I am worried too
> much.  When the user does not know if the usage text comes to the
> standard output or to the standard error, and if the usage text is
> very long or not, they probably would learn quickly that the safest
> thing to do is to
> 
> 	$ git unknown-command -h >&2 | less
> 
> And at that point, it does not matter which between the standard
> output and the standard error streams we write "unknown-command is
> aliased to ...".

Yeah. I think if "git foo -h" produces a bunch of output you didn't expect, then "git help foo" or "git foo --help" may be the next thing you reach for. That's not so different than running the command even without any aliases involved.

-Peff
Junio C Hamano· Sep 30, 2018, 05:27 UTC · re: Jeff King · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> And I think this has to be stderr. We're polluting the output of the
> aliased command with our extra message, so we have two choices:
>
>   1. Pollute stderr, and risk copious stdout (or a pager) scrolling it
>      off the screen.
>
>   2. Pollute stdout, at which point our message may be confused as part
>      of the actual output of the command (and that may not even be
>      immediately noticed if it is passed through a shell pipeline or
>      into a file).
>
> Choice (2) seems like a regression to me. Choice (1) is unfortunate in
> some cases, but is no worse than today's behavior.

I think the output of "git foo -h" changing (i.e. has "aliased to..." message in front) is about the same degree of regression as "git foo --help" no longer giving "aliased to..." information anywhere, though.

Show 14 quoted lines
>> Even the first two "better" cases share the same glitch if the "foo
>> ...
>> thing to do is to
>> 
>> 	$ git unknown-command -h >&2 | less
>> 
>> And at that point, it does not matter which between the standard
>> output and the standard error streams we write "unknown-command is
>> aliased to ...".
>
> Yeah. I think if "git foo -h" produces a bunch of output you didn't
> expect, then "git help foo" or "git foo --help" may be the next thing
> you reach for. That's not so different than running the command even
> without any aliases involved.

Hmmm. With the "teach 'git foo -h' to output 'foo is aliased to bar' to the standard error before running 'git bar -h'", plus "'git foo --help' now goes straight to 'git bar --help'", "git foo --help" no longer tells us that foo is aliased to bar. Presumably "git help foo" will still give "foo is bar" and stop?

Jeff King· Sep 30, 2018, 05:53 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Sat, Sep 29, 2018 at 10:27:15PM -0700, Junio C Hamano wrote:
Show 20 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > And I think this has to be stderr. We're polluting the output of the
> > aliased command with our extra message, so we have two choices:
> >
> >   1. Pollute stderr, and risk copious stdout (or a pager) scrolling it
> >      off the screen.
> >
> >   2. Pollute stdout, at which point our message may be confused as part
> >      of the actual output of the command (and that may not even be
> >      immediately noticed if it is passed through a shell pipeline or
> >      into a file).
> >
> > Choice (2) seems like a regression to me. Choice (1) is unfortunate in
> > some cases, but is no worse than today's behavior.
> 
> I think the output of "git foo -h" changing (i.e. has "aliased
> to..."  message in front) is about the same degree of regression as
> "git foo --help" no longer giving "aliased to..." information
> anywhere, though.

Hmm. They seem quite different to me. Changing "--help" output is something that's only going to impact what the user sees (a manpage versus the alias message). And the user can follow-up by asking for what they wanted.

Whereas if I have an alias that currently understands "-h", and I do something like:

  git foo -h | wc -l

if we output to stdout, that's going to produce subtly broken results. But if we output to stderr instead, then they may see the extra message, but it's obvious what's happening, and it's probably an annoyance at worst).

Show 10 quoted lines
> > Yeah. I think if "git foo -h" produces a bunch of output you didn't
> > expect, then "git help foo" or "git foo --help" may be the next thing
> > you reach for. That's not so different than running the command even
> > without any aliases involved.
> 
> Hmmm.  With the "teach 'git foo -h' to output 'foo is aliased to
> bar' to the standard error before running 'git bar -h'", plus "'git
> foo --help' now goes straight to 'git bar --help'", "git foo --help"
> no longer tells us that foo is aliased to bar.  Presumably "git help
> foo" will still give "foo is bar" and stop?
Yes, that was the intent in the behavior I laid out earlier.
-Peff
Rasmus Villemoes· Sep 28, 2018, 07:40 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On 2018-09-26 17:16, Junio C Hamano wrote:
Show 24 quoted lines
> Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
> 
>> +		/*
>> +		 * We use split_cmdline() to get the first word of the
>> +		 * alias, to ensure that we use the same rules as when
>> +		 * the alias is actually used. split_cmdline()
>> +		 * modifies alias in-place.
>> +		 */
>> +		count = split_cmdline(alias, &argv);
>> +		if (count < 0)
>> +			die("Bad alias.%s string: %s", cmd,
>> +			    split_cmdline_strerror(count));
>> +
>> +		if (follow_alias > 0) {
>> +			fprintf_ln(stderr,
>> +				   _("Continuing to help for %s in %0.1f seconds."),
>> +				   alias, follow_alias/10.0);
>> +			sleep_millisec(follow_alias * 100);
>> +		}
>> +		return alias;
> 
> If you have "[alias] cp = cherry-pick -n", split_cmdline discards
> "-n" and the follow-alias prompt does not even tell you that it did
> so,

That's not really true, as I deliberately did the split_cmdline after printing the "is an alias for", but before "continuing to help for", so this would precisely tell you

  cp is an alias for 'cherry-pick -n'
  continuing to help for 'cherry-pick' in 1.5 seconds
> and you get "git help cherry-pick".  This code somehow expects
> you to know to jump to the section that describes the "--no-commit"
> option.  I do not think that is a reasonable expectation.

No, in that case I would not expect git cp --help to jump to that section anymore than I would expect "git cherry-pick -n --help" to magically do that (and that would be impossible in general, if more options are bundled in the alias).

> When you have "[alias] cp = cherry-pick -n", "git cp --help" should
> not do "git help cherry-pick".  Only a single word that exactly
> matches a git command should get this treatment.

I considered that, and could certainly live with that. But it seems the discussion took a different turn in another part of the thread, so I'll continue there.

Rasmus
Junio C Hamano· Sep 28, 2018, 17:00 UTC · re: Rasmus Villemoes · lore

Re: [PATCH] help: allow redirecting to help for aliased command

Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
Show 18 quoted lines
>>> +		if (follow_alias > 0) {
>>> +			fprintf_ln(stderr,
>>> +				   _("Continuing to help for %s in %0.1f seconds."),
>>> +				   alias, follow_alias/10.0);
>>> +			sleep_millisec(follow_alias * 100);
>>> +		}
>>> +		return alias;
>> 
>> If you have "[alias] cp = cherry-pick -n", split_cmdline discards
>> "-n" and the follow-alias prompt does not even tell you that it did
>> so,
>
> That's not really true, as I deliberately did the split_cmdline after
> printing the "is an alias for", but before "continuing to help for", so
> this would precisely tell you
>
>   cp is an alias for 'cherry-pick -n'
>   continuing to help for 'cherry-pick' in 1.5 seconds

Yes, but notice that cherry-pick appears twice---I do not know about you, but I know at least my eyes will be drawn to the last mention that does not have '-n' stronger than the one before/above that line.

In any case, I think Peff's "Let's teach 'git cp -h' to prefix what 'cp' is aliased to before invoking 'git cherry-pick -n -h' (and let it fail)" approach is much more robust, so let's do that without emulating that command-typo-correction codepath.

Duy Nguyen· Sep 26, 2018, 15:16 UTC · re: Rasmus Villemoes · lore

Re: [PATCH] help: allow redirecting to help for aliased command

On Wed, Sep 26, 2018 at 12:29 PM Rasmus Villemoes <rv@rasmusvillemoes.dk> wrote:
Show 18 quoted lines
>
> I often use 'git <cmd> --help' as a quick way to get the documentation
> for a command. However, I've also trained my muscle memory to use my
> aliases (cp=cherry-pick, co=checkout etc.), which means that I often end
> up doing
>
>   git cp --help
>
> to which git correctly informs me that cp is an alias for
> cherry-pick. However, I already knew that, and what I really wanted was
> the man page for the cherry-pick command.
>
> This introduces a help.followAlias config option that transparently
> redirects to (the first word of) the alias text (provided of course it
> is not a shell command), similar to the option for autocorrect of
> misspelled commands.
>
> The documentation in config.txt could probably be improved.

While at there, maybe you could also mention the behavior of "git help" when given an alias, in git-help.txt. And you could also add a hint to suggest this new config help.followAlias there.

-- 
Duy
Rasmus Villemoes· Oct 1, 2018, 11:21 UTC · re: Rasmus Villemoes · lore

[PATCH v2 1/3] 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.

(0) When invoked as "git help foo", we continue to print the "foo is aliased to bar" message and nothing else.

(1) If foo is an alias for a shell command, print "foo is aliased to !bar" as usual.

(2) Otherwise, break the alias string into words, and pretend that "git word0 --help" was called.

At least for me, getting the man page for git-cherry-pick directly with "git cp --help" is more useful (and how I expect an alias to behave) than the short "is aliased to" notice. It is also consistent with "--help" generally providing more comprehensive help than "-h".

I believe that printing the "is aliased to" message also in case (2) has value: Depending on pager setup, or if the user has help.format=web, the message is still present immediately above the prompt when the user quits the pager/returns to the terminal. That serves as an explanation for why one was redirected to "man git-cherry-pick" from "git cp --help", and if cp is actually 'cherry-pick -n', it reminds the user that using cp has some flag implicitly set before firing off the next command.

It also provides some useful info in case we end up erroring out, either in the "bad alias string" check, or in the "No manual entry for gitbar" case.

[1] https://public-inbox.org/git/20180926102636.30691-1-rv@rasmusvillemoes.dk/ [2] https://public-inbox.org/git/20180926184914.GC30680@sigill.intra.peff.net/

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 builtin/help.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
Show changes to builtin/help.c +23 −3
diff --git a/builtin/help.c b/builtin/help.c
index 8d4f6dd301..4802a06f37 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -415,9 +415,29 @@ static const char *check_git_cmd(const char* cmd)
 
 	alias = alias_lookup(cmd);
 	if (alias) {
-		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
-		free(alias);
-		exit(0);
+		const char **argv;
+		int count;
+
+		/*
+		 * If we were invoked as "git help cmd", or cmd is an
+		 * alias for a shell command, we inform the user what
+		 * cmd is an alias for and do nothing else.
+		 */
+		if (!exclude_guides || alias[0] == '!') {
+			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
+			free(alias);
+			exit(0);
+		}
+		/*
+		 * Otherwise, we pretend that the command was "git
+		 * word0 --help.
+		 */
+		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
+		count = split_cmdline(alias, &argv);
+		if (count < 0)
+			die(_("bad alias.%s string: %s"), cmd,
+			    split_cmdline_strerror(count));
+		return alias;
 	}
 
 	if (exclude_guides)
-- 
2.19.0
Rasmus Villemoes· Oct 1, 2018, 11:21 UTC · re: Rasmus Villemoes · lore

[PATCH v2 2/3] git.c: handle_alias: prepend alias info when first argument is -h

Most git commands respond to -h anywhere in the command line, or at least as a first and lone argument, by printing the usage information. For aliases, we can provide a little more information that might be useful in interpreting/understanding the following output by prepending a line telling that the command is an alias, and for what.

When one invokes a simple alias, such as "cp = cherry-pick" with -h, this results in

$ git cp -h 'cp' is aliased to 'cherry-pick' usage: git cherry-pick [<options>] <commit-ish>... ...

When the alias consists of more than one word, this provides the additional benefit of informing the user which options are implicit in using the alias, e.g. with "cp = cherry-pick -n":

$ git cp -h 'cp' is aliased to 'cherry-pick -n' usage: git cherry-pick [<options>] <commit-ish>... ...

For shell commands, we cannot know how it responds to -h, but printing this line to stderr should not hurt, and can help in figuring out what is happening in a case like

$ git sc -h 'sc' is aliased to '!somecommand' somecommand: invalid option '-h'

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 git.c | 3 +++
 1 file changed, 3 insertions(+)
Show changes to git.c +3 −0
diff --git a/git.c b/git.c
index a6f4b44af5..0211c2d4c0 100644
--- a/git.c
+++ b/git.c
@@ -318,6 +318,9 @@ static int handle_alias(int *argcp, const char ***argv)
 	alias_command = (*argv)[0];
 	alias_string = alias_lookup(alias_command);
 	if (alias_string) {
+		if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
+			fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
+				   alias_command, alias_string);
 		if (alias_string[0] == '!') {
 			struct child_process child = CHILD_PROCESS_INIT;
 			int nongit_ok;
-- 
2.19.0
Jeff King· Oct 3, 2018, 02:16 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v2 2/3] git.c: handle_alias: prepend alias info when first argument is -h

On Mon, Oct 01, 2018 at 01:21:06PM +0200, Rasmus Villemoes wrote:
Show 30 quoted lines
> Most git commands respond to -h anywhere in the command line, or at
> least as a first and lone argument, by printing the usage
> information. For aliases, we can provide a little more information that
> might be useful in interpreting/understanding the following output by
> prepending a line telling that the command is an alias, and for what.
> 
> When one invokes a simple alias, such as "cp = cherry-pick"
> with -h, this results in
> 
> $ git cp -h
> 'cp' is aliased to 'cherry-pick'
> usage: git cherry-pick [<options>] <commit-ish>...
> ...
> 
> When the alias consists of more than one word, this provides the
> additional benefit of informing the user which options are implicit in
> using the alias, e.g. with "cp = cherry-pick -n":
> 
> $ git cp -h
> 'cp' is aliased to 'cherry-pick -n'
> usage: git cherry-pick [<options>] <commit-ish>...
> ...
> 
> For shell commands, we cannot know how it responds to -h, but printing
> this line to stderr should not hurt, and can help in figuring out what
> is happening in a case like
> 
> $ git sc -h
> 'sc' is aliased to '!somecommand'
> somecommand: invalid option '-h'
Nicely explained.
Show 14 quoted lines
> diff --git a/git.c b/git.c
> index a6f4b44af5..0211c2d4c0 100644
> --- a/git.c
> +++ b/git.c
> @@ -318,6 +318,9 @@ static int handle_alias(int *argcp, const char ***argv)
>  	alias_command = (*argv)[0];
>  	alias_string = alias_lookup(alias_command);
>  	if (alias_string) {
> +		if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
> +			fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
> +				   alias_command, alias_string);
>  		if (alias_string[0] == '!') {
>  			struct child_process child = CHILD_PROCESS_INIT;
>  			int nongit_ok;
And the implementation makes sense.
-Peff
Rasmus Villemoes· Oct 1, 2018, 11:21 UTC · re: Rasmus Villemoes · lore

[PATCH v2 3/3] git-help.txt: document "git help cmd" vs "git cmd --help" for aliases

This documents the existing behaviour of "git help cmd" when cmd is an alias, as well as providing a hint to use the "git cmd --help" form to be taken directly to the man page for the aliased command.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 Documentation/git-help.txt | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to Documentation/git-help.txt +4 −0
diff --git a/Documentation/git-help.txt b/Documentation/git-help.txt
index 83d25d825a..37e85868fd 100644
--- a/Documentation/git-help.txt
+++ b/Documentation/git-help.txt
@@ -29,6 +29,10 @@ guide is brought up. The 'man' program is used by default for this
 purpose, but this can be overridden by other options or configuration
 variables.
 
+If an alias is given, git prints a note explaining what it is an alias
+for on standard output. To get the manual page for the aliased
+command, use `git COMMAND --help`.
+
 Note that `git --help ...` is identical to `git help ...` because the
 former is internally converted into the latter.
 
-- 
2.19.0
Jeff King· Oct 3, 2018, 02:18 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v2 3/3] git-help.txt: document "git help cmd" vs "git cmd --help" for aliases

On Mon, Oct 01, 2018 at 01:21:07PM +0200, Rasmus Villemoes wrote:
> This documents the existing behaviour of "git help cmd" when cmd is an
> alias, as well as providing a hint to use the "git cmd --help" form to
> be taken directly to the man page for the aliased command.
Good idea.
Show 11 quoted lines
> diff --git a/Documentation/git-help.txt b/Documentation/git-help.txt
> index 83d25d825a..37e85868fd 100644
> --- a/Documentation/git-help.txt
> +++ b/Documentation/git-help.txt
> @@ -29,6 +29,10 @@ guide is brought up. The 'man' program is used by default for this
>  purpose, but this can be overridden by other options or configuration
>  variables.
>  
> +If an alias is given, git prints a note explaining what it is an alias
> +for on standard output. To get the manual page for the aliased
> +command, use `git COMMAND --help`.
Funny English: "what it is an...". Maybe:
  If an alias is given, git shows the definition of the alias on
  standard output. To get the manual page...
-Peff
Rasmus Villemoes· Oct 3, 2018, 06:25 UTC · re: Jeff King · lore

Re: [PATCH v2 3/3] git-help.txt: document "git help cmd" vs "git cmd --help" for aliases

On 2018-10-03 04:18, Jeff King wrote:
Show 11 quoted lines
> On Mon, Oct 01, 2018 at 01:21:07PM +0200, Rasmus Villemoes wrote:
> 
>>  
>> +If an alias is given, git prints a note explaining what it is an alias
>> +for on standard output. To get the manual page for the aliased
>> +command, use `git COMMAND --help`.
> 
> Funny English: "what it is an...". Maybe:
> 
>   If an alias is given, git shows the definition of the alias on
>   standard output. To get the manual page...
Much better, thanks.
Rasmus
Jeff King· Oct 3, 2018, 02:13 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v2 1/3] help: redirect to aliased commands for "git cmd --help"

On Mon, Oct 01, 2018 at 01:21:05PM +0200, Rasmus Villemoes wrote:
Show 16 quoted lines
> 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.
> 
> (0) When invoked as "git help foo", we continue to print the "foo is
> aliased to bar" message and nothing else.
> 
> (1) If foo is an alias for a shell command, print "foo is aliased to
> !bar" as usual.
> 
> (2) Otherwise, break the alias string into words, and pretend that "git
> word0 --help" was called.
> 
> At least for me, getting the man page for git-cherry-pick directly with
> "git cp --help" is more useful (and how I expect an alias to behave)
> than the short "is aliased to" notice. It is also consistent with
> "--help" generally providing more comprehensive help than "-h".
Makes sense.
Show 12 quoted lines
> I believe that printing the "is aliased to" message also in case (2) has
> value: Depending on pager setup, or if the user has help.format=web, the
> message is still present immediately above the prompt when the user
> quits the pager/returns to the terminal. That serves as an explanation
> for why one was redirected to "man git-cherry-pick" from "git cp
> --help", and if cp is actually 'cherry-pick -n', it reminds the user
> that using cp has some flag implicitly set before firing off the next
> command.
> 
> It also provides some useful info in case we end up erroring out, either
> in the "bad alias string" check, or in the "No manual entry for gitbar"
> case.

OK, I buy that line of reasoning. And in the other cases, it shouldn't _hurt_ anything.

Show 24 quoted lines
> diff --git a/builtin/help.c b/builtin/help.c
> index 8d4f6dd301..4802a06f37 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -415,9 +415,29 @@ static const char *check_git_cmd(const char* cmd)
>  
>  	alias = alias_lookup(cmd);
>  	if (alias) {
> -		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> -		free(alias);
> -		exit(0);
> +		const char **argv;
> +		int count;
> +
> +		/*
> +		 * If we were invoked as "git help cmd", or cmd is an
> +		 * alias for a shell command, we inform the user what
> +		 * cmd is an alias for and do nothing else.
> +		 */
> +		if (!exclude_guides || alias[0] == '!') {
> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> +			free(alias);
> +			exit(0);
> +		}

I'm not sure I understand why exclude_guides is relevant. We check it below when we know that we _don't_ have an alias. Hrm. I guess you're using it here as a proxy for "git foo --help" being used instead of "git help foo". The comment probably needs to spell out that exclude_guides is the same as your "we were invoked as...".

I wonder if we could change the name of that option. It is an undocumented, hidden option that we use internally, so it should be OK to do so (or we could always add another one). That might prevent somebody in the future from using --exclude-guides in more places and breaking your assumption here.

Show 10 quoted lines
> +		/*
> +		 * Otherwise, we pretend that the command was "git
> +		 * word0 --help.
> +		 */
> +		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
> +		count = split_cmdline(alias, &argv);
> +		if (count < 0)
> +			die(_("bad alias.%s string: %s"), cmd,
> +			    split_cmdline_strerror(count));
> +		return alias;

So we split only to find argv[0] here. But then we don't return it. That works because the split is done in place, meaning we must have inserted a NUL in alias. That's sufficiently subtle that it might be worth spelling it out in a comment.

We don't need to free alias here as we do above, because we're passing it back. We should free argv, though, I think (not its elements, just the array itself).

Unfortunately the caller is going to leak our returned "alias", but I'm not sure we can do much about it. I'm not overly concerned with the memory, but it is going to trigger leak-checkers (and we're trying to quiet them down, not go the other way). I think it may be OK to overlook that and just UNLEAK() it in cmd_help().

-Peff
Rasmus Villemoes· Oct 3, 2018, 06:24 UTC · re: Jeff King · lore

Re: [PATCH v2 1/3] help: redirect to aliased commands for "git cmd --help"

On 2018-10-03 04:13, Jeff King wrote:
Show 15 quoted lines
>> +		/*
>> +		 * If we were invoked as "git help cmd", or cmd is an
>> +		 * alias for a shell command, we inform the user what
>> +		 * cmd is an alias for and do nothing else.
>> +		 */
>> +		if (!exclude_guides || alias[0] == '!') {
>> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
>> +			free(alias);
>> +			exit(0);
>> +		}
> 
> I'm not sure I understand why exclude_guides is relevant. We check it
> below when we know that we _don't_ have an alias. Hrm. I guess you're
> using it here as a proxy for "git foo --help" being used instead of "git
> help foo".

Exactly. Perhaps it's abusing the existing machinery, but I didn't know how else to distinguish the two cases, and didn't feel like introducing another way of passing on the exact same information.

> The comment probably needs to spell out that exclude_guides
> is the same as your "we were invoked as...".

Will do. That will also make the string --exclude-guides (i.e., with a dash) appear in the comment, making it more likely to be found should anyone change when and how --exclude-guides is implied.

Show 5 quoted lines
> I wonder if we could change the name of that option. It is an
> undocumented, hidden option that we use internally, so it should be OK
> to do so (or we could always add another one). That might prevent
> somebody in the future from using --exclude-guides in more places and
> breaking your assumption here.

Perhaps, but I think that's better left for a separate patch, if really necessary even with the expanded comment.

Show 10 quoted lines
>> +		count = split_cmdline(alias, &argv);
>> +		if (count < 0)
>> +			die(_("bad alias.%s string: %s"), cmd,
>> +			    split_cmdline_strerror(count));
>> +		return alias;
> 
> So we split only to find argv[0] here. But then we don't return it. That
> works because the split is done in place, meaning we must have inserted
> a NUL in alias. That's sufficiently subtle that it might be worth
> spelling it out in a comment.
OK, I actually had precisely

+ /* + * We use split_cmdline() to get the first word of the + * alias, to ensure that we use the same rules as when + * the alias is actually used. split_cmdline() + * modifies alias in-place. + */

in v1, but thought it might be overly verbose. I'll put it back in.
> We don't need to free alias here as we do above, because we're passing
> it back. We should free argv, though, I think (not its elements, just
> the array itself).

Yeah, I thought about this, and removing free(argv) was the last thing I did before sending v1 - because we were going to leak alias anyway. I'm happy to put it back in, along with...

> Unfortunately the caller is going to leak our returned "alias", [...] I think it may be OK to overlook
> that and just UNLEAK() it in cmd_help().

...this. Except I'd rather do the UNLEAK in check_git_cmd (the documentation does say "only from cmd_* functions or their direct helpers") to make it a more targeted annotation.

Thanks, Rasmus

Jeff King· Oct 3, 2018, 07:06 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v2 1/3] help: redirect to aliased commands for "git cmd --help"

On Wed, Oct 03, 2018 at 08:24:14AM +0200, Rasmus Villemoes wrote:
Show 6 quoted lines
> > The comment probably needs to spell out that exclude_guides
> > is the same as your "we were invoked as...".
> 
> Will do. That will also make the string --exclude-guides (i.e., with a
> dash) appear in the comment, making it more likely to be found should
> anyone change when and how --exclude-guides is implied.
OK. I can live with that.
Show 15 quoted lines
> > So we split only to find argv[0] here. But then we don't return it. That
> > works because the split is done in place, meaning we must have inserted
> > a NUL in alias. That's sufficiently subtle that it might be worth
> > spelling it out in a comment.
> 
> OK, I actually had precisely
> 
> +		/*
> +		 * We use split_cmdline() to get the first word of the
> +		 * alias, to ensure that we use the same rules as when
> +		 * the alias is actually used. split_cmdline()
> +		 * modifies alias in-place.
> +		 */
> 
> in v1, but thought it might be overly verbose. I'll put it back in.
:) That's perfect.
Show 7 quoted lines
> > We don't need to free alias here as we do above, because we're passing
> > it back. We should free argv, though, I think (not its elements, just
> > the array itself).
> 
> Yeah, I thought about this, and removing free(argv) was the last thing I
> did before sending v1 - because we were going to leak alias anyway. I'm
> happy to put it back in, along with...

Thanks. I think this is different than "alias" because we really do leak it _here_, whereas alias lives on and can be UNLEAKed later.

Show 6 quoted lines
> > Unfortunately the caller is going to leak our returned "alias", [...] I think it may be OK to overlook
> > that and just UNLEAK() it in cmd_help().
> 
> ...this. Except I'd rather do the UNLEAK in check_git_cmd (the
> documentation does say "only from cmd_* functions or their direct
> helpers") to make it a more targeted annotation.
Yeah, I think that's fine. Thanks!
-Peff
Rasmus Villemoes· Oct 3, 2018, 11:42 UTC · re: Rasmus Villemoes · lore

[PATCH v3 0/3] alias help tweaks

v2: Added patches 2 and 3, made "git cmd --help" unconditionally (no config option, no delay) redirect to the aliased command's help, preserve pre-existing behaviour of the spelling "git help cmd".

v3: Add some additional comments in patch 1 and avoid triggering leak checker reports. Use better wording in patch 3.

Rasmus Villemoes (3):
  help: redirect to aliased commands for "git cmd --help"
  git.c: handle_alias: prepend alias info when first argument is -h
  git-help.txt: document "git help cmd" vs "git cmd --help" for aliases
 Documentation/git-help.txt |  4 ++++
 builtin/help.c             | 34 +++++++++++++++++++++++++++++++---
 git.c                      |  3 +++
 3 files changed, 38 insertions(+), 3 deletions(-)
-- 
2.19.0
Rasmus Villemoes· Oct 3, 2018, 11:42 UTC · re: Rasmus Villemoes · lore

[PATCH v3 1/3] 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.

(0) When invoked as "git help foo", we continue to print the "foo is aliased to bar" message and nothing else.

(1) If foo is an alias for a shell command, print "foo is aliased to !bar" as usual.

(2) Otherwise, break the alias string into words, and pretend that "git word0 --help" was called.

At least for me, getting the man page for git-cherry-pick directly with "git cp --help" is more useful (and how I expect an alias to behave) than the short "is aliased to" notice. It is also consistent with "--help" generally providing more comprehensive help than "-h".

I believe that printing the "is aliased to" message also in case (2) has value: Depending on pager setup, or if the user has help.format=web, the message is still present immediately above the prompt when the user quits the pager/returns to the terminal. That serves as an explanation for why one was redirected to "man git-cherry-pick" from "git cp --help", and if cp is actually 'cherry-pick -n', it reminds the user that using cp has some flag implicitly set before firing off the next command.

It also provides some useful info in case we end up erroring out, either in the "bad alias string" check, or in the "No manual entry for gitbar" case.

[1] https://public-inbox.org/git/20180926102636.30691-1-rv@rasmusvillemoes.dk/ [2] https://public-inbox.org/git/20180926184914.GC30680@sigill.intra.peff.net/

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 builtin/help.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
Show changes to builtin/help.c +31 −3
diff --git a/builtin/help.c b/builtin/help.c
index 8d4f6dd301..e0e3fe62e9 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -415,9 +415,37 @@ static const char *check_git_cmd(const char* cmd)
 
 	alias = alias_lookup(cmd);
 	if (alias) {
-		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
-		free(alias);
-		exit(0);
+		const char **argv;
+		int count;
+
+		/*
+		 * handle_builtin() in git.c rewrites "git cmd --help"
+		 * to "git help --exclude-guides cmd", so we can use
+		 * exclude_guides to distinguish "git cmd --help" from
+		 * "git help cmd". In the latter case, or if cmd is an
+		 * alias for a shell command, just print the alias
+		 * definition.
+		 */
+		if (!exclude_guides || alias[0] == '!') {
+			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
+			free(alias);
+			exit(0);
+		}
+		/*
+		 * Otherwise, we pretend that the command was "git
+		 * word0 --help". We use split_cmdline() to get the
+		 * first word of the alias, to ensure that we use the
+		 * same rules as when the alias is actually
+		 * used. split_cmdline() modifies alias in-place.
+		 */
+		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
+		count = split_cmdline(alias, &argv);
+		if (count < 0)
+			die(_("bad alias.%s string: %s"), cmd,
+			    split_cmdline_strerror(count));
+		free(argv);
+		UNLEAK(alias);
+		return alias;
 	}
 
 	if (exclude_guides)
-- 
2.19.0
Junio C Hamano· Oct 5, 2018, 08:19 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v3 1/3] help: redirect to aliased commands for "git cmd --help"

Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
Show 29 quoted lines
> 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.
>
> (0) When invoked as "git help foo", we continue to print the "foo is
> aliased to bar" message and nothing else.
>
> (1) If foo is an alias for a shell command, print "foo is aliased to
> !bar" as usual.
>
> (2) Otherwise, break the alias string into words, and pretend that "git
> word0 --help" was called.
>
> At least for me, getting the man page for git-cherry-pick directly with
> "git cp --help" is more useful (and how I expect an alias to behave)
> than the short "is aliased to" notice. It is also consistent with
> "--help" generally providing more comprehensive help than "-h".
>
> I believe that printing the "is aliased to" message also in case (2) has
> value: Depending on pager setup, or if the user has help.format=web, the
> message is still present immediately above the prompt when the user
> quits the pager/returns to the terminal. That serves as an explanation
> for why one was redirected to "man git-cherry-pick" from "git cp
> --help", and if cp is actually 'cherry-pick -n', it reminds the user
> that using cp has some flag implicitly set before firing off the next
> command.
>
> It also provides some useful info in case we end up erroring out, either
> in the "bad alias string" check, or in the "No manual entry for gitbar"
> case.

These two paragraphs were misleading, because they sounded as if you were lamenting that you were somehow forbidden from doing so even though you believe doing it is the right thing.

But that is not what is happening. I think we should update the (2) above to mention what you actually do in the code, perhaps like so:

    (2) Otherwise, show "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.  The former
        is necessary to help users when 'foo' is aliased to a
        command with an option (e.g. "[alias] cp = cherry-pick -n"),
        and hopefully remain visible when help.format=web is used,
        "git bar --help" errors out, or the manpage of "git bar" is
        short enough. It may not help if the help shows manpage on
        the terminal as usual, though.

As we explain why we show the alias information before going to the manpage in the item itself and a brief discussion of pros-and-cons, we can safely lose the "I believe..." paragraph, which looks somewhat out of place in a log message.

It also is strange to count from (0); if the patchset is rerolled again, I'd prefer to see these start counting from (1), in which case this item will become (3).

Show 53 quoted lines
> [1] https://public-inbox.org/git/20180926102636.30691-1-rv@rasmusvillemoes.dk/
> [2] https://public-inbox.org/git/20180926184914.GC30680@sigill.intra.peff.net/
>
> Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
> ---
>  builtin/help.c | 34 +++++++++++++++++++++++++++++++---
>  1 file changed, 31 insertions(+), 3 deletions(-)
>
> diff --git a/builtin/help.c b/builtin/help.c
> index 8d4f6dd301..e0e3fe62e9 100644
> --- a/builtin/help.c
> +++ b/builtin/help.c
> @@ -415,9 +415,37 @@ static const char *check_git_cmd(const char* cmd)
>  
>  	alias = alias_lookup(cmd);
>  	if (alias) {
> -		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> -		free(alias);
> -		exit(0);
> +		const char **argv;
> +		int count;
> +
> +		/*
> +		 * handle_builtin() in git.c rewrites "git cmd --help"
> +		 * to "git help --exclude-guides cmd", so we can use
> +		 * exclude_guides to distinguish "git cmd --help" from
> +		 * "git help cmd". In the latter case, or if cmd is an
> +		 * alias for a shell command, just print the alias
> +		 * definition.
> +		 */
> +		if (!exclude_guides || alias[0] == '!') {
> +			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
> +			free(alias);
> +			exit(0);
> +		}
> +		/*
> +		 * Otherwise, we pretend that the command was "git
> +		 * word0 --help". We use split_cmdline() to get the
> +		 * first word of the alias, to ensure that we use the
> +		 * same rules as when the alias is actually
> +		 * used. split_cmdline() modifies alias in-place.
> +		 */
> +		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
> +		count = split_cmdline(alias, &argv);
> +		if (count < 0)
> +			die(_("bad alias.%s string: %s"), cmd,
> +			    split_cmdline_strerror(count));
> +		free(argv);
> +		UNLEAK(alias);
> +		return alias;
>  	}
>  
>  	if (exclude_guides)
Rasmus Villemoes· Oct 5, 2018, 10:22 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 1/3] help: redirect to aliased commands for "git cmd --help"

On 2018-10-05 10:19, Junio C Hamano wrote:
Show 22 quoted lines
> Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
> 
>>
>> I believe that printing the "is aliased to" message also in case (2) has
>> value: Depending on pager setup, or if the user has help.format=web, the
>> message is still present immediately above the prompt when the user
>> quits the pager/returns to the terminal. That serves as an explanation
>> for why one was redirected to "man git-cherry-pick" from "git cp
>> --help", and if cp is actually 'cherry-pick -n', it reminds the user
>> that using cp has some flag implicitly set before firing off the next
>> command.
>>
>> It also provides some useful info in case we end up erroring out, either
>> in the "bad alias string" check, or in the "No manual entry for gitbar"
>> case.
> 
> These two paragraphs were misleading, because they sounded as if you
> were lamenting that you were somehow forbidden from doing so even
> though you believe doing it is the right thing.
> 
> But that is not what is happening.  I think we should update the (2)
> above to mention what you actually do in the code, perhaps like so:
Yes, what I wrote was probably better placed below ---.
>         and hopefully remain visible when help.format=web is used,
>        "git bar --help" errors out, or the manpage of "git bar" is
>        short enough. It may not help if the help shows manpage on

or, as in my case, the pager does not clear the terminal. I even think that's the default behaviour (due to X in $LESS) - at least, I don't have any magic in the environment or .gitconfig to achieve this. So it's not visible while the man page is shown in the pager, but upon exit from the pager.

> It also is strange to count from (0); if the patchset is rerolled
> again, I'd prefer to see these start counting from (1), in which
> case this item will become (3).
If you prefer, I can send a v4.
Rasmus
Junio C Hamano· Oct 5, 2018, 16:47 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v3 1/3] help: redirect to aliased commands for "git cmd --help"

Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
Show 5 quoted lines
>> It also is strange to count from (0); if the patchset is rerolled
>> again, I'd prefer to see these start counting from (1), in which
>> case this item will become (3).
>
> If you prefer, I can send a v4.
Sure, if you prefer, you can send a v4 for me to look at and queue.
Thanks.
Rasmus Villemoes· Oct 3, 2018, 11:42 UTC · re: Rasmus Villemoes · lore

[PATCH v3 2/3] git.c: handle_alias: prepend alias info when first argument is -h

Most git commands respond to -h anywhere in the command line, or at least as a first and lone argument, by printing the usage information. For aliases, we can provide a little more information that might be useful in interpreting/understanding the following output by prepending a line telling that the command is an alias, and for what.

When one invokes a simple alias, such as "cp = cherry-pick" with -h, this results in

$ git cp -h 'cp' is aliased to 'cherry-pick' usage: git cherry-pick [<options>] <commit-ish>... ...

When the alias consists of more than one word, this provides the additional benefit of informing the user which options are implicit in using the alias, e.g. with "cp = cherry-pick -n":

$ git cp -h 'cp' is aliased to 'cherry-pick -n' usage: git cherry-pick [<options>] <commit-ish>... ...

For shell commands, we cannot know how it responds to -h, but printing this line to stderr should not hurt, and can help in figuring out what is happening in a case like

$ git sc -h 'sc' is aliased to '!somecommand' somecommand: invalid option '-h'

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 git.c | 3 +++
 1 file changed, 3 insertions(+)
Show changes to git.c +3 −0
diff --git a/git.c b/git.c
index a6f4b44af5..0211c2d4c0 100644
--- a/git.c
+++ b/git.c
@@ -318,6 +318,9 @@ static int handle_alias(int *argcp, const char ***argv)
 	alias_command = (*argv)[0];
 	alias_string = alias_lookup(alias_command);
 	if (alias_string) {
+		if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
+			fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
+				   alias_command, alias_string);
 		if (alias_string[0] == '!') {
 			struct child_process child = CHILD_PROCESS_INIT;
 			int nongit_ok;
-- 
2.19.0
Rasmus Villemoes· Oct 3, 2018, 11:42 UTC · re: Rasmus Villemoes · lore

[PATCH v3 3/3] git-help.txt: document "git help cmd" vs "git cmd --help" for aliases

This documents the existing behaviour of "git help cmd" when cmd is an alias, as well as providing a hint to use the "git cmd --help" form to be taken directly to the man page for the aliased command.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 Documentation/git-help.txt | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to Documentation/git-help.txt +4 −0
diff --git a/Documentation/git-help.txt b/Documentation/git-help.txt
index 83d25d825a..86a6b42345 100644
--- a/Documentation/git-help.txt
+++ b/Documentation/git-help.txt
@@ -29,6 +29,10 @@ guide is brought up. The 'man' program is used by default for this
 purpose, but this can be overridden by other options or configuration
 variables.
 
+If an alias is given, git shows the definition of the alias on
+standard output. To get the manual page for the aliased command, use
+`git COMMAND --help`.
+
 Note that `git --help ...` is identical to `git help ...` because the
 former is internally converted into the latter.
 
-- 
2.19.0
Jeff King· Oct 4, 2018, 00:10 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v3 0/3] alias help tweaks

On Wed, Oct 03, 2018 at 01:42:39PM +0200, Rasmus Villemoes wrote:
Show 6 quoted lines
> v2: Added patches 2 and 3, made "git cmd --help" unconditionally (no
> config option, no delay) redirect to the aliased command's help,
> preserve pre-existing behaviour of the spelling "git help cmd".
> 
> v3: Add some additional comments in patch 1 and avoid triggering leak
> checker reports. Use better wording in patch 3.
Thanks, v3 looks good to me!
-Peff
Rasmus Villemoes· Oct 9, 2018, 11:59 UTC · re: Rasmus Villemoes · lore

[PATCH v4 0/3] alias help tweaks

v2: Added patches 2 and 3, made "git cmd --help" unconditionally (no config option, no delay) redirect to the aliased command's help, preserve pre-existing behaviour of the spelling "git help cmd".

v3: Add some additional comments in patch 1 and avoid triggering leak checker reports. Use better wording in patch 3.

v4: Reword commit log in patch 1.
Rasmus Villemoes (3):
  help: redirect to aliased commands for "git cmd --help"
  git.c: handle_alias: prepend alias info when first argument is -h
  git-help.txt: document "git help cmd" vs "git cmd --help" for aliases
 Documentation/git-help.txt |  4 ++++
 builtin/help.c             | 34 +++++++++++++++++++++++++++++++---
 git.c                      |  3 +++
 3 files changed, 38 insertions(+), 3 deletions(-)
-- 
2.19.1.4.g721af0fda3
Rasmus Villemoes· Oct 9, 2018, 11:59 UTC · re: Rasmus Villemoes · lore

[PATCH v4 1/3] 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/20180926102636.30691-1-rv@rasmusvillemoes.dk/ [2] https://public-inbox.org/git/20180926184914.GC30680@sigill.intra.peff.net/

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 builtin/help.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
Show changes to builtin/help.c +31 −3
diff --git a/builtin/help.c b/builtin/help.c
index 8d4f6dd301..e0e3fe62e9 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -415,9 +415,37 @@ static const char *check_git_cmd(const char* cmd)
 
 	alias = alias_lookup(cmd);
 	if (alias) {
-		printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
-		free(alias);
-		exit(0);
+		const char **argv;
+		int count;
+
+		/*
+		 * handle_builtin() in git.c rewrites "git cmd --help"
+		 * to "git help --exclude-guides cmd", so we can use
+		 * exclude_guides to distinguish "git cmd --help" from
+		 * "git help cmd". In the latter case, or if cmd is an
+		 * alias for a shell command, just print the alias
+		 * definition.
+		 */
+		if (!exclude_guides || alias[0] == '!') {
+			printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
+			free(alias);
+			exit(0);
+		}
+		/*
+		 * Otherwise, we pretend that the command was "git
+		 * word0 --help". We use split_cmdline() to get the
+		 * first word of the alias, to ensure that we use the
+		 * same rules as when the alias is actually
+		 * used. split_cmdline() modifies alias in-place.
+		 */
+		fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
+		count = split_cmdline(alias, &argv);
+		if (count < 0)
+			die(_("bad alias.%s string: %s"), cmd,
+			    split_cmdline_strerror(count));
+		free(argv);
+		UNLEAK(alias);
+		return alias;
 	}
 
 	if (exclude_guides)
-- 
2.19.1.4.g721af0fda3
Rasmus Villemoes· Oct 9, 2018, 11:59 UTC · re: Rasmus Villemoes · lore

[PATCH v4 2/3] git.c: handle_alias: prepend alias info when first argument is -h

Most git commands respond to -h anywhere in the command line, or at least as a first and lone argument, by printing the usage information. For aliases, we can provide a little more information that might be useful in interpreting/understanding the following output by prepending a line telling that the command is an alias, and for what.

When one invokes a simple alias, such as "cp = cherry-pick" with -h, this results in

$ git cp -h 'cp' is aliased to 'cherry-pick' usage: git cherry-pick [<options>] <commit-ish>... ...

When the alias consists of more than one word, this provides the additional benefit of informing the user which options are implicit in using the alias, e.g. with "cp = cherry-pick -n":

$ git cp -h 'cp' is aliased to 'cherry-pick -n' usage: git cherry-pick [<options>] <commit-ish>... ...

For shell commands, we cannot know how it responds to -h, but printing this line to stderr should not hurt, and can help in figuring out what is happening in a case like

$ git sc -h 'sc' is aliased to '!somecommand' somecommand: invalid option '-h'

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 git.c | 3 +++
 1 file changed, 3 insertions(+)
Show changes to git.c +3 −0
diff --git a/git.c b/git.c
index a6f4b44af5..0211c2d4c0 100644
--- a/git.c
+++ b/git.c
@@ -318,6 +318,9 @@ static int handle_alias(int *argcp, const char ***argv)
 	alias_command = (*argv)[0];
 	alias_string = alias_lookup(alias_command);
 	if (alias_string) {
+		if (*argcp > 1 && !strcmp((*argv)[1], "-h"))
+			fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
+				   alias_command, alias_string);
 		if (alias_string[0] == '!') {
 			struct child_process child = CHILD_PROCESS_INIT;
 			int nongit_ok;
-- 
2.19.1.4.g721af0fda3
Rasmus Villemoes· Oct 9, 2018, 11:59 UTC · re: Rasmus Villemoes · lore

[PATCH v4 3/3] git-help.txt: document "git help cmd" vs "git cmd --help" for aliases

This documents the existing behaviour of "git help cmd" when cmd is an alias, as well as providing a hint to use the "git cmd --help" form to be taken directly to the man page for the aliased command.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 Documentation/git-help.txt | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to Documentation/git-help.txt +4 −0
diff --git a/Documentation/git-help.txt b/Documentation/git-help.txt
index 83d25d825a..86a6b42345 100644
--- a/Documentation/git-help.txt
+++ b/Documentation/git-help.txt
@@ -29,6 +29,10 @@ guide is brought up. The 'man' program is used by default for this
 purpose, but this can be overridden by other options or configuration
 variables.
 
+If an alias is given, git shows the definition of the alias on
+standard output. To get the manual page for the aliased command, use
+`git COMMAND --help`.
+
 Note that `git --help ...` is identical to `git help ...` because the
 former is internally converted into the latter.
 
-- 
2.19.1.4.g721af0fda3
Junio C Hamano· Oct 12, 2018, 03:17 UTC · re: Rasmus Villemoes · lore

Re: [PATCH v4 0/3] alias help tweaks

Rasmus Villemoes <rv@rasmusvillemoes.dk> writes:
Show 8 quoted lines
> v2: Added patches 2 and 3, made "git cmd --help" unconditionally (no
> config option, no delay) redirect to the aliased command's help,
> preserve pre-existing behaviour of the spelling "git help cmd".
>
> v3: Add some additional comments in patch 1 and avoid triggering leak
> checker reports. Use better wording in patch 3.
>
> v4: Reword commit log in patch 1.

Sorry for failing to point it out and let the style carried over to v4, but the above is insufficient for a cover latter. Those who missed an earlier round has no clue what these patches are about, and there is not even a pointer to find an earlier discussion in the list archive.

I think the patches are good with the rounds of reviews it went through, so let's merge it to 'next'. Here is what I plan to start the merge message of the series:

     "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.
Please do the cover-letter better next time.
Thanks.
Show 10 quoted lines
>
> Rasmus Villemoes (3):
>   help: redirect to aliased commands for "git cmd --help"
>   git.c: handle_alias: prepend alias info when first argument is -h
>   git-help.txt: document "git help cmd" vs "git cmd --help" for aliases
>
>  Documentation/git-help.txt |  4 ++++
>  builtin/help.c             | 34 +++++++++++++++++++++++++++++++---
>  git.c                      |  3 +++
>  3 files changed, 38 insertions(+), 3 deletions(-)

← back to recent threads