Merge branch 'cr/push-force-tag-update'
[git.git] / dir.c
blob9afd388604c05b5c7f7c34cd19f1e6ff2a14fdce
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, const char *path, int len,
18 int check_only, const struct path_simplify *simplify);
19 static int get_dtype(struct dirent *de, const char *path, int len);
21 /* helper string functions with support for the ignore_case flag */
22 int strcmp_icase(const char *a, const char *b)
24 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
27 int strncmp_icase(const char *a, const char *b, size_t count)
29 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
32 int fnmatch_icase(const char *pattern, const char *string, int flags)
34 return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
37 inline int git_fnmatch(const char *pattern, const char *string,
38 int flags, int prefix)
40 int fnm_flags = 0;
41 if (flags & GFNM_PATHNAME)
42 fnm_flags |= FNM_PATHNAME;
43 if (prefix > 0) {
44 if (strncmp(pattern, string, prefix))
45 return FNM_NOMATCH;
46 pattern += prefix;
47 string += prefix;
49 if (flags & GFNM_ONESTAR) {
50 int pattern_len = strlen(++pattern);
51 int string_len = strlen(string);
52 return string_len < pattern_len ||
53 strcmp(pattern,
54 string + string_len - pattern_len);
56 return fnmatch(pattern, string, fnm_flags);
59 static size_t common_prefix_len(const char **pathspec)
61 const char *n, *first;
62 size_t max = 0;
64 if (!pathspec)
65 return max;
67 first = *pathspec;
68 while ((n = *pathspec++)) {
69 size_t i, len = 0;
70 for (i = 0; first == n || i < max; i++) {
71 char c = n[i];
72 if (!c || c != first[i] || is_glob_special(c))
73 break;
74 if (c == '/')
75 len = i + 1;
77 if (first == n || len < max) {
78 max = len;
79 if (!max)
80 break;
83 return max;
87 * Returns a copy of the longest leading path common among all
88 * pathspecs.
90 char *common_prefix(const char **pathspec)
92 unsigned long len = common_prefix_len(pathspec);
94 return len ? xmemdupz(*pathspec, len) : NULL;
97 int fill_directory(struct dir_struct *dir, const char **pathspec)
99 size_t len;
102 * Calculate common prefix for the pathspec, and
103 * use that to optimize the directory walk
105 len = common_prefix_len(pathspec);
107 /* Read the directory and prune it */
108 read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
109 return len;
112 int within_depth(const char *name, int namelen,
113 int depth, int max_depth)
115 const char *cp = name, *cpe = name + namelen;
117 while (cp < cpe) {
118 if (*cp++ != '/')
119 continue;
120 depth++;
121 if (depth > max_depth)
122 return 0;
124 return 1;
128 * Does 'match' match the given name?
129 * A match is found if
131 * (1) the 'match' string is leading directory of 'name', or
132 * (2) the 'match' string is a wildcard and matches 'name', or
133 * (3) the 'match' string is exactly the same as 'name'.
135 * and the return value tells which case it was.
137 * It returns 0 when there is no match.
139 static int match_one(const char *match, const char *name, int namelen)
141 int matchlen;
143 /* If the match was just the prefix, we matched */
144 if (!*match)
145 return MATCHED_RECURSIVELY;
147 if (ignore_case) {
148 for (;;) {
149 unsigned char c1 = tolower(*match);
150 unsigned char c2 = tolower(*name);
151 if (c1 == '\0' || is_glob_special(c1))
152 break;
153 if (c1 != c2)
154 return 0;
155 match++;
156 name++;
157 namelen--;
159 } else {
160 for (;;) {
161 unsigned char c1 = *match;
162 unsigned char c2 = *name;
163 if (c1 == '\0' || is_glob_special(c1))
164 break;
165 if (c1 != c2)
166 return 0;
167 match++;
168 name++;
169 namelen--;
175 * If we don't match the matchstring exactly,
176 * we need to match by fnmatch
178 matchlen = strlen(match);
179 if (strncmp_icase(match, name, matchlen))
180 return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
182 if (namelen == matchlen)
183 return MATCHED_EXACTLY;
184 if (match[matchlen-1] == '/' || name[matchlen] == '/')
185 return MATCHED_RECURSIVELY;
186 return 0;
190 * Given a name and a list of pathspecs, see if the name matches
191 * any of the pathspecs. The caller is also interested in seeing
192 * all pathspec matches some names it calls this function with
193 * (otherwise the user could have mistyped the unmatched pathspec),
194 * and a mark is left in seen[] array for pathspec element that
195 * actually matched anything.
197 int match_pathspec(const char **pathspec, const char *name, int namelen,
198 int prefix, char *seen)
200 int i, retval = 0;
202 if (!pathspec)
203 return 1;
205 name += prefix;
206 namelen -= prefix;
208 for (i = 0; pathspec[i] != NULL; i++) {
209 int how;
210 const char *match = pathspec[i] + prefix;
211 if (seen && seen[i] == MATCHED_EXACTLY)
212 continue;
213 how = match_one(match, name, namelen);
214 if (how) {
215 if (retval < how)
216 retval = how;
217 if (seen && seen[i] < how)
218 seen[i] = how;
221 return retval;
225 * Does 'match' match the given name?
226 * A match is found if
228 * (1) the 'match' string is leading directory of 'name', or
229 * (2) the 'match' string is a wildcard and matches 'name', or
230 * (3) the 'match' string is exactly the same as 'name'.
232 * and the return value tells which case it was.
234 * It returns 0 when there is no match.
236 static int match_pathspec_item(const struct pathspec_item *item, int prefix,
237 const char *name, int namelen)
239 /* name/namelen has prefix cut off by caller */
240 const char *match = item->match + prefix;
241 int matchlen = item->len - prefix;
243 /* If the match was just the prefix, we matched */
244 if (!*match)
245 return MATCHED_RECURSIVELY;
247 if (matchlen <= namelen && !strncmp(match, name, matchlen)) {
248 if (matchlen == namelen)
249 return MATCHED_EXACTLY;
251 if (match[matchlen-1] == '/' || name[matchlen] == '/')
252 return MATCHED_RECURSIVELY;
255 if (item->nowildcard_len < item->len &&
256 !git_fnmatch(match, name,
257 item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,
258 item->nowildcard_len - prefix))
259 return MATCHED_FNMATCH;
261 return 0;
265 * Given a name and a list of pathspecs, see if the name matches
266 * any of the pathspecs. The caller is also interested in seeing
267 * all pathspec matches some names it calls this function with
268 * (otherwise the user could have mistyped the unmatched pathspec),
269 * and a mark is left in seen[] array for pathspec element that
270 * actually matched anything.
272 int match_pathspec_depth(const struct pathspec *ps,
273 const char *name, int namelen,
274 int prefix, char *seen)
276 int i, retval = 0;
278 if (!ps->nr) {
279 if (!ps->recursive || ps->max_depth == -1)
280 return MATCHED_RECURSIVELY;
282 if (within_depth(name, namelen, 0, ps->max_depth))
283 return MATCHED_EXACTLY;
284 else
285 return 0;
288 name += prefix;
289 namelen -= prefix;
291 for (i = ps->nr - 1; i >= 0; i--) {
292 int how;
293 if (seen && seen[i] == MATCHED_EXACTLY)
294 continue;
295 how = match_pathspec_item(ps->items+i, prefix, name, namelen);
296 if (ps->recursive && ps->max_depth != -1 &&
297 how && how != MATCHED_FNMATCH) {
298 int len = ps->items[i].len;
299 if (name[len] == '/')
300 len++;
301 if (within_depth(name+len, namelen-len, 0, ps->max_depth))
302 how = MATCHED_EXACTLY;
303 else
304 how = 0;
306 if (how) {
307 if (retval < how)
308 retval = how;
309 if (seen && seen[i] < how)
310 seen[i] = how;
313 return retval;
317 * Return the length of the "simple" part of a path match limiter.
319 static int simple_length(const char *match)
321 int len = -1;
323 for (;;) {
324 unsigned char c = *match++;
325 len++;
326 if (c == '\0' || is_glob_special(c))
327 return len;
331 static int no_wildcard(const char *string)
333 return string[simple_length(string)] == '\0';
336 void parse_exclude_pattern(const char **pattern,
337 int *patternlen,
338 int *flags,
339 int *nowildcardlen)
341 const char *p = *pattern;
342 size_t i, len;
344 *flags = 0;
345 if (*p == '!') {
346 *flags |= EXC_FLAG_NEGATIVE;
347 p++;
349 len = strlen(p);
350 if (len && p[len - 1] == '/') {
351 len--;
352 *flags |= EXC_FLAG_MUSTBEDIR;
354 for (i = 0; i < len; i++) {
355 if (p[i] == '/')
356 break;
358 if (i == len)
359 *flags |= EXC_FLAG_NODIR;
360 *nowildcardlen = simple_length(p);
362 * we should have excluded the trailing slash from 'p' too,
363 * but that's one more allocation. Instead just make sure
364 * nowildcardlen does not exceed real patternlen
366 if (*nowildcardlen > len)
367 *nowildcardlen = len;
368 if (*p == '*' && no_wildcard(p + 1))
369 *flags |= EXC_FLAG_ENDSWITH;
370 *pattern = p;
371 *patternlen = len;
374 void add_exclude(const char *string, const char *base,
375 int baselen, struct exclude_list *which)
377 struct exclude *x;
378 int patternlen;
379 int flags;
380 int nowildcardlen;
382 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
383 if (flags & EXC_FLAG_MUSTBEDIR) {
384 char *s;
385 x = xmalloc(sizeof(*x) + patternlen + 1);
386 s = (char *)(x+1);
387 memcpy(s, string, patternlen);
388 s[patternlen] = '\0';
389 x->pattern = s;
390 } else {
391 x = xmalloc(sizeof(*x));
392 x->pattern = string;
394 x->patternlen = patternlen;
395 x->nowildcardlen = nowildcardlen;
396 x->base = base;
397 x->baselen = baselen;
398 x->flags = flags;
399 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
400 which->excludes[which->nr++] = x;
403 static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
405 int pos, len;
406 unsigned long sz;
407 enum object_type type;
408 void *data;
409 struct index_state *istate = &the_index;
411 len = strlen(path);
412 pos = index_name_pos(istate, path, len);
413 if (pos < 0)
414 return NULL;
415 if (!ce_skip_worktree(istate->cache[pos]))
416 return NULL;
417 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
418 if (!data || type != OBJ_BLOB) {
419 free(data);
420 return NULL;
422 *size = xsize_t(sz);
423 return data;
426 void free_excludes(struct exclude_list *el)
428 int i;
430 for (i = 0; i < el->nr; i++)
431 free(el->excludes[i]);
432 free(el->excludes);
434 el->nr = 0;
435 el->excludes = NULL;
438 int add_excludes_from_file_to_list(const char *fname,
439 const char *base,
440 int baselen,
441 char **buf_p,
442 struct exclude_list *which,
443 int check_index)
445 struct stat st;
446 int fd, i;
447 size_t size = 0;
448 char *buf, *entry;
450 fd = open(fname, O_RDONLY);
451 if (fd < 0 || fstat(fd, &st) < 0) {
452 if (errno != ENOENT)
453 warn_on_inaccessible(fname);
454 if (0 <= fd)
455 close(fd);
456 if (!check_index ||
457 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
458 return -1;
459 if (size == 0) {
460 free(buf);
461 return 0;
463 if (buf[size-1] != '\n') {
464 buf = xrealloc(buf, size+1);
465 buf[size++] = '\n';
468 else {
469 size = xsize_t(st.st_size);
470 if (size == 0) {
471 close(fd);
472 return 0;
474 buf = xmalloc(size+1);
475 if (read_in_full(fd, buf, size) != size) {
476 free(buf);
477 close(fd);
478 return -1;
480 buf[size++] = '\n';
481 close(fd);
484 if (buf_p)
485 *buf_p = buf;
486 entry = buf;
487 for (i = 0; i < size; i++) {
488 if (buf[i] == '\n') {
489 if (entry != buf + i && entry[0] != '#') {
490 buf[i - (i && buf[i-1] == '\r')] = 0;
491 add_exclude(entry, base, baselen, which);
493 entry = buf + i + 1;
496 return 0;
499 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
501 if (add_excludes_from_file_to_list(fname, "", 0, NULL,
502 &dir->exclude_list[EXC_FILE], 0) < 0)
503 die("cannot use %s as an exclude file", fname);
506 static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
508 struct exclude_list *el;
509 struct exclude_stack *stk = NULL;
510 int current;
512 if ((!dir->exclude_per_dir) ||
513 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
514 return; /* too long a path -- ignore */
516 /* Pop the ones that are not the prefix of the path being checked. */
517 el = &dir->exclude_list[EXC_DIRS];
518 while ((stk = dir->exclude_stack) != NULL) {
519 if (stk->baselen <= baselen &&
520 !strncmp(dir->basebuf, base, stk->baselen))
521 break;
522 dir->exclude_stack = stk->prev;
523 while (stk->exclude_ix < el->nr)
524 free(el->excludes[--el->nr]);
525 free(stk->filebuf);
526 free(stk);
529 /* Read from the parent directories and push them down. */
530 current = stk ? stk->baselen : -1;
531 while (current < baselen) {
532 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
533 const char *cp;
535 if (current < 0) {
536 cp = base;
537 current = 0;
539 else {
540 cp = strchr(base + current + 1, '/');
541 if (!cp)
542 die("oops in prep_exclude");
543 cp++;
545 stk->prev = dir->exclude_stack;
546 stk->baselen = cp - base;
547 stk->exclude_ix = el->nr;
548 memcpy(dir->basebuf + current, base + current,
549 stk->baselen - current);
550 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
551 add_excludes_from_file_to_list(dir->basebuf,
552 dir->basebuf, stk->baselen,
553 &stk->filebuf, el, 1);
554 dir->exclude_stack = stk;
555 current = stk->baselen;
557 dir->basebuf[baselen] = '\0';
560 int match_basename(const char *basename, int basenamelen,
561 const char *pattern, int prefix, int patternlen,
562 int flags)
564 if (prefix == patternlen) {
565 if (!strcmp_icase(pattern, basename))
566 return 1;
567 } else if (flags & EXC_FLAG_ENDSWITH) {
568 if (patternlen - 1 <= basenamelen &&
569 !strcmp_icase(pattern + 1,
570 basename + basenamelen - patternlen + 1))
571 return 1;
572 } else {
573 if (fnmatch_icase(pattern, basename, 0) == 0)
574 return 1;
576 return 0;
579 int match_pathname(const char *pathname, int pathlen,
580 const char *base, int baselen,
581 const char *pattern, int prefix, int patternlen,
582 int flags)
584 const char *name;
585 int namelen;
588 * match with FNM_PATHNAME; the pattern has base implicitly
589 * in front of it.
591 if (*pattern == '/') {
592 pattern++;
593 prefix--;
597 * baselen does not count the trailing slash. base[] may or
598 * may not end with a trailing slash though.
600 if (pathlen < baselen + 1 ||
601 (baselen && pathname[baselen] != '/') ||
602 strncmp_icase(pathname, base, baselen))
603 return 0;
605 namelen = baselen ? pathlen - baselen - 1 : pathlen;
606 name = pathname + pathlen - namelen;
608 if (prefix) {
610 * if the non-wildcard part is longer than the
611 * remaining pathname, surely it cannot match.
613 if (prefix > namelen)
614 return 0;
616 if (strncmp_icase(pattern, name, prefix))
617 return 0;
618 pattern += prefix;
619 name += prefix;
620 namelen -= prefix;
623 return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
626 /* Scan the list and let the last match determine the fate.
627 * Return 1 for exclude, 0 for include and -1 for undecided.
629 int excluded_from_list(const char *pathname,
630 int pathlen, const char *basename, int *dtype,
631 struct exclude_list *el)
633 int i;
635 if (!el->nr)
636 return -1; /* undefined */
638 for (i = el->nr - 1; 0 <= i; i--) {
639 struct exclude *x = el->excludes[i];
640 const char *exclude = x->pattern;
641 int to_exclude = x->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
642 int prefix = x->nowildcardlen;
644 if (x->flags & EXC_FLAG_MUSTBEDIR) {
645 if (*dtype == DT_UNKNOWN)
646 *dtype = get_dtype(NULL, pathname, pathlen);
647 if (*dtype != DT_DIR)
648 continue;
651 if (x->flags & EXC_FLAG_NODIR) {
652 if (match_basename(basename,
653 pathlen - (basename - pathname),
654 exclude, prefix, x->patternlen,
655 x->flags))
656 return to_exclude;
657 continue;
660 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
661 if (match_pathname(pathname, pathlen,
662 x->base, x->baselen ? x->baselen - 1 : 0,
663 exclude, prefix, x->patternlen, x->flags))
664 return to_exclude;
666 return -1; /* undecided */
669 static int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
671 int pathlen = strlen(pathname);
672 int st;
673 const char *basename = strrchr(pathname, '/');
674 basename = (basename) ? basename+1 : pathname;
676 prep_exclude(dir, pathname, basename-pathname);
677 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
678 switch (excluded_from_list(pathname, pathlen, basename,
679 dtype_p, &dir->exclude_list[st])) {
680 case 0:
681 return 0;
682 case 1:
683 return 1;
686 return 0;
689 void path_exclude_check_init(struct path_exclude_check *check,
690 struct dir_struct *dir)
692 check->dir = dir;
693 strbuf_init(&check->path, 256);
696 void path_exclude_check_clear(struct path_exclude_check *check)
698 strbuf_release(&check->path);
702 * Is this name excluded? This is for a caller like show_files() that
703 * do not honor directory hierarchy and iterate through paths that are
704 * possibly in an ignored directory.
706 * A path to a directory known to be excluded is left in check->path to
707 * optimize for repeated checks for files in the same excluded directory.
709 int path_excluded(struct path_exclude_check *check,
710 const char *name, int namelen, int *dtype)
712 int i;
713 struct strbuf *path = &check->path;
716 * we allow the caller to pass namelen as an optimization; it
717 * must match the length of the name, as we eventually call
718 * excluded() on the whole name string.
720 if (namelen < 0)
721 namelen = strlen(name);
723 if (path->len &&
724 path->len <= namelen &&
725 !memcmp(name, path->buf, path->len) &&
726 (!name[path->len] || name[path->len] == '/'))
727 return 1;
729 strbuf_setlen(path, 0);
730 for (i = 0; name[i]; i++) {
731 int ch = name[i];
733 if (ch == '/') {
734 int dt = DT_DIR;
735 if (excluded(check->dir, path->buf, &dt))
736 return 1;
738 strbuf_addch(path, ch);
741 /* An entry in the index; cannot be a directory with subentries */
742 strbuf_setlen(path, 0);
744 return excluded(check->dir, name, dtype);
747 static struct dir_entry *dir_entry_new(const char *pathname, int len)
749 struct dir_entry *ent;
751 ent = xmalloc(sizeof(*ent) + len + 1);
752 ent->len = len;
753 memcpy(ent->name, pathname, len);
754 ent->name[len] = 0;
755 return ent;
758 static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
760 if (cache_name_exists(pathname, len, ignore_case))
761 return NULL;
763 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
764 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
767 struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
769 if (!cache_name_is_other(pathname, len))
770 return NULL;
772 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
773 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
776 enum exist_status {
777 index_nonexistent = 0,
778 index_directory,
779 index_gitdir
783 * Do not use the alphabetically stored index to look up
784 * the directory name; instead, use the case insensitive
785 * name hash.
787 static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
789 struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
790 unsigned char endchar;
792 if (!ce)
793 return index_nonexistent;
794 endchar = ce->name[len];
797 * The cache_entry structure returned will contain this dirname
798 * and possibly additional path components.
800 if (endchar == '/')
801 return index_directory;
804 * If there are no additional path components, then this cache_entry
805 * represents a submodule. Submodules, despite being directories,
806 * are stored in the cache without a closing slash.
808 if (!endchar && S_ISGITLINK(ce->ce_mode))
809 return index_gitdir;
811 /* This should never be hit, but it exists just in case. */
812 return index_nonexistent;
816 * The index sorts alphabetically by entry name, which
817 * means that a gitlink sorts as '\0' at the end, while
818 * a directory (which is defined not as an entry, but as
819 * the files it contains) will sort with the '/' at the
820 * end.
822 static enum exist_status directory_exists_in_index(const char *dirname, int len)
824 int pos;
826 if (ignore_case)
827 return directory_exists_in_index_icase(dirname, len);
829 pos = cache_name_pos(dirname, len);
830 if (pos < 0)
831 pos = -pos-1;
832 while (pos < active_nr) {
833 struct cache_entry *ce = active_cache[pos++];
834 unsigned char endchar;
836 if (strncmp(ce->name, dirname, len))
837 break;
838 endchar = ce->name[len];
839 if (endchar > '/')
840 break;
841 if (endchar == '/')
842 return index_directory;
843 if (!endchar && S_ISGITLINK(ce->ce_mode))
844 return index_gitdir;
846 return index_nonexistent;
850 * When we find a directory when traversing the filesystem, we
851 * have three distinct cases:
853 * - ignore it
854 * - see it as a directory
855 * - recurse into it
857 * and which one we choose depends on a combination of existing
858 * git index contents and the flags passed into the directory
859 * traversal routine.
861 * Case 1: If we *already* have entries in the index under that
862 * directory name, we always recurse into the directory to see
863 * all the files.
865 * Case 2: If we *already* have that directory name as a gitlink,
866 * we always continue to see it as a gitlink, regardless of whether
867 * there is an actual git directory there or not (it might not
868 * be checked out as a subproject!)
870 * Case 3: if we didn't have it in the index previously, we
871 * have a few sub-cases:
873 * (a) if "show_other_directories" is true, we show it as
874 * just a directory, unless "hide_empty_directories" is
875 * also true and the directory is empty, in which case
876 * we just ignore it entirely.
877 * (b) if it looks like a git directory, and we don't have
878 * 'no_gitlinks' set we treat it as a gitlink, and show it
879 * as a directory.
880 * (c) otherwise, we recurse into it.
882 enum directory_treatment {
883 show_directory,
884 ignore_directory,
885 recurse_into_directory
888 static enum directory_treatment treat_directory(struct dir_struct *dir,
889 const char *dirname, int len,
890 const struct path_simplify *simplify)
892 /* The "len-1" is to strip the final '/' */
893 switch (directory_exists_in_index(dirname, len-1)) {
894 case index_directory:
895 return recurse_into_directory;
897 case index_gitdir:
898 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
899 return ignore_directory;
900 return show_directory;
902 case index_nonexistent:
903 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
904 break;
905 if (!(dir->flags & DIR_NO_GITLINKS)) {
906 unsigned char sha1[20];
907 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
908 return show_directory;
910 return recurse_into_directory;
913 /* This is the "show_other_directories" case */
914 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
915 return show_directory;
916 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
917 return ignore_directory;
918 return show_directory;
922 * This is an inexact early pruning of any recursive directory
923 * reading - if the path cannot possibly be in the pathspec,
924 * return true, and we'll skip it early.
926 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
928 if (simplify) {
929 for (;;) {
930 const char *match = simplify->path;
931 int len = simplify->len;
933 if (!match)
934 break;
935 if (len > pathlen)
936 len = pathlen;
937 if (!memcmp(path, match, len))
938 return 0;
939 simplify++;
941 return 1;
943 return 0;
947 * This function tells us whether an excluded path matches a
948 * list of "interesting" pathspecs. That is, whether a path matched
949 * by any of the pathspecs could possibly be ignored by excluding
950 * the specified path. This can happen if:
952 * 1. the path is mentioned explicitly in the pathspec
954 * 2. the path is a directory prefix of some element in the
955 * pathspec
957 static int exclude_matches_pathspec(const char *path, int len,
958 const struct path_simplify *simplify)
960 if (simplify) {
961 for (; simplify->path; simplify++) {
962 if (len == simplify->len
963 && !memcmp(path, simplify->path, len))
964 return 1;
965 if (len < simplify->len
966 && simplify->path[len] == '/'
967 && !memcmp(path, simplify->path, len))
968 return 1;
971 return 0;
974 static int get_index_dtype(const char *path, int len)
976 int pos;
977 struct cache_entry *ce;
979 ce = cache_name_exists(path, len, 0);
980 if (ce) {
981 if (!ce_uptodate(ce))
982 return DT_UNKNOWN;
983 if (S_ISGITLINK(ce->ce_mode))
984 return DT_DIR;
986 * Nobody actually cares about the
987 * difference between DT_LNK and DT_REG
989 return DT_REG;
992 /* Try to look it up as a directory */
993 pos = cache_name_pos(path, len);
994 if (pos >= 0)
995 return DT_UNKNOWN;
996 pos = -pos-1;
997 while (pos < active_nr) {
998 ce = active_cache[pos++];
999 if (strncmp(ce->name, path, len))
1000 break;
1001 if (ce->name[len] > '/')
1002 break;
1003 if (ce->name[len] < '/')
1004 continue;
1005 if (!ce_uptodate(ce))
1006 break; /* continue? */
1007 return DT_DIR;
1009 return DT_UNKNOWN;
1012 static int get_dtype(struct dirent *de, const char *path, int len)
1014 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
1015 struct stat st;
1017 if (dtype != DT_UNKNOWN)
1018 return dtype;
1019 dtype = get_index_dtype(path, len);
1020 if (dtype != DT_UNKNOWN)
1021 return dtype;
1022 if (lstat(path, &st))
1023 return dtype;
1024 if (S_ISREG(st.st_mode))
1025 return DT_REG;
1026 if (S_ISDIR(st.st_mode))
1027 return DT_DIR;
1028 if (S_ISLNK(st.st_mode))
1029 return DT_LNK;
1030 return dtype;
1033 enum path_treatment {
1034 path_ignored,
1035 path_handled,
1036 path_recurse
1039 static enum path_treatment treat_one_path(struct dir_struct *dir,
1040 struct strbuf *path,
1041 const struct path_simplify *simplify,
1042 int dtype, struct dirent *de)
1044 int exclude = excluded(dir, path->buf, &dtype);
1045 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
1046 && exclude_matches_pathspec(path->buf, path->len, simplify))
1047 dir_add_ignored(dir, path->buf, path->len);
1050 * Excluded? If we don't explicitly want to show
1051 * ignored files, ignore it
1053 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
1054 return path_ignored;
1056 if (dtype == DT_UNKNOWN)
1057 dtype = get_dtype(de, path->buf, path->len);
1060 * Do we want to see just the ignored files?
1061 * We still need to recurse into directories,
1062 * even if we don't ignore them, since the
1063 * directory may contain files that we do..
1065 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
1066 if (dtype != DT_DIR)
1067 return path_ignored;
1070 switch (dtype) {
1071 default:
1072 return path_ignored;
1073 case DT_DIR:
1074 strbuf_addch(path, '/');
1075 switch (treat_directory(dir, path->buf, path->len, simplify)) {
1076 case show_directory:
1077 if (exclude != !!(dir->flags
1078 & DIR_SHOW_IGNORED))
1079 return path_ignored;
1080 break;
1081 case recurse_into_directory:
1082 return path_recurse;
1083 case ignore_directory:
1084 return path_ignored;
1086 break;
1087 case DT_REG:
1088 case DT_LNK:
1089 break;
1091 return path_handled;
1094 static enum path_treatment treat_path(struct dir_struct *dir,
1095 struct dirent *de,
1096 struct strbuf *path,
1097 int baselen,
1098 const struct path_simplify *simplify)
1100 int dtype;
1102 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1103 return path_ignored;
1104 strbuf_setlen(path, baselen);
1105 strbuf_addstr(path, de->d_name);
1106 if (simplify_away(path->buf, path->len, simplify))
1107 return path_ignored;
1109 dtype = DTYPE(de);
1110 return treat_one_path(dir, path, simplify, dtype, de);
1114 * Read a directory tree. We currently ignore anything but
1115 * directories, regular files and symlinks. That's because git
1116 * doesn't handle them at all yet. Maybe that will change some
1117 * day.
1119 * Also, we ignore the name ".git" (even if it is not a directory).
1120 * That likely will not change.
1122 static int read_directory_recursive(struct dir_struct *dir,
1123 const char *base, int baselen,
1124 int check_only,
1125 const struct path_simplify *simplify)
1127 DIR *fdir;
1128 int contents = 0;
1129 struct dirent *de;
1130 struct strbuf path = STRBUF_INIT;
1132 strbuf_add(&path, base, baselen);
1134 fdir = opendir(path.len ? path.buf : ".");
1135 if (!fdir)
1136 goto out;
1138 while ((de = readdir(fdir)) != NULL) {
1139 switch (treat_path(dir, de, &path, baselen, simplify)) {
1140 case path_recurse:
1141 contents += read_directory_recursive(dir, path.buf,
1142 path.len, 0,
1143 simplify);
1144 continue;
1145 case path_ignored:
1146 continue;
1147 case path_handled:
1148 break;
1150 contents++;
1151 if (check_only)
1152 break;
1153 dir_add_name(dir, path.buf, path.len);
1155 closedir(fdir);
1156 out:
1157 strbuf_release(&path);
1159 return contents;
1162 static int cmp_name(const void *p1, const void *p2)
1164 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1165 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1167 return cache_name_compare(e1->name, e1->len,
1168 e2->name, e2->len);
1171 static struct path_simplify *create_simplify(const char **pathspec)
1173 int nr, alloc = 0;
1174 struct path_simplify *simplify = NULL;
1176 if (!pathspec)
1177 return NULL;
1179 for (nr = 0 ; ; nr++) {
1180 const char *match;
1181 if (nr >= alloc) {
1182 alloc = alloc_nr(alloc);
1183 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
1185 match = *pathspec++;
1186 if (!match)
1187 break;
1188 simplify[nr].path = match;
1189 simplify[nr].len = simple_length(match);
1191 simplify[nr].path = NULL;
1192 simplify[nr].len = 0;
1193 return simplify;
1196 static void free_simplify(struct path_simplify *simplify)
1198 free(simplify);
1201 static int treat_leading_path(struct dir_struct *dir,
1202 const char *path, int len,
1203 const struct path_simplify *simplify)
1205 struct strbuf sb = STRBUF_INIT;
1206 int baselen, rc = 0;
1207 const char *cp;
1209 while (len && path[len - 1] == '/')
1210 len--;
1211 if (!len)
1212 return 1;
1213 baselen = 0;
1214 while (1) {
1215 cp = path + baselen + !!baselen;
1216 cp = memchr(cp, '/', path + len - cp);
1217 if (!cp)
1218 baselen = len;
1219 else
1220 baselen = cp - path;
1221 strbuf_setlen(&sb, 0);
1222 strbuf_add(&sb, path, baselen);
1223 if (!is_directory(sb.buf))
1224 break;
1225 if (simplify_away(sb.buf, sb.len, simplify))
1226 break;
1227 if (treat_one_path(dir, &sb, simplify,
1228 DT_DIR, NULL) == path_ignored)
1229 break; /* do not recurse into it */
1230 if (len <= baselen) {
1231 rc = 1;
1232 break; /* finished checking */
1235 strbuf_release(&sb);
1236 return rc;
1239 int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
1241 struct path_simplify *simplify;
1243 if (has_symlink_leading_path(path, len))
1244 return dir->nr;
1246 simplify = create_simplify(pathspec);
1247 if (!len || treat_leading_path(dir, path, len, simplify))
1248 read_directory_recursive(dir, path, len, 0, simplify);
1249 free_simplify(simplify);
1250 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1251 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1252 return dir->nr;
1255 int file_exists(const char *f)
1257 struct stat sb;
1258 return lstat(f, &sb) == 0;
1262 * Given two normalized paths (a trailing slash is ok), if subdir is
1263 * outside dir, return -1. Otherwise return the offset in subdir that
1264 * can be used as relative path to dir.
1266 int dir_inside_of(const char *subdir, const char *dir)
1268 int offset = 0;
1270 assert(dir && subdir && *dir && *subdir);
1272 while (*dir && *subdir && *dir == *subdir) {
1273 dir++;
1274 subdir++;
1275 offset++;
1278 /* hel[p]/me vs hel[l]/yeah */
1279 if (*dir && *subdir)
1280 return -1;
1282 if (!*subdir)
1283 return !*dir ? offset : -1; /* same dir */
1285 /* foo/[b]ar vs foo/[] */
1286 if (is_dir_sep(dir[-1]))
1287 return is_dir_sep(subdir[-1]) ? offset : -1;
1289 /* foo[/]bar vs foo[] */
1290 return is_dir_sep(*subdir) ? offset + 1 : -1;
1293 int is_inside_dir(const char *dir)
1295 char cwd[PATH_MAX];
1296 if (!dir)
1297 return 0;
1298 if (!getcwd(cwd, sizeof(cwd)))
1299 die_errno("can't find the current directory");
1300 return dir_inside_of(cwd, dir) >= 0;
1303 int is_empty_dir(const char *path)
1305 DIR *dir = opendir(path);
1306 struct dirent *e;
1307 int ret = 1;
1309 if (!dir)
1310 return 0;
1312 while ((e = readdir(dir)) != NULL)
1313 if (!is_dot_or_dotdot(e->d_name)) {
1314 ret = 0;
1315 break;
1318 closedir(dir);
1319 return ret;
1322 static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
1324 DIR *dir;
1325 struct dirent *e;
1326 int ret = 0, original_len = path->len, len, kept_down = 0;
1327 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1328 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
1329 unsigned char submodule_head[20];
1331 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1332 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
1333 /* Do not descend and nuke a nested git work tree. */
1334 if (kept_up)
1335 *kept_up = 1;
1336 return 0;
1339 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
1340 dir = opendir(path->buf);
1341 if (!dir) {
1342 /* an empty dir could be removed even if it is unreadble */
1343 if (!keep_toplevel)
1344 return rmdir(path->buf);
1345 else
1346 return -1;
1348 if (path->buf[original_len - 1] != '/')
1349 strbuf_addch(path, '/');
1351 len = path->len;
1352 while ((e = readdir(dir)) != NULL) {
1353 struct stat st;
1354 if (is_dot_or_dotdot(e->d_name))
1355 continue;
1357 strbuf_setlen(path, len);
1358 strbuf_addstr(path, e->d_name);
1359 if (lstat(path->buf, &st))
1360 ; /* fall thru */
1361 else if (S_ISDIR(st.st_mode)) {
1362 if (!remove_dir_recurse(path, flag, &kept_down))
1363 continue; /* happy */
1364 } else if (!only_empty && !unlink(path->buf))
1365 continue; /* happy, too */
1367 /* path too long, stat fails, or non-directory still exists */
1368 ret = -1;
1369 break;
1371 closedir(dir);
1373 strbuf_setlen(path, original_len);
1374 if (!ret && !keep_toplevel && !kept_down)
1375 ret = rmdir(path->buf);
1376 else if (kept_up)
1378 * report the uplevel that it is not an error that we
1379 * did not rmdir() our directory.
1381 *kept_up = !ret;
1382 return ret;
1385 int remove_dir_recursively(struct strbuf *path, int flag)
1387 return remove_dir_recurse(path, flag, NULL);
1390 void setup_standard_excludes(struct dir_struct *dir)
1392 const char *path;
1393 char *xdg_path;
1395 dir->exclude_per_dir = ".gitignore";
1396 path = git_path("info/exclude");
1397 if (!excludes_file) {
1398 home_config_paths(NULL, &xdg_path, "ignore");
1399 excludes_file = xdg_path;
1401 if (!access_or_warn(path, R_OK))
1402 add_excludes_from_file(dir, path);
1403 if (excludes_file && !access_or_warn(excludes_file, R_OK))
1404 add_excludes_from_file(dir, excludes_file);
1407 int remove_path(const char *name)
1409 char *slash;
1411 if (unlink(name) && errno != ENOENT)
1412 return -1;
1414 slash = strrchr(name, '/');
1415 if (slash) {
1416 char *dirs = xstrdup(name);
1417 slash = dirs + (slash - name);
1418 do {
1419 *slash = '\0';
1420 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1421 free(dirs);
1423 return 0;
1426 static int pathspec_item_cmp(const void *a_, const void *b_)
1428 struct pathspec_item *a, *b;
1430 a = (struct pathspec_item *)a_;
1431 b = (struct pathspec_item *)b_;
1432 return strcmp(a->match, b->match);
1435 int init_pathspec(struct pathspec *pathspec, const char **paths)
1437 const char **p = paths;
1438 int i;
1440 memset(pathspec, 0, sizeof(*pathspec));
1441 if (!p)
1442 return 0;
1443 while (*p)
1444 p++;
1445 pathspec->raw = paths;
1446 pathspec->nr = p - paths;
1447 if (!pathspec->nr)
1448 return 0;
1450 pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1451 for (i = 0; i < pathspec->nr; i++) {
1452 struct pathspec_item *item = pathspec->items+i;
1453 const char *path = paths[i];
1455 item->match = path;
1456 item->len = strlen(path);
1457 item->nowildcard_len = simple_length(path);
1458 item->flags = 0;
1459 if (item->nowildcard_len < item->len) {
1460 pathspec->has_wildcard = 1;
1461 if (path[item->nowildcard_len] == '*' &&
1462 no_wildcard(path + item->nowildcard_len + 1))
1463 item->flags |= PATHSPEC_ONESTAR;
1467 qsort(pathspec->items, pathspec->nr,
1468 sizeof(struct pathspec_item), pathspec_item_cmp);
1470 return 0;
1473 void free_pathspec(struct pathspec *pathspec)
1475 free(pathspec->items);
1476 pathspec->items = NULL;