1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-26 00:06:16 +02:00
git/t/t2019-checkout-ambiguous-re...
Jeff King df714f81a7 check_filename: tighten dwim-wildcard ambiguity
When specifying both revisions and pathnames, we allow
"<rev> -- <pathspec>" to be spelled without the "--" as long
as it is not ambiguous. The original logic was something
like:

  1. Resolve each item with get_sha1(). If successful,
     we know it can be a <rev>. Verify that it _isn't_ a
     filename, using verify_non_filename(), and complain of
     ambiguity otherwise.

  2. If get_sha1() didn't succeed, make sure that it _is_
     a file, using verify_filename(). If not, complain
     that it is neither a <rev> nor a <pathspec>.

Both verify_filename() and verify_non_filename() rely on
check_filename(), which definitely said "yes, this is a
file" or "no, it is not" using lstat().

Commit 28fcc0b (pathspec: avoid the need of "--" when
wildcard is used, 2015-05-02) introduced a convenience
feature: check_filename() will consider anything with
wildcard meta-characters as a possible filename, without
even checking the filesystem.

This works well for case 2. For such a wildcard, we would
previously have died and said "it is neither". Post-28fcc0b,
we assume it's a pathspec and proceed.

But it makes some instances of case 1 worse. We may have an
extended sha1 expression that contains meta-characters
(e.g., "HEAD^{/foo.*bar}"), and we now complain that it's
also a filename, due to the wildcard characters (even though
that wildcard would not match anything in the filesystem).

One solution would be to actually expand the pathname and
see if it matches anything on the filesystem. But that's
potentially expensive, and we do not have to be so rigorous
for this DWIM magic (if you want rigor, use "--").

Instead, we can just use different rules for cases 1 and 2.
When we know something is a rev, we will complain only if it
meets a much higher standard for "this is also a file";
namely that it actually exists in the filesystem. Case 2
remains the same: we use the looser "it could be a filename"
standard introduced by 28fcc0b.

We can accomplish this by pulling the wildcard logic out of
check_filename() and putting it into verify_filename(). Its
partner verify_non_filename() does not need a change, since
check_filename() goes back to implementing the "higher
standard".

Besides these two callers of check_filename(), there is one
other: git-checkout does a similar DWIM itself. It hits this
code path only after get_sha1() has returned failure, making
it case 2, which gets the special wildcard treatment.

Note that we drop the tests in t2019 in favor of a more
complete set in t6133. t2019 was not the right place for
them (it's about refname ambiguity, not dwim parsing
ambiguity), and the second test explicitly checked for the
opposite result of the case we are fixing here (which didn't
really make any sense; as shown by the test_must_fail in the
test, it would only serve to annoy people).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-02-10 13:53:20 -08:00

60 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
test_description='checkout handling of ambiguous (branch/tag) refs'
. ./test-lib.sh
test_expect_success 'setup ambiguous refs' '
test_commit branch file &&
git branch ambiguity &&
git branch vagueness &&
test_commit tag file &&
git tag ambiguity &&
git tag vagueness HEAD:file &&
test_commit other file
'
test_expect_success 'checkout ambiguous ref succeeds' '
git checkout ambiguity >stdout 2>stderr
'
test_expect_success 'checkout produces ambiguity warning' '
grep "warning.*ambiguous" stderr
'
test_expect_success 'checkout chooses branch over tag' '
echo refs/heads/ambiguity >expect &&
git symbolic-ref HEAD >actual &&
test_cmp expect actual &&
echo branch >expect &&
test_cmp expect file
'
test_expect_success 'checkout reports switch to branch' '
test_i18ngrep "Switched to branch" stderr &&
test_i18ngrep ! "^HEAD is now at" stderr
'
test_expect_success 'checkout vague ref succeeds' '
git checkout vagueness >stdout 2>stderr &&
test_set_prereq VAGUENESS_SUCCESS
'
test_expect_success VAGUENESS_SUCCESS 'checkout produces ambiguity warning' '
grep "warning.*ambiguous" stderr
'
test_expect_success VAGUENESS_SUCCESS 'checkout chooses branch over tag' '
echo refs/heads/vagueness >expect &&
git symbolic-ref HEAD >actual &&
test_cmp expect actual &&
echo branch >expect &&
test_cmp expect file
'
test_expect_success VAGUENESS_SUCCESS 'checkout reports switch to branch' '
test_i18ngrep "Switched to branch" stderr &&
test_i18ngrep ! "^HEAD is now at" stderr
'
test_done