Skip to content

Commit

Permalink
dir.c: refactor is_excluded_from_list()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Adam Spiers authored and Junio C Hamano committed Dec 28, 2012
1 parent 6d24e7a commit 578cd7c
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,22 +602,26 @@ int match_pathname(const char *pathname, int pathlen,
return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
}

/* Scan the list and let the last match determine the fate.
* Return 1 for exclude, 0 for include and -1 for undecided.
/*
* Scan the given exclude list in reverse to see whether pathname
* should be ignored. The first match (i.e. the last on the list), if
* any, determines the fate. Returns the exclude_list element which
* matched, or NULL for undecided.
*/
int is_excluded_from_list(const char *pathname,
int pathlen, const char *basename, int *dtype,
struct exclude_list *el)
static struct exclude *last_exclude_matching_from_list(const char *pathname,
int pathlen,
const char *basename,
int *dtype,
struct exclude_list *el)
{
int i;

if (!el->nr)
return -1; /* undefined */
return NULL; /* undefined */

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

if (x->flags & EXC_FLAG_MUSTBEDIR) {
Expand All @@ -632,16 +636,31 @@ int is_excluded_from_list(const char *pathname,
pathlen - (basename - pathname),
exclude, prefix, x->patternlen,
x->flags))
return to_exclude;
return x;
continue;
}

assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
if (match_pathname(pathname, pathlen,
x->base, x->baselen ? x->baselen - 1 : 0,
exclude, prefix, x->patternlen, x->flags))
return to_exclude;
return x;
}
return NULL; /* undecided */
}

/*
* Scan the list and let the last match determine the fate.
* Return 1 for exclude, 0 for include and -1 for undecided.
*/
int is_excluded_from_list(const char *pathname,
int pathlen, const char *basename, int *dtype,
struct exclude_list *el)
{
struct exclude *exclude;
exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
if (exclude)
return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
return -1; /* undecided */
}

Expand Down

0 comments on commit 578cd7c

Please sign in to comment.