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 void add_exclude(const char *string, const char *base,
15 int baselen, struct exclude_list *which)
17 struct exclude *x = xmalloc(sizeof (*x));
22 if (which->nr == which->alloc) {
23 which->alloc = alloc_nr(which->alloc);
24 which->excludes = realloc(which->excludes,
25 which->alloc * sizeof(x));
27 which->excludes[which->nr++] = x;
30 static int add_excludes_from_file_1(const char *fname,
33 struct exclude_list *which)
39 fd = open(fname, O_RDONLY);
42 size = lseek(fd, 0, SEEK_END);
45 lseek(fd, 0, SEEK_SET);
50 buf = xmalloc(size+1);
51 if (read(fd, buf, size) != size)
57 for (i = 0; i < size; i++) {
59 if (entry != buf + i && entry[0] != '#') {
60 buf[i - (i && buf[i-1] == '\r')] = 0;
61 add_exclude(entry, base, baselen, which);
74 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
76 if (add_excludes_from_file_1(fname, "", 0,
77 &dir->exclude_list[EXC_FILE]) < 0)
78 die("cannot use %s as an exclude file", fname);
81 static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
83 char exclude_file[PATH_MAX];
84 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
85 int current_nr = el->nr;
87 if (dir->exclude_per_dir) {
88 memcpy(exclude_file, base, baselen);
89 strcpy(exclude_file + baselen, dir->exclude_per_dir);
90 add_excludes_from_file_1(exclude_file, base, baselen, el);
95 static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
97 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
100 free(el->excludes[--el->nr]);
103 /* Scan the list and let the last match determines the fate.
104 * Return 1 for exclude, 0 for include and -1 for undecided.
106 static int excluded_1(const char *pathname,
108 struct exclude_list *el)
113 for (i = el->nr - 1; 0 <= i; i--) {
114 struct exclude *x = el->excludes[i];
115 const char *exclude = x->pattern;
118 if (*exclude == '!') {
123 if (!strchr(exclude, '/')) {
125 const char *basename = strrchr(pathname, '/');
126 basename = (basename) ? basename+1 : pathname;
127 if (fnmatch(exclude, basename, 0) == 0)
131 /* match with FNM_PATHNAME:
132 * exclude has base (baselen long) implicitly
135 int baselen = x->baselen;
139 if (pathlen < baselen ||
140 (baselen && pathname[baselen-1] != '/') ||
141 strncmp(pathname, x->base, baselen))
144 if (fnmatch(exclude, pathname+baselen,
150 return -1; /* undecided */
153 int excluded(struct dir_struct *dir, const char *pathname)
155 int pathlen = strlen(pathname);
158 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
159 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
169 static void add_name(struct dir_struct *dir, const char *pathname, int len)
171 struct dir_entry *ent;
173 if (cache_name_pos(pathname, len) >= 0)
176 if (dir->nr == dir->alloc) {
177 int alloc = alloc_nr(dir->alloc);
179 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
181 ent = xmalloc(sizeof(*ent) + len + 1);
183 memcpy(ent->name, pathname, len);
185 dir->entries[dir->nr++] = ent;
188 static int dir_exists(const char *dirname, int len)
190 int pos = cache_name_pos(dirname, len);
194 if (pos >= active_nr) /* can't */
196 return !strncmp(active_cache[pos]->name, dirname, len);
200 * Read a directory tree. We currently ignore anything but
201 * directories, regular files and symlinks. That's because git
202 * doesn't handle them at all yet. Maybe that will change some
205 * Also, we ignore the name ".git" (even if it is not a directory).
206 * That likely will not change.
208 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen)
210 DIR *fdir = opendir(path);
216 char fullname[MAXPATHLEN + 1];
217 memcpy(fullname, base, baselen);
219 exclude_stk = push_exclude_per_directory(dir, base, baselen);
221 while ((de = readdir(fdir)) != NULL) {
224 if ((de->d_name[0] == '.') &&
225 (de->d_name[1] == 0 ||
226 !strcmp(de->d_name + 1, ".") ||
227 !strcmp(de->d_name + 1, "git")))
229 len = strlen(de->d_name);
230 memcpy(fullname + baselen, de->d_name, len+1);
231 if (excluded(dir, fullname) != dir->show_ignored) {
232 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
239 int subdir, rewind_base;
243 if (lstat(fullname, &st))
245 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
247 if (!S_ISDIR(st.st_mode))
251 memcpy(fullname + baselen + len, "/", 2);
253 rewind_base = dir->nr;
254 subdir = read_directory_recursive(dir, fullname, fullname,
256 if (dir->show_other_directories &&
257 (subdir || !dir->hide_empty_directories) &&
258 !dir_exists(fullname, baselen + len)) {
259 // Rewind the read subdirectory
260 while (dir->nr > rewind_base)
261 free(dir->entries[--dir->nr]);
270 add_name(dir, fullname, baselen + len);
275 pop_exclude_per_directory(dir, exclude_stk);
281 static int cmp_name(const void *p1, const void *p2)
283 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
284 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
286 return cache_name_compare(e1->name, e1->len,
290 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
293 * Make sure to do the per-directory exclude for all the
294 * directories leading up to our base.
297 if (dir->exclude_per_dir) {
298 char *p, *pp = xmalloc(baselen+1);
299 memcpy(pp, base, baselen+1);
304 push_exclude_per_directory(dir, pp, p-pp);
318 read_directory_recursive(dir, path, base, baselen);
319 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);