alias: use run_command api to execute aliases

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>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Erik Faye-Lund 2011-01-07 00:00:38 +01:00 committed by Junio C Hamano
parent 685e9d9145
commit 7f51f8bc2b

34
git.c
View File

@ -177,24 +177,24 @@ static int handle_alias(int *argcp, const char ***argv)
alias_string = alias_lookup(alias_command); alias_string = alias_lookup(alias_command);
if (alias_string) { if (alias_string) {
if (alias_string[0] == '!') { if (alias_string[0] == '!') {
commit_pager_choice(); const char **alias_argv;
if (*argcp > 1) { int argc = *argcp, i;
struct strbuf buf;
strbuf_init(&buf, PATH_MAX); commit_pager_choice();
strbuf_addstr(&buf, alias_string);
sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); /* build alias_argv */
free(alias_string); alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
alias_string = buf.buf; alias_argv[0] = alias_string + 1;
} for (i = 1; i < argc; ++i)
trace_printf("trace: alias to shell cmd: %s => %s\n", alias_argv[i] = (*argv)[i];
alias_command, alias_string + 1); alias_argv[argc] = NULL;
ret = system(alias_string + 1);
if (ret >= 0 && WIFEXITED(ret) && ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
WEXITSTATUS(ret) != 127) if (ret >= 0) /* normal exit */
exit(WEXITSTATUS(ret)); exit(ret);
die("Failed to run '%s' when expanding alias '%s'",
alias_string + 1, alias_command); die_errno("While expanding alias '%s': '%s'",
alias_command, alias_string + 1);
} }
count = split_cmdline(alias_string, &new_argv); count = split_cmdline(alias_string, &new_argv);
if (count < 0) if (count < 0)