2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
11 struct path_simplify {
16 int common_prefix(const char **pathspec)
18 const char *path, *slash, *next;
25 slash = strrchr(path, '/');
29 prefix = slash - path + 1;
30 while ((next = *++pathspec) != NULL) {
31 int len = strlen(next);
32 if (len >= prefix && !memcmp(path, next, len))
37 if (next[--len] != '/')
39 if (memcmp(path, next, len+1))
49 * Does 'match' matches the given name?
52 * (1) the 'match' string is leading directory of 'name', or
53 * (2) the 'match' string is a wildcard and matches 'name', or
54 * (3) the 'match' string is exactly the same as 'name'.
56 * and the return value tells which case it was.
58 * It returns 0 when there is no match.
60 static int match_one(const char *match, const char *name, int namelen)
64 /* If the match was just the prefix, we matched */
65 matchlen = strlen(match);
67 return MATCHED_RECURSIVELY;
70 * If we don't match the matchstring exactly,
71 * we need to match by fnmatch
73 if (strncmp(match, name, matchlen))
74 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
77 return MATCHED_EXACTLY;
78 if (match[matchlen-1] == '/' || name[matchlen] == '/')
79 return MATCHED_RECURSIVELY;
84 * Given a name and a list of pathspecs, see if the name matches
85 * any of the pathspecs. The caller is also interested in seeing
86 * all pathspec matches some names it calls this function with
87 * (otherwise the user could have mistyped the unmatched pathspec),
88 * and a mark is left in seen[] array for pathspec element that
89 * actually matched anything.
91 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
99 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
101 if (retval && *seen == MATCHED_EXACTLY)
104 how = match_one(match, name, namelen);
115 void add_exclude(const char *string, const char *base,
116 int baselen, struct exclude_list *which)
118 struct exclude *x = xmalloc(sizeof (*x));
122 x->baselen = baselen;
123 if (which->nr == which->alloc) {
124 which->alloc = alloc_nr(which->alloc);
125 which->excludes = xrealloc(which->excludes,
126 which->alloc * sizeof(x));
128 which->excludes[which->nr++] = x;
131 static int add_excludes_from_file_1(const char *fname,
134 struct exclude_list *which)
141 fd = open(fname, O_RDONLY);
142 if (fd < 0 || fstat(fd, &st) < 0)
144 size = xsize_t(st.st_size);
149 buf = xmalloc(size+1);
150 if (read_in_full(fd, buf, size) != size)
156 for (i = 0; i < size; i++) {
157 if (buf[i] == '\n') {
158 if (entry != buf + i && entry[0] != '#') {
159 buf[i - (i && buf[i-1] == '\r')] = 0;
160 add_exclude(entry, base, baselen, which);
173 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
175 if (add_excludes_from_file_1(fname, "", 0,
176 &dir->exclude_list[EXC_FILE]) < 0)
177 die("cannot use %s as an exclude file", fname);
180 int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
182 char exclude_file[PATH_MAX];
183 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
184 int current_nr = el->nr;
186 if (dir->exclude_per_dir) {
187 memcpy(exclude_file, base, baselen);
188 strcpy(exclude_file + baselen, dir->exclude_per_dir);
189 add_excludes_from_file_1(exclude_file, base, baselen, el);
194 void pop_exclude_per_directory(struct dir_struct *dir, int stk)
196 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
199 free(el->excludes[--el->nr]);
202 /* Scan the list and let the last match determines the fate.
203 * Return 1 for exclude, 0 for include and -1 for undecided.
205 static int excluded_1(const char *pathname,
207 struct exclude_list *el)
212 for (i = el->nr - 1; 0 <= i; i--) {
213 struct exclude *x = el->excludes[i];
214 const char *exclude = x->pattern;
217 if (*exclude == '!') {
222 if (!strchr(exclude, '/')) {
224 const char *basename = strrchr(pathname, '/');
225 basename = (basename) ? basename+1 : pathname;
226 if (fnmatch(exclude, basename, 0) == 0)
230 /* match with FNM_PATHNAME:
231 * exclude has base (baselen long) implicitly
234 int baselen = x->baselen;
238 if (pathlen < baselen ||
239 (baselen && pathname[baselen-1] != '/') ||
240 strncmp(pathname, x->base, baselen))
243 if (fnmatch(exclude, pathname+baselen,
249 return -1; /* undecided */
252 int excluded(struct dir_struct *dir, const char *pathname)
254 int pathlen = strlen(pathname);
257 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
258 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
268 struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
270 struct dir_entry *ent;
272 if (cache_name_pos(pathname, len) >= 0)
275 if (dir->nr == dir->alloc) {
276 int alloc = alloc_nr(dir->alloc);
278 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
280 ent = xmalloc(sizeof(*ent) + len + 1);
281 ent->ignored = ent->ignored_dir = 0;
283 memcpy(ent->name, pathname, len);
285 dir->entries[dir->nr++] = ent;
289 static int dir_exists(const char *dirname, int len)
291 int pos = cache_name_pos(dirname, len);
295 if (pos >= active_nr) /* can't */
297 return !strncmp(active_cache[pos]->name, dirname, len);
301 * This is an inexact early pruning of any recursive directory
302 * reading - if the path cannot possibly be in the pathspec,
303 * return true, and we'll skip it early.
305 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
309 const char *match = simplify->path;
310 int len = simplify->len;
316 if (!memcmp(path, match, len))
326 * Read a directory tree. We currently ignore anything but
327 * directories, regular files and symlinks. That's because git
328 * doesn't handle them at all yet. Maybe that will change some
331 * Also, we ignore the name ".git" (even if it is not a directory).
332 * That likely will not change.
334 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
336 DIR *fdir = opendir(path);
342 char fullname[PATH_MAX + 1];
343 memcpy(fullname, base, baselen);
345 exclude_stk = push_exclude_per_directory(dir, base, baselen);
347 while ((de = readdir(fdir)) != NULL) {
350 if ((de->d_name[0] == '.') &&
351 (de->d_name[1] == 0 ||
352 !strcmp(de->d_name + 1, ".") ||
353 !strcmp(de->d_name + 1, "git")))
355 len = strlen(de->d_name);
356 memcpy(fullname + baselen, de->d_name, len+1);
357 if (simplify_away(fullname, baselen + len, simplify))
359 if (excluded(dir, fullname) != dir->show_ignored) {
360 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
370 if (lstat(fullname, &st))
372 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
374 if (!S_ISDIR(st.st_mode))
378 memcpy(fullname + baselen + len, "/", 2);
380 if (dir->show_other_directories &&
381 !dir_exists(fullname, baselen + len)) {
382 if (dir->hide_empty_directories &&
383 !read_directory_recursive(dir,
385 baselen + len, 1, simplify))
390 contents += read_directory_recursive(dir,
391 fullname, fullname, baselen + len, 0, simplify);
401 dir_add_name(dir, fullname, baselen + len);
406 pop_exclude_per_directory(dir, exclude_stk);
412 static int cmp_name(const void *p1, const void *p2)
414 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
415 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
417 return cache_name_compare(e1->name, e1->len,
422 * Return the length of the "simple" part of a path match limiter.
424 static int simple_length(const char *match)
426 const char special[256] = {
428 ['\\'] = 1, ['*'] = 1,
434 unsigned char c = *match++;
441 static struct path_simplify *create_simplify(const char **pathspec)
444 struct path_simplify *simplify = NULL;
449 for (nr = 0 ; ; nr++) {
452 alloc = alloc_nr(alloc);
453 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
458 simplify[nr].path = match;
459 simplify[nr].len = simple_length(match);
461 simplify[nr].path = NULL;
462 simplify[nr].len = 0;
466 static void free_simplify(struct path_simplify *simplify)
472 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
474 struct path_simplify *simplify = create_simplify(pathspec);
477 * Make sure to do the per-directory exclude for all the
478 * directories leading up to our base.
481 if (dir->exclude_per_dir) {
482 char *p, *pp = xmalloc(baselen+1);
483 memcpy(pp, base, baselen+1);
488 push_exclude_per_directory(dir, pp, p-pp);
502 read_directory_recursive(dir, path, base, baselen, 0, simplify);
503 free_simplify(simplify);
504 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
509 file_exists(const char *f)
512 return stat(f, &sb) == 0;