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 <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Spiers committed Dec 27, 2012 at 02:32 UTC 578cd7c3eae11e552bbc34b29f35e273a455988e
1 file changed +28 -9
dir.c
+28 -9
@@ -602,22 +602,26 @@ int match_pathname(const char *pathname, int pathlen,
602 return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
603 }
604
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.
610 */
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)
616 {
617 int i;
618
619 if (!el->nr)
615 - return -1; /* undefined */
620 + return NULL; /* undefined */
621
622 for (i = el->nr - 1; 0 <= i; i--) {
623 struct exclude *x = el->excludes[i];
624 const char *exclude = x->pattern;
620 - int to_exclude = x->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
625 int prefix = x->nowildcardlen;
626
627 if (x->flags & EXC_FLAG_MUSTBEDIR) {
@@ -632,7 +636,7 @@ int is_excluded_from_list(const char *pathname,
636 pathlen - (basename - pathname),
637 exclude, prefix, x->patternlen,
638 x->flags))
635 - return to_exclude;
639 + return x;
640 continue;
641 }
642
@@ -640,8 +644,23 @@ int is_excluded_from_list(const char *pathname,
644 if (match_pathname(pathname, pathlen,
645 x->base, x->baselen ? x->baselen - 1 : 0,
646 exclude, prefix, x->patternlen, x->flags))
643 - return to_exclude;
647 + return x;
648 }
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;
664 return -1; /* undecided */
665 }
666