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
14 int common_prefix(const char **pathspec)
16 const char *path, *slash, *next;
23 slash = strrchr(path, '/');
27 prefix = slash - path + 1;
28 while ((next = *++pathspec) != NULL) {
29 int len = strlen(next);
30 if (len >= prefix && !memcmp(path, next, len))
35 if (next[--len] != '/')
37 if (memcmp(path, next, len+1))
46 static int match_one(const char *match, const char *name, int namelen)
50 /* If the match was just the prefix, we matched */
51 matchlen = strlen(match);
56 * If we don't match the matchstring exactly,
57 * we need to match by fnmatch
59 if (strncmp(match, name, matchlen))
60 return !fnmatch(match, name, 0);
63 * If we did match the string exactly, we still
64 * need to make sure that it happened on a path
65 * component boundary (ie either the last character
66 * of the match was '/', or the next character of
67 * the name was '/' or the terminating NUL.
69 return match[matchlen-1] == '/' ||
70 name[matchlen] == '/' ||
74 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
82 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
86 if (match_one(match, name, namelen)) {
94 void add_exclude(const char *string, const char *base,
95 int baselen, struct exclude_list *which)
97 struct exclude *x = xmalloc(sizeof (*x));
101 x->baselen = baselen;
102 if (which->nr == which->alloc) {
103 which->alloc = alloc_nr(which->alloc);
104 which->excludes = realloc(which->excludes,
105 which->alloc * sizeof(x));
107 which->excludes[which->nr++] = x;
110 static int add_excludes_from_file_1(const char *fname,
113 struct exclude_list *which)
119 fd = open(fname, O_RDONLY);
122 size = lseek(fd, 0, SEEK_END);
125 lseek(fd, 0, SEEK_SET);
130 buf = xmalloc(size+1);
131 if (read(fd, buf, size) != size)
137 for (i = 0; i < size; i++) {
138 if (buf[i] == '\n') {
139 if (entry != buf + i && entry[0] != '#') {
140 buf[i - (i && buf[i-1] == '\r')] = 0;
141 add_exclude(entry, base, baselen, which);
154 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
156 if (add_excludes_from_file_1(fname, "", 0,
157 &dir->exclude_list[EXC_FILE]) < 0)
158 die("cannot use %s as an exclude file", fname);
161 static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
163 char exclude_file[PATH_MAX];
164 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
165 int current_nr = el->nr;
167 if (dir->exclude_per_dir) {
168 memcpy(exclude_file, base, baselen);
169 strcpy(exclude_file + baselen, dir->exclude_per_dir);
170 add_excludes_from_file_1(exclude_file, base, baselen, el);
175 static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
177 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
180 free(el->excludes[--el->nr]);
183 /* Scan the list and let the last match determines the fate.
184 * Return 1 for exclude, 0 for include and -1 for undecided.
186 static int excluded_1(const char *pathname,
188 struct exclude_list *el)
193 for (i = el->nr - 1; 0 <= i; i--) {
194 struct exclude *x = el->excludes[i];
195 const char *exclude = x->pattern;
198 if (*exclude == '!') {
203 if (!strchr(exclude, '/')) {
205 const char *basename = strrchr(pathname, '/');
206 basename = (basename) ? basename+1 : pathname;
207 if (fnmatch(exclude, basename, 0) == 0)
211 /* match with FNM_PATHNAME:
212 * exclude has base (baselen long) implicitly
215 int baselen = x->baselen;
219 if (pathlen < baselen ||
220 (baselen && pathname[baselen-1] != '/') ||
221 strncmp(pathname, x->base, baselen))
224 if (fnmatch(exclude, pathname+baselen,
230 return -1; /* undecided */
233 int excluded(struct dir_struct *dir, const char *pathname)
235 int pathlen = strlen(pathname);
238 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
239 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
249 static void add_name(struct dir_struct *dir, const char *pathname, int len)
251 struct dir_entry *ent;
253 if (cache_name_pos(pathname, len) >= 0)
256 if (dir->nr == dir->alloc) {
257 int alloc = alloc_nr(dir->alloc);
259 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
261 ent = xmalloc(sizeof(*ent) + len + 1);
263 memcpy(ent->name, pathname, len);
265 dir->entries[dir->nr++] = ent;
268 static int dir_exists(const char *dirname, int len)
270 int pos = cache_name_pos(dirname, len);
274 if (pos >= active_nr) /* can't */
276 return !strncmp(active_cache[pos]->name, dirname, len);
280 * Read a directory tree. We currently ignore anything but
281 * directories, regular files and symlinks. That's because git
282 * doesn't handle them at all yet. Maybe that will change some
285 * Also, we ignore the name ".git" (even if it is not a directory).
286 * That likely will not change.
288 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen)
290 DIR *fdir = opendir(path);
296 char fullname[MAXPATHLEN + 1];
297 memcpy(fullname, base, baselen);
299 exclude_stk = push_exclude_per_directory(dir, base, baselen);
301 while ((de = readdir(fdir)) != NULL) {
304 if ((de->d_name[0] == '.') &&
305 (de->d_name[1] == 0 ||
306 !strcmp(de->d_name + 1, ".") ||
307 !strcmp(de->d_name + 1, "git")))
309 len = strlen(de->d_name);
310 memcpy(fullname + baselen, de->d_name, len+1);
311 if (excluded(dir, fullname) != dir->show_ignored) {
312 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
319 int subdir, rewind_base;
323 if (lstat(fullname, &st))
325 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
327 if (!S_ISDIR(st.st_mode))
331 memcpy(fullname + baselen + len, "/", 2);
333 rewind_base = dir->nr;
334 subdir = read_directory_recursive(dir, fullname, fullname,
336 if (dir->show_other_directories &&
337 (subdir || !dir->hide_empty_directories) &&
338 !dir_exists(fullname, baselen + len)) {
339 // Rewind the read subdirectory
340 while (dir->nr > rewind_base)
341 free(dir->entries[--dir->nr]);
350 add_name(dir, fullname, baselen + len);
355 pop_exclude_per_directory(dir, exclude_stk);
361 static int cmp_name(const void *p1, const void *p2)
363 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
364 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
366 return cache_name_compare(e1->name, e1->len,
370 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
373 * Make sure to do the per-directory exclude for all the
374 * directories leading up to our base.
377 if (dir->exclude_per_dir) {
378 char *p, *pp = xmalloc(baselen+1);
379 memcpy(pp, base, baselen+1);
384 push_exclude_per_directory(dir, pp, p-pp);
398 read_directory_recursive(dir, path, base, baselen);
399 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);