@matheusbsbs / matheus-bagliano / commits / 503253771e
Fork 0

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>

Christian Couder committed Apr 25, 2009 at 06:55 UTC 503253771e93fd9f29c9f0773223d456677452c4
2 files changed +49 -1
Documentation/git-rev-parse.txt
+34 -1
@@ -30,6 +30,11 @@ OPTIONS
30 Only meaningful in `--parseopt` mode. Tells the option parser to echo
31 out the first `--` met instead of skipping it.
32
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 +
38 --revs-only::
39 Do not output flags and parameters not meant for
40 'git-rev-list' command.
@@ -64,7 +69,8 @@ OPTIONS
69 properly quoted for consumption by shell. Useful when
70 you expect your parameter to contain whitespaces and
71 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.
74
75 --not::
76 When showing object names, prefix them with '{caret}' and
@@ -406,6 +412,33 @@ C? option C with an optional argument"
412 eval `echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?`
413 ------------
414
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 +
442 EXAMPLES
443 --------
444
builtin-rev-parse.c
+15
@@ -402,6 +402,18 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
402 return 0;
403 }
404
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 +
417 static void die_no_single_rev(int quiet)
418 {
419 if (quiet)
@@ -419,6 +431,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
431 if (argc > 1 && !strcmp("--parseopt", argv[1]))
432 return cmd_parseopt(argc - 1, argv + 1, prefix);
433
434 + if (argc > 1 && !strcmp("--sq-quote", argv[1]))
435 + return cmd_sq_quote(argc - 2, argv + 2);
436 +
437 prefix = setup_git_directory();
438 git_config(git_default_config, NULL);
439 for (i = 1; i < argc; i++) {