{"thread":{"id":"19035","subject":"[PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","startedAt":"2009-04-24T06:28:59Z","lastAt":"2009-04-25T05:06:40Z","messageCount":5,"participants":["Christian Couder","Junio C Hamano","Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"112160","messageId":"20090424062902.3705.44704.chriscool@tuxfamily.org","threadId":"19035","inReplyTo":null,"subject":"[PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-04-24T06:28:59Z","receivedAt":"2009-04-24T06:28:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Documentation/git-rev-parse.txt |   18 ++++++++++++++++++\n builtin-rev-parse.c             |   15 +++++++++++++++\n 2 files changed, 33 insertions(+), 0 deletions(-)\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex fba30b1..239704c 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -30,6 +30,9 @@ 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 section below).\n+\n --revs-only::\n \tDo not output flags and parameters not meant for\n \t'git-rev-list' command.\n@@ -406,6 +409,21 @@ 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`.\n+\n+Example\n+~~~~~~~\n+\n+------------\n+$ git rev-parse --sq-quote \"'''\" '\"\"\"' \"arg with space\"\n+ ''\\'''\\'''\\''' '\"\"\"' 'arg with space'\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"},{"id":"112168","messageId":"7v8wlqii2y.fsf@gitster.siamese.dyndns.org","threadId":"19035","inReplyTo":"20090424062902.3705.44704.chriscool@tuxfamily.org","subject":"Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-04-24T08:14:45Z","receivedAt":"2009-04-24T08:14:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> writes:\n\n> @@ -30,6 +30,9 @@ 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 section below).\n> +\n\nHmph, I wonder how this interacts with the existing --sq option to the\nsame command in the mental model of end users.\n\n> +Example\n> +~~~~~~~\n> +\n> +------------\n> +$ git rev-parse --sq-quote \"'''\" '\"\"\"' \"arg with space\"\n> + ''\\'''\\'''\\''' '\"\"\"' 'arg with space'\n> +------------\n\nYuck --- does asciidoc formats this correctly?\n\n    ... goes and tries ...\n\nNot very readable.  A better example might be to demonstrate something\nlike this:\n\n\t$ cat >your-git-script.sh <<\\EOF\n\t#!/bin/sh\n\t# quote user-supplied arguments\n\targs=$(git rev-parse --sq-quote \"$@\")\n        # and use it inside a handcrafted command line\n        command=\"git frotz -n24 $args\"\n        eval \"$command\"\n\tEOF\n\n\t$ sh your-git-script.sh \"a b'c\"\n\ni.e, put stress on how to use it, not on how it works internally.\n"},{"id":"112170","messageId":"49F17945.8000004@viscovery.net","threadId":"19035","inReplyTo":"7v8wlqii2y.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2009-04-24T08:33:09Z","receivedAt":"2009-04-24T08:33:09Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Junio C Hamano schrieb:\n> Not very readable.  A better example might be to demonstrate something\n> like this:\n> \n> \t$ cat >your-git-script.sh <<\\EOF\n> \t#!/bin/sh\n> \t# quote user-supplied arguments\n> \targs=$(git rev-parse --sq-quote \"$@\")\n>         # and use it inside a handcrafted command line\n>         command=\"git frotz -n24 $args\"\n>         eval \"$command\"\n> \tEOF\n> \n> \t$ sh your-git-script.sh \"a b'c\"\n> \n> i.e, put stress on how to use it, not on how it works internally.\n\nHmm, that makes me wonder why we special-case shell-quoting and implement\nit in an executable. Why don't we have perl-quoting, C-quoting,\nPHP-quoting, $language-of-your-choice quoting, etc, too?\n\nI think we should simply move sq() from git-am to git-sh-setup and use\nthat in git-bisect.\n\n-- Hannes\n"},{"id":"112283","messageId":"200904250657.43344.chriscool@tuxfamily.org","threadId":"19035","inReplyTo":"7v8wlqii2y.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-04-25T04:57:43Z","receivedAt":"2009-04-25T04:57:43Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Le vendredi 24 avril 2009, Junio C Hamano a écrit :\n> Christian Couder <chriscool@tuxfamily.org> writes:\n> > @@ -30,6 +30,9 @@ 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 section\n> > below). +\n>\n> Hmph, I wonder how this interacts with the existing --sq option to the\n> same command in the mental model of end users.\n\nI just sent a new version where difference between --sq and --sq-quote are \ndiscussed.\n\n> > +Example\n> > +~~~~~~~\n> > +\n> > +------------\n> > +$ git rev-parse --sq-quote \"'''\" '\"\"\"' \"arg with space\"\n> > + ''\\'''\\'''\\''' '\"\"\"' 'arg with space'\n> > +------------\n>\n> Yuck --- does asciidoc formats this correctly?\n>\n>     ... goes and tries ...\n>\n> Not very readable.  A better example might be to demonstrate something\n> like this:\n>\n> \t$ cat >your-git-script.sh <<\\EOF\n> \t#!/bin/sh\n> \t# quote user-supplied arguments\n> \targs=$(git rev-parse --sq-quote \"$@\")\n>         # and use it inside a handcrafted command line\n>         command=\"git frotz -n24 $args\"\n>         eval \"$command\"\n> \tEOF\n>\n> \t$ sh your-git-script.sh \"a b'c\"\n>\n> i.e, put stress on how to use it, not on how it works internally.\n\nI agree your example is better. I used it in the v2 I just sent.\n\nThanks,\nChristian.\n"},{"id":"112284","messageId":"200904250706.40533.chriscool@tuxfamily.org","threadId":"19035","inReplyTo":"49F17945.8000004@viscovery.net","subject":"Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2009-04-25T05:06:40Z","receivedAt":"2009-04-25T05:06:40Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Le vendredi 24 avril 2009, Johannes Sixt a écrit :\n> Junio C Hamano schrieb:\n> > Not very readable.  A better example might be to demonstrate something\n> > like this:\n> >\n> > \t$ cat >your-git-script.sh <<\\EOF\n> > \t#!/bin/sh\n> > \t# quote user-supplied arguments\n> > \targs=$(git rev-parse --sq-quote \"$@\")\n> >         # and use it inside a handcrafted command line\n> >         command=\"git frotz -n24 $args\"\n> >         eval \"$command\"\n> > \tEOF\n> >\n> > \t$ sh your-git-script.sh \"a b'c\"\n> >\n> > i.e, put stress on how to use it, not on how it works internally.\n>\n> Hmm, that makes me wonder why we special-case shell-quoting and implement\n> it in an executable. Why don't we have perl-quoting, C-quoting,\n> PHP-quoting, $language-of-your-choice quoting, etc, too?\n\nBecause there are a lot of shell scripts in the Git source code and it's an \nimportant problem for shell scripts to properly handle arguments.\n\n> I think we should simply move sq() from git-am to git-sh-setup and use\n> that in git-bisect.\n\nWe already have an implementation of shell quoting in C, why not use it \neverywhere instead of having 2 implementations?\n\nAnd what happens if someone want to port to C a shell script that uses sq()?\nThe implementation used will have to be the one in C, so why not use it \nright now?\n\nRegards,\nChristian.\n"}]}