Skip to content

Commit 5032537

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 38ef750 commit 5032537

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

Documentation/git-rev-parse.txt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ OPTIONS
3030
Only meaningful in `--parseopt` mode. Tells the option parser to echo
3131
out the first `--` met instead of skipping it.
3232

33+
--sq-quote::
34+
Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
35+
section below). In contrast to the `--sq` option below, this
36+
mode does only quoting. Nothing else is done to command input.
37+
3338
--revs-only::
3439
Do not output flags and parameters not meant for
3540
'git-rev-list' command.
@@ -64,7 +69,8 @@ OPTIONS
6469
properly quoted for consumption by shell. Useful when
6570
you expect your parameter to contain whitespaces and
6671
newlines (e.g. when using pickaxe `-S` with
67-
'git-diff-\*').
72+
'git-diff-\*'). In contrast to the `--sq-quote` option,
73+
the command input is still interpreted as usual.
6874

6975
--not::
7076
When showing object names, prefix them with '{caret}' and
@@ -406,6 +412,33 @@ C? option C with an optional argument"
406412
eval `echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?`
407413
------------
408414

415+
SQ-QUOTE
416+
--------
417+
418+
In `--sq-quote` mode, 'git-rev-parse' echoes on the standard output a
419+
single line suitable for `sh(1)` `eval`. This line is made by
420+
normalizing the arguments following `--sq-quote`. Nothing other than
421+
quoting the arguments is done.
422+
423+
If you want command input to still be interpreted as usual by
424+
'git-rev-parse' before the output is shell quoted, see the `--sq`
425+
option.
426+
427+
Example
428+
~~~~~~~
429+
430+
------------
431+
$ cat >your-git-script.sh <<\EOF
432+
#!/bin/sh
433+
args=$(git rev-parse --sq-quote "$@") # quote user-supplied arguments
434+
command="git frotz -n24 $args" # and use it inside a handcrafted
435+
# command line
436+
eval "$command"
437+
EOF
438+
439+
$ sh your-git-script.sh "a b'c"
440+
------------
441+
409442
EXAMPLES
410443
--------
411444

builtin-rev-parse.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
402402
return 0;
403403
}
404404

405+
static int cmd_sq_quote(int argc, const char **argv)
406+
{
407+
struct strbuf buf = STRBUF_INIT;
408+
409+
if (argc)
410+
sq_quote_argv(&buf, argv, 0);
411+
printf("%s\n", buf.buf);
412+
strbuf_release(&buf);
413+
414+
return 0;
415+
}
416+
405417
static void die_no_single_rev(int quiet)
406418
{
407419
if (quiet)
@@ -419,6 +431,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
419431
if (argc > 1 && !strcmp("--parseopt", argv[1]))
420432
return cmd_parseopt(argc - 1, argv + 1, prefix);
421433

434+
if (argc > 1 && !strcmp("--sq-quote", argv[1]))
435+
return cmd_sq_quote(argc - 2, argv + 2);
436+
422437
prefix = setup_git_directory();
423438
git_config(git_default_config, NULL);
424439
for (i = 1; i < argc; i++) {

0 commit comments

Comments
 (0)