Skip to content

Commit 578cd7c

Browse files
aspiersgitster
authored andcommitted
dir.c: refactor is_excluded_from_list()
The excluded function uses a new helper function called last_exclude_matching_from_list() to perform the inner loop over all of the exclude patterns. The helper just tells us whether the path is included, excluded, or undecided. However, it may be useful to know _which_ pattern was triggered. So let's pass out the entire exclude match, which contains the status information we were already passing out. Further patches can make use of this. This is a modified forward port of a patch from 2009 by Jeff King: http://article.gmane.org/gmane.comp.version-control.git/108815 Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d24e7a commit 578cd7c

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

dir.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,22 +602,26 @@ int match_pathname(const char *pathname, int pathlen,
602602
return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
603603
}
604604

605-
/* Scan the list and let the last match determine the fate.
606-
* Return 1 for exclude, 0 for include and -1 for undecided.
605+
/*
606+
* Scan the given exclude list in reverse to see whether pathname
607+
* should be ignored. The first match (i.e. the last on the list), if
608+
* any, determines the fate. Returns the exclude_list element which
609+
* matched, or NULL for undecided.
607610
*/
608-
int is_excluded_from_list(const char *pathname,
609-
int pathlen, const char *basename, int *dtype,
610-
struct exclude_list *el)
611+
static struct exclude *last_exclude_matching_from_list(const char *pathname,
612+
int pathlen,
613+
const char *basename,
614+
int *dtype,
615+
struct exclude_list *el)
611616
{
612617
int i;
613618

614619
if (!el->nr)
615-
return -1; /* undefined */
620+
return NULL; /* undefined */
616621

617622
for (i = el->nr - 1; 0 <= i; i--) {
618623
struct exclude *x = el->excludes[i];
619624
const char *exclude = x->pattern;
620-
int to_exclude = x->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
621625
int prefix = x->nowildcardlen;
622626

623627
if (x->flags & EXC_FLAG_MUSTBEDIR) {
@@ -632,16 +636,31 @@ int is_excluded_from_list(const char *pathname,
632636
pathlen - (basename - pathname),
633637
exclude, prefix, x->patternlen,
634638
x->flags))
635-
return to_exclude;
639+
return x;
636640
continue;
637641
}
638642

639643
assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
640644
if (match_pathname(pathname, pathlen,
641645
x->base, x->baselen ? x->baselen - 1 : 0,
642646
exclude, prefix, x->patternlen, x->flags))
643-
return to_exclude;
647+
return x;
644648
}
649+
return NULL; /* undecided */
650+
}
651+
652+
/*
653+
* Scan the list and let the last match determine the fate.
654+
* Return 1 for exclude, 0 for include and -1 for undecided.
655+
*/
656+
int is_excluded_from_list(const char *pathname,
657+
int pathlen, const char *basename, int *dtype,
658+
struct exclude_list *el)
659+
{
660+
struct exclude *exclude;
661+
exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
662+
if (exclude)
663+
return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
645664
return -1; /* undecided */
646665
}
647666

0 commit comments

Comments
 (0)