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

Re: [PATCH v2 resend] git.c: make autocorrected aliases work

From
APAlexander Potashev <aspotashev@gmail.com>
Date
Jan 4, 2009, 17:28 UTC
Message-ID
<20090104172833.GA7139@myhost>
In-Reply-To
<1231089361-12619-1-git-send-email-dato@net.com.org.es>
On 18:16 Sun 04 Jan     , Adeodato Simó wrote:
Show 92 quoted lines
> help_unknown_cmd() is able to autocorrect a command to an alias, and not
> only to internal or external commands. However, main() was not passing the
> autocorrected command through handle_alias(), hence it failed if it was an
> alias.
> 
> This commit makes the autocorrected command go through handle_alias(), once
> handle_internal_command() and execv_dashed_external() have been tried. Since
> this is done twice in main() now, moved that logic to a new run_argv()
> function.
> 
> Also, print the same "Expansion of alias 'x' failed" message when the alias
> was autocorrected, rather than a generic "Failed to run command 'x'".
> 
> Signed-off-by: Adeodato Simó <dato@net.com.org.es>
> ---
> 
> Meh, I didn't realize that by attaching an incremental diff, I'd break
> `git am`. Sorry about that.
> 
>  git.c |   53 +++++++++++++++++++++++++++++++++--------------------
>  1 files changed, 33 insertions(+), 20 deletions(-)
> 
> diff --git a/git.c b/git.c
> index e0d9071..ee331aa 100644
> --- a/git.c
> +++ b/git.c
> @@ -416,12 +416,34 @@ static void execv_dashed_external(const char **argv)
>  	strbuf_release(&cmd);
>  }
>  
> +static int run_argv(int *argcp, const char ***argv)
> +{
> +	int done_alias = 0;
> +
> +	while (1) {
> +		/* See if it's an internal command */
> +		handle_internal_command(*argcp, *argv);
> +
> +		/* .. then try the external ones */
> +		execv_dashed_external(*argv);
> +
> +		/* It could be an alias -- this works around the insanity
> +		 * of overriding "git log" with "git show" by having
> +		 * alias.log = show
> +		 */
> +		if (done_alias || !handle_alias(argcp, argv))
> +			break;
> +		done_alias = 1;
> +	}
> +
> +	return done_alias;
> +}
> +
>  
>  int main(int argc, const char **argv)
>  {
>  	const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
>  	char *slash = (char *)cmd + strlen(cmd);
> -	int done_alias = 0;
>  
>  	/*
>  	 * Take the basename of argv[0] as the command
> @@ -479,31 +501,22 @@ int main(int argc, const char **argv)
>  	setup_path();
>  
>  	while (1) {
> -		/* See if it's an internal command */
> -		handle_internal_command(argc, argv);
> -
> -		/* .. then try the external ones */
> -		execv_dashed_external(argv);
> -
> -		/* It could be an alias -- this works around the insanity
> -		 * of overriding "git log" with "git show" by having
> -		 * alias.log = show
> -		 */
> -		if (done_alias || !handle_alias(&argc, &argv))
> +		static int done_help = 0;
> +		static int was_alias = 0;
> +		was_alias = run_argv(&argc, &argv);
> +		if (errno != ENOENT)
>  			break;
> -		done_alias = 1;
> -	}
> -
> -	if (errno == ENOENT) {
> -		if (done_alias) {
> +		if (was_alias) {
>  			fprintf(stderr, "Expansion of alias '%s' failed; "
>  				"'%s' is not a git-command\n",
>  				cmd, argv[0]);
>  			exit(1);
Why not using 'die' here?
		die("Expansion of alias '%s' failed;
			'%s' is not a git-command",
  			cmd, argv[0]);
DISCLAIMER: I have never used git's 'die'
Show 14 quoted lines
>  		}
> -		argv[0] = help_unknown_cmd(cmd);
> -		handle_internal_command(argc, argv);
> -		execv_dashed_external(argv);
> +		if (!done_help) {
> +			cmd = argv[0] = help_unknown_cmd(cmd);
> +			done_help = 1;
> +		} else
> +			break;
>  	}
>  
>  	fprintf(stderr, "Failed to run command '%s': %s\n",
> -- 
> 1.6.1.62.g677ca
Previous: Adeodato SimóNext: Junio C Hamano
Message 4 of 6 in “git.c: make autocorrected aliases work”
  1. git.c: make autocorrected aliases workAdeodato Simó, Jan 4, 2009
  2. git.c: make autocorrected aliases workAdeodato Simó, Jan 4, 2009
  3. git.c: make autocorrected aliases workAdeodato Simó, Jan 4, 2009
  4. Alexander PotashevJan 4, 2009
  5. Junio C HamanoJan 6, 2009
  6. Junio C HamanoJan 6, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.