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 = xrealloc(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)
120 fd = open(fname, O_RDONLY);
121 if (fd < 0 || fstat(fd, &st) < 0)
128 buf = xmalloc(size+1);
129 if (read(fd, buf, size) != size)
135 for (i = 0; i < size; i++) {
136 if (buf[i] == '\n') {
137 if (entry != buf + i && entry[0] != '#') {
138 buf[i - (i && buf[i-1] == '\r')] = 0;
139 add_exclude(entry, base, baselen, which);
152 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
154 if (add_excludes_from_file_1(fname, "", 0,
155 &dir->exclude_list[EXC_FILE]) < 0)
156 die("cannot use %s as an exclude file", fname);
159 static int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
161 char exclude_file[PATH_MAX];
162 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
163 int current_nr = el->nr;
165 if (dir->exclude_per_dir) {
166 memcpy(exclude_file, base, baselen);
167 strcpy(exclude_file + baselen, dir->exclude_per_dir);
168 add_excludes_from_file_1(exclude_file, base, baselen, el);
173 static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
175 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
178 free(el->excludes[--el->nr]);
181 /* Scan the list and let the last match determines the fate.
182 * Return 1 for exclude, 0 for include and -1 for undecided.
184 static int excluded_1(const char *pathname,
186 struct exclude_list *el)
191 for (i = el->nr - 1; 0 <= i; i--) {
192 struct exclude *x = el->excludes[i];
193 const char *exclude = x->pattern;
196 if (*exclude == '!') {
201 if (!strchr(exclude, '/')) {
203 const char *basename = strrchr(pathname, '/');
204 basename = (basename) ? basename+1 : pathname;
205 if (fnmatch(exclude, basename, 0) == 0)
209 /* match with FNM_PATHNAME:
210 * exclude has base (baselen long) implicitly
213 int baselen = x->baselen;
217 if (pathlen < baselen ||
218 (baselen && pathname[baselen-1] != '/') ||
219 strncmp(pathname, x->base, baselen))
222 if (fnmatch(exclude, pathname+baselen,
228 return -1; /* undecided */
231 int excluded(struct dir_struct *dir, const char *pathname)
233 int pathlen = strlen(pathname);
236 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
237 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
247 static void add_name(struct dir_struct *dir, const char *pathname, int len)
249 struct dir_entry *ent;
251 if (cache_name_pos(pathname, len) >= 0)
254 if (dir->nr == dir->alloc) {
255 int alloc = alloc_nr(dir->alloc);
257 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
259 ent = xmalloc(sizeof(*ent) + len + 1);
261 memcpy(ent->name, pathname, len);
263 dir->entries[dir->nr++] = ent;
266 static int dir_exists(const char *dirname, int len)
268 int pos = cache_name_pos(dirname, len);
272 if (pos >= active_nr) /* can't */
274 return !strncmp(active_cache[pos]->name, dirname, len);
278 * Read a directory tree. We currently ignore anything but
279 * directories, regular files and symlinks. That's because git
280 * doesn't handle them at all yet. Maybe that will change some
283 * Also, we ignore the name ".git" (even if it is not a directory).
284 * That likely will not change.
286 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only)
288 DIR *fdir = opendir(path);
294 char fullname[PATH_MAX + 1];
295 memcpy(fullname, base, baselen);
297 exclude_stk = push_exclude_per_directory(dir, base, baselen);
299 while ((de = readdir(fdir)) != NULL) {
302 if ((de->d_name[0] == '.') &&
303 (de->d_name[1] == 0 ||
304 !strcmp(de->d_name + 1, ".") ||
305 !strcmp(de->d_name + 1, "git")))
307 len = strlen(de->d_name);
308 memcpy(fullname + baselen, de->d_name, len+1);
309 if (excluded(dir, fullname) != dir->show_ignored) {
310 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
320 if (lstat(fullname, &st))
322 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
324 if (!S_ISDIR(st.st_mode))
328 memcpy(fullname + baselen + len, "/", 2);
330 if (dir->show_other_directories &&
331 !dir_exists(fullname, baselen + len)) {
332 if (dir->hide_empty_directories &&
333 !read_directory_recursive(dir,
340 contents += read_directory_recursive(dir,
341 fullname, fullname, baselen + len, 0);
351 add_name(dir, fullname, baselen + len);
356 pop_exclude_per_directory(dir, exclude_stk);
362 static int cmp_name(const void *p1, const void *p2)
364 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
365 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
367 return cache_name_compare(e1->name, e1->len,
371 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
374 * Make sure to do the per-directory exclude for all the
375 * directories leading up to our base.
378 if (dir->exclude_per_dir) {
379 char *p, *pp = xmalloc(baselen+1);
380 memcpy(pp, base, baselen+1);
385 push_exclude_per_directory(dir, pp, p-pp);
399 read_directory_recursive(dir, path, base, baselen, 0);
400 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
405 file_exists(const char *f)
408 return stat(f, &sb) == 0;