parse-options: allow --end-of-options as a synonym for "--"
authorJeff King <peff@peff.net>
Tue, 6 Aug 2019 14:40:16 +0000 (10:40 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 6 Aug 2019 20:05:39 +0000 (13:05 -0700)
The revision option parser recently learned about --end-of-options, but
that's not quite enough for all callers. Some of them, like git-log,
pick out some options using parse_options(), and then feed the remainder
to setup_revisions(). For those cases we need to stop parse_options()
from finding more options when it sees --end-of-options, and to retain
that option in argv so that setup_revisions() can see it as well.

Let's handle this the same as we do "--". We can even piggy-back on the
handling of PARSE_OPT_KEEP_DASHDASH, because any caller that wants to
retain one will want to retain the other.

I've included two tests here. The "log" test covers "--source", which is
one of the options it handles with parse_options(), and would fail
before this patch. There's also a test that uses the parse-options
helper directly. That confirms that the option is handled correctly even
in cases without KEEP_DASHDASH or setup_revisions(). I.e., it is safe to
use --end-of-options in place of "--" in other programs.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
parse-options.c
t/t0040-parse-options.sh
t/t4202-log.sh
index 87b26a1d922e8905fe369b164ae5cad7ea372426..b42f54d48b96c05a207ffb02eac8c235c2b723b0 100644 (file)
@@ -780,7 +780,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
                        continue;
                }
 
-               if (!arg[2]) { /* "--" */
+               if (!arg[2] /* "--" */ ||
+                   !strcmp(arg + 2, "end-of-options")) {
                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
                                ctx->argc--;
                                ctx->argv++;
index cebc77fab0b254fc2e6f63e7eb68956b2b3dec9c..705a136ed92c99cda688f5e267204e59b0e532a9 100755 (executable)
@@ -399,4 +399,11 @@ test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
                test-tool parse-options --ye
 '
 
+test_expect_success '--end-of-options treats remainder as args' '
+       test-tool parse-options \
+           --expect="verbose: -1" \
+           --expect="arg 00: --verbose" \
+           --end-of-options --verbose
+'
+
 test_done
index c20209324c8e71c677b70ce2217cf6439385024b..e88ccb04a9715b92e47976e66753c38c2547babf 100755 (executable)
@@ -1707,4 +1707,11 @@ test_expect_success '--exclude-promisor-objects does not BUG-crash' '
        test_must_fail git log --exclude-promisor-objects source-a
 '
 
+test_expect_success 'log --end-of-options' '
+       git update-ref refs/heads/--source HEAD &&
+       git log --end-of-options --source >actual &&
+       git log >expect &&
+       test_cmp expect actual
+'
+
 test_done