{"thread":{"id":"26224","subject":"[PATCH v3] alias: use run_command api to execute aliases","startedAt":"2011-01-06T23:00:38Z","lastAt":"2011-01-06T23:00:38Z","messageCount":1,"participants":["Erik Faye-Lund"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"159081","messageId":"1294354838-6336-1-git-send-email-kusmabite@gmail.com","threadId":"26224","inReplyTo":null,"subject":"[PATCH v3] alias: use run_command api to execute aliases","fromName":"Erik Faye-Lund","fromEmail":"kusmabite@gmail.com","sentAt":"2011-01-06T23:00:38Z","receivedAt":"2011-01-06T23:00:38Z","isPatch":true,"sender":{"key":"kusmabite@gmail.com","avatar":"https://avatars.githubusercontent.com/u/47073?v=4"},"body":"On Windows, system() executes with cmd.exe instead of /bin/sh. This\nmeans that aliases currently has to be batch-scripts instead of\nbourne-scripts. On top of that, cmd.exe does not handle single quotes,\nwhich is what the code-path currently uses to handle arguments with\nspaces.\n\nTo solve both problems in one go, use run_command_v_opt() to execute\nthe alias. It already does the right thing prepend \"sh -c \" to the\nalias.\n\nSigned-off-by: Erik Faye-Lund <kusmabite@gmail.com>\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n---\n\nFixed the issues pointed out by Jonathan Nieder. For real this time.\nAlso changed die to die_errno, as suggested by Johannes Sixt.\n\n git.c |   34 +++++++++++++++++-----------------\n 1 files changed, 17 insertions(+), 17 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 68334f6..23610aa 100644\n--- a/git.c\n+++ b/git.c\n@@ -177,24 +177,24 @@ static int handle_alias(int *argcp, const char ***argv)\n \talias_string = alias_lookup(alias_command);\n \tif (alias_string) {\n \t\tif (alias_string[0] == '!') {\n+\t\t\tconst char **alias_argv;\n+\t\t\tint argc = *argcp, i;\n+\n \t\t\tcommit_pager_choice();\n-\t\t\tif (*argcp > 1) {\n-\t\t\t\tstruct strbuf buf;\n-\n-\t\t\t\tstrbuf_init(&buf, PATH_MAX);\n-\t\t\t\tstrbuf_addstr(&buf, alias_string);\n-\t\t\t\tsq_quote_argv(&buf, (*argv) + 1, PATH_MAX);\n-\t\t\t\tfree(alias_string);\n-\t\t\t\talias_string = buf.buf;\n-\t\t\t}\n-\t\t\ttrace_printf(\"trace: alias to shell cmd: %s => %s\\n\",\n-\t\t\t\t     alias_command, alias_string + 1);\n-\t\t\tret = system(alias_string + 1);\n-\t\t\tif (ret >= 0 && WIFEXITED(ret) &&\n-\t\t\t    WEXITSTATUS(ret) != 127)\n-\t\t\t\texit(WEXITSTATUS(ret));\n-\t\t\tdie(\"Failed to run '%s' when expanding alias '%s'\",\n-\t\t\t    alias_string + 1, alias_command);\n+\n+\t\t\t/* build alias_argv */\n+\t\t\talias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));\n+\t\t\talias_argv[0] = alias_string + 1;\n+\t\t\tfor (i = 1; i < argc; ++i)\n+\t\t\t\talias_argv[i] = (*argv)[i];\n+\t\t\talias_argv[argc] = NULL;\n+\n+\t\t\tret = run_command_v_opt(alias_argv, RUN_USING_SHELL);\n+\t\t\tif (ret >= 0)   /* normal exit */\n+\t\t\t\texit(ret);\n+\n+\t\t\tdie_errno(\"While expanding alias '%s': '%s'\",\n+\t\t\t    alias_command, alias_string + 1);\n \t\t}\n \t\tcount = split_cmdline(alias_string, &new_argv);\n \t\tif (count < 0)\n-- \n1.7.3.3.585.g74f6e\n"}]}