threads / rfc / 26220

RFC patchalias: use run_command api to execute aliases

Subject: [PATCH/RFC] alias: use run_command api to execute aliases

## tl;dr

7 messages between Jan 6, 2011 and Jan 7, 2011. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Erik Faye-Lund· Jan 6, 2011, 19:13 UTC · lore

On Windows, system() executes with cmd.exe instead of /bin/sh. This means that aliases currently has to be batch-scripts instead of bourne-scripts. On top of that, cmd.exe does not handle single quotes, which is what the code-path currently uses to handle arguments with spaces.

To solve both problems in one go, use run_command_v_opt() to execute the alias. It already does the right thing prepend "sh -c " to the alias.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
This fixes issue 553 in the msysGit issue tracker.
 git.c |   25 +++++++++++++------------
 1 files changed, 13 insertions(+), 12 deletions(-)
Show changes to git.c +13 −12
diff --git a/git.c b/git.c
index 68334f6..5b0b9d8 100644
--- a/git.c
+++ b/git.c
@@ -177,19 +177,20 @@ static int handle_alias(int *argcp, const char ***argv)
 	alias_string = alias_lookup(alias_command);
 	if (alias_string) {
 		if (alias_string[0] == '!') {
+			const char **alias_argv;
+			int i;
+
 			commit_pager_choice();
-			if (*argcp > 1) {
-				struct strbuf buf;
-
-				strbuf_init(&buf, PATH_MAX);
-				strbuf_addstr(&buf, alias_string);
-				sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
-				free(alias_string);
-				alias_string = buf.buf;
-			}
-			trace_printf("trace: alias to shell cmd: %s => %s\n",
-				     alias_command, alias_string + 1);
-			ret = system(alias_string + 1);
+
+			/* build alias_argv */
+			alias_argv = malloc(sizeof(char *) * *argcp + 1);
+			alias_argv[0] = alias_string + 1;
+			for (i = 1; i < *argcp; ++i)
+				alias_argv[i] = (*argv)[i];
+			alias_argv[*argcp] = NULL;
+
+			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
+
 			if (ret >= 0 && WIFEXITED(ret) &&
 			    WEXITSTATUS(ret) != 127)
 				exit(WEXITSTATUS(ret));
-- 
1.7.3.3.585.g74f6e
Jonathan Nieder· Jan 6, 2011, 19:41 UTC · re: Erik Faye-Lund · lore

Re: [PATCH/RFC] alias: use run_command api to execute aliases

Erik Faye-Lund wrote:
> --- a/git.c
> +++ b/git.c
> @@ -177,19 +177,20 @@ static int handle_alias(int *argcp, const char ***argv)
[...]
> -			trace_printf("trace: alias to shell cmd: %s => %s\n",
> -				     alias_command, alias_string + 1);
Replaced by
	trace: run_command: ...

(followed by "trace: exec: ..." on non-Windows (execv_shell_cmd)). Ok.

> -			ret = system(alias_string + 1);
> +
> +			/* build alias_argv */
> +			alias_argv = malloc(sizeof(char *) * *argcp + 1);

This seems to be missing parentheses, so valgrind will complain except on 8-bit systems. ;-)

What if malloc fails?
> +			alias_argv[0] = alias_string + 1;
> +			for (i = 1; i < *argcp; ++i)
> +				alias_argv[i] = (*argv)[i];
> +			alias_argv[*argcp] = NULL;
Nit: all these *argcp are noisy.
> +
> +			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
> +
>  			if (ret >= 0 && WIFEXITED(ret) &&

The return value from run_command and from system do not mean the same thing.

>  			die("Failed to run '%s' when expanding alias '%s'",
>  			    alias_string + 1, alias_command);

run_command already prints an error message, but this one still seems useful since it mentions the alias.

Except as noted above,
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Thanks.
Show changes to git.c +6 −8
diff --git a/git.c b/git.c
index 5b0b9d8..dbf061d 100644
--- a/git.c
+++ b/git.c
@@ -178,22 +178,20 @@ static int handle_alias(int *argcp, const char ***argv)
 	if (alias_string) {
 		if (alias_string[0] == '!') {
 			const char **alias_argv;
-			int i;
+			int argc = *argcp, i;
 
 			commit_pager_choice();
 
-			/* build alias_argv */
-			alias_argv = malloc(sizeof(char *) * *argcp + 1);
+			alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
 			alias_argv[0] = alias_string + 1;
-			for (i = 1; i < *argcp; ++i)
+			for (i = 1; i < argc; ++i)
 				alias_argv[i] = (*argv)[i];
-			alias_argv[*argcp] = NULL;
+			alias_argv[argc] = NULL;
 
 			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
+			if (ret >= 0)	/* normal exit */
+				exit(ret);
 
-			if (ret >= 0 && WIFEXITED(ret) &&
-			    WEXITSTATUS(ret) != 127)
-				exit(WEXITSTATUS(ret));
 			die("Failed to run '%s' when expanding alias '%s'",
 			    alias_string + 1, alias_command);
 		}
Erik Faye-Lund· Jan 6, 2011, 19:52 UTC · re: Jonathan Nieder · lore

Re: [PATCH/RFC] alias: use run_command api to execute aliases

On Thu, Jan 6, 2011 at 8:41 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
Show 26 quoted lines
> Erik Faye-Lund wrote:
>
>> --- a/git.c
>> +++ b/git.c
>> @@ -177,19 +177,20 @@ static int handle_alias(int *argcp, const char ***argv)
> [...]
>> -                     trace_printf("trace: alias to shell cmd: %s => %s\n",
>> -                                  alias_command, alias_string + 1);
>
> Replaced by
>
>        trace: run_command: ...
>
> (followed by "trace: exec: ..." on non-Windows (execv_shell_cmd)).
> Ok.
>
>> -                     ret = system(alias_string + 1);
>> +
>> +                     /* build alias_argv */
>> +                     alias_argv = malloc(sizeof(char *) * *argcp + 1);
>
> This seems to be missing parentheses, so valgrind will complain
> except on 8-bit systems. ;-)
>
> What if malloc fails?
>
2x whoops :)
Show 7 quoted lines
>> +                     alias_argv[0] = alias_string + 1;
>> +                     for (i = 1; i < *argcp; ++i)
>> +                             alias_argv[i] = (*argv)[i];
>> +                     alias_argv[*argcp] = NULL;
>
> Nit: all these *argcp are noisy.
>
Yes. Fetching argc once is cleaner, thanks.
Show 8 quoted lines
>> +
>> +                     ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
>> +
>>                       if (ret >= 0 && WIFEXITED(ret) &&
>
> The return value from run_command and from system do not mean
> the same thing.
>
Yet another "whoops" :)
Show 9 quoted lines
>>                       die("Failed to run '%s' when expanding alias '%s'",
>>                           alias_string + 1, alias_command);
>
> run_command already prints an error message, but this one still
> seems useful since it mentions the alias.
>
> Except as noted above,
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
>

Thanks, I agree with all your comments. But why did you remove the "/* build alias_argv */"-comment? :)

v2 coming up!
Johannes Schindelin· Jan 7, 2011, 01:17 UTC · re: Erik Faye-Lund · lore

Re: [msysGit] [PATCH/RFC] alias: use run_command api to execute aliases

Hi,
On Thu, 6 Jan 2011, Erik Faye-Lund wrote:
Show 9 quoted lines
> On Windows, system() executes with cmd.exe instead of /bin/sh. This
> means that aliases currently has to be batch-scripts instead of
> bourne-scripts. On top of that, cmd.exe does not handle single quotes,
> which is what the code-path currently uses to handle arguments with
> spaces.
> 
> To solve both problems in one go, use run_command_v_opt() to execute
> the alias. It already does the right thing prepend "sh -c " to the
> alias.

Would this not break setups where aliases were defined to execute batch scripts?

If this is true, I'm of two minds here.

Ciao, Dscho

Erik Faye-Lund· Jan 7, 2011, 14:24 UTC · re: Johannes Schindelin · lore

Re: [msysGit] [PATCH/RFC] alias: use run_command api to execute aliases

On Fri, Jan 7, 2011 at 2:17 AM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

Show 19 quoted lines
> Hi,
>
> On Thu, 6 Jan 2011, Erik Faye-Lund wrote:
>
>> On Windows, system() executes with cmd.exe instead of /bin/sh. This
>> means that aliases currently has to be batch-scripts instead of
>> bourne-scripts. On top of that, cmd.exe does not handle single quotes,
>> which is what the code-path currently uses to handle arguments with
>> spaces.
>>
>> To solve both problems in one go, use run_command_v_opt() to execute
>> the alias. It already does the right thing prepend "sh -c " to the
>> alias.
>
> Would this not break setups where aliases were defined to execute batch
> scripts?
>
> If this is true, I'm of two minds here.
>

It would indeed, but I wouldn't worry TOO much about it. We've clearly told the users that Git for Windows is a tool that you have to be willing to work on to use.

But I'm kind of of two minds here myself, but for a slightly different reason: I think Git for Windows SHOULD use cmd.exe to execute scripts. We should be able to lose the msys-environment and still have the basic functionality working. In that sense, this is a step in the wrong direction. But I'd rather have all code use the same code-path to execute scripts, and make a bit switch to cmd.exe together with porting all supplied scripts to batch-files some time in the future.

Johannes Schindelin· Jan 7, 2011, 14:51 UTC · re: Erik Faye-Lund · lore

Re: [msysGit] [PATCH/RFC] alias: use run_command api to execute aliases

Hi,
On Fri, 7 Jan 2011, Erik Faye-Lund wrote:
Show 31 quoted lines
> On Fri, Jan 7, 2011 at 2:17 AM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
>
> > On Thu, 6 Jan 2011, Erik Faye-Lund wrote:
> >
> >> On Windows, system() executes with cmd.exe instead of /bin/sh. This 
> >> means that aliases currently has to be batch-scripts instead of 
> >> bourne-scripts. On top of that, cmd.exe does not handle single 
> >> quotes, which is what the code-path currently uses to handle 
> >> arguments with spaces.
> >>
> >> To solve both problems in one go, use run_command_v_opt() to execute 
> >> the alias. It already does the right thing prepend "sh -c " to the 
> >> alias.
> >
> > Would this not break setups where aliases were defined to execute 
> > batch scripts?
> >
> > If this is true, I'm of two minds here.
> 
> It would indeed, but I wouldn't worry TOO much about it. We've clearly 
> told the users that Git for Windows is a tool that you have to be 
> willing to work on to use.
> 
> But I'm kind of of two minds here myself, but for a slightly different 
> reason: I think Git for Windows SHOULD use cmd.exe to execute scripts. 
> We should be able to lose the msys-environment and still have the basic 
> functionality working. In that sense, this is a step in the wrong 
> direction. But I'd rather have all code use the same code-path to 
> execute scripts, and make a bit switch to cmd.exe together with porting 
> all supplied scripts to batch-files some time in the future.
Okay, strike my objections, I agree now. Feel free to apply!

Thanks, Dscho

Junio C Hamano· Jan 7, 2011, 19:21 UTC · re: Johannes Schindelin · lore

Re: [msysGit] [PATCH/RFC] alias: use run_command api to execute aliases

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 9 quoted lines
>> But I'm kind of of two minds here myself, but for a slightly different 
>> reason: I think Git for Windows SHOULD use cmd.exe to execute scripts. 
>> We should be able to lose the msys-environment and still have the basic 
>> functionality working. In that sense, this is a step in the wrong 
>> direction. But I'd rather have all code use the same code-path to 
>> execute scripts, and make a bit switch to cmd.exe together with porting 
>> all supplied scripts to batch-files some time in the future.
>
> Okay, strike my objections, I agree now. Feel free to apply!

FWIW, I agree with Erik's reasoning, too. Thanks, both.

← back to recent threads