Skip to content

Commit 782cd4c

Browse files
committed
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 <[email protected]>
1 parent 93921b0 commit 782cd4c

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

builtin/ls-files.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ static void show_ru_info(void)
200200
}
201201
}
202202

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+
203209
static void show_files(struct dir_struct *dir)
204210
{
205211
int i;
@@ -220,7 +226,7 @@ static void show_files(struct dir_struct *dir)
220226
for (i = 0; i < active_nr; i++) {
221227
struct cache_entry *ce = active_cache[i];
222228
if ((dir->flags & DIR_SHOW_IGNORED) &&
223-
!path_excluded(&check, ce))
229+
!ce_excluded(&check, ce))
224230
continue;
225231
if (show_unmerged && !ce_stage(ce))
226232
continue;
@@ -236,7 +242,7 @@ static void show_files(struct dir_struct *dir)
236242
struct stat st;
237243
int err;
238244
if ((dir->flags & DIR_SHOW_IGNORED) &&
239-
!path_excluded(&check, ce))
245+
!ce_excluded(&check, ce))
240246
continue;
241247
if (ce->ce_flags & CE_UPDATE)
242248
continue;

dir.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -593,31 +593,40 @@ void path_exclude_check_clear(struct path_exclude_check *check)
593593
}
594594

595595
/*
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
597597
* do not honor directory hierarchy and iterate through paths that are
598598
* possibly in an ignored directory.
599599
*
600600
* A path to a directory known to be excluded is left in check->path to
601601
* optimize for repeated checks for files in the same excluded directory.
602602
*/
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)
604605
{
605-
int i, dtype;
606+
int i;
606607
struct strbuf *path = &check->path;
607608

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+
608617
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] == '/'))
612621
return 1;
613622

614623
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];
617626

618627
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))
621630
return 1;
622631
}
623632
strbuf_addch(path, ch);
@@ -626,8 +635,7 @@ int path_excluded(struct path_exclude_check *check, struct cache_entry *ce)
626635
/* An entry in the index; cannot be a directory with subentries */
627636
strbuf_setlen(path, 0);
628637

629-
dtype = ce_to_dtype(ce);
630-
return excluded(check->dir, ce->name, &dtype);
638+
return excluded(check->dir, name, dtype);
631639
}
632640

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

dir.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ struct path_exclude_check {
9292
};
9393
extern void path_exclude_check_init(struct path_exclude_check *, struct dir_struct *);
9494
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+
9697

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

0 commit comments

Comments
 (0)