1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-03 12:16:16 +02:00
git/t/t4208-log-magic-pathspec.sh
Jeff King c99eddd835 verify_filename(): treat ":(magic)" as a pathspec
For commands that take revisions and pathspecs, magic
pathspecs like ":(exclude)foo" require the user to specify
a disambiguating "--", since they do not match a file in the
filesystem, like:

  git grep foo -- :(exclude)bar

This makes them more annoying to use than they need to be.
We loosened the rules for wildcards in 28fcc0b71 (pathspec:
avoid the need of "--" when wildcard is used, 2015-05-02).
Let's do the same for pathspecs with long-form magic.

We already handle the short-forms ":/" and ":^" specially in
check_filename(), so we don't need to handle them here. And
in fact, we could do the same with long-form magic, parsing
out the actual filename and making sure it exists. But there
are a few reasons not to do it that way:

  - the parsing gets much more complicated, and we'd want to
    hand it off to the pathspec code. But that code isn't
    ready to do this kind of speculative parsing (it's happy
    to die() when it sees a syntactically invalid pathspec).

  - not all pathspec magic maps to a filesystem path. E.g.,
    :(attr) should be treated as a pathspec regardless of
    what is in the filesystem

  - we can be a bit looser with ":(" than with the
    short-form ":/", because it is much less likely to have
    a false positive. Whereas ":/" also means "search for a
    commit with this regex".

Note that because the change is in verify_filename() and not
in its helper check_filename(), this doesn't affect the
verify_non_filename() case. I.e., if an item that matches
our new rule doesn't resolve as an object, we may fallback
to treating it as a pathspec (rather than complaining it
doesn't exist). But if it does resolve (e.g., as a file in
the index that starts with an open-paren), we won't then
complain that it's also a valid pathspec. This matches the
wildcard-exception behavior.

And of course in either case, one can always insert the "--"
to get more precise results.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-05-29 11:36:56 +09:00

97 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
test_description='magic pathspec tests using git-log'
. ./test-lib.sh
test_expect_success 'setup' '
test_commit initial &&
test_tick &&
git commit --allow-empty -m empty &&
mkdir sub
'
test_expect_success '"git log :/" should not be ambiguous' '
git log :/
'
test_expect_success '"git log :/a" should be ambiguous (applied both rev and worktree)' '
: >a &&
test_must_fail git log :/a 2>error &&
test_i18ngrep ambiguous error
'
test_expect_success '"git log :/a -- " should not be ambiguous' '
git log :/a --
'
test_expect_success '"git log -- :/a" should not be ambiguous' '
git log -- :/a
'
# This differs from the ":/a" check above in that :/in looks like a pathspec,
# but doesn't match an actual file.
test_expect_success '"git log :/in" should not be ambiguous' '
git log :/in
'
test_expect_success '"git log :" should be ambiguous' '
test_must_fail git log : 2>error &&
test_i18ngrep ambiguous error
'
test_expect_success 'git log -- :' '
git log -- :
'
test_expect_success 'git log HEAD -- :/' '
cat >expected <<-EOF &&
24b24cf initial
EOF
(cd sub && git log --oneline HEAD -- :/ >../actual) &&
test_cmp expected actual
'
test_expect_success '"git log :^sub" is not ambiguous' '
git log :^sub
'
test_expect_success '"git log :^does-not-exist" does not match anything' '
test_must_fail git log :^does-not-exist
'
test_expect_success '"git log :!" behaves the same as :^' '
git log :!sub &&
test_must_fail git log :!does-not-exist
'
test_expect_success '"git log :(exclude)sub" is not ambiguous' '
git log ":(exclude)sub"
'
test_expect_success '"git log :(exclude)sub --" must resolve as an object' '
test_must_fail git log ":(exclude)sub" --
'
test_expect_success '"git log :(unknown-magic) complains of bogus magic' '
test_must_fail git log ":(unknown-magic)" 2>error &&
test_i18ngrep pathspec.magic error
'
test_expect_success 'command line pathspec parsing for "git log"' '
git reset --hard &&
>a &&
git add a &&
git commit -m "add an empty a" --allow-empty &&
echo 1 >a &&
git commit -a -m "update a to 1" &&
git checkout HEAD^ &&
echo 2 >a &&
git commit -a -m "update a to 2" &&
test_must_fail git merge master &&
git add a &&
git log --merge -- a
'
test_done