Skip to content

Commit 5cacf3d

Browse files
committed
Merge branch 'uk/rev-parse-parse-opt'
* uk/rev-parse-parse-opt: parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse more tests for git rev-parse --parse-opt
2 parents 3115fe9 + 6e0800e commit 5cacf3d

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

Documentation/git-rev-parse.txt

Lines changed: 5 additions & 0 deletions
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+
--stop-at-non-option::
34+
Only meaningful in `--parseopt` mode. Lets the option parser stop at
35+
the first non-option argument. This can be used to parse sub-commands
36+
that take options themself.
37+
3338
--sq-quote::
3439
Use 'git-rev-parse' in shell quoting mode (see SQ-QUOTE
3540
section below). In contrast to the `--sq` option below, this

builtin-rev-parse.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,17 @@ static const char *skipspaces(const char *s)
301301

302302
static int cmd_parseopt(int argc, const char **argv, const char *prefix)
303303
{
304-
static int keep_dashdash = 0;
304+
static int keep_dashdash = 0, stop_at_non_option = 0;
305305
static char const * const parseopt_usage[] = {
306306
"git rev-parse --parseopt [options] -- [<args>...]",
307307
NULL
308308
};
309309
static struct option parseopt_opts[] = {
310310
OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
311311
"keep the `--` passed as an arg"),
312+
OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
313+
"stop parsing after the "
314+
"first non-option argument"),
312315
OPT_END(),
313316
};
314317

@@ -394,7 +397,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
394397
ALLOC_GROW(opts, onb + 1, osz);
395398
memset(opts + onb, 0, sizeof(opts[onb]));
396399
argc = parse_options(argc, argv, prefix, opts, usage,
397-
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
400+
keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
401+
stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);
398402

399403
strbuf_addf(&parsed, " --");
400404
sq_quote_argv(&parsed, argv, 0);

t/t1502-rev-parse-parseopt.sh

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ Extras
2020
2121
EOF
2222

23-
test_expect_success 'test --parseopt help output' '
24-
git rev-parse --parseopt -- -h 2> output.err <<EOF
23+
cat > optionspec << EOF
2524
some-command [options] <args>...
2625
2726
some-command does foo and bar!
@@ -37,7 +36,47 @@ C? option C with an optional argument
3736
Extras
3837
extra1 line above used to cause a segfault but no longer does
3938
EOF
39+
40+
test_expect_success 'test --parseopt help output' '
41+
git rev-parse --parseopt -- -h 2> output.err < optionspec
4042
test_cmp expect.err output.err
4143
'
4244

45+
cat > expect <<EOF
46+
set -- --foo --bar 'ham' -- 'arg'
47+
EOF
48+
49+
test_expect_success 'test --parseopt' '
50+
git rev-parse --parseopt -- --foo --bar=ham arg < optionspec > output &&
51+
test_cmp expect output
52+
'
53+
54+
test_expect_success 'test --parseopt with mixed options and arguments' '
55+
git rev-parse --parseopt -- --foo arg --bar=ham < optionspec > output &&
56+
test_cmp expect output
57+
'
58+
59+
cat > expect <<EOF
60+
set -- --foo -- 'arg' '--bar=ham'
61+
EOF
62+
63+
test_expect_success 'test --parseopt with --' '
64+
git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
65+
test_cmp expect output
66+
'
67+
68+
test_expect_success 'test --parseopt --stop-at-non-option' '
69+
git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
70+
test_cmp expect output
71+
'
72+
73+
cat > expect <<EOF
74+
set -- --foo -- '--' 'arg' '--bar=ham'
75+
EOF
76+
77+
test_expect_success 'test --parseopt --keep-dashdash' '
78+
git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
79+
test_cmp expect output
80+
'
81+
4382
test_done

0 commit comments

Comments
 (0)