Skip to content

Commit 4bbb830

Browse files
committed
Merge branch 'jc/directory-attrs-regression-fix' into maint-1.8.1
A pattern "dir" (without trailing slash) in the attributes file stopped matching a directory "dir" by mistake with an earlier change that wanted to allow pattern "dir/" to also match. * jc/directory-attrs-regression-fix: t: check that a pattern without trailing slash matches a directory dir.c::match_pathname(): pay attention to the length of string parameters dir.c::match_pathname(): adjust patternlen when shifting pattern dir.c::match_basename(): pay attention to the length of string parameters attr.c::path_matches(): special case paths that end with a slash attr.c::path_matches(): the basename is part of the pathname
2 parents 0e9b327 + efa5f82 commit 4bbb830

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
lines changed

attr.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,24 +657,24 @@ static void prepare_attr_stack(const char *path, int dirlen)
657657
}
658658

659659
static int path_matches(const char *pathname, int pathlen,
660-
const char *basename,
660+
int basename_offset,
661661
const struct pattern *pat,
662662
const char *base, int baselen)
663663
{
664664
const char *pattern = pat->pattern;
665665
int prefix = pat->nowildcardlen;
666+
int isdir = (pathlen && pathname[pathlen - 1] == '/');
666667

667-
if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
668-
((!pathlen) || (pathname[pathlen-1] != '/')))
668+
if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
669669
return 0;
670670

671671
if (pat->flags & EXC_FLAG_NODIR) {
672-
return match_basename(basename,
673-
pathlen - (basename - pathname),
672+
return match_basename(pathname + basename_offset,
673+
pathlen - basename_offset - isdir,
674674
pattern, prefix,
675675
pat->patternlen, pat->flags);
676676
}
677-
return match_pathname(pathname, pathlen,
677+
return match_pathname(pathname, pathlen - isdir,
678678
base, baselen,
679679
pattern, prefix, pat->patternlen, pat->flags);
680680
}
@@ -703,7 +703,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
703703
return rem;
704704
}
705705

706-
static int fill(const char *path, int pathlen, const char *basename,
706+
static int fill(const char *path, int pathlen, int basename_offset,
707707
struct attr_stack *stk, int rem)
708708
{
709709
int i;
@@ -713,7 +713,7 @@ static int fill(const char *path, int pathlen, const char *basename,
713713
struct match_attr *a = stk->attrs[i];
714714
if (a->is_macro)
715715
continue;
716-
if (path_matches(path, pathlen, basename,
716+
if (path_matches(path, pathlen, basename_offset,
717717
&a->u.pat, base, stk->originlen))
718718
rem = fill_one("fill", a, rem);
719719
}
@@ -752,18 +752,19 @@ static void collect_all_attrs(const char *path)
752752
{
753753
struct attr_stack *stk;
754754
int i, pathlen, rem, dirlen;
755-
const char *basename, *cp, *last_slash = NULL;
755+
const char *cp, *last_slash = NULL;
756+
int basename_offset;
756757

757758
for (cp = path; *cp; cp++) {
758759
if (*cp == '/' && cp[1])
759760
last_slash = cp;
760761
}
761762
pathlen = cp - path;
762763
if (last_slash) {
763-
basename = last_slash + 1;
764+
basename_offset = last_slash + 1 - path;
764765
dirlen = last_slash - path;
765766
} else {
766-
basename = path;
767+
basename_offset = 0;
767768
dirlen = 0;
768769
}
769770

@@ -773,7 +774,7 @@ static void collect_all_attrs(const char *path)
773774

774775
rem = attr_nr;
775776
for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
776-
rem = fill(path, pathlen, basename, stk, rem);
777+
rem = fill(path, pathlen, basename_offset, stk, rem);
777778
}
778779

779780
int git_check_attr(const char *path, int num, struct git_attr_check *check)

dir.c

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ int fnmatch_icase(const char *pattern, const char *string, int flags)
3434
return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
3535
}
3636

37+
static int fnmatch_icase_mem(const char *pattern, int patternlen,
38+
const char *string, int stringlen,
39+
int flags)
40+
{
41+
int match_status;
42+
struct strbuf pat_buf = STRBUF_INIT;
43+
struct strbuf str_buf = STRBUF_INIT;
44+
const char *use_pat = pattern;
45+
const char *use_str = string;
46+
47+
if (pattern[patternlen]) {
48+
strbuf_add(&pat_buf, pattern, patternlen);
49+
use_pat = pat_buf.buf;
50+
}
51+
if (string[stringlen]) {
52+
strbuf_add(&str_buf, string, stringlen);
53+
use_str = str_buf.buf;
54+
}
55+
56+
match_status = fnmatch_icase(use_pat, use_str, flags);
57+
58+
strbuf_release(&pat_buf);
59+
strbuf_release(&str_buf);
60+
61+
return match_status;
62+
}
63+
3764
static size_t common_prefix_len(const char **pathspec)
3865
{
3966
const char *n, *first;
@@ -537,15 +564,20 @@ int match_basename(const char *basename, int basenamelen,
537564
int flags)
538565
{
539566
if (prefix == patternlen) {
540-
if (!strcmp_icase(pattern, basename))
567+
if (patternlen == basenamelen &&
568+
!strncmp_icase(pattern, basename, basenamelen))
541569
return 1;
542570
} else if (flags & EXC_FLAG_ENDSWITH) {
571+
/* "*literal" matching against "fooliteral" */
543572
if (patternlen - 1 <= basenamelen &&
544-
!strcmp_icase(pattern + 1,
545-
basename + basenamelen - patternlen + 1))
573+
!strncmp_icase(pattern + 1,
574+
basename + basenamelen - (patternlen - 1),
575+
patternlen - 1))
546576
return 1;
547577
} else {
548-
if (fnmatch_icase(pattern, basename, 0) == 0)
578+
if (fnmatch_icase_mem(pattern, patternlen,
579+
basename, basenamelen,
580+
0) == 0)
549581
return 1;
550582
}
551583
return 0;
@@ -565,6 +597,7 @@ int match_pathname(const char *pathname, int pathlen,
565597
*/
566598
if (*pattern == '/') {
567599
pattern++;
600+
patternlen--;
568601
prefix--;
569602
}
570603

@@ -591,11 +624,22 @@ int match_pathname(const char *pathname, int pathlen,
591624
if (strncmp_icase(pattern, name, prefix))
592625
return 0;
593626
pattern += prefix;
627+
patternlen -= prefix;
594628
name += prefix;
595629
namelen -= prefix;
630+
631+
/*
632+
* If the whole pattern did not have a wildcard,
633+
* then our prefix match is all we need; we
634+
* do not need to call fnmatch at all.
635+
*/
636+
if (!patternlen && !namelen)
637+
return 1;
596638
}
597639

598-
return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
640+
return fnmatch_icase_mem(pattern, patternlen,
641+
name, namelen,
642+
FNM_PATHNAME) == 0;
599643
}
600644

601645
/* Scan the list and let the last match determine the fate.

t/t5002-archive-attr-pattern.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ test_expect_success 'setup' '
2727
echo ignored-only-if-dir/ export-ignore >>.git/info/attributes &&
2828
git add ignored-only-if-dir &&
2929
30+
mkdir -p ignored-without-slash &&
31+
echo "ignored without slash" >ignored-without-slash/foo &&
32+
git add ignored-without-slash/foo &&
33+
echo "ignored-without-slash export-ignore" >>.git/info/attributes &&
34+
35+
mkdir -p wildcard-without-slash &&
36+
echo "ignored without slash" >wildcard-without-slash/foo &&
37+
git add wildcard-without-slash/foo &&
38+
echo "wild*-without-slash export-ignore" >>.git/info/attributes &&
39+
40+
mkdir -p deep/and/slashless &&
41+
echo "ignored without slash" >deep/and/slashless/foo &&
42+
git add deep/and/slashless/foo &&
43+
echo "deep/and/slashless export-ignore" >>.git/info/attributes &&
44+
45+
mkdir -p deep/with/wildcard &&
46+
echo "ignored without slash" >deep/with/wildcard/foo &&
47+
git add deep/with/wildcard/foo &&
48+
echo "deep/*t*/wildcard export-ignore" >>.git/info/attributes &&
3049
3150
mkdir -p one-level-lower/two-levels-lower/ignored-only-if-dir &&
3251
echo ignored by ignored dir >one-level-lower/two-levels-lower/ignored-only-if-dir/ignored-by-ignored-dir &&
@@ -49,6 +68,14 @@ test_expect_exists archive/not-ignored-dir/ignored-only-if-dir
4968
test_expect_exists archive/not-ignored-dir/
5069
test_expect_missing archive/ignored-only-if-dir/
5170
test_expect_missing archive/ignored-ony-if-dir/ignored-by-ignored-dir
71+
test_expect_missing archive/ignored-without-slash/ &&
72+
test_expect_missing archive/ignored-without-slash/foo &&
73+
test_expect_missing archive/wildcard-without-slash/
74+
test_expect_missing archive/wildcard-without-slash/foo &&
75+
test_expect_missing archive/deep/and/slashless/ &&
76+
test_expect_missing archive/deep/and/slashless/foo &&
77+
test_expect_missing archive/deep/with/wildcard/ &&
78+
test_expect_missing archive/deep/with/wildcard/foo &&
5279
test_expect_exists archive/one-level-lower/
5380
test_expect_missing archive/one-level-lower/two-levels-lower/ignored-only-if-dir/
5481
test_expect_missing archive/one-level-lower/two-levels-lower/ignored-ony-if-dir/ignored-by-ignored-dir

0 commit comments

Comments
 (0)