Update :/abc ambiguity check

:/abc may mean two things:

- as a revision, it means the revision that has "abc" in commit
  message.

- as a pathpec, it means "abc" from root.

Currently we see ":/abc" as a rev (most of the time), but never see it
as a pathspec even if "abc" exists and "git log :/abc" will gladly
take ":/abc" as rev even it's ambiguous. This patch makes it:

- ambiguous when "abc" exists on worktree
- a rev if abc does not exist on worktree
- a path if abc is not found in any commits (although better use
  "--" to avoid ambiguation because searching through commit DAG is
  expensive)

A plus from this patch is, because ":/" never matches anything as a
rev, it is never considered a valid rev and because root directory
always exists, ":/" is always unambiguously seen as a pathspec.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2013-01-21 20:00:48 +07:00 committed by Junio C Hamano
parent fe73786b48
commit 4db86e8b6e
2 changed files with 23 additions and 3 deletions

View File

@ -66,7 +66,14 @@ int check_filename(const char *prefix, const char *arg)
const char *name;
struct stat st;
name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
if (!prefixcmp(arg, ":/")) {
if (arg[2] == '\0') /* ":/" is root dir, always exists */
return 1;
name = arg + 2;
} else if (prefix)
name = prefix_filename(prefix, strlen(prefix), arg);
else
name = arg;
if (!lstat(name, &st))
return 1; /* file exists */
if (errno == ENOENT || errno == ENOTDIR)

View File

@ -11,11 +11,24 @@ test_expect_success 'setup' '
mkdir sub
'
test_expect_success '"git log :/" should be ambiguous' '
test_must_fail git log :/ 2>error &&
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 &&
grep 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
'
test_expect_success '"git log :" should be ambiguous' '
test_must_fail git log : 2>error &&
grep ambiguous error