Skip to content

Commit 341f8a6

Browse files
committed
Merge branch 'jk/escaped-wildcard-dwim'
Disambiguation logic to tell revisions and pathspec apart has been tweaked so that backslash-escaped glob special characters do not count in the "wildcards are pathspec" rule. * jk/escaped-wildcard-dwim: verify_filename(): handle backslashes in "wildcards are pathspecs" rule
2 parents b486d2e + 39e21c6 commit 341f8a6

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

setup.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,26 @@ static void NORETURN die_verify_filename(struct repository *r,
197197
*/
198198
static int looks_like_pathspec(const char *arg)
199199
{
200-
/* anything with a wildcard character */
201-
if (!no_wildcard(arg))
202-
return 1;
200+
const char *p;
201+
int escaped = 0;
202+
203+
/*
204+
* Wildcard characters imply the user is looking to match pathspecs
205+
* that aren't in the filesystem. Note that this doesn't include
206+
* backslash even though it's a glob special; by itself it doesn't
207+
* cause any increase in the match. Likewise ignore backslash-escaped
208+
* wildcard characters.
209+
*/
210+
for (p = arg; *p; p++) {
211+
if (escaped) {
212+
escaped = 0;
213+
} else if (is_glob_special(*p)) {
214+
if (*p == '\\')
215+
escaped = 1;
216+
else
217+
return 1;
218+
}
219+
}
203220

204221
/* long-form pathspec magic */
205222
if (starts_with(arg, ":("))

t/t1506-rev-parse-diagnosis.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,18 @@ test_expect_success 'reject Nth ancestor if N is too high' '
222222
test_must_fail git rev-parse HEAD~100000000000000000000000000000000
223223
'
224224

225+
test_expect_success 'pathspecs with wildcards are not ambiguous' '
226+
echo "*.c" >expect &&
227+
git rev-parse "*.c" >actual &&
228+
test_cmp expect actual
229+
'
230+
231+
test_expect_success 'backslash does not trigger wildcard rule' '
232+
test_must_fail git rev-parse "foo\\bar"
233+
'
234+
235+
test_expect_success 'escaped char does not trigger wildcard rule' '
236+
test_must_fail git rev-parse "foo\\*bar"
237+
'
238+
225239
test_done

0 commit comments

Comments
 (0)