rev-parse parseopt: do not search help text for flag chars
authorBrandon Casey <drafnel@gmail.com>
Sun, 17 Sep 2017 22:28:15 +0000 (15:28 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 Sep 2017 03:13:07 +0000 (12:13 +0900)
When searching for flag characters in the option spec, we should ensure
the search stays within the bounds of the option spec and does not enter
the help text portion of the spec. So when we find the boundary white
space marking the start of the help text, let's mark it with a nul
character. Then when we search for flag characters starting from the
beginning of the string we'll stop at the nul and won't enter the help
text.

Now, the following option spec:

exclame this does something!

will produce this 'set' expression when --exclame is specified:

set -- --exclame --

instead of this one:

set -- --exclame this does something --

Mark t1502.4 and t1502.5 as fixed.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t1502-rev-parse-parseopt.sh
index c78b7b33d6604bb38a16e30d64cfabee0fd67f40..1cd55a2bd3f76d45e09088b586cd9abfee442717 100644 (file)
@@ -434,7 +434,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
        /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
        while (strbuf_getline(&sb, stdin) != EOF) {
                const char *s;
-               const char *help;
+               char *help;
                struct option *o;
 
                if (!sb.len)
@@ -451,8 +451,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                        continue;
                }
 
+               *help = '\0';
+
                o->type = OPTION_CALLBACK;
-               o->help = xstrdup(skipspaces(help));
+               o->help = xstrdup(skipspaces(help+1));
                o->value = &parsed;
                o->flags = PARSE_OPT_NOARG;
                o->callback = &parseopt_dump;
index 910fc56aecd5fa306bd47bc64856de8733b3c7f8..3d895e05638945bb308f2cd326516b131e36463a 100755 (executable)
@@ -85,12 +85,12 @@ set -- --foo --bar 'ham' -b --aswitch -- 'arg'
 EOF
 "
 
-test_expect_failure 'test --parseopt' '
+test_expect_success 'test --parseopt' '
        git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
        test_cmp expect output
 '
 
-test_expect_failure 'test --parseopt with mixed options and arguments' '
+test_expect_success 'test --parseopt with mixed options and arguments' '
        git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
        test_cmp expect output
 '