cvsserver: Let --base-path and pserver get along just fine
[git/gitweb.git] / dir.c
blobf543f50f42d5bca1a6a6f981e8bb1b3cafd352ea
1 /*
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
7 */
8 #include "cache.h"
9 #include "dir.h"
10 #include "refs.h"
12 struct path_simplify {
13 int len;
14 const char *path;
17 static int read_directory_recursive(struct dir_struct *dir,
18 const char *path, const char *base, int baselen,
19 int check_only, const struct path_simplify *simplify);
21 int common_prefix(const char **pathspec)
23 const char *path, *slash, *next;
24 int prefix;
26 if (!pathspec)
27 return 0;
29 path = *pathspec;
30 slash = strrchr(path, '/');
31 if (!slash)
32 return 0;
34 prefix = slash - path + 1;
35 while ((next = *++pathspec) != NULL) {
36 int len = strlen(next);
37 if (len >= prefix && !memcmp(path, next, prefix))
38 continue;
39 len = prefix - 1;
40 for (;;) {
41 if (!len)
42 return 0;
43 if (next[--len] != '/')
44 continue;
45 if (memcmp(path, next, len+1))
46 continue;
47 prefix = len + 1;
48 break;
51 return prefix;
55 * Does 'match' matches the given name?
56 * A match is found if
58 * (1) the 'match' string is leading directory of 'name', or
59 * (2) the 'match' string is a wildcard and matches 'name', or
60 * (3) the 'match' string is exactly the same as 'name'.
62 * and the return value tells which case it was.
64 * It returns 0 when there is no match.
66 static int match_one(const char *match, const char *name, int namelen)
68 int matchlen;
70 /* If the match was just the prefix, we matched */
71 matchlen = strlen(match);
72 if (!matchlen)
73 return MATCHED_RECURSIVELY;
76 * If we don't match the matchstring exactly,
77 * we need to match by fnmatch
79 if (strncmp(match, name, matchlen))
80 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
82 if (!name[matchlen])
83 return MATCHED_EXACTLY;
84 if (match[matchlen-1] == '/' || name[matchlen] == '/')
85 return MATCHED_RECURSIVELY;
86 return 0;
90 * Given a name and a list of pathspecs, see if the name matches
91 * any of the pathspecs. The caller is also interested in seeing
92 * all pathspec matches some names it calls this function with
93 * (otherwise the user could have mistyped the unmatched pathspec),
94 * and a mark is left in seen[] array for pathspec element that
95 * actually matched anything.
97 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
99 int retval;
100 const char *match;
102 name += prefix;
103 namelen -= prefix;
105 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
106 int how;
107 if (retval && *seen == MATCHED_EXACTLY)
108 continue;
109 match += prefix;
110 how = match_one(match, name, namelen);
111 if (how) {
112 if (retval < how)
113 retval = how;
114 if (*seen < how)
115 *seen = how;
118 return retval;
121 void add_exclude(const char *string, const char *base,
122 int baselen, struct exclude_list *which)
124 struct exclude *x = xmalloc(sizeof (*x));
126 x->pattern = string;
127 x->base = base;
128 x->baselen = baselen;
129 if (which->nr == which->alloc) {
130 which->alloc = alloc_nr(which->alloc);
131 which->excludes = xrealloc(which->excludes,
132 which->alloc * sizeof(x));
134 which->excludes[which->nr++] = x;
137 static int add_excludes_from_file_1(const char *fname,
138 const char *base,
139 int baselen,
140 struct exclude_list *which)
142 struct stat st;
143 int fd, i;
144 size_t size;
145 char *buf, *entry;
147 fd = open(fname, O_RDONLY);
148 if (fd < 0 || fstat(fd, &st) < 0)
149 goto err;
150 size = xsize_t(st.st_size);
151 if (size == 0) {
152 close(fd);
153 return 0;
155 buf = xmalloc(size+1);
156 if (read_in_full(fd, buf, size) != size)
157 goto err;
158 close(fd);
160 buf[size++] = '\n';
161 entry = buf;
162 for (i = 0; i < size; i++) {
163 if (buf[i] == '\n') {
164 if (entry != buf + i && entry[0] != '#') {
165 buf[i - (i && buf[i-1] == '\r')] = 0;
166 add_exclude(entry, base, baselen, which);
168 entry = buf + i + 1;
171 return 0;
173 err:
174 if (0 <= fd)
175 close(fd);
176 return -1;
179 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
181 if (add_excludes_from_file_1(fname, "", 0,
182 &dir->exclude_list[EXC_FILE]) < 0)
183 die("cannot use %s as an exclude file", fname);
186 int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
188 char exclude_file[PATH_MAX];
189 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
190 int current_nr = el->nr;
192 if (dir->exclude_per_dir) {
193 memcpy(exclude_file, base, baselen);
194 strcpy(exclude_file + baselen, dir->exclude_per_dir);
195 add_excludes_from_file_1(exclude_file, base, baselen, el);
197 return current_nr;
200 void pop_exclude_per_directory(struct dir_struct *dir, int stk)
202 struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
204 while (stk < el->nr)
205 free(el->excludes[--el->nr]);
208 /* Scan the list and let the last match determines the fate.
209 * Return 1 for exclude, 0 for include and -1 for undecided.
211 static int excluded_1(const char *pathname,
212 int pathlen,
213 struct exclude_list *el)
215 int i;
217 if (el->nr) {
218 for (i = el->nr - 1; 0 <= i; i--) {
219 struct exclude *x = el->excludes[i];
220 const char *exclude = x->pattern;
221 int to_exclude = 1;
223 if (*exclude == '!') {
224 to_exclude = 0;
225 exclude++;
228 if (!strchr(exclude, '/')) {
229 /* match basename */
230 const char *basename = strrchr(pathname, '/');
231 basename = (basename) ? basename+1 : pathname;
232 if (fnmatch(exclude, basename, 0) == 0)
233 return to_exclude;
235 else {
236 /* match with FNM_PATHNAME:
237 * exclude has base (baselen long) implicitly
238 * in front of it.
240 int baselen = x->baselen;
241 if (*exclude == '/')
242 exclude++;
244 if (pathlen < baselen ||
245 (baselen && pathname[baselen-1] != '/') ||
246 strncmp(pathname, x->base, baselen))
247 continue;
249 if (fnmatch(exclude, pathname+baselen,
250 FNM_PATHNAME) == 0)
251 return to_exclude;
255 return -1; /* undecided */
258 int excluded(struct dir_struct *dir, const char *pathname)
260 int pathlen = strlen(pathname);
261 int st;
263 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
264 switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
265 case 0:
266 return 0;
267 case 1:
268 return 1;
271 return 0;
274 struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
276 struct dir_entry *ent;
278 if (cache_name_pos(pathname, len) >= 0)
279 return NULL;
281 if (dir->nr == dir->alloc) {
282 int alloc = alloc_nr(dir->alloc);
283 dir->alloc = alloc;
284 dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
286 ent = xmalloc(sizeof(*ent) + len + 1);
287 ent->ignored = ent->ignored_dir = 0;
288 ent->len = len;
289 memcpy(ent->name, pathname, len);
290 ent->name[len] = 0;
291 dir->entries[dir->nr++] = ent;
292 return ent;
295 enum exist_status {
296 index_nonexistent = 0,
297 index_directory,
298 index_gitdir,
302 * The index sorts alphabetically by entry name, which
303 * means that a gitlink sorts as '\0' at the end, while
304 * a directory (which is defined not as an entry, but as
305 * the files it contains) will sort with the '/' at the
306 * end.
308 static enum exist_status directory_exists_in_index(const char *dirname, int len)
310 int pos = cache_name_pos(dirname, len);
311 if (pos < 0)
312 pos = -pos-1;
313 while (pos < active_nr) {
314 struct cache_entry *ce = active_cache[pos++];
315 unsigned char endchar;
317 if (strncmp(ce->name, dirname, len))
318 break;
319 endchar = ce->name[len];
320 if (endchar > '/')
321 break;
322 if (endchar == '/')
323 return index_directory;
324 if (!endchar && S_ISGITLINK(ntohl(ce->ce_mode)))
325 return index_gitdir;
327 return index_nonexistent;
331 * When we find a directory when traversing the filesystem, we
332 * have three distinct cases:
334 * - ignore it
335 * - see it as a directory
336 * - recurse into it
338 * and which one we choose depends on a combination of existing
339 * git index contents and the flags passed into the directory
340 * traversal routine.
342 * Case 1: If we *already* have entries in the index under that
343 * directory name, we always recurse into the directory to see
344 * all the files.
346 * Case 2: If we *already* have that directory name as a gitlink,
347 * we always continue to see it as a gitlink, regardless of whether
348 * there is an actual git directory there or not (it might not
349 * be checked out as a subproject!)
351 * Case 3: if we didn't have it in the index previously, we
352 * have a few sub-cases:
354 * (a) if "show_other_directories" is true, we show it as
355 * just a directory, unless "hide_empty_directories" is
356 * also true and the directory is empty, in which case
357 * we just ignore it entirely.
358 * (b) if it looks like a git directory, and we don't have
359 * 'no_gitlinks' set we treat it as a gitlink, and show it
360 * as a directory.
361 * (c) otherwise, we recurse into it.
363 enum directory_treatment {
364 show_directory,
365 ignore_directory,
366 recurse_into_directory,
369 static enum directory_treatment treat_directory(struct dir_struct *dir,
370 const char *dirname, int len,
371 const struct path_simplify *simplify)
373 /* The "len-1" is to strip the final '/' */
374 switch (directory_exists_in_index(dirname, len-1)) {
375 case index_directory:
376 return recurse_into_directory;
378 case index_gitdir:
379 if (dir->show_other_directories)
380 return ignore_directory;
381 return show_directory;
383 case index_nonexistent:
384 if (dir->show_other_directories)
385 break;
386 if (!dir->no_gitlinks) {
387 unsigned char sha1[20];
388 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
389 return show_directory;
391 return recurse_into_directory;
394 /* This is the "show_other_directories" case */
395 if (!dir->hide_empty_directories)
396 return show_directory;
397 if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
398 return ignore_directory;
399 return show_directory;
403 * This is an inexact early pruning of any recursive directory
404 * reading - if the path cannot possibly be in the pathspec,
405 * return true, and we'll skip it early.
407 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
409 if (simplify) {
410 for (;;) {
411 const char *match = simplify->path;
412 int len = simplify->len;
414 if (!match)
415 break;
416 if (len > pathlen)
417 len = pathlen;
418 if (!memcmp(path, match, len))
419 return 0;
420 simplify++;
422 return 1;
424 return 0;
428 * Read a directory tree. We currently ignore anything but
429 * directories, regular files and symlinks. That's because git
430 * doesn't handle them at all yet. Maybe that will change some
431 * day.
433 * Also, we ignore the name ".git" (even if it is not a directory).
434 * That likely will not change.
436 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)
438 DIR *fdir = opendir(path);
439 int contents = 0;
441 if (fdir) {
442 int exclude_stk;
443 struct dirent *de;
444 char fullname[PATH_MAX + 1];
445 memcpy(fullname, base, baselen);
447 exclude_stk = push_exclude_per_directory(dir, base, baselen);
449 while ((de = readdir(fdir)) != NULL) {
450 int len;
451 int exclude;
453 if ((de->d_name[0] == '.') &&
454 (de->d_name[1] == 0 ||
455 !strcmp(de->d_name + 1, ".") ||
456 !strcmp(de->d_name + 1, "git")))
457 continue;
458 len = strlen(de->d_name);
459 /* Ignore overly long pathnames! */
460 if (len + baselen + 8 > sizeof(fullname))
461 continue;
462 memcpy(fullname + baselen, de->d_name, len+1);
463 if (simplify_away(fullname, baselen + len, simplify))
464 continue;
466 exclude = excluded(dir, fullname);
467 if (exclude != dir->show_ignored) {
468 if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
469 continue;
473 switch (DTYPE(de)) {
474 struct stat st;
475 default:
476 continue;
477 case DT_UNKNOWN:
478 if (lstat(fullname, &st))
479 continue;
480 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
481 break;
482 if (!S_ISDIR(st.st_mode))
483 continue;
484 /* fallthrough */
485 case DT_DIR:
486 memcpy(fullname + baselen + len, "/", 2);
487 len++;
488 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
489 case show_directory:
490 if (exclude != dir->show_ignored)
491 continue;
492 break;
493 case recurse_into_directory:
494 contents += read_directory_recursive(dir,
495 fullname, fullname, baselen + len, 0, simplify);
496 continue;
497 case ignore_directory:
498 continue;
500 break;
501 case DT_REG:
502 case DT_LNK:
503 break;
505 contents++;
506 if (check_only)
507 goto exit_early;
508 else
509 dir_add_name(dir, fullname, baselen + len);
511 exit_early:
512 closedir(fdir);
514 pop_exclude_per_directory(dir, exclude_stk);
517 return contents;
520 static int cmp_name(const void *p1, const void *p2)
522 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
523 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
525 return cache_name_compare(e1->name, e1->len,
526 e2->name, e2->len);
530 * Return the length of the "simple" part of a path match limiter.
532 static int simple_length(const char *match)
534 const char special[256] = {
535 [0] = 1, ['?'] = 1,
536 ['\\'] = 1, ['*'] = 1,
537 ['['] = 1
539 int len = -1;
541 for (;;) {
542 unsigned char c = *match++;
543 len++;
544 if (special[c])
545 return len;
549 static struct path_simplify *create_simplify(const char **pathspec)
551 int nr, alloc = 0;
552 struct path_simplify *simplify = NULL;
554 if (!pathspec)
555 return NULL;
557 for (nr = 0 ; ; nr++) {
558 const char *match;
559 if (nr >= alloc) {
560 alloc = alloc_nr(alloc);
561 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
563 match = *pathspec++;
564 if (!match)
565 break;
566 simplify[nr].path = match;
567 simplify[nr].len = simple_length(match);
569 simplify[nr].path = NULL;
570 simplify[nr].len = 0;
571 return simplify;
574 static void free_simplify(struct path_simplify *simplify)
576 if (simplify)
577 free(simplify);
580 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
582 struct path_simplify *simplify = create_simplify(pathspec);
585 * Make sure to do the per-directory exclude for all the
586 * directories leading up to our base.
588 if (baselen) {
589 if (dir->exclude_per_dir) {
590 char *p, *pp = xmalloc(baselen+1);
591 memcpy(pp, base, baselen+1);
592 p = pp;
593 while (1) {
594 char save = *p;
595 *p = 0;
596 push_exclude_per_directory(dir, pp, p-pp);
597 *p++ = save;
598 if (!save)
599 break;
600 p = strchr(p, '/');
601 if (p)
602 p++;
603 else
604 p = pp + baselen;
606 free(pp);
610 read_directory_recursive(dir, path, base, baselen, 0, simplify);
611 free_simplify(simplify);
612 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
613 return dir->nr;
617 file_exists(const char *f)
619 struct stat sb;
620 return stat(f, &sb) == 0;