From: Junio C Hamano Date: Sun, 24 Sep 2017 10:39:40 +0000 (+0900) Subject: Merge branch 'bc/rev-parse-parseopt-fix' into next X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/e479bce0ff792afc2254077f62fbbd2946c8083c?hp=999ccd41aad2a02549ebfc33667660b0298efb11 Merge branch 'bc/rev-parse-parseopt-fix' into next Recent versions of "git rev-parse --parseopt" did not parse the option specification that does not have the optional flags (*=?!) correctly, which has been corrected. * bc/rev-parse-parseopt-fix: git-rebase: don't ignore unexpected command line arguments rev-parse parseopt: interpret any whitespace as start of help text rev-parse parseopt: do not search help text for flag chars t1502: demonstrate rev-parse --parseopt option mis-parsing --- diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 9f24004c0a..37f4209c24 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -387,6 +387,14 @@ static const char *skipspaces(const char *s) return s; } +static char *findspace(const char *s) +{ + for (; *s; s++) + if (isspace(*s)) + return (char*)s; + return NULL; +} + static int cmd_parseopt(int argc, const char **argv, const char *prefix) { static int keep_dashdash = 0, stop_at_non_option = 0; @@ -434,7 +442,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) /* parse: (|,|)[*=?!]*? SP+ */ while (strbuf_getline(&sb, stdin) != EOF) { const char *s; - const char *help; + char *help; struct option *o; if (!sb.len) @@ -444,15 +452,17 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) memset(opts + onb, 0, sizeof(opts[onb])); o = &opts[onb++]; - help = strchr(sb.buf, ' '); - if (!help || *sb.buf == ' ') { + help = findspace(sb.buf); + if (!help || sb.buf == help) { o->type = OPTION_GROUP; o->help = xstrdup(skipspaces(sb.buf)); 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; diff --git a/git-rebase.sh b/git-rebase.sh index ad8415e3cf..6344e8d5e3 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -350,6 +350,9 @@ do shift break ;; + *) + usage + ;; esac shift done diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh index 310f93fd30..6e1b45fd12 100755 --- a/t/t1502-rev-parse-parseopt.sh +++ b/t/t1502-rev-parse-parseopt.sh @@ -28,6 +28,9 @@ test_expect_success 'setup optionspec' ' |g,fluf?path short and long option optional argument |longest=very-long-argument-hint a very long argument hint |pair=key=value with an equals sign in the hint +|aswitch help te=t contains? fl*g characters!` +|bswitch=hint hint has trailing tab character +|cswitch switch has trailing tab character |short-hint=a with a one symbol hint | |Extras @@ -62,6 +65,9 @@ test_expect_success 'test --parseopt help output' ' | --longest | a very long argument hint | --pair with an equals sign in the hint +| --aswitch help te=t contains? fl*g characters!` +| --bswitch hint has trailing tab character +| --cswitch switch has trailing tab character | --short-hint with a one symbol hint | |Extras @@ -75,17 +81,17 @@ END_EXPECT test_expect_success 'setup expect.1' " cat > expect < output && + git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output && test_cmp expect output ' test_expect_success 'test --parseopt with mixed options and arguments' ' - git rev-parse --parseopt -- --foo arg --bar=ham --baz < optionspec > output && + git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output && test_cmp expect output '