Skip to content

Commit 7b51969

Browse files
committed
Merge branch 'nd/magic-pathspec-from-root'
When giving arguments without "--" disambiguation, object names that come earlier on the command line must not be interpretable as pathspecs and pathspecs that come later on the command line must not be interpretable as object names. Tweak the disambiguation rule so that ":/" (no other string before or after) is always interpreted as a pathspec, to avoid having to say "git cmd -- :/". * nd/magic-pathspec-from-root: grep: avoid accepting ambiguous revision Update :/abc ambiguity check
2 parents b596574 + 0b0ecaa commit 7b51969

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

builtin/grep.c

+2
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
823823
struct object *object = parse_object(sha1);
824824
if (!object)
825825
die(_("bad object %s"), arg);
826+
if (!seen_dashdash)
827+
verify_non_filename(prefix, arg);
826828
add_object_array(object, arg, &list);
827829
continue;
828830
}

setup.c

+8-1
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

+15-2
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)