rev-parse: add --sq-quote to shell quote arguments

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Christian Couder
2009-04-25 06:55:26 +02:00
committed by Junio C Hamano
parent 38ef7507d1
commit 503253771e
2 changed files with 49 additions and 1 deletions

View File

@ -30,6 +30,11 @@ OPTIONS
Only meaningful in `--parseopt` mode. Tells the option parser to echo Only meaningful in `--parseopt` mode. Tells the option parser to echo
out the first `--` met instead of skipping it. out the first `--` met instead of skipping it.
--sq-quote::
Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
section below). In contrast to the `--sq` option below, this
mode does only quoting. Nothing else is done to command input.
--revs-only:: --revs-only::
Do not output flags and parameters not meant for Do not output flags and parameters not meant for
'git-rev-list' command. 'git-rev-list' command.
@ -64,7 +69,8 @@ OPTIONS
properly quoted for consumption by shell. Useful when properly quoted for consumption by shell. Useful when
you expect your parameter to contain whitespaces and you expect your parameter to contain whitespaces and
newlines (e.g. when using pickaxe `-S` with newlines (e.g. when using pickaxe `-S` with
'git-diff-\*'). 'git-diff-\*'). In contrast to the `--sq-quote` option,
the command input is still interpreted as usual.
--not:: --not::
When showing object names, prefix them with '{caret}' and When showing object names, prefix them with '{caret}' and
@ -406,6 +412,33 @@ C? option C with an optional argument"
eval `echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?` 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`. Nothing other than
quoting the arguments is done.
If you want command input to still be interpreted as usual by
'git-rev-parse' before the output is shell quoted, see the `--sq`
option.
Example
~~~~~~~
------------
$ cat >your-git-script.sh <<\EOF
#!/bin/sh
args=$(git rev-parse --sq-quote "$@") # quote user-supplied arguments
command="git frotz -n24 $args" # and use it inside a handcrafted
# command line
eval "$command"
EOF
$ sh your-git-script.sh "a b'c"
------------
EXAMPLES EXAMPLES
-------- --------

View File

@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
return 0; 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) static void die_no_single_rev(int quiet)
{ {
if (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])) if (argc > 1 && !strcmp("--parseopt", argv[1]))
return cmd_parseopt(argc - 1, argv + 1, prefix); 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(); prefix = setup_git_directory();
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {