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>

Junio C Hamano committed Jun 5, 2012 at 21:17 UTC 782cd4c0f6e0fef5147cb738009dde6e778f4932
3 files changed +30 -15
builtin/ls-files.c
+8 -2
@@ -200,6 +200,12 @@ static void show_ru_info(void)
200 }
201 }
202
203 +static int ce_excluded(struct path_exclude_check *check, struct cache_entry *ce)
204 +{
205 + int dtype = ce_to_dtype(ce);
206 + return path_excluded(check, ce->name, ce_namelen(ce), &dtype);
207 +}
208 +
209 static void show_files(struct dir_struct *dir)
210 {
211 int i;
@@ -220,7 +226,7 @@ static void show_files(struct dir_struct *dir)
226 for (i = 0; i < active_nr; i++) {
227 struct cache_entry *ce = active_cache[i];
228 if ((dir->flags & DIR_SHOW_IGNORED) &&
223 - !path_excluded(&check, ce))
229 + !ce_excluded(&check, ce))
230 continue;
231 if (show_unmerged && !ce_stage(ce))
232 continue;
@@ -236,7 +242,7 @@ static void show_files(struct dir_struct *dir)
242 struct stat st;
243 int err;
244 if ((dir->flags & DIR_SHOW_IGNORED) &&
239 - !path_excluded(&check, ce))
245 + !ce_excluded(&check, ce))
246 continue;
247 if (ce->ce_flags & CE_UPDATE)
248 continue;
dir.c
+20 -12
@@ -593,31 +593,40 @@ void path_exclude_check_clear(struct path_exclude_check *check)
593 }
594
595 /*
596 - * Is the ce->name excluded? This is for a caller like show_files() that
596 + * Is this name excluded? This is for a caller like show_files() that
597 * do not honor directory hierarchy and iterate through paths that are
598 * possibly in an ignored directory.
599 *
600 * A path to a directory known to be excluded is left in check->path to
601 * optimize for repeated checks for files in the same excluded directory.
602 */
603 -int path_excluded(struct path_exclude_check *check, struct cache_entry *ce)
603 +int path_excluded(struct path_exclude_check *check,
604 + const char *name, int namelen, int *dtype)
605 {
605 - int i, dtype;
606 + int i;
607 struct strbuf *path = &check->path;
608
609 + /*
610 + * we allow the caller to pass namelen as an optimization; it
611 + * must match the length of the name, as we eventually call
612 + * excluded() on the whole name string.
613 + */
614 + if (namelen < 0)
615 + namelen = strlen(name);
616 +
617 if (path->len &&
609 - path->len <= ce_namelen(ce) &&
610 - !memcmp(ce->name, path->buf, path->len) &&
611 - (!ce->name[path->len] || ce->name[path->len] == '/'))
618 + path->len <= namelen &&
619 + !memcmp(name, path->buf, path->len) &&
620 + (!name[path->len] || name[path->len] == '/'))
621 return 1;
622
623 strbuf_setlen(path, 0);
615 - for (i = 0; ce->name[i]; i++) {
616 - int ch = ce->name[i];
624 + for (i = 0; name[i]; i++) {
625 + int ch = name[i];
626
627 if (ch == '/') {
619 - dtype = DT_DIR;
620 - if (excluded(check->dir, path->buf, &dtype))
628 + int dt = DT_DIR;
629 + if (excluded(check->dir, path->buf, &dt))
630 return 1;
631 }
632 strbuf_addch(path, ch);
@@ -626,8 +635,7 @@ int path_excluded(struct path_exclude_check *check, struct cache_entry *ce)
635 /* An entry in the index; cannot be a directory with subentries */
636 strbuf_setlen(path, 0);
637
629 - dtype = ce_to_dtype(ce);
630 - return excluded(check->dir, ce->name, &dtype);
638 + return excluded(check->dir, name, dtype);
639 }
640
641 static struct dir_entry *dir_entry_new(const char *pathname, int len)
dir.h
+2 -1
@@ -92,7 +92,8 @@ struct path_exclude_check {
92 };
93 extern void path_exclude_check_init(struct path_exclude_check *, struct dir_struct *);
94 extern void path_exclude_check_clear(struct path_exclude_check *);
95 -extern int path_excluded(struct path_exclude_check *, struct cache_entry *);
95 +extern int path_excluded(struct path_exclude_check *, const char *, int namelen, int *dtype);
96 +
97
98 extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
99 char **buf_p, struct exclude_list *which, int check_index);