{"thread":{"id":"19056","subject":"[PATCH 1/3 v2] rev-parse: add --sq-quote to shell quote arguments","startedAt":"2009-04-25T04:55:26Z","lastAt":"2009-04-25T04:55:26Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":2,"patchTotal":3},"messages":[{"id":"112282","messageId":"20090425065526.46540d0c.chriscool@tuxfamily.org","threadId":"19056","inReplyTo":null,"subject":"[PATCH 1/3 v2] rev-parse: add --sq-quote to shell quote arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-04-25T04:55:26Z","receivedAt":"2009-04-25T04:55:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Documentation/git-rev-parse.txt |   35 ++++++++++++++++++++++++++++++++++-\n builtin-rev-parse.c             |   15 +++++++++++++++\n 2 files changed, 49 insertions(+), 1 deletions(-)\n\n\tChanges compared to the previous version are:\n\n\t- the documentation now explain how --sq and --sq-quote are different,\n\t- the example has been replaced by the one Junio gave.\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex fba30b1..0fa8156 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -30,6 +30,11 @@ OPTIONS\n \tOnly meaningful in `--parseopt` mode. Tells the option parser to echo\n \tout the first `--` met instead of skipping it.\n \n+--sq-quote::\n+\tUse 'git-rev-parse' in shell quoting mode (see SQ-QUOTE\n+\tsection below). In contrast to the `--sq` option below, this\n+\tmode does only quoting. Nothing else is done to command input.\n+\n --revs-only::\n \tDo not output flags and parameters not meant for\n \t'git-rev-list' command.\n@@ -64,7 +69,8 @@ OPTIONS\n \tproperly quoted for consumption by shell.  Useful when\n \tyou expect your parameter to contain whitespaces and\n \tnewlines (e.g. when using pickaxe `-S` with\n-\t'git-diff-\\*').\n+\t'git-diff-\\*'). In contrast to the `--sq-quote` option,\n+\tthe command input is still interpreted as usual.\n \n --not::\n \tWhen showing object names, prefix them with '{caret}' and\n@@ -406,6 +412,33 @@ C?        option C with an optional argument\"\n eval `echo \"$OPTS_SPEC\" | git rev-parse --parseopt -- \"$@\" || echo exit $?`\n ------------\n \n+SQ-QUOTE\n+--------\n+\n+In `--sq-quote` mode, 'git-rev-parse' echoes on the standard output a\n+single line suitable for `sh(1)` `eval`. This line is made by\n+normalizing the arguments following `--sq-quote`. Nothing other than\n+quoting the arguments is done.\n+\n+If you want command input to still be interpreted as usual by\n+'git-rev-parse' before the output is shell quoted, see the `--sq`\n+option.\n+\n+Example\n+~~~~~~~\n+\n+------------\n+$ cat >your-git-script.sh <<\\EOF\n+#!/bin/sh\n+args=$(git rev-parse --sq-quote \"$@\")   # quote user-supplied arguments\n+command=\"git frotz -n24 $args\"          # and use it inside a handcrafted\n+                                        # command line\n+eval \"$command\"\n+EOF\n+\n+$ sh your-git-script.sh \"a b'c\"\n+------------\n+\n EXAMPLES\n --------\n \ndiff --git a/builtin-rev-parse.c b/builtin-rev-parse.c\nindex 22c6d6a..c5b3d6e 100644\n--- a/builtin-rev-parse.c\n+++ b/builtin-rev-parse.c\n@@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)\n \treturn 0;\n }\n \n+static int cmd_sq_quote(int argc, const char **argv)\n+{\n+\tstruct strbuf buf = STRBUF_INIT;\n+\n+\tif (argc)\n+\t\tsq_quote_argv(&buf, argv, 0);\n+\tprintf(\"%s\\n\", buf.buf);\n+\tstrbuf_release(&buf);\n+\n+\treturn 0;\n+}\n+\n static void die_no_single_rev(int quiet)\n {\n \tif (quiet)\n@@ -419,6 +431,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \tif (argc > 1 && !strcmp(\"--parseopt\", argv[1]))\n \t\treturn cmd_parseopt(argc - 1, argv + 1, prefix);\n \n+\tif (argc > 1 && !strcmp(\"--sq-quote\", argv[1]))\n+\t\treturn cmd_sq_quote(argc - 2, argv + 2);\n+\n \tprefix = setup_git_directory();\n \tgit_config(git_default_config, NULL);\n \tfor (i = 1; i < argc; i++) {\n-- \n1.6.3.rc1.112.g17e25\n"}]}