Skip to content

Commit f4cd69a

Browse files
aspiersgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 578cd7c commit f4cd69a

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

dir.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -664,24 +664,44 @@ int is_excluded_from_list(const char *pathname,
664664
return -1; /* undecided */
665665
}
666666

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)
668676
{
669677
int pathlen = strlen(pathname);
670678
int st;
679+
struct exclude *exclude;
671680
const char *basename = strrchr(pathname, '/');
672681
basename = (basename) ? basename+1 : pathname;
673682

674683
prep_exclude(dir, pathname, basename-pathname);
675684
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;
684690
}
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;
685705
return 0;
686706
}
687707

0 commit comments

Comments
 (0)