Skip to content

Commit 4db86e8

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe73786 commit 4db86e8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

setup.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ int check_filename(const char *prefix, const char *arg)
6666
const char *name;
6767
struct stat st;
6868

69-
name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
69+
if (!prefixcmp(arg, ":/")) {
70+
if (arg[2] == '\0') /* ":/" is root dir, always exists */
71+
return 1;
72+
name = arg + 2;
73+
} else if (prefix)
74+
name = prefix_filename(prefix, strlen(prefix), arg);
75+
else
76+
name = arg;
7077
if (!lstat(name, &st))
7178
return 1; /* file exists */
7279
if (errno == ENOENT || errno == ENOTDIR)

t/t4208-log-magic-pathspec.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@ test_expect_success 'setup' '
1111
mkdir sub
1212
'
1313

14-
test_expect_success '"git log :/" should be ambiguous' '
15-
test_must_fail git log :/ 2>error &&
14+
test_expect_success '"git log :/" should not be ambiguous' '
15+
git log :/
16+
'
17+
18+
test_expect_success '"git log :/a" should be ambiguous (applied both rev and worktree)' '
19+
: >a &&
20+
test_must_fail git log :/a 2>error &&
1621
grep ambiguous error
1722
'
1823

24+
test_expect_success '"git log :/a -- " should not be ambiguous' '
25+
git log :/a --
26+
'
27+
28+
test_expect_success '"git log -- :/a" should not be ambiguous' '
29+
git log -- :/a
30+
'
31+
1932
test_expect_success '"git log :" should be ambiguous' '
2033
test_must_fail git log : 2>error &&
2134
grep ambiguous error

0 commit comments

Comments
 (0)