git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments
@ 2009-04-24  6:28 Christian Couder
  2009-04-24  8:14 ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Christian Couder @ 2009-04-24  6:28 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git


Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-rev-parse.txt |   18 ++++++++++++++++++
 builtin-rev-parse.c             |   15 +++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
index fba30b1..239704c 100644
--- a/Documentation/git-rev-parse.txt
+++ b/Documentation/git-rev-parse.txt
@@ -30,6 +30,9 @@ OPTIONS
 	Only meaningful in `--parseopt` mode. Tells the option parser to echo
 	out the first `--` met instead of skipping it.
 
+--sq-quote::
+	Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE section below).
+
 --revs-only::
 	Do not output flags and parameters not meant for
 	'git-rev-list' command.
@@ -406,6 +409,21 @@ C?        option C with an optional argument"
 eval `echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?`
 ------------
 
+SQ-QUOTE
+--------
+
+In `--sq-quote` mode, 'git-rev-parse' echoes on the standard output a
+single line suitable for `sh(1)` `eval`. This line is made by
+normalizing the arguments following `--sq-quote`.
+
+Example
+~~~~~~~
+
+------------
+$ git rev-parse --sq-quote "'''" '"""' "arg with space"
+ ''\'''\'''\''' '"""' 'arg with space'
+------------
+
 EXAMPLES
 --------
 
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index 22c6d6a..c5b3d6e 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
+static int cmd_sq_quote(int argc, const char **argv)
+{
+	struct strbuf buf = STRBUF_INIT;
+
+	if (argc)
+		sq_quote_argv(&buf, argv, 0);
+	printf("%s\n", buf.buf);
+	strbuf_release(&buf);
+
+	return 0;
+}
+
 static void die_no_single_rev(int quiet)
 {
 	if (quiet)
@@ -419,6 +431,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 	if (argc > 1 && !strcmp("--parseopt", argv[1]))
 		return cmd_parseopt(argc - 1, argv + 1, prefix);
 
+	if (argc > 1 && !strcmp("--sq-quote", argv[1]))
+		return cmd_sq_quote(argc - 2, argv + 2);
+
 	prefix = setup_git_directory();
 	git_config(git_default_config, NULL);
 	for (i = 1; i < argc; i++) {
-- 
1.6.3.rc1.112.g17e25

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments
  2009-04-24  6:28 [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments Christian Couder
@ 2009-04-24  8:14 ` Junio C Hamano
  2009-04-24  8:33   ` Johannes Sixt
  2009-04-25  4:57   ` Christian Couder
  0 siblings, 2 replies; 5+ messages in thread
From: Junio C Hamano @ 2009-04-24  8:14 UTC (permalink / raw)
  To: Christian Couder; +Cc: git

Christian Couder <chriscool@tuxfamily.org> writes:

> @@ -30,6 +30,9 @@ OPTIONS
>  	Only meaningful in `--parseopt` mode. Tells the option parser to echo
>  	out the first `--` met instead of skipping it.
>  
> +--sq-quote::
> +	Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE section below).
> +

Hmph, I wonder how this interacts with the existing --sq option to the
same command in the mental model of end users.

> +Example
> +~~~~~~~
> +
> +------------
> +$ git rev-parse --sq-quote "'''" '"""' "arg with space"
> + ''\'''\'''\''' '"""' 'arg with space'
> +------------

Yuck --- does asciidoc formats this correctly?

    ... goes and tries ...

Not very readable.  A better example might be to demonstrate something
like this:

	$ cat >your-git-script.sh <<\EOF
	#!/bin/sh
	# quote user-supplied arguments
	args=$(git rev-parse --sq-quote "$@")
        # and use it inside a handcrafted command line
        command="git frotz -n24 $args"
        eval "$command"
	EOF

	$ sh your-git-script.sh "a b'c"

i.e, put stress on how to use it, not on how it works internally.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments
  2009-04-24  8:14 ` Junio C Hamano
@ 2009-04-24  8:33   ` Johannes Sixt
  2009-04-25  5:06     ` Christian Couder
  2009-04-25  4:57   ` Christian Couder
  1 sibling, 1 reply; 5+ messages in thread
From: Johannes Sixt @ 2009-04-24  8:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Christian Couder, git

Junio C Hamano schrieb:
> Not very readable.  A better example might be to demonstrate something
> like this:
> 
> 	$ cat >your-git-script.sh <<\EOF
> 	#!/bin/sh
> 	# quote user-supplied arguments
> 	args=$(git rev-parse --sq-quote "$@")
>         # and use it inside a handcrafted command line
>         command="git frotz -n24 $args"
>         eval "$command"
> 	EOF
> 
> 	$ sh your-git-script.sh "a b'c"
> 
> i.e, put stress on how to use it, not on how it works internally.

Hmm, that makes me wonder why we special-case shell-quoting and implement
it in an executable. Why don't we have perl-quoting, C-quoting,
PHP-quoting, $language-of-your-choice quoting, etc, too?

I think we should simply move sq() from git-am to git-sh-setup and use
that in git-bisect.

-- Hannes

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments
  2009-04-24  8:14 ` Junio C Hamano
  2009-04-24  8:33   ` Johannes Sixt
@ 2009-04-25  4:57   ` Christian Couder
  1 sibling, 0 replies; 5+ messages in thread
From: Christian Couder @ 2009-04-25  4:57 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Le vendredi 24 avril 2009, Junio C Hamano a écrit :
> Christian Couder <chriscool@tuxfamily.org> writes:
> > @@ -30,6 +30,9 @@ OPTIONS
> >  	Only meaningful in `--parseopt` mode. Tells the option parser to echo
> >  	out the first `--` met instead of skipping it.
> >
> > +--sq-quote::
> > +	Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE section
> > below). +
>
> Hmph, I wonder how this interacts with the existing --sq option to the
> same command in the mental model of end users.

I just sent a new version where difference between --sq and --sq-quote are 
discussed.

> > +Example
> > +~~~~~~~
> > +
> > +------------
> > +$ git rev-parse --sq-quote "'''" '"""' "arg with space"
> > + ''\'''\'''\''' '"""' 'arg with space'
> > +------------
>
> Yuck --- does asciidoc formats this correctly?
>
>     ... goes and tries ...
>
> Not very readable.  A better example might be to demonstrate something
> like this:
>
> 	$ cat >your-git-script.sh <<\EOF
> 	#!/bin/sh
> 	# quote user-supplied arguments
> 	args=$(git rev-parse --sq-quote "$@")
>         # and use it inside a handcrafted command line
>         command="git frotz -n24 $args"
>         eval "$command"
> 	EOF
>
> 	$ sh your-git-script.sh "a b'c"
>
> i.e, put stress on how to use it, not on how it works internally.

I agree your example is better. I used it in the v2 I just sent.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments
  2009-04-24  8:33   ` Johannes Sixt
@ 2009-04-25  5:06     ` Christian Couder
  0 siblings, 0 replies; 5+ messages in thread
From: Christian Couder @ 2009-04-25  5:06 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Junio C Hamano, git

Le vendredi 24 avril 2009, Johannes Sixt a écrit :
> Junio C Hamano schrieb:
> > Not very readable.  A better example might be to demonstrate something
> > like this:
> >
> > 	$ cat >your-git-script.sh <<\EOF
> > 	#!/bin/sh
> > 	# quote user-supplied arguments
> > 	args=$(git rev-parse --sq-quote "$@")
> >         # and use it inside a handcrafted command line
> >         command="git frotz -n24 $args"
> >         eval "$command"
> > 	EOF
> >
> > 	$ sh your-git-script.sh "a b'c"
> >
> > i.e, put stress on how to use it, not on how it works internally.
>
> Hmm, that makes me wonder why we special-case shell-quoting and implement
> it in an executable. Why don't we have perl-quoting, C-quoting,
> PHP-quoting, $language-of-your-choice quoting, etc, too?

Because there are a lot of shell scripts in the Git source code and it's an 
important problem for shell scripts to properly handle arguments.

> I think we should simply move sq() from git-am to git-sh-setup and use
> that in git-bisect.

We already have an implementation of shell quoting in C, why not use it 
everywhere instead of having 2 implementations?

And what happens if someone want to port to C a shell script that uses sq()?
The implementation used will have to be the one in C, so why not use it 
right now?

Regards,
Christian.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-04-25  5:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-24  6:28 [PATCH 1/3] rev-parse: add --sq-quote to shell quote arguments Christian Couder
2009-04-24  8:14 ` Junio C Hamano
2009-04-24  8:33   ` Johannes Sixt
2009-04-25  5:06     ` Christian Couder
2009-04-25  4:57   ` Christian Couder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).