dir.c: refactor is_excluded()

In a similar way to the previous commit, this extracts a new helper function last_exclude_matching() which returns the last exclude_list element which matched, or NULL if no match was found. is_excluded() becomes a wrapper around this, and just returns 0 or 1 depending on whether any matching exclude_list element was found. This allows callers to find out _why_ a given path was excluded, rather than just whether it was or not, paving the way for a new git sub-command which allows users to test their exclude lists from the command line. 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 f4cd69a67469eff90bee8e31529ca4e03c1afdf2
1 file changed +29 -9
dir.c
+29 -9
@@ -664,24 +664,44 @@ int is_excluded_from_list(const char *pathname,
664 return -1; /* undecided */
665 }
666
667 -static int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
667 +/*
668 + * Loads the exclude lists for the directory containing pathname, then
669 + * scans all exclude lists to determine whether pathname is excluded.
670 + * Returns the exclude_list element which matched, or NULL for
671 + * undecided.
672 + */
673 +static struct exclude *last_exclude_matching(struct dir_struct *dir,
674 + const char *pathname,
675 + int *dtype_p)
676 {
677 int pathlen = strlen(pathname);
678 int st;
679 + struct exclude *exclude;
680 const char *basename = strrchr(pathname, '/');
681 basename = (basename) ? basename+1 : pathname;
682
683 prep_exclude(dir, pathname, basename-pathname);
684 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
676 - switch (is_excluded_from_list(pathname, pathlen,
677 - basename, dtype_p,
678 - &dir->exclude_list[st])) {
679 - case 0:
680 - return 0;
681 - case 1:
682 - return 1;
683 - }
685 + exclude = last_exclude_matching_from_list(
686 + pathname, pathlen, basename, dtype_p,
687 + &dir->exclude_list[st]);
688 + if (exclude)
689 + return exclude;
690 }
691 + return NULL;
692 +}
693 +
694 +/*
695 + * Loads the exclude lists for the directory containing pathname, then
696 + * scans all exclude lists to determine whether pathname is excluded.
697 + * Returns 1 if true, otherwise 0.
698 + */
699 +static int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
700 +{
701 + struct exclude *exclude =
702 + last_exclude_matching(dir, pathname, dtype_p);
703 + if (exclude)
704 + return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
705 return 0;
706 }
707