tree_entry_interesting(): fix depth limit with overlapping pathspecs
[git/mjg.git] / dir.c
blob5b4e2b1cb32c9a00d001d4202d8ebb09e30e7103
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 static int common_prefix(const char **pathspec)
39 const char *path, *slash, *next;
40 int prefix;
42 if (!pathspec)
43 return 0;
45 path = *pathspec;
46 slash = strrchr(path, '/');
47 if (!slash)
48 return 0;
51 * The first 'prefix' characters of 'path' are common leading
52 * path components among the pathspecs we have seen so far,
53 * including the trailing slash.
55 prefix = slash - path + 1;
56 while ((next = *++pathspec) != NULL) {
57 int len, last_matching_slash = -1;
58 for (len = 0; len < prefix && next[len] == path[len]; len++)
59 if (next[len] == '/')
60 last_matching_slash = len;
61 if (len == prefix)
62 continue;
63 if (last_matching_slash < 0)
64 return 0;
65 prefix = last_matching_slash + 1;
67 return prefix;
70 int fill_directory(struct dir_struct *dir, const char **pathspec)
72 const char *path;
73 int len;
76 * Calculate common prefix for the pathspec, and
77 * use that to optimize the directory walk
79 len = common_prefix(pathspec);
80 path = "";
82 if (len)
83 path = xmemdupz(*pathspec, len);
85 /* Read the directory and prune it */
86 read_directory(dir, path, len, pathspec);
87 return len;
90 int within_depth(const char *name, int namelen,
91 int depth, int max_depth)
93 const char *cp = name, *cpe = name + namelen;
95 while (cp < cpe) {
96 if (*cp++ != '/')
97 continue;
98 depth++;
99 if (depth > max_depth)
100 return 0;
102 return 1;
106 * Does 'match' match the given name?
107 * A match is found if
109 * (1) the 'match' string is leading directory of 'name', or
110 * (2) the 'match' string is a wildcard and matches 'name', or
111 * (3) the 'match' string is exactly the same as 'name'.
113 * and the return value tells which case it was.
115 * It returns 0 when there is no match.
117 static int match_one(const char *match, const char *name, int namelen)
119 int matchlen;
121 /* If the match was just the prefix, we matched */
122 if (!*match)
123 return MATCHED_RECURSIVELY;
125 if (ignore_case) {
126 for (;;) {
127 unsigned char c1 = tolower(*match);
128 unsigned char c2 = tolower(*name);
129 if (c1 == '\0' || is_glob_special(c1))
130 break;
131 if (c1 != c2)
132 return 0;
133 match++;
134 name++;
135 namelen--;
137 } else {
138 for (;;) {
139 unsigned char c1 = *match;
140 unsigned char c2 = *name;
141 if (c1 == '\0' || is_glob_special(c1))
142 break;
143 if (c1 != c2)
144 return 0;
145 match++;
146 name++;
147 namelen--;
153 * If we don't match the matchstring exactly,
154 * we need to match by fnmatch
156 matchlen = strlen(match);
157 if (strncmp_icase(match, name, matchlen))
158 return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
160 if (namelen == matchlen)
161 return MATCHED_EXACTLY;
162 if (match[matchlen-1] == '/' || name[matchlen] == '/')
163 return MATCHED_RECURSIVELY;
164 return 0;
168 * Given a name and a list of pathspecs, see if the name matches
169 * any of the pathspecs. The caller is also interested in seeing
170 * all pathspec matches some names it calls this function with
171 * (otherwise the user could have mistyped the unmatched pathspec),
172 * and a mark is left in seen[] array for pathspec element that
173 * actually matched anything.
175 int match_pathspec(const char **pathspec, const char *name, int namelen,
176 int prefix, char *seen)
178 int i, retval = 0;
180 if (!pathspec)
181 return 1;
183 name += prefix;
184 namelen -= prefix;
186 for (i = 0; pathspec[i] != NULL; i++) {
187 int how;
188 const char *match = pathspec[i] + prefix;
189 if (seen && seen[i] == MATCHED_EXACTLY)
190 continue;
191 how = match_one(match, name, namelen);
192 if (how) {
193 if (retval < how)
194 retval = how;
195 if (seen && seen[i] < how)
196 seen[i] = how;
199 return retval;
202 static int no_wildcard(const char *string)
204 return string[strcspn(string, "*?[{\\")] == '\0';
207 void add_exclude(const char *string, const char *base,
208 int baselen, struct exclude_list *which)
210 struct exclude *x;
211 size_t len;
212 int to_exclude = 1;
213 int flags = 0;
215 if (*string == '!') {
216 to_exclude = 0;
217 string++;
219 len = strlen(string);
220 if (len && string[len - 1] == '/') {
221 char *s;
222 x = xmalloc(sizeof(*x) + len);
223 s = (char *)(x+1);
224 memcpy(s, string, len - 1);
225 s[len - 1] = '\0';
226 string = s;
227 x->pattern = s;
228 flags = EXC_FLAG_MUSTBEDIR;
229 } else {
230 x = xmalloc(sizeof(*x));
231 x->pattern = string;
233 x->to_exclude = to_exclude;
234 x->patternlen = strlen(string);
235 x->base = base;
236 x->baselen = baselen;
237 x->flags = flags;
238 if (!strchr(string, '/'))
239 x->flags |= EXC_FLAG_NODIR;
240 if (no_wildcard(string))
241 x->flags |= EXC_FLAG_NOWILDCARD;
242 if (*string == '*' && no_wildcard(string+1))
243 x->flags |= EXC_FLAG_ENDSWITH;
244 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
245 which->excludes[which->nr++] = x;
248 static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
250 int pos, len;
251 unsigned long sz;
252 enum object_type type;
253 void *data;
254 struct index_state *istate = &the_index;
256 len = strlen(path);
257 pos = index_name_pos(istate, path, len);
258 if (pos < 0)
259 return NULL;
260 if (!ce_skip_worktree(istate->cache[pos]))
261 return NULL;
262 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
263 if (!data || type != OBJ_BLOB) {
264 free(data);
265 return NULL;
267 *size = xsize_t(sz);
268 return data;
271 void free_excludes(struct exclude_list *el)
273 int i;
275 for (i = 0; i < el->nr; i++)
276 free(el->excludes[i]);
277 free(el->excludes);
279 el->nr = 0;
280 el->excludes = NULL;
283 int add_excludes_from_file_to_list(const char *fname,
284 const char *base,
285 int baselen,
286 char **buf_p,
287 struct exclude_list *which,
288 int check_index)
290 struct stat st;
291 int fd, i;
292 size_t size = 0;
293 char *buf, *entry;
295 fd = open(fname, O_RDONLY);
296 if (fd < 0 || fstat(fd, &st) < 0) {
297 if (0 <= fd)
298 close(fd);
299 if (!check_index ||
300 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
301 return -1;
302 if (size == 0) {
303 free(buf);
304 return 0;
306 if (buf[size-1] != '\n') {
307 buf = xrealloc(buf, size+1);
308 buf[size++] = '\n';
311 else {
312 size = xsize_t(st.st_size);
313 if (size == 0) {
314 close(fd);
315 return 0;
317 buf = xmalloc(size+1);
318 if (read_in_full(fd, buf, size) != size) {
319 free(buf);
320 close(fd);
321 return -1;
323 buf[size++] = '\n';
324 close(fd);
327 if (buf_p)
328 *buf_p = buf;
329 entry = buf;
330 for (i = 0; i < size; i++) {
331 if (buf[i] == '\n') {
332 if (entry != buf + i && entry[0] != '#') {
333 buf[i - (i && buf[i-1] == '\r')] = 0;
334 add_exclude(entry, base, baselen, which);
336 entry = buf + i + 1;
339 return 0;
342 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
344 if (add_excludes_from_file_to_list(fname, "", 0, NULL,
345 &dir->exclude_list[EXC_FILE], 0) < 0)
346 die("cannot use %s as an exclude file", fname);
349 static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
351 struct exclude_list *el;
352 struct exclude_stack *stk = NULL;
353 int current;
355 if ((!dir->exclude_per_dir) ||
356 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
357 return; /* too long a path -- ignore */
359 /* Pop the ones that are not the prefix of the path being checked. */
360 el = &dir->exclude_list[EXC_DIRS];
361 while ((stk = dir->exclude_stack) != NULL) {
362 if (stk->baselen <= baselen &&
363 !strncmp(dir->basebuf, base, stk->baselen))
364 break;
365 dir->exclude_stack = stk->prev;
366 while (stk->exclude_ix < el->nr)
367 free(el->excludes[--el->nr]);
368 free(stk->filebuf);
369 free(stk);
372 /* Read from the parent directories and push them down. */
373 current = stk ? stk->baselen : -1;
374 while (current < baselen) {
375 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
376 const char *cp;
378 if (current < 0) {
379 cp = base;
380 current = 0;
382 else {
383 cp = strchr(base + current + 1, '/');
384 if (!cp)
385 die("oops in prep_exclude");
386 cp++;
388 stk->prev = dir->exclude_stack;
389 stk->baselen = cp - base;
390 stk->exclude_ix = el->nr;
391 memcpy(dir->basebuf + current, base + current,
392 stk->baselen - current);
393 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
394 add_excludes_from_file_to_list(dir->basebuf,
395 dir->basebuf, stk->baselen,
396 &stk->filebuf, el, 1);
397 dir->exclude_stack = stk;
398 current = stk->baselen;
400 dir->basebuf[baselen] = '\0';
403 /* Scan the list and let the last match determine the fate.
404 * Return 1 for exclude, 0 for include and -1 for undecided.
406 int excluded_from_list(const char *pathname,
407 int pathlen, const char *basename, int *dtype,
408 struct exclude_list *el)
410 int i;
412 if (el->nr) {
413 for (i = el->nr - 1; 0 <= i; i--) {
414 struct exclude *x = el->excludes[i];
415 const char *exclude = x->pattern;
416 int to_exclude = x->to_exclude;
418 if (x->flags & EXC_FLAG_MUSTBEDIR) {
419 if (*dtype == DT_UNKNOWN)
420 *dtype = get_dtype(NULL, pathname, pathlen);
421 if (*dtype != DT_DIR)
422 continue;
425 if (x->flags & EXC_FLAG_NODIR) {
426 /* match basename */
427 if (x->flags & EXC_FLAG_NOWILDCARD) {
428 if (!strcmp_icase(exclude, basename))
429 return to_exclude;
430 } else if (x->flags & EXC_FLAG_ENDSWITH) {
431 if (x->patternlen - 1 <= pathlen &&
432 !strcmp_icase(exclude + 1, pathname + pathlen - x->patternlen + 1))
433 return to_exclude;
434 } else {
435 if (fnmatch_icase(exclude, basename, 0) == 0)
436 return to_exclude;
439 else {
440 /* match with FNM_PATHNAME:
441 * exclude has base (baselen long) implicitly
442 * in front of it.
444 int baselen = x->baselen;
445 if (*exclude == '/')
446 exclude++;
448 if (pathlen < baselen ||
449 (baselen && pathname[baselen-1] != '/') ||
450 strncmp_icase(pathname, x->base, baselen))
451 continue;
453 if (x->flags & EXC_FLAG_NOWILDCARD) {
454 if (!strcmp_icase(exclude, pathname + baselen))
455 return to_exclude;
456 } else {
457 if (fnmatch_icase(exclude, pathname+baselen,
458 FNM_PATHNAME) == 0)
459 return to_exclude;
464 return -1; /* undecided */
467 int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
469 int pathlen = strlen(pathname);
470 int st;
471 const char *basename = strrchr(pathname, '/');
472 basename = (basename) ? basename+1 : pathname;
474 prep_exclude(dir, pathname, basename-pathname);
475 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
476 switch (excluded_from_list(pathname, pathlen, basename,
477 dtype_p, &dir->exclude_list[st])) {
478 case 0:
479 return 0;
480 case 1:
481 return 1;
484 return 0;
487 static struct dir_entry *dir_entry_new(const char *pathname, int len)
489 struct dir_entry *ent;
491 ent = xmalloc(sizeof(*ent) + len + 1);
492 ent->len = len;
493 memcpy(ent->name, pathname, len);
494 ent->name[len] = 0;
495 return ent;
498 static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
500 if (cache_name_exists(pathname, len, ignore_case))
501 return NULL;
503 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
504 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
507 struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
509 if (!cache_name_is_other(pathname, len))
510 return NULL;
512 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
513 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
516 enum exist_status {
517 index_nonexistent = 0,
518 index_directory,
519 index_gitdir
523 * Do not use the alphabetically stored index to look up
524 * the directory name; instead, use the case insensitive
525 * name hash.
527 static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
529 struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
530 unsigned char endchar;
532 if (!ce)
533 return index_nonexistent;
534 endchar = ce->name[len];
537 * The cache_entry structure returned will contain this dirname
538 * and possibly additional path components.
540 if (endchar == '/')
541 return index_directory;
544 * If there are no additional path components, then this cache_entry
545 * represents a submodule. Submodules, despite being directories,
546 * are stored in the cache without a closing slash.
548 if (!endchar && S_ISGITLINK(ce->ce_mode))
549 return index_gitdir;
551 /* This should never be hit, but it exists just in case. */
552 return index_nonexistent;
556 * The index sorts alphabetically by entry name, which
557 * means that a gitlink sorts as '\0' at the end, while
558 * a directory (which is defined not as an entry, but as
559 * the files it contains) will sort with the '/' at the
560 * end.
562 static enum exist_status directory_exists_in_index(const char *dirname, int len)
564 int pos;
566 if (ignore_case)
567 return directory_exists_in_index_icase(dirname, len);
569 pos = cache_name_pos(dirname, len);
570 if (pos < 0)
571 pos = -pos-1;
572 while (pos < active_nr) {
573 struct cache_entry *ce = active_cache[pos++];
574 unsigned char endchar;
576 if (strncmp(ce->name, dirname, len))
577 break;
578 endchar = ce->name[len];
579 if (endchar > '/')
580 break;
581 if (endchar == '/')
582 return index_directory;
583 if (!endchar && S_ISGITLINK(ce->ce_mode))
584 return index_gitdir;
586 return index_nonexistent;
590 * When we find a directory when traversing the filesystem, we
591 * have three distinct cases:
593 * - ignore it
594 * - see it as a directory
595 * - recurse into it
597 * and which one we choose depends on a combination of existing
598 * git index contents and the flags passed into the directory
599 * traversal routine.
601 * Case 1: If we *already* have entries in the index under that
602 * directory name, we always recurse into the directory to see
603 * all the files.
605 * Case 2: If we *already* have that directory name as a gitlink,
606 * we always continue to see it as a gitlink, regardless of whether
607 * there is an actual git directory there or not (it might not
608 * be checked out as a subproject!)
610 * Case 3: if we didn't have it in the index previously, we
611 * have a few sub-cases:
613 * (a) if "show_other_directories" is true, we show it as
614 * just a directory, unless "hide_empty_directories" is
615 * also true and the directory is empty, in which case
616 * we just ignore it entirely.
617 * (b) if it looks like a git directory, and we don't have
618 * 'no_gitlinks' set we treat it as a gitlink, and show it
619 * as a directory.
620 * (c) otherwise, we recurse into it.
622 enum directory_treatment {
623 show_directory,
624 ignore_directory,
625 recurse_into_directory
628 static enum directory_treatment treat_directory(struct dir_struct *dir,
629 const char *dirname, int len,
630 const struct path_simplify *simplify)
632 /* The "len-1" is to strip the final '/' */
633 switch (directory_exists_in_index(dirname, len-1)) {
634 case index_directory:
635 return recurse_into_directory;
637 case index_gitdir:
638 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
639 return ignore_directory;
640 return show_directory;
642 case index_nonexistent:
643 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
644 break;
645 if (!(dir->flags & DIR_NO_GITLINKS)) {
646 unsigned char sha1[20];
647 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
648 return show_directory;
650 return recurse_into_directory;
653 /* This is the "show_other_directories" case */
654 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
655 return show_directory;
656 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
657 return ignore_directory;
658 return show_directory;
662 * This is an inexact early pruning of any recursive directory
663 * reading - if the path cannot possibly be in the pathspec,
664 * return true, and we'll skip it early.
666 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
668 if (simplify) {
669 for (;;) {
670 const char *match = simplify->path;
671 int len = simplify->len;
673 if (!match)
674 break;
675 if (len > pathlen)
676 len = pathlen;
677 if (!memcmp(path, match, len))
678 return 0;
679 simplify++;
681 return 1;
683 return 0;
687 * This function tells us whether an excluded path matches a
688 * list of "interesting" pathspecs. That is, whether a path matched
689 * by any of the pathspecs could possibly be ignored by excluding
690 * the specified path. This can happen if:
692 * 1. the path is mentioned explicitly in the pathspec
694 * 2. the path is a directory prefix of some element in the
695 * pathspec
697 static int exclude_matches_pathspec(const char *path, int len,
698 const struct path_simplify *simplify)
700 if (simplify) {
701 for (; simplify->path; simplify++) {
702 if (len == simplify->len
703 && !memcmp(path, simplify->path, len))
704 return 1;
705 if (len < simplify->len
706 && simplify->path[len] == '/'
707 && !memcmp(path, simplify->path, len))
708 return 1;
711 return 0;
714 static int get_index_dtype(const char *path, int len)
716 int pos;
717 struct cache_entry *ce;
719 ce = cache_name_exists(path, len, 0);
720 if (ce) {
721 if (!ce_uptodate(ce))
722 return DT_UNKNOWN;
723 if (S_ISGITLINK(ce->ce_mode))
724 return DT_DIR;
726 * Nobody actually cares about the
727 * difference between DT_LNK and DT_REG
729 return DT_REG;
732 /* Try to look it up as a directory */
733 pos = cache_name_pos(path, len);
734 if (pos >= 0)
735 return DT_UNKNOWN;
736 pos = -pos-1;
737 while (pos < active_nr) {
738 ce = active_cache[pos++];
739 if (strncmp(ce->name, path, len))
740 break;
741 if (ce->name[len] > '/')
742 break;
743 if (ce->name[len] < '/')
744 continue;
745 if (!ce_uptodate(ce))
746 break; /* continue? */
747 return DT_DIR;
749 return DT_UNKNOWN;
752 static int get_dtype(struct dirent *de, const char *path, int len)
754 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
755 struct stat st;
757 if (dtype != DT_UNKNOWN)
758 return dtype;
759 dtype = get_index_dtype(path, len);
760 if (dtype != DT_UNKNOWN)
761 return dtype;
762 if (lstat(path, &st))
763 return dtype;
764 if (S_ISREG(st.st_mode))
765 return DT_REG;
766 if (S_ISDIR(st.st_mode))
767 return DT_DIR;
768 if (S_ISLNK(st.st_mode))
769 return DT_LNK;
770 return dtype;
773 enum path_treatment {
774 path_ignored,
775 path_handled,
776 path_recurse
779 static enum path_treatment treat_one_path(struct dir_struct *dir,
780 char *path, int *len,
781 const struct path_simplify *simplify,
782 int dtype, struct dirent *de)
784 int exclude = excluded(dir, path, &dtype);
785 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
786 && exclude_matches_pathspec(path, *len, simplify))
787 dir_add_ignored(dir, path, *len);
790 * Excluded? If we don't explicitly want to show
791 * ignored files, ignore it
793 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
794 return path_ignored;
796 if (dtype == DT_UNKNOWN)
797 dtype = get_dtype(de, path, *len);
800 * Do we want to see just the ignored files?
801 * We still need to recurse into directories,
802 * even if we don't ignore them, since the
803 * directory may contain files that we do..
805 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
806 if (dtype != DT_DIR)
807 return path_ignored;
810 switch (dtype) {
811 default:
812 return path_ignored;
813 case DT_DIR:
814 memcpy(path + *len, "/", 2);
815 (*len)++;
816 switch (treat_directory(dir, path, *len, simplify)) {
817 case show_directory:
818 if (exclude != !!(dir->flags
819 & DIR_SHOW_IGNORED))
820 return path_ignored;
821 break;
822 case recurse_into_directory:
823 return path_recurse;
824 case ignore_directory:
825 return path_ignored;
827 break;
828 case DT_REG:
829 case DT_LNK:
830 break;
832 return path_handled;
835 static enum path_treatment treat_path(struct dir_struct *dir,
836 struct dirent *de,
837 char *path, int path_max,
838 int baselen,
839 const struct path_simplify *simplify,
840 int *len)
842 int dtype;
844 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
845 return path_ignored;
846 *len = strlen(de->d_name);
847 /* Ignore overly long pathnames! */
848 if (*len + baselen + 8 > path_max)
849 return path_ignored;
850 memcpy(path + baselen, de->d_name, *len + 1);
851 *len += baselen;
852 if (simplify_away(path, *len, simplify))
853 return path_ignored;
855 dtype = DTYPE(de);
856 return treat_one_path(dir, path, len, simplify, dtype, de);
860 * Read a directory tree. We currently ignore anything but
861 * directories, regular files and symlinks. That's because git
862 * doesn't handle them at all yet. Maybe that will change some
863 * day.
865 * Also, we ignore the name ".git" (even if it is not a directory).
866 * That likely will not change.
868 static int read_directory_recursive(struct dir_struct *dir,
869 const char *base, int baselen,
870 int check_only,
871 const struct path_simplify *simplify)
873 DIR *fdir = opendir(*base ? base : ".");
874 int contents = 0;
876 if (fdir) {
877 struct dirent *de;
878 char path[PATH_MAX + 1];
879 memcpy(path, base, baselen);
881 while ((de = readdir(fdir)) != NULL) {
882 int len;
883 switch (treat_path(dir, de, path, sizeof(path),
884 baselen, simplify, &len)) {
885 case path_recurse:
886 contents += read_directory_recursive
887 (dir, path, len, 0, simplify);
888 continue;
889 case path_ignored:
890 continue;
891 case path_handled:
892 break;
894 contents++;
895 if (check_only)
896 goto exit_early;
897 else
898 dir_add_name(dir, path, len);
900 exit_early:
901 closedir(fdir);
904 return contents;
907 static int cmp_name(const void *p1, const void *p2)
909 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
910 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
912 return cache_name_compare(e1->name, e1->len,
913 e2->name, e2->len);
917 * Return the length of the "simple" part of a path match limiter.
919 static int simple_length(const char *match)
921 int len = -1;
923 for (;;) {
924 unsigned char c = *match++;
925 len++;
926 if (c == '\0' || is_glob_special(c))
927 return len;
931 static struct path_simplify *create_simplify(const char **pathspec)
933 int nr, alloc = 0;
934 struct path_simplify *simplify = NULL;
936 if (!pathspec)
937 return NULL;
939 for (nr = 0 ; ; nr++) {
940 const char *match;
941 if (nr >= alloc) {
942 alloc = alloc_nr(alloc);
943 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
945 match = *pathspec++;
946 if (!match)
947 break;
948 simplify[nr].path = match;
949 simplify[nr].len = simple_length(match);
951 simplify[nr].path = NULL;
952 simplify[nr].len = 0;
953 return simplify;
956 static void free_simplify(struct path_simplify *simplify)
958 free(simplify);
961 static int treat_leading_path(struct dir_struct *dir,
962 const char *path, int len,
963 const struct path_simplify *simplify)
965 char pathbuf[PATH_MAX];
966 int baselen, blen;
967 const char *cp;
969 while (len && path[len - 1] == '/')
970 len--;
971 if (!len)
972 return 1;
973 baselen = 0;
974 while (1) {
975 cp = path + baselen + !!baselen;
976 cp = memchr(cp, '/', path + len - cp);
977 if (!cp)
978 baselen = len;
979 else
980 baselen = cp - path;
981 memcpy(pathbuf, path, baselen);
982 pathbuf[baselen] = '\0';
983 if (!is_directory(pathbuf))
984 return 0;
985 if (simplify_away(pathbuf, baselen, simplify))
986 return 0;
987 blen = baselen;
988 if (treat_one_path(dir, pathbuf, &blen, simplify,
989 DT_DIR, NULL) == path_ignored)
990 return 0; /* do not recurse into it */
991 if (len <= baselen)
992 return 1; /* finished checking */
996 int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
998 struct path_simplify *simplify;
1000 if (has_symlink_leading_path(path, len))
1001 return dir->nr;
1003 simplify = create_simplify(pathspec);
1004 if (!len || treat_leading_path(dir, path, len, simplify))
1005 read_directory_recursive(dir, path, len, 0, simplify);
1006 free_simplify(simplify);
1007 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1008 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1009 return dir->nr;
1012 int file_exists(const char *f)
1014 struct stat sb;
1015 return lstat(f, &sb) == 0;
1019 * get_relative_cwd() gets the prefix of the current working directory
1020 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
1022 * As a convenience, it also returns NULL if 'dir' is already NULL. The
1023 * reason for this behaviour is that it is natural for functions returning
1024 * directory names to return NULL to say "this directory does not exist"
1025 * or "this directory is invalid". These cases are usually handled the
1026 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1027 * returns NULL for both of them.
1029 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1030 * unifies the handling of "outside work tree" with "no work tree at all".
1032 char *get_relative_cwd(char *buffer, int size, const char *dir)
1034 char *cwd = buffer;
1036 if (!dir)
1037 return NULL;
1038 if (!getcwd(buffer, size))
1039 die_errno("can't find the current directory");
1041 if (!is_absolute_path(dir))
1042 dir = make_absolute_path(dir);
1044 while (*dir && *dir == *cwd) {
1045 dir++;
1046 cwd++;
1048 if (*dir)
1049 return NULL;
1050 switch (*cwd) {
1051 case '\0':
1052 return cwd;
1053 case '/':
1054 return cwd + 1;
1055 default:
1057 * dir can end with a path separator when it's root
1058 * directory. Return proper prefix in that case.
1060 if (dir[-1] == '/')
1061 return cwd;
1062 return NULL;
1066 int is_inside_dir(const char *dir)
1068 char buffer[PATH_MAX];
1069 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1072 int is_empty_dir(const char *path)
1074 DIR *dir = opendir(path);
1075 struct dirent *e;
1076 int ret = 1;
1078 if (!dir)
1079 return 0;
1081 while ((e = readdir(dir)) != NULL)
1082 if (!is_dot_or_dotdot(e->d_name)) {
1083 ret = 0;
1084 break;
1087 closedir(dir);
1088 return ret;
1091 int remove_dir_recursively(struct strbuf *path, int flag)
1093 DIR *dir;
1094 struct dirent *e;
1095 int ret = 0, original_len = path->len, len;
1096 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1097 unsigned char submodule_head[20];
1099 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1100 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
1101 /* Do not descend and nuke a nested git work tree. */
1102 return 0;
1104 dir = opendir(path->buf);
1105 if (!dir)
1106 return -1;
1107 if (path->buf[original_len - 1] != '/')
1108 strbuf_addch(path, '/');
1110 len = path->len;
1111 while ((e = readdir(dir)) != NULL) {
1112 struct stat st;
1113 if (is_dot_or_dotdot(e->d_name))
1114 continue;
1116 strbuf_setlen(path, len);
1117 strbuf_addstr(path, e->d_name);
1118 if (lstat(path->buf, &st))
1119 ; /* fall thru */
1120 else if (S_ISDIR(st.st_mode)) {
1121 if (!remove_dir_recursively(path, only_empty))
1122 continue; /* happy */
1123 } else if (!only_empty && !unlink(path->buf))
1124 continue; /* happy, too */
1126 /* path too long, stat fails, or non-directory still exists */
1127 ret = -1;
1128 break;
1130 closedir(dir);
1132 strbuf_setlen(path, original_len);
1133 if (!ret)
1134 ret = rmdir(path->buf);
1135 return ret;
1138 void setup_standard_excludes(struct dir_struct *dir)
1140 const char *path;
1142 dir->exclude_per_dir = ".gitignore";
1143 path = git_path("info/exclude");
1144 if (!access(path, R_OK))
1145 add_excludes_from_file(dir, path);
1146 if (excludes_file && !access(excludes_file, R_OK))
1147 add_excludes_from_file(dir, excludes_file);
1150 int remove_path(const char *name)
1152 char *slash;
1154 if (unlink(name) && errno != ENOENT)
1155 return -1;
1157 slash = strrchr(name, '/');
1158 if (slash) {
1159 char *dirs = xstrdup(name);
1160 slash = dirs + (slash - name);
1161 do {
1162 *slash = '\0';
1163 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1164 free(dirs);
1166 return 0;
1169 static int pathspec_item_cmp(const void *a_, const void *b_)
1171 struct pathspec_item *a, *b;
1173 a = (struct pathspec_item *)a_;
1174 b = (struct pathspec_item *)b_;
1175 return strcmp(a->match, b->match);
1178 int init_pathspec(struct pathspec *pathspec, const char **paths)
1180 const char **p = paths;
1181 int i;
1183 memset(pathspec, 0, sizeof(*pathspec));
1184 if (!p)
1185 return 0;
1186 while (*p)
1187 p++;
1188 pathspec->raw = paths;
1189 pathspec->nr = p - paths;
1190 if (!pathspec->nr)
1191 return 0;
1193 pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1194 for (i = 0; i < pathspec->nr; i++) {
1195 struct pathspec_item *item = pathspec->items+i;
1196 const char *path = paths[i];
1198 item->match = path;
1199 item->len = strlen(path);
1202 qsort(pathspec->items, pathspec->nr,
1203 sizeof(struct pathspec_item), pathspec_item_cmp);
1205 return 0;
1208 void free_pathspec(struct pathspec *pathspec)
1210 free(pathspec->items);
1211 pathspec->items = NULL;