path_excluded(): update API to less cache-entry centric

It was stupid of me to make the API too much cache-entry specific;
the caller may want to check arbitrary pathname without having a
corresponding cache-entry to see if a path is ignored.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Junio C Hamano 2012-06-05 21:17:52 -07:00
parent 93921b07e9
commit 782cd4c0f6
3 changed files with 30 additions and 15 deletions

View File

@ -200,6 +200,12 @@ static void show_ru_info(void)
} }
} }


static int ce_excluded(struct path_exclude_check *check, struct cache_entry *ce)
{
int dtype = ce_to_dtype(ce);
return path_excluded(check, ce->name, ce_namelen(ce), &dtype);
}

static void show_files(struct dir_struct *dir) static void show_files(struct dir_struct *dir)
{ {
int i; int i;
@ -220,7 +226,7 @@ static void show_files(struct dir_struct *dir)
for (i = 0; i < active_nr; i++) { for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i]; struct cache_entry *ce = active_cache[i];
if ((dir->flags & DIR_SHOW_IGNORED) && if ((dir->flags & DIR_SHOW_IGNORED) &&
!path_excluded(&check, ce)) !ce_excluded(&check, ce))
continue; continue;
if (show_unmerged && !ce_stage(ce)) if (show_unmerged && !ce_stage(ce))
continue; continue;
@ -236,7 +242,7 @@ static void show_files(struct dir_struct *dir)
struct stat st; struct stat st;
int err; int err;
if ((dir->flags & DIR_SHOW_IGNORED) && if ((dir->flags & DIR_SHOW_IGNORED) &&
!path_excluded(&check, ce)) !ce_excluded(&check, ce))
continue; continue;
if (ce->ce_flags & CE_UPDATE) if (ce->ce_flags & CE_UPDATE)
continue; continue;

32
dir.c
View File

@ -593,31 +593,40 @@ void path_exclude_check_clear(struct path_exclude_check *check)
} }


/* /*
* Is the ce->name excluded? This is for a caller like show_files() that * Is this name excluded? This is for a caller like show_files() that
* do not honor directory hierarchy and iterate through paths that are * do not honor directory hierarchy and iterate through paths that are
* possibly in an ignored directory. * possibly in an ignored directory.
* *
* A path to a directory known to be excluded is left in check->path to * A path to a directory known to be excluded is left in check->path to
* optimize for repeated checks for files in the same excluded directory. * optimize for repeated checks for files in the same excluded directory.
*/ */
int path_excluded(struct path_exclude_check *check, struct cache_entry *ce) int path_excluded(struct path_exclude_check *check,
const char *name, int namelen, int *dtype)
{ {
int i, dtype; int i;
struct strbuf *path = &check->path; struct strbuf *path = &check->path;


/*
* we allow the caller to pass namelen as an optimization; it
* must match the length of the name, as we eventually call
* excluded() on the whole name string.
*/
if (namelen < 0)
namelen = strlen(name);

if (path->len && if (path->len &&
path->len <= ce_namelen(ce) && path->len <= namelen &&
!memcmp(ce->name, path->buf, path->len) && !memcmp(name, path->buf, path->len) &&
(!ce->name[path->len] || ce->name[path->len] == '/')) (!name[path->len] || name[path->len] == '/'))
return 1; return 1;


strbuf_setlen(path, 0); strbuf_setlen(path, 0);
for (i = 0; ce->name[i]; i++) { for (i = 0; name[i]; i++) {
int ch = ce->name[i]; int ch = name[i];


if (ch == '/') { if (ch == '/') {
dtype = DT_DIR; int dt = DT_DIR;
if (excluded(check->dir, path->buf, &dtype)) if (excluded(check->dir, path->buf, &dt))
return 1; return 1;
} }
strbuf_addch(path, ch); strbuf_addch(path, ch);
@ -626,8 +635,7 @@ int path_excluded(struct path_exclude_check *check, struct cache_entry *ce)
/* An entry in the index; cannot be a directory with subentries */ /* An entry in the index; cannot be a directory with subentries */
strbuf_setlen(path, 0); strbuf_setlen(path, 0);


dtype = ce_to_dtype(ce); return excluded(check->dir, name, dtype);
return excluded(check->dir, ce->name, &dtype);
} }


static struct dir_entry *dir_entry_new(const char *pathname, int len) static struct dir_entry *dir_entry_new(const char *pathname, int len)

3
dir.h
View File

@ -92,7 +92,8 @@ struct path_exclude_check {
}; };
extern void path_exclude_check_init(struct path_exclude_check *, struct dir_struct *); extern void path_exclude_check_init(struct path_exclude_check *, struct dir_struct *);
extern void path_exclude_check_clear(struct path_exclude_check *); extern void path_exclude_check_clear(struct path_exclude_check *);
extern int path_excluded(struct path_exclude_check *, struct cache_entry *); extern int path_excluded(struct path_exclude_check *, const char *, int namelen, int *dtype);



extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen, extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
char **buf_p, struct exclude_list *which, int check_index); char **buf_p, struct exclude_list *which, int check_index);