{"thread":{"id":"24659","subject":"[PATCH/RFC] split_cmdline: Allow caller to access error string","startedAt":"2010-08-07T05:13:39Z","lastAt":"2010-08-11T18:31:45Z","messageCount":2,"participants":["Greg Brockman","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"147375","messageId":"1281158019-25995-1-git-send-email-gdb@mit.edu","threadId":"24659","inReplyTo":null,"subject":"[PATCH/RFC] split_cmdline: Allow caller to access error string","fromName":"Greg Brockman","fromEmail":"gdb@mit.edu","sentAt":"2010-08-07T05:13:39Z","receivedAt":"2010-08-07T05:13:39Z","isPatch":true,"sender":{"key":"gdb@mit.edu","avatar":"https://gravatar.com/avatar/3b907cf60d0cadf6d0659d5429d971b16965fb6b1e3f5acd26a6ba2974b45da4?d=mp&s=160"},"body":"This allows the caller to add its own error message to that returned\nby split_cmdline.  Thus error output following a failed split_cmdline\ncan be of the form\n\nfatal: Bad alias.test string: cmdline ends with \\\n\nrather than\n\nerror: cmdline ends with \\\nfatal: Bad alias.test string\n\nSigned-off-by: Greg Brockman <gdb@mit.edu>\n---\n\nFor reference, this functionality was suggested by Junio in\nhttp://article.gmane.org/gmane.comp.version-control.git/151330/.  If\nwe decide to go this route, the git-shell patches currently in pu should\nbe patched to use split_cmdline_strerror.\n\nThanks in advance for whatever comments you have on this implementation.\n\n alias.c         |   11 ++++++++---\n builtin/merge.c |    3 ++-\n cache.h         |    2 ++\n git.c           |    3 ++-\n 4 files changed, 14 insertions(+), 5 deletions(-)\n\ndiff --git a/alias.c b/alias.c\nindex 372b7d8..6f771cb 100644\n--- a/alias.c\n+++ b/alias.c\n@@ -1,7 +1,9 @@\n #include \"cache.h\"\n-\n static const char *alias_key;\n static char *alias_val;\n+#define SPLIT_CMDLINE_BAD_ENDING 1\n+#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2\n+static const char *split_cmdline_errors = { \"cmdline ends with \\\\\", \"unclosed quote\" };\n \n static int alias_lookup_cb(const char *k, const char *v, void *cb)\n {\n@@ -53,7 +55,7 @@ int split_cmdline(char *cmdline, const char ***argv)\n \t\t\t\tif (!c) {\n \t\t\t\t\tfree(*argv);\n \t\t\t\t\t*argv = NULL;\n-\t\t\t\t\treturn error(\"cmdline ends with \\\\\");\n+\t\t\t\t\treturn -SPLIT_CMDLINE_BAD_ENDING;\n \t\t\t\t}\n \t\t\t}\n \t\t\tcmdline[dst++] = c;\n@@ -66,7 +68,7 @@ int split_cmdline(char *cmdline, const char ***argv)\n \tif (quoted) {\n \t\tfree(*argv);\n \t\t*argv = NULL;\n-\t\treturn error(\"unclosed quote\");\n+\t\treturn -SPLIT_CMDLINE_UNCLOSED_QUOTE;\n \t}\n \n \tALLOC_GROW(*argv, count+1, size);\n@@ -75,3 +77,6 @@ int split_cmdline(char *cmdline, const char ***argv)\n \treturn count;\n }\n \n+const char *split_cmdline_strerror(int split_cmdline_errno) {\n+\treturn split_cmdline_errors[-split_cmdline_errno-1];\n+}\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 37ce4f5..b488263 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -486,7 +486,8 @@ static int git_merge_config(const char *k, const char *v, void *cb)\n \t\tbuf = xstrdup(v);\n \t\targc = split_cmdline(buf, &argv);\n \t\tif (argc < 0)\n-\t\t\tdie(\"Bad branch.%s.mergeoptions string\", branch);\n+\t\t\tdie(\"Bad branch.%s.mergeoptions string: %s\", branch,\n+\t\t\t    split_cmdline_strerror(argc));\n \t\targv = xrealloc(argv, sizeof(*argv) * (argc + 2));\n \t\tmemmove(argv + 1, argv, sizeof(*argv) * (argc + 1));\n \t\targc++;\ndiff --git a/cache.h b/cache.h\nindex c9fa3df..7466ad0 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1096,6 +1096,8 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix);\n \n char *alias_lookup(const char *alias);\n int split_cmdline(char *cmdline, const char ***argv);\n+/* Takes a negative value returned by split_cmdline */\n+const char *split_cmdline_strerror(int cmdline_errno);\n \n /* builtin/merge.c */\n int checkout_fast_forward(const unsigned char *from, const unsigned char *to);\ndiff --git a/git.c b/git.c\nindex f37028b..6fc07a5 100644\n--- a/git.c\n+++ b/git.c\n@@ -188,7 +188,8 @@ static int handle_alias(int *argcp, const char ***argv)\n \t\t}\n \t\tcount = split_cmdline(alias_string, &new_argv);\n \t\tif (count < 0)\n-\t\t\tdie(\"Bad alias.%s string\", alias_command);\n+\t\t\tdie(\"Bad alias.%s string: %s\", alias_command,\n+\t\t\t    split_cmdline_strerror(count));\n \t\toption_count = handle_options(&new_argv, &count, &envchanged);\n \t\tif (envchanged)\n \t\t\tdie(\"alias '%s' changes environment variables\\n\"\n-- \n1.7.0.4\n"},{"id":"147800","messageId":"7vhbj1yp9q.fsf@alter.siamese.dyndns.org","threadId":"24659","inReplyTo":"1281158019-25995-1-git-send-email-gdb@mit.edu","subject":"Re: [PATCH/RFC] split_cmdline: Allow caller to access error string","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-08-11T18:31:45Z","receivedAt":"2010-08-11T18:31:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Greg Brockman <gdb@MIT.EDU> writes:\n\n> This allows the caller to add its own error message to that returned\n> by split_cmdline.  Thus error output following a failed split_cmdline\n> can be of the form\n>\n> fatal: Bad alias.test string: cmdline ends with \\\n>\n> rather than\n>\n> error: cmdline ends with \\\n> fatal: Bad alias.test string\n>\n> Signed-off-by: Greg Brockman <gdb@mit.edu>\n> ---\n\n> diff --git a/alias.c b/alias.c\n> index 372b7d8..6f771cb 100644\n> --- a/alias.c\n> +++ b/alias.c\n> @@ -1,7 +1,9 @@\n> ...\n> +#define SPLIT_CMDLINE_BAD_ENDING 1\n> +#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2\n> +static const char *split_cmdline_errors = { \"cmdline ends with \\\\\", \"unclosed quote\" };\n> ...\n> @@ -53,7 +55,7 @@ int split_cmdline(char *cmdline, const char ***argv)\n>  \t\t\t\tif (!c) {\n>  \t\t\t\t\tfree(*argv);\n>  \t\t\t\t\t*argv = NULL;\n> -\t\t\t\t\treturn error(\"cmdline ends with \\\\\");\n> +\t\t\t\t\treturn -SPLIT_CMDLINE_BAD_ENDING;\n>  \t\t\t\t}\n>  \t\t\t}\n>  \t\t\tcmdline[dst++] = c;\n>...\n> @@ -75,3 +77,6 @@ int split_cmdline(char *cmdline, const char ***argv)\n>  \treturn count;\n>  }\n>  \n> +const char *split_cmdline_strerror(int split_cmdline_errno) {\n> +\treturn split_cmdline_errors[-split_cmdline_errno-1];\n> +}\n\nLooks like a reasonable way to go.  Thanks.\n"}]}