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 int common_prefix(const char **pathspec)
13 const char *path, *slash, *next;
20 slash = strrchr(path, '/');
24 prefix = slash - path + 1;
25 while ((next = *++pathspec) != NULL) {
26 int len = strlen(next);
27 if (len >= prefix && !memcmp(path, next, len))
32 if (next[--len] != '/')
34 if (memcmp(path, next, len+1))
44 * Does 'match' matches the given name?
47 * (1) the 'match' string is leading directory of 'name', or
48 * (2) the 'match' string is a wildcard and matches 'name', or
49 * (3) the 'match' string is exactly the same as 'name'.
51 * and the return value tells which case it was.
53 * It returns 0 when there is no match.
55 static int match_one(const char *match, const char *name, int namelen)
59 /* If the match was just the prefix, we matched */
60 matchlen = strlen(match);
62 return MATCHED_RECURSIVELY;
65 * If we don't match the matchstring exactly,
66 * we need to match by fnmatch
68 if (strncmp(match, name, matchlen))
69 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
72 return MATCHED_EXACTLY;
73 if (match[matchlen-1] == '/' || name[matchlen] == '/')
74 return MATCHED_RECURSIVELY;
79 * Given a name and a list of pathspecs, see if the name matches
80 * any of the pathspecs. The caller is also interested in seeing
81 * all pathspec matches some names it calls this function with
82 * (otherwise the user could have mistyped the unmatched pathspec),
83 * and a mark is left in seen[] array for pathspec element that
84 * actually matched anything.
86 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
94 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
96 if (retval && *seen == MATCHED_EXACTLY)
99 how = match_one(match, name, namelen);
110 void add_exclude(const char *string, const char *base,
111 int baselen, struct exclude_list *which)
113 struct exclude *x = xmalloc(sizeof (*x));
117 x->baselen = baselen;
118 if (which->nr == which->alloc) {
119 which->alloc = alloc_nr(which->alloc);
120 which->excludes = xrealloc(which->excludes,
121 which->alloc * sizeof(x));
123 which->excludes[which->nr++] = x;
126 static int add_excludes_from_file_1(const char *fname,
129 struct exclude_list *which)
136 fd = open(fname, O_RDONLY);
137 if (fd < 0 || fstat(fd, &st) < 0)
144 buf = xmalloc(size+1);
145 if (read(fd, buf, size) != size)
151 for (i = 0; i < size; i++) {
152 if (buf[i] == '\n') {
153 if (entry != buf + i && entry[0] != '#') {
154 buf[i - (i && buf[i-1] == '\r')] = 0;
155 add_exclude(entry, base, baselen, which);
168 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
170 if (add_excludes_from_file_1(fname, "", 0,
171 &dir->exclude_list[EXC_FILE]) < 0)
172 die("cannot use %s as an exclude file", fname);
175 int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
177 char exclude_file[PATH_MAX];
178 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
179 int current_nr = el->nr;
181 if (dir->exclude_per_dir) {
182 memcpy(exclude_file, base, baselen);
183 strcpy(exclude_file + baselen, dir->exclude_per_dir);
184 add_excludes_from_file_1(exclude_file, base, baselen, el);
189 void pop_exclude_per_directory(struct dir_struct *dir, int stk)
191 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
194 free(el->excludes[--el->nr]);
197 /* Scan the list and let the last match determines the fate.
198 * Return 1 for exclude, 0 for include and -1 for undecided.
200 static int excluded_1(const char *pathname,
202 struct exclude_list *el)
207 for (i = el->nr - 1; 0 <= i; i--) {
208 struct exclude *x = el->excludes[i];
209 const char *exclude = x->pattern;
212 if (*exclude == '!') {
217 if (!strchr(exclude, '/')) {
219 const char *basename = strrchr(pathname, '/');
220 basename = (basename) ? basename+1 : pathname;
221 if (fnmatch(exclude, basename, 0) == 0)
225 /* match with FNM_PATHNAME:
226 * exclude has base (baselen long) implicitly
229 int baselen = x->baselen;
233 if (pathlen < baselen ||
234 (baselen && pathname[baselen-1] != '/') ||
235 strncmp(pathname, x->base, baselen))
238 if (fnmatch(exclude, pathname+baselen,
244 return -1; /* undecided */
247 int excluded(struct dir_struct *dir, const char *pathname)
249 int pathlen = strlen(pathname);
252 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
253 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
263 static void add_name(struct dir_struct *dir, const char *pathname, int len)
265 struct dir_entry *ent;
267 if (cache_name_pos(pathname, len) >= 0)
270 if (dir->nr == dir->alloc) {
271 int alloc = alloc_nr(dir->alloc);
273 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
275 ent = xmalloc(sizeof(*ent) + len + 1);
277 memcpy(ent->name, pathname, len);
279 dir->entries[dir->nr++] = ent;
282 static int dir_exists(const char *dirname, int len)
284 int pos = cache_name_pos(dirname, len);
288 if (pos >= active_nr) /* can't */
290 return !strncmp(active_cache[pos]->name, dirname, len);
294 * Read a directory tree. We currently ignore anything but
295 * directories, regular files and symlinks. That's because git
296 * doesn't handle them at all yet. Maybe that will change some
299 * Also, we ignore the name ".git" (even if it is not a directory).
300 * That likely will not change.
302 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only)
304 DIR *fdir = opendir(path);
310 char fullname[PATH_MAX + 1];
311 memcpy(fullname, base, baselen);
313 exclude_stk = push_exclude_per_directory(dir, base, baselen);
315 while ((de = readdir(fdir)) != NULL) {
318 if ((de->d_name[0] == '.') &&
319 (de->d_name[1] == 0 ||
320 !strcmp(de->d_name + 1, ".") ||
321 !strcmp(de->d_name + 1, "git")))
323 len = strlen(de->d_name);
324 memcpy(fullname + baselen, de->d_name, len+1);
325 if (excluded(dir, fullname) != dir->show_ignored) {
326 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
336 if (lstat(fullname, &st))
338 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
340 if (!S_ISDIR(st.st_mode))
344 memcpy(fullname + baselen + len, "/", 2);
346 if (dir->show_other_directories &&
347 !dir_exists(fullname, baselen + len)) {
348 if (dir->hide_empty_directories &&
349 !read_directory_recursive(dir,
356 contents += read_directory_recursive(dir,
357 fullname, fullname, baselen + len, 0);
367 add_name(dir, fullname, baselen + len);
372 pop_exclude_per_directory(dir, exclude_stk);
378 static int cmp_name(const void *p1, const void *p2)
380 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
381 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
383 return cache_name_compare(e1->name, e1->len,
387 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
390 * Make sure to do the per-directory exclude for all the
391 * directories leading up to our base.
394 if (dir->exclude_per_dir) {
395 char *p, *pp = xmalloc(baselen+1);
396 memcpy(pp, base, baselen+1);
401 push_exclude_per_directory(dir, pp, p-pp);
415 read_directory_recursive(dir, path, base, baselen, 0);
416 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
421 file_exists(const char *f)
424 return stat(f, &sb) == 0;