Skip to content

Commit 5236467

Browse files
committed
Merge branch 'jk/match-pathname-fix'
The wildmatch code had a corner case bug that mistakenly makes "foo**/bar" match with "foobar", which has been corrected. * jk/match-pathname-fix: match_pathname(): give fnmatch one char of prefix context match_pathname(): reorder prefix-match check
2 parents ecf2f52 + 1940a02 commit 5236467

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

dir.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,18 +1388,25 @@ int match_pathname(const char *pathname, int pathlen,
13881388

13891389
if (fspathncmp(pattern, name, prefix))
13901390
return 0;
1391-
pattern += prefix;
1392-
patternlen -= prefix;
1393-
name += prefix;
1394-
namelen -= prefix;
13951391

13961392
/*
13971393
* If the whole pattern did not have a wildcard,
13981394
* then our prefix match is all we need; we
13991395
* do not need to call fnmatch at all.
14001396
*/
1401-
if (!patternlen && !namelen)
1397+
if (patternlen == prefix && namelen == prefix)
14021398
return 1;
1399+
1400+
/*
1401+
* Retain one character of the prefix to
1402+
* pass to fnmatch, which lets it distinguish
1403+
* the start of a directory component correctly.
1404+
*/
1405+
prefix--;
1406+
pattern += prefix;
1407+
patternlen -= prefix;
1408+
name += prefix;
1409+
namelen -= prefix;
14031410
}
14041411

14051412
return fnmatch_icase_mem(pattern, patternlen,

t/t0008-ignores.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,17 @@ test_expect_success 'directories and ** matches' '
847847
test_cmp expect actual
848848
'
849849

850+
test_expect_success '** not confused by matching leading prefix' '
851+
cat >.gitignore <<-\EOF &&
852+
foo**/bar
853+
EOF
854+
git check-ignore foobar foo/bar >actual &&
855+
cat >expect <<-\EOF &&
856+
foo/bar
857+
EOF
858+
test_cmp expect actual
859+
'
860+
850861
############################################################################
851862
#
852863
# test whitespace handling

0 commit comments

Comments
 (0)