Re: [PATCH v2 6/6] exclude: filter patterns by directory level
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 10, 2013, 08:20 UTC
- Message-ID
- <7vtxojd5u7.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <1362896070-17456-7-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> A non-basename pattern that does not contain /**/ can't match anything > outside the attached directory. Record its directory level and avoid > matching unless the pathname is also at the same directory level.
Without defining what a "directory level" is, the above is a bit hard to grok, but I think you mean an entry "b/c/*.c" that appears in "a/.gitignore" file will want to match a path that is directly in "a/b/c" directory (and not in its subdirectories), "a/b/x.c" at the two levels deep subdirectory or "a/b/c/d/x.c" that is four levels deep will never match the pattern.
The logic feels sound.
Show 33 quoted lines
> diff --git a/dir.c b/dir.c
> index 880b5e6..de7a6ba 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -360,10 +360,12 @@ static int no_wildcard(const char *string)
> void parse_exclude_pattern(const char **pattern,
> int *patternlen,
> int *flags,
> - int *nowildcardlen)
> + int *nowildcardlen,
> + int *dirs_p)
> {
> const char *p = *pattern;
> size_t i, len;
> + int dirs;
>
> *flags = 0;
> if (*p == '!') {
> @@ -375,12 +377,15 @@ void parse_exclude_pattern(const char **pattern,
> len--;
> *flags |= EXC_FLAG_MUSTBEDIR;
> }
> - for (i = 0; i < len; i++) {
> + for (i = 0, dirs = 0; i < len; i++) {
> if (p[i] == '/')
> - break;
> + dirs++;
> }
> - if (i == len)
> + if (!dirs)
> *flags |= EXC_FLAG_NODIR;
> + else if (*p == '/')
> + dirs--;I presume this is to compensate for a pattern like "/pat" whose leading slash is only to anchor the pattern at the level. Correct?
Show 11 quoted lines
> @@ -415,11 +423,26 @@ void add_exclude(const char *string, const char *base, > x = xmalloc(sizeof(*x)); > x->pattern = string; > } > + /* > + * TODO: nowildcardlen < patternlen is a stricter than > + * necessary mainly to exclude "**" that breaks directory > + * boundary. Patterns like "/foo-*" should be fine. > + */ > + if ((flags & EXC_FLAG_NODIR) || nowildcardlen < patternlen) > + dirs = -1;
OK, so an entry "README" to match README in any subdirectory will becomes (dirs < 0) and the matcher below will not short-circuit the comparison. Good.
Show 31 quoted lines
> + else {
> + int i;
> + for (i = 0; i < baselen; i++) {
> + if (base[i] == '/')
> + dirs++;
> + }
> + }
> x->patternlen = patternlen;
> x->nowildcardlen = nowildcardlen;
> x->base = base;
> x->baselen = baselen;
> x->flags = flags;
> + x->dirs = dirs;
> x->srcpos = srcpos;
> ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
> el->excludes[el->nr++] = x;
> @@ -701,7 +724,7 @@ int match_pathname(const char *pathname, int pathlen,
> * matched, or NULL for undecided.
> */
> static struct exclude *last_exclude_matching_from_list(const char *pathname,
> - int pathlen,
> + int pathlen, int dirs,
> const char *basename,
> int *dtype,
> struct exclude_list *el)
> @@ -732,6 +755,9 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
> continue;
> }
>
> + if (dirs >= 0 && x->dirs >= 0 && x->dirs != dirs)
> + continue;