Skip to content

Commit e05e2ae

Browse files
peffgitster
authored andcommitted
rev-parse: don't accept options after dashdash
Because of the order in which we check options in rev-parse, there are a few options we accept even after a "--". This is wrong, because the whole point of "--" is to say "everything after here is a path". Let's move the "did we see a dashdash" check (it's called "as_is" in the code) to the top of the parsing loop. Note there is one subtlety here. The options are ordered so that some are checked before we even see if we're in a repository (they continue the loop, and if we get past a certain point, then we do the repository setup). By moving the as_is check higher, it's also in that "before setup" section, even though it might look at the repository via verify_filename(). However, this works out: we'd never set as_is until we parse "--", and we don't parse that until after doing the setup. An alternative here to avoid the subtlety is to put the as_is check at the top of the post-setup options. But then every pre-setup option would have to remember to check "if (!as_is && !strcmp(...))". So while this is a bit magical, it's harder for future code to get wrong. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898f807 commit e05e2ae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

builtin/rev-parse.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
622622
for (i = 1; i < argc; i++) {
623623
const char *arg = argv[i];
624624

625+
if (as_is) {
626+
if (show_file(arg, output_prefix) && as_is < 2)
627+
verify_filename(prefix, arg, 0);
628+
continue;
629+
}
630+
625631
if (!strcmp(arg, "--local-env-vars")) {
626632
int i;
627633
for (i = 0; local_repo_env[i]; i++)
@@ -655,11 +661,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
655661
i++;
656662
continue;
657663
}
658-
if (as_is) {
659-
if (show_file(arg, output_prefix) && as_is < 2)
660-
verify_filename(prefix, arg, 0);
661-
continue;
662-
}
663664
if (!strcmp(arg,"-n")) {
664665
if (++i >= argc)
665666
die("-n requires an argument");

t/t1506-rev-parse-diagnosis.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,13 @@ test_expect_success 'escaped char does not trigger wildcard rule' '
254254
test_must_fail git rev-parse "foo\\*bar"
255255
'
256256

257+
test_expect_success 'arg after dashdash not interpreted as option' '
258+
cat >expect <<-\EOF &&
259+
--
260+
--local-env-vars
261+
EOF
262+
git rev-parse -- --local-env-vars >actual &&
263+
test_cmp expect actual
264+
'
265+
257266
test_done

0 commit comments

Comments
 (0)