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>
Erik Faye-Lund committed
Jan 7, 2011 at 00:00 UTC
7f51f8bc2b9d0556f2fbe20056e8fc8839766c99
1 file changed
+17
-17
git.c
+17
-17
@@ -177,24 +177,24 @@ static int handle_alias(int *argcp, const char ***argv)
177
alias_string = alias_lookup(alias_command);
178
if (alias_string) {
179
if (alias_string[0] == '!') {
180
+ const char **alias_argv;
181
+ int argc = *argcp, i;
182
+
183
commit_pager_choice();
181
- if (*argcp > 1) {
182
- struct strbuf buf;
183
-
184
- strbuf_init(&buf, PATH_MAX);
185
- strbuf_addstr(&buf, alias_string);
186
- sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
187
- free(alias_string);
188
- alias_string = buf.buf;
189
- }
190
- trace_printf("trace: alias to shell cmd: %s => %s\n",
191
- alias_command, alias_string + 1);
192
- ret = system(alias_string + 1);
193
- if (ret >= 0 && WIFEXITED(ret) &&
194
- WEXITSTATUS(ret) != 127)
195
- exit(WEXITSTATUS(ret));
196
- die("Failed to run '%s' when expanding alias '%s'",
197
- alias_string + 1, alias_command);
184
+
185
+ /* build alias_argv */
186
+ alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
187
+ alias_argv[0] = alias_string + 1;
188
+ for (i = 1; i < argc; ++i)
189
+ alias_argv[i] = (*argv)[i];
190
+ alias_argv[argc] = NULL;
191
+
192
+ ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
193
+ if (ret >= 0) /* normal exit */
194
+ exit(ret);
195
+
196
+ die_errno("While expanding alias '%s': '%s'",
197
+ alias_command, alias_string + 1);
198
}
199
count = split_cmdline(alias_string, &new_argv);
200
if (count < 0)