describe: document and test --first-parent
[git/mjg.git] / dir.c
bloba5fe0a040666a661a84d78e066119c69528cd329
1 /*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * See Documentation/technical/api-directory-listing.txt
7 * Copyright (C) Linus Torvalds, 2005-2006
8 * Junio Hamano, 2005-2006
9 */
10 #include "cache.h"
11 #include "dir.h"
12 #include "refs.h"
13 #include "wildmatch.h"
15 struct path_simplify {
16 int len;
17 const char *path;
20 static void read_directory_recursive(struct dir_struct *dir,
21 const char *path, int len,
22 int check_only,
23 const struct path_simplify *simplify,
24 int *contents);
25 static int get_dtype(struct dirent *de, const char *path, int len);
27 /* helper string functions with support for the ignore_case flag */
28 int strcmp_icase(const char *a, const char *b)
30 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
33 int strncmp_icase(const char *a, const char *b, size_t count)
35 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
38 int fnmatch_icase(const char *pattern, const char *string, int flags)
40 return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
43 inline int git_fnmatch(const char *pattern, const char *string,
44 int flags, int prefix)
46 int fnm_flags = 0;
47 if (flags & GFNM_PATHNAME)
48 fnm_flags |= FNM_PATHNAME;
49 if (prefix > 0) {
50 if (strncmp(pattern, string, prefix))
51 return FNM_NOMATCH;
52 pattern += prefix;
53 string += prefix;
55 if (flags & GFNM_ONESTAR) {
56 int pattern_len = strlen(++pattern);
57 int string_len = strlen(string);
58 return string_len < pattern_len ||
59 strcmp(pattern,
60 string + string_len - pattern_len);
62 return fnmatch(pattern, string, fnm_flags);
65 static size_t common_prefix_len(const char **pathspec)
67 const char *n, *first;
68 size_t max = 0;
69 int literal = limit_pathspec_to_literal();
71 if (!pathspec)
72 return max;
74 first = *pathspec;
75 while ((n = *pathspec++)) {
76 size_t i, len = 0;
77 for (i = 0; first == n || i < max; i++) {
78 char c = n[i];
79 if (!c || c != first[i] || (!literal && is_glob_special(c)))
80 break;
81 if (c == '/')
82 len = i + 1;
84 if (first == n || len < max) {
85 max = len;
86 if (!max)
87 break;
90 return max;
94 * Returns a copy of the longest leading path common among all
95 * pathspecs.
97 char *common_prefix(const char **pathspec)
99 unsigned long len = common_prefix_len(pathspec);
101 return len ? xmemdupz(*pathspec, len) : NULL;
104 int fill_directory(struct dir_struct *dir, const char **pathspec)
106 size_t len;
109 * Calculate common prefix for the pathspec, and
110 * use that to optimize the directory walk
112 len = common_prefix_len(pathspec);
114 /* Read the directory and prune it */
115 read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
116 return len;
119 int within_depth(const char *name, int namelen,
120 int depth, int max_depth)
122 const char *cp = name, *cpe = name + namelen;
124 while (cp < cpe) {
125 if (*cp++ != '/')
126 continue;
127 depth++;
128 if (depth > max_depth)
129 return 0;
131 return 1;
135 * Does 'match' match the given name?
136 * A match is found if
138 * (1) the 'match' string is leading directory of 'name', or
139 * (2) the 'match' string is a wildcard and matches 'name', or
140 * (3) the 'match' string is exactly the same as 'name'.
142 * and the return value tells which case it was.
144 * It returns 0 when there is no match.
146 static int match_one(const char *match, const char *name, int namelen)
148 int matchlen;
149 int literal = limit_pathspec_to_literal();
151 /* If the match was just the prefix, we matched */
152 if (!*match)
153 return MATCHED_RECURSIVELY;
155 if (ignore_case) {
156 for (;;) {
157 unsigned char c1 = tolower(*match);
158 unsigned char c2 = tolower(*name);
159 if (c1 == '\0' || (!literal && is_glob_special(c1)))
160 break;
161 if (c1 != c2)
162 return 0;
163 match++;
164 name++;
165 namelen--;
167 } else {
168 for (;;) {
169 unsigned char c1 = *match;
170 unsigned char c2 = *name;
171 if (c1 == '\0' || (!literal && is_glob_special(c1)))
172 break;
173 if (c1 != c2)
174 return 0;
175 match++;
176 name++;
177 namelen--;
182 * If we don't match the matchstring exactly,
183 * we need to match by fnmatch
185 matchlen = strlen(match);
186 if (strncmp_icase(match, name, matchlen)) {
187 if (literal)
188 return 0;
189 return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
192 if (namelen == matchlen)
193 return MATCHED_EXACTLY;
194 if (match[matchlen-1] == '/' || name[matchlen] == '/')
195 return MATCHED_RECURSIVELY;
196 return 0;
200 * Given a name and a list of pathspecs, returns the nature of the
201 * closest (i.e. most specific) match of the name to any of the
202 * pathspecs.
204 * The caller typically calls this multiple times with the same
205 * pathspec and seen[] array but with different name/namelen
206 * (e.g. entries from the index) and is interested in seeing if and
207 * how each pathspec matches all the names it calls this function
208 * with. A mark is left in the seen[] array for each pathspec element
209 * indicating the closest type of match that element achieved, so if
210 * seen[n] remains zero after multiple invocations, that means the nth
211 * pathspec did not match any names, which could indicate that the
212 * user mistyped the nth pathspec.
214 int match_pathspec(const char **pathspec, const char *name, int namelen,
215 int prefix, char *seen)
217 int i, retval = 0;
219 if (!pathspec)
220 return 1;
222 name += prefix;
223 namelen -= prefix;
225 for (i = 0; pathspec[i] != NULL; i++) {
226 int how;
227 const char *match = pathspec[i] + prefix;
228 if (seen && seen[i] == MATCHED_EXACTLY)
229 continue;
230 how = match_one(match, name, namelen);
231 if (how) {
232 if (retval < how)
233 retval = how;
234 if (seen && seen[i] < how)
235 seen[i] = how;
238 return retval;
242 * Does 'match' match the given name?
243 * A match is found if
245 * (1) the 'match' string is leading directory of 'name', or
246 * (2) the 'match' string is a wildcard and matches 'name', or
247 * (3) the 'match' string is exactly the same as 'name'.
249 * and the return value tells which case it was.
251 * It returns 0 when there is no match.
253 static int match_pathspec_item(const struct pathspec_item *item, int prefix,
254 const char *name, int namelen)
256 /* name/namelen has prefix cut off by caller */
257 const char *match = item->match + prefix;
258 int matchlen = item->len - prefix;
260 /* If the match was just the prefix, we matched */
261 if (!*match)
262 return MATCHED_RECURSIVELY;
264 if (matchlen <= namelen && !strncmp(match, name, matchlen)) {
265 if (matchlen == namelen)
266 return MATCHED_EXACTLY;
268 if (match[matchlen-1] == '/' || name[matchlen] == '/')
269 return MATCHED_RECURSIVELY;
272 if (item->nowildcard_len < item->len &&
273 !git_fnmatch(match, name,
274 item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,
275 item->nowildcard_len - prefix))
276 return MATCHED_FNMATCH;
278 return 0;
282 * Given a name and a list of pathspecs, returns the nature of the
283 * closest (i.e. most specific) match of the name to any of the
284 * pathspecs.
286 * The caller typically calls this multiple times with the same
287 * pathspec and seen[] array but with different name/namelen
288 * (e.g. entries from the index) and is interested in seeing if and
289 * how each pathspec matches all the names it calls this function
290 * with. A mark is left in the seen[] array for each pathspec element
291 * indicating the closest type of match that element achieved, so if
292 * seen[n] remains zero after multiple invocations, that means the nth
293 * pathspec did not match any names, which could indicate that the
294 * user mistyped the nth pathspec.
296 int match_pathspec_depth(const struct pathspec *ps,
297 const char *name, int namelen,
298 int prefix, char *seen)
300 int i, retval = 0;
302 if (!ps->nr) {
303 if (!ps->recursive || ps->max_depth == -1)
304 return MATCHED_RECURSIVELY;
306 if (within_depth(name, namelen, 0, ps->max_depth))
307 return MATCHED_EXACTLY;
308 else
309 return 0;
312 name += prefix;
313 namelen -= prefix;
315 for (i = ps->nr - 1; i >= 0; i--) {
316 int how;
317 if (seen && seen[i] == MATCHED_EXACTLY)
318 continue;
319 how = match_pathspec_item(ps->items+i, prefix, name, namelen);
320 if (ps->recursive && ps->max_depth != -1 &&
321 how && how != MATCHED_FNMATCH) {
322 int len = ps->items[i].len;
323 if (name[len] == '/')
324 len++;
325 if (within_depth(name+len, namelen-len, 0, ps->max_depth))
326 how = MATCHED_EXACTLY;
327 else
328 how = 0;
330 if (how) {
331 if (retval < how)
332 retval = how;
333 if (seen && seen[i] < how)
334 seen[i] = how;
337 return retval;
341 * Return the length of the "simple" part of a path match limiter.
343 static int simple_length(const char *match)
345 int len = -1;
347 for (;;) {
348 unsigned char c = *match++;
349 len++;
350 if (c == '\0' || is_glob_special(c))
351 return len;
355 static int no_wildcard(const char *string)
357 return string[simple_length(string)] == '\0';
360 void parse_exclude_pattern(const char **pattern,
361 int *patternlen,
362 int *flags,
363 int *nowildcardlen)
365 const char *p = *pattern;
366 size_t i, len;
368 *flags = 0;
369 if (*p == '!') {
370 *flags |= EXC_FLAG_NEGATIVE;
371 p++;
373 len = strlen(p);
374 if (len && p[len - 1] == '/') {
375 len--;
376 *flags |= EXC_FLAG_MUSTBEDIR;
378 for (i = 0; i < len; i++) {
379 if (p[i] == '/')
380 break;
382 if (i == len)
383 *flags |= EXC_FLAG_NODIR;
384 *nowildcardlen = simple_length(p);
386 * we should have excluded the trailing slash from 'p' too,
387 * but that's one more allocation. Instead just make sure
388 * nowildcardlen does not exceed real patternlen
390 if (*nowildcardlen > len)
391 *nowildcardlen = len;
392 if (*p == '*' && no_wildcard(p + 1))
393 *flags |= EXC_FLAG_ENDSWITH;
394 *pattern = p;
395 *patternlen = len;
398 void add_exclude(const char *string, const char *base,
399 int baselen, struct exclude_list *el, int srcpos)
401 struct exclude *x;
402 int patternlen;
403 int flags;
404 int nowildcardlen;
406 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
407 if (flags & EXC_FLAG_MUSTBEDIR) {
408 char *s;
409 x = xmalloc(sizeof(*x) + patternlen + 1);
410 s = (char *)(x+1);
411 memcpy(s, string, patternlen);
412 s[patternlen] = '\0';
413 x->pattern = s;
414 } else {
415 x = xmalloc(sizeof(*x));
416 x->pattern = string;
418 x->patternlen = patternlen;
419 x->nowildcardlen = nowildcardlen;
420 x->base = base;
421 x->baselen = baselen;
422 x->flags = flags;
423 x->srcpos = srcpos;
424 ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
425 el->excludes[el->nr++] = x;
426 x->el = el;
429 static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
431 int pos, len;
432 unsigned long sz;
433 enum object_type type;
434 void *data;
435 struct index_state *istate = &the_index;
437 len = strlen(path);
438 pos = index_name_pos(istate, path, len);
439 if (pos < 0)
440 return NULL;
441 if (!ce_skip_worktree(istate->cache[pos]))
442 return NULL;
443 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
444 if (!data || type != OBJ_BLOB) {
445 free(data);
446 return NULL;
448 *size = xsize_t(sz);
449 return data;
453 * Frees memory within el which was allocated for exclude patterns and
454 * the file buffer. Does not free el itself.
456 void clear_exclude_list(struct exclude_list *el)
458 int i;
460 for (i = 0; i < el->nr; i++)
461 free(el->excludes[i]);
462 free(el->excludes);
463 free(el->filebuf);
465 el->nr = 0;
466 el->excludes = NULL;
467 el->filebuf = NULL;
470 int add_excludes_from_file_to_list(const char *fname,
471 const char *base,
472 int baselen,
473 struct exclude_list *el,
474 int check_index)
476 struct stat st;
477 int fd, i, lineno = 1;
478 size_t size = 0;
479 char *buf, *entry;
481 fd = open(fname, O_RDONLY);
482 if (fd < 0 || fstat(fd, &st) < 0) {
483 if (errno != ENOENT)
484 warn_on_inaccessible(fname);
485 if (0 <= fd)
486 close(fd);
487 if (!check_index ||
488 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
489 return -1;
490 if (size == 0) {
491 free(buf);
492 return 0;
494 if (buf[size-1] != '\n') {
495 buf = xrealloc(buf, size+1);
496 buf[size++] = '\n';
499 else {
500 size = xsize_t(st.st_size);
501 if (size == 0) {
502 close(fd);
503 return 0;
505 buf = xmalloc(size+1);
506 if (read_in_full(fd, buf, size) != size) {
507 free(buf);
508 close(fd);
509 return -1;
511 buf[size++] = '\n';
512 close(fd);
515 el->filebuf = buf;
516 entry = buf;
517 for (i = 0; i < size; i++) {
518 if (buf[i] == '\n') {
519 if (entry != buf + i && entry[0] != '#') {
520 buf[i - (i && buf[i-1] == '\r')] = 0;
521 add_exclude(entry, base, baselen, el, lineno);
523 lineno++;
524 entry = buf + i + 1;
527 return 0;
530 struct exclude_list *add_exclude_list(struct dir_struct *dir,
531 int group_type, const char *src)
533 struct exclude_list *el;
534 struct exclude_list_group *group;
536 group = &dir->exclude_list_group[group_type];
537 ALLOC_GROW(group->el, group->nr + 1, group->alloc);
538 el = &group->el[group->nr++];
539 memset(el, 0, sizeof(*el));
540 el->src = src;
541 return el;
545 * Used to set up core.excludesfile and .git/info/exclude lists.
547 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
549 struct exclude_list *el;
550 el = add_exclude_list(dir, EXC_FILE, fname);
551 if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0)
552 die("cannot use %s as an exclude file", fname);
556 * Loads the per-directory exclude list for the substring of base
557 * which has a char length of baselen.
559 static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
561 struct exclude_list_group *group;
562 struct exclude_list *el;
563 struct exclude_stack *stk = NULL;
564 int current;
566 if ((!dir->exclude_per_dir) ||
567 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
568 return; /* too long a path -- ignore */
570 group = &dir->exclude_list_group[EXC_DIRS];
572 /* Pop the exclude lists from the EXCL_DIRS exclude_list_group
573 * which originate from directories not in the prefix of the
574 * path being checked. */
575 while ((stk = dir->exclude_stack) != NULL) {
576 if (stk->baselen <= baselen &&
577 !strncmp(dir->basebuf, base, stk->baselen))
578 break;
579 el = &group->el[dir->exclude_stack->exclude_ix];
580 dir->exclude_stack = stk->prev;
581 free((char *)el->src); /* see strdup() below */
582 clear_exclude_list(el);
583 free(stk);
584 group->nr--;
587 /* Read from the parent directories and push them down. */
588 current = stk ? stk->baselen : -1;
589 while (current < baselen) {
590 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
591 const char *cp;
593 if (current < 0) {
594 cp = base;
595 current = 0;
597 else {
598 cp = strchr(base + current + 1, '/');
599 if (!cp)
600 die("oops in prep_exclude");
601 cp++;
603 stk->prev = dir->exclude_stack;
604 stk->baselen = cp - base;
605 memcpy(dir->basebuf + current, base + current,
606 stk->baselen - current);
607 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
609 * dir->basebuf gets reused by the traversal, but we
610 * need fname to remain unchanged to ensure the src
611 * member of each struct exclude correctly
612 * back-references its source file. Other invocations
613 * of add_exclude_list provide stable strings, so we
614 * strdup() and free() here in the caller.
616 el = add_exclude_list(dir, EXC_DIRS, strdup(dir->basebuf));
617 stk->exclude_ix = group->nr - 1;
618 add_excludes_from_file_to_list(dir->basebuf,
619 dir->basebuf, stk->baselen,
620 el, 1);
621 dir->exclude_stack = stk;
622 current = stk->baselen;
624 dir->basebuf[baselen] = '\0';
627 int match_basename(const char *basename, int basenamelen,
628 const char *pattern, int prefix, int patternlen,
629 int flags)
631 if (prefix == patternlen) {
632 if (!strcmp_icase(pattern, basename))
633 return 1;
634 } else if (flags & EXC_FLAG_ENDSWITH) {
635 if (patternlen - 1 <= basenamelen &&
636 !strcmp_icase(pattern + 1,
637 basename + basenamelen - patternlen + 1))
638 return 1;
639 } else {
640 if (fnmatch_icase(pattern, basename, 0) == 0)
641 return 1;
643 return 0;
646 int match_pathname(const char *pathname, int pathlen,
647 const char *base, int baselen,
648 const char *pattern, int prefix, int patternlen,
649 int flags)
651 const char *name;
652 int namelen;
655 * match with FNM_PATHNAME; the pattern has base implicitly
656 * in front of it.
658 if (*pattern == '/') {
659 pattern++;
660 prefix--;
664 * baselen does not count the trailing slash. base[] may or
665 * may not end with a trailing slash though.
667 if (pathlen < baselen + 1 ||
668 (baselen && pathname[baselen] != '/') ||
669 strncmp_icase(pathname, base, baselen))
670 return 0;
672 namelen = baselen ? pathlen - baselen - 1 : pathlen;
673 name = pathname + pathlen - namelen;
675 if (prefix) {
677 * if the non-wildcard part is longer than the
678 * remaining pathname, surely it cannot match.
680 if (prefix > namelen)
681 return 0;
683 if (strncmp_icase(pattern, name, prefix))
684 return 0;
685 pattern += prefix;
686 name += prefix;
687 namelen -= prefix;
690 return wildmatch(pattern, name,
691 WM_PATHNAME | (ignore_case ? WM_CASEFOLD : 0),
692 NULL) == 0;
696 * Scan the given exclude list in reverse to see whether pathname
697 * should be ignored. The first match (i.e. the last on the list), if
698 * any, determines the fate. Returns the exclude_list element which
699 * matched, or NULL for undecided.
701 static struct exclude *last_exclude_matching_from_list(const char *pathname,
702 int pathlen,
703 const char *basename,
704 int *dtype,
705 struct exclude_list *el)
707 int i;
709 if (!el->nr)
710 return NULL; /* undefined */
712 for (i = el->nr - 1; 0 <= i; i--) {
713 struct exclude *x = el->excludes[i];
714 const char *exclude = x->pattern;
715 int prefix = x->nowildcardlen;
717 if (x->flags & EXC_FLAG_MUSTBEDIR) {
718 if (*dtype == DT_UNKNOWN)
719 *dtype = get_dtype(NULL, pathname, pathlen);
720 if (*dtype != DT_DIR)
721 continue;
724 if (x->flags & EXC_FLAG_NODIR) {
725 if (match_basename(basename,
726 pathlen - (basename - pathname),
727 exclude, prefix, x->patternlen,
728 x->flags))
729 return x;
730 continue;
733 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
734 if (match_pathname(pathname, pathlen,
735 x->base, x->baselen ? x->baselen - 1 : 0,
736 exclude, prefix, x->patternlen, x->flags))
737 return x;
739 return NULL; /* undecided */
743 * Scan the list and let the last match determine the fate.
744 * Return 1 for exclude, 0 for include and -1 for undecided.
746 int is_excluded_from_list(const char *pathname,
747 int pathlen, const char *basename, int *dtype,
748 struct exclude_list *el)
750 struct exclude *exclude;
751 exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
752 if (exclude)
753 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
754 return -1; /* undecided */
758 * Loads the exclude lists for the directory containing pathname, then
759 * scans all exclude lists to determine whether pathname is excluded.
760 * Returns the exclude_list element which matched, or NULL for
761 * undecided.
763 static struct exclude *last_exclude_matching(struct dir_struct *dir,
764 const char *pathname,
765 int *dtype_p)
767 int pathlen = strlen(pathname);
768 int i, j;
769 struct exclude_list_group *group;
770 struct exclude *exclude;
771 const char *basename = strrchr(pathname, '/');
772 basename = (basename) ? basename+1 : pathname;
774 prep_exclude(dir, pathname, basename-pathname);
776 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
777 group = &dir->exclude_list_group[i];
778 for (j = group->nr - 1; j >= 0; j--) {
779 exclude = last_exclude_matching_from_list(
780 pathname, pathlen, basename, dtype_p,
781 &group->el[j]);
782 if (exclude)
783 return exclude;
786 return NULL;
790 * Loads the exclude lists for the directory containing pathname, then
791 * scans all exclude lists to determine whether pathname is excluded.
792 * Returns 1 if true, otherwise 0.
794 static int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
796 struct exclude *exclude =
797 last_exclude_matching(dir, pathname, dtype_p);
798 if (exclude)
799 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
800 return 0;
803 void path_exclude_check_init(struct path_exclude_check *check,
804 struct dir_struct *dir)
806 check->dir = dir;
807 check->exclude = NULL;
808 strbuf_init(&check->path, 256);
811 void path_exclude_check_clear(struct path_exclude_check *check)
813 strbuf_release(&check->path);
817 * For each subdirectory in name, starting with the top-most, checks
818 * to see if that subdirectory is excluded, and if so, returns the
819 * corresponding exclude structure. Otherwise, checks whether name
820 * itself (which is presumably a file) is excluded.
822 * A path to a directory known to be excluded is left in check->path to
823 * optimize for repeated checks for files in the same excluded directory.
825 struct exclude *last_exclude_matching_path(struct path_exclude_check *check,
826 const char *name, int namelen,
827 int *dtype)
829 int i;
830 struct strbuf *path = &check->path;
831 struct exclude *exclude;
834 * we allow the caller to pass namelen as an optimization; it
835 * must match the length of the name, as we eventually call
836 * is_excluded() on the whole name string.
838 if (namelen < 0)
839 namelen = strlen(name);
842 * If path is non-empty, and name is equal to path or a
843 * subdirectory of path, name should be excluded, because
844 * it's inside a directory which is already known to be
845 * excluded and was previously left in check->path.
847 if (path->len &&
848 path->len <= namelen &&
849 !memcmp(name, path->buf, path->len) &&
850 (!name[path->len] || name[path->len] == '/'))
851 return check->exclude;
853 strbuf_setlen(path, 0);
854 for (i = 0; name[i]; i++) {
855 int ch = name[i];
857 if (ch == '/') {
858 int dt = DT_DIR;
859 exclude = last_exclude_matching(check->dir,
860 path->buf, &dt);
861 if (exclude) {
862 check->exclude = exclude;
863 return exclude;
866 strbuf_addch(path, ch);
869 /* An entry in the index; cannot be a directory with subentries */
870 strbuf_setlen(path, 0);
872 return last_exclude_matching(check->dir, name, dtype);
876 * Is this name excluded? This is for a caller like show_files() that
877 * do not honor directory hierarchy and iterate through paths that are
878 * possibly in an ignored directory.
880 int is_path_excluded(struct path_exclude_check *check,
881 const char *name, int namelen, int *dtype)
883 struct exclude *exclude =
884 last_exclude_matching_path(check, name, namelen, dtype);
885 if (exclude)
886 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
887 return 0;
890 static struct dir_entry *dir_entry_new(const char *pathname, int len)
892 struct dir_entry *ent;
894 ent = xmalloc(sizeof(*ent) + len + 1);
895 ent->len = len;
896 memcpy(ent->name, pathname, len);
897 ent->name[len] = 0;
898 return ent;
901 static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
903 if (!(dir->flags & DIR_SHOW_IGNORED) &&
904 cache_name_exists(pathname, len, ignore_case))
905 return NULL;
907 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
908 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
911 struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
913 if (!cache_name_is_other(pathname, len))
914 return NULL;
916 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
917 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
920 enum exist_status {
921 index_nonexistent = 0,
922 index_directory,
923 index_gitdir
927 * Do not use the alphabetically stored index to look up
928 * the directory name; instead, use the case insensitive
929 * name hash.
931 static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
933 struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
934 unsigned char endchar;
936 if (!ce)
937 return index_nonexistent;
938 endchar = ce->name[len];
941 * The cache_entry structure returned will contain this dirname
942 * and possibly additional path components.
944 if (endchar == '/')
945 return index_directory;
948 * If there are no additional path components, then this cache_entry
949 * represents a submodule. Submodules, despite being directories,
950 * are stored in the cache without a closing slash.
952 if (!endchar && S_ISGITLINK(ce->ce_mode))
953 return index_gitdir;
955 /* This should never be hit, but it exists just in case. */
956 return index_nonexistent;
960 * The index sorts alphabetically by entry name, which
961 * means that a gitlink sorts as '\0' at the end, while
962 * a directory (which is defined not as an entry, but as
963 * the files it contains) will sort with the '/' at the
964 * end.
966 static enum exist_status directory_exists_in_index(const char *dirname, int len)
968 int pos;
970 if (ignore_case)
971 return directory_exists_in_index_icase(dirname, len);
973 pos = cache_name_pos(dirname, len);
974 if (pos < 0)
975 pos = -pos-1;
976 while (pos < active_nr) {
977 struct cache_entry *ce = active_cache[pos++];
978 unsigned char endchar;
980 if (strncmp(ce->name, dirname, len))
981 break;
982 endchar = ce->name[len];
983 if (endchar > '/')
984 break;
985 if (endchar == '/')
986 return index_directory;
987 if (!endchar && S_ISGITLINK(ce->ce_mode))
988 return index_gitdir;
990 return index_nonexistent;
994 * When we find a directory when traversing the filesystem, we
995 * have three distinct cases:
997 * - ignore it
998 * - see it as a directory
999 * - recurse into it
1001 * and which one we choose depends on a combination of existing
1002 * git index contents and the flags passed into the directory
1003 * traversal routine.
1005 * Case 1: If we *already* have entries in the index under that
1006 * directory name, we recurse into the directory to see all the files,
1007 * unless the directory is excluded and we want to show ignored
1008 * directories
1010 * Case 2: If we *already* have that directory name as a gitlink,
1011 * we always continue to see it as a gitlink, regardless of whether
1012 * there is an actual git directory there or not (it might not
1013 * be checked out as a subproject!)
1015 * Case 3: if we didn't have it in the index previously, we
1016 * have a few sub-cases:
1018 * (a) if "show_other_directories" is true, we show it as
1019 * just a directory, unless "hide_empty_directories" is
1020 * also true and the directory is empty, in which case
1021 * we just ignore it entirely.
1022 * if we are looking for ignored directories, look if it
1023 * contains only ignored files to decide if it must be shown as
1024 * ignored or not.
1025 * (b) if it looks like a git directory, and we don't have
1026 * 'no_gitlinks' set we treat it as a gitlink, and show it
1027 * as a directory.
1028 * (c) otherwise, we recurse into it.
1030 enum directory_treatment {
1031 show_directory,
1032 ignore_directory,
1033 recurse_into_directory
1036 static enum directory_treatment treat_directory(struct dir_struct *dir,
1037 const char *dirname, int len, int exclude,
1038 const struct path_simplify *simplify)
1040 int contents = 0;
1041 /* The "len-1" is to strip the final '/' */
1042 switch (directory_exists_in_index(dirname, len-1)) {
1043 case index_directory:
1044 if ((dir->flags & DIR_SHOW_OTHER_DIRECTORIES) && exclude)
1045 break;
1047 return recurse_into_directory;
1049 case index_gitdir:
1050 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1051 return ignore_directory;
1052 return show_directory;
1054 case index_nonexistent:
1055 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1056 break;
1057 if (!(dir->flags & DIR_NO_GITLINKS)) {
1058 unsigned char sha1[20];
1059 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
1060 return show_directory;
1062 return recurse_into_directory;
1065 /* This is the "show_other_directories" case */
1068 * We are looking for ignored files and our directory is not ignored,
1069 * check if it contains only ignored files
1071 if ((dir->flags & DIR_SHOW_IGNORED) && !exclude) {
1072 dir->flags &= ~DIR_SHOW_IGNORED;
1073 dir->flags |= DIR_HIDE_EMPTY_DIRECTORIES;
1074 read_directory_recursive(dir, dirname, len, 1, simplify, &contents);
1075 dir->flags &= ~DIR_HIDE_EMPTY_DIRECTORIES;
1076 dir->flags |= DIR_SHOW_IGNORED;
1078 return contents ? ignore_directory : show_directory;
1080 if (!(dir->flags & DIR_SHOW_IGNORED) &&
1081 !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1082 return show_directory;
1083 read_directory_recursive(dir, dirname, len, 1, simplify, &contents);
1084 if (!contents)
1085 return ignore_directory;
1086 return show_directory;
1090 * Decide what to do when we find a file while traversing the
1091 * filesystem. Mostly two cases:
1093 * 1. We are looking for ignored files
1094 * (a) File is ignored, include it
1095 * (b) File is in ignored path, include it
1096 * (c) File is not ignored, exclude it
1098 * 2. Other scenarios, include the file if not excluded
1100 * Return 1 for exclude, 0 for include.
1102 static int treat_file(struct dir_struct *dir, struct strbuf *path, int exclude, int *dtype)
1104 struct path_exclude_check check;
1105 int exclude_file = 0;
1107 if (exclude)
1108 exclude_file = !(dir->flags & DIR_SHOW_IGNORED);
1109 else if (dir->flags & DIR_SHOW_IGNORED) {
1110 /* Always exclude indexed files */
1111 struct cache_entry *ce = index_name_exists(&the_index,
1112 path->buf, path->len, ignore_case);
1114 if (ce)
1115 return 1;
1117 path_exclude_check_init(&check, dir);
1119 if (!is_path_excluded(&check, path->buf, path->len, dtype))
1120 exclude_file = 1;
1122 path_exclude_check_clear(&check);
1125 return exclude_file;
1129 * This is an inexact early pruning of any recursive directory
1130 * reading - if the path cannot possibly be in the pathspec,
1131 * return true, and we'll skip it early.
1133 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
1135 if (simplify) {
1136 for (;;) {
1137 const char *match = simplify->path;
1138 int len = simplify->len;
1140 if (!match)
1141 break;
1142 if (len > pathlen)
1143 len = pathlen;
1144 if (!memcmp(path, match, len))
1145 return 0;
1146 simplify++;
1148 return 1;
1150 return 0;
1154 * This function tells us whether an excluded path matches a
1155 * list of "interesting" pathspecs. That is, whether a path matched
1156 * by any of the pathspecs could possibly be ignored by excluding
1157 * the specified path. This can happen if:
1159 * 1. the path is mentioned explicitly in the pathspec
1161 * 2. the path is a directory prefix of some element in the
1162 * pathspec
1164 static int exclude_matches_pathspec(const char *path, int len,
1165 const struct path_simplify *simplify)
1167 if (simplify) {
1168 for (; simplify->path; simplify++) {
1169 if (len == simplify->len
1170 && !memcmp(path, simplify->path, len))
1171 return 1;
1172 if (len < simplify->len
1173 && simplify->path[len] == '/'
1174 && !memcmp(path, simplify->path, len))
1175 return 1;
1178 return 0;
1181 static int get_index_dtype(const char *path, int len)
1183 int pos;
1184 struct cache_entry *ce;
1186 ce = cache_name_exists(path, len, 0);
1187 if (ce) {
1188 if (!ce_uptodate(ce))
1189 return DT_UNKNOWN;
1190 if (S_ISGITLINK(ce->ce_mode))
1191 return DT_DIR;
1193 * Nobody actually cares about the
1194 * difference between DT_LNK and DT_REG
1196 return DT_REG;
1199 /* Try to look it up as a directory */
1200 pos = cache_name_pos(path, len);
1201 if (pos >= 0)
1202 return DT_UNKNOWN;
1203 pos = -pos-1;
1204 while (pos < active_nr) {
1205 ce = active_cache[pos++];
1206 if (strncmp(ce->name, path, len))
1207 break;
1208 if (ce->name[len] > '/')
1209 break;
1210 if (ce->name[len] < '/')
1211 continue;
1212 if (!ce_uptodate(ce))
1213 break; /* continue? */
1214 return DT_DIR;
1216 return DT_UNKNOWN;
1219 static int get_dtype(struct dirent *de, const char *path, int len)
1221 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
1222 struct stat st;
1224 if (dtype != DT_UNKNOWN)
1225 return dtype;
1226 dtype = get_index_dtype(path, len);
1227 if (dtype != DT_UNKNOWN)
1228 return dtype;
1229 if (lstat(path, &st))
1230 return dtype;
1231 if (S_ISREG(st.st_mode))
1232 return DT_REG;
1233 if (S_ISDIR(st.st_mode))
1234 return DT_DIR;
1235 if (S_ISLNK(st.st_mode))
1236 return DT_LNK;
1237 return dtype;
1240 enum path_treatment {
1241 path_ignored,
1242 path_handled,
1243 path_recurse
1246 static enum path_treatment treat_one_path(struct dir_struct *dir,
1247 struct strbuf *path,
1248 const struct path_simplify *simplify,
1249 int dtype, struct dirent *de,
1250 int exclude_shortcut_ok)
1252 int exclude;
1254 if (dtype == DT_UNKNOWN)
1255 dtype = get_dtype(de, path->buf, path->len);
1257 if (exclude_shortcut_ok &&
1258 !(dir->flags & DIR_SHOW_IGNORED) &&
1259 !(dir->flags & DIR_COLLECT_IGNORED) &&
1260 dtype != DT_DIR &&
1261 cache_name_exists(path->buf, path->len, ignore_case))
1262 return path_ignored;
1264 exclude = is_excluded(dir, path->buf, &dtype);
1266 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
1267 && exclude_matches_pathspec(path->buf, path->len, simplify))
1268 dir_add_ignored(dir, path->buf, path->len);
1271 * Excluded? If we don't explicitly want to show
1272 * ignored files, ignore it
1274 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
1275 return path_ignored;
1277 switch (dtype) {
1278 default:
1279 return path_ignored;
1280 case DT_DIR:
1281 strbuf_addch(path, '/');
1283 switch (treat_directory(dir, path->buf, path->len, exclude, simplify)) {
1284 case show_directory:
1285 break;
1286 case recurse_into_directory:
1287 return path_recurse;
1288 case ignore_directory:
1289 return path_ignored;
1291 break;
1292 case DT_REG:
1293 case DT_LNK:
1294 switch (treat_file(dir, path, exclude, &dtype)) {
1295 case 1:
1296 return path_ignored;
1297 default:
1298 break;
1301 return path_handled;
1304 static enum path_treatment treat_path(struct dir_struct *dir,
1305 struct dirent *de,
1306 struct strbuf *path,
1307 int baselen,
1308 const struct path_simplify *simplify,
1309 int exclude_shortcut_ok)
1311 int dtype;
1313 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1314 return path_ignored;
1315 strbuf_setlen(path, baselen);
1316 strbuf_addstr(path, de->d_name);
1317 if (simplify_away(path->buf, path->len, simplify))
1318 return path_ignored;
1320 dtype = DTYPE(de);
1321 return treat_one_path(dir, path, simplify, dtype, de, exclude_shortcut_ok);
1325 * Read a directory tree. We currently ignore anything but
1326 * directories, regular files and symlinks. That's because git
1327 * doesn't handle them at all yet. Maybe that will change some
1328 * day.
1330 * Also, we ignore the name ".git" (even if it is not a directory).
1331 * That likely will not change.
1333 static void read_directory_recursive(struct dir_struct *dir,
1334 const char *base, int baselen,
1335 int check_only,
1336 const struct path_simplify *simplify,
1337 int *contents)
1339 DIR *fdir;
1340 struct dirent *de;
1341 struct strbuf path = STRBUF_INIT;
1343 strbuf_add(&path, base, baselen);
1345 fdir = opendir(path.len ? path.buf : ".");
1346 if (!fdir)
1347 goto out;
1349 while ((de = readdir(fdir)) != NULL) {
1350 switch (treat_path(dir, de, &path, baselen,
1351 simplify,
1352 !check_only && !contents)) {
1353 case path_recurse:
1354 read_directory_recursive(dir, path.buf,
1355 path.len, 0,
1356 simplify,
1357 contents);
1358 continue;
1359 case path_ignored:
1360 continue;
1361 case path_handled:
1362 break;
1365 * Update the last argument to treat_path if anything
1366 * else is done after this point. This is because if
1367 * treat_path's exclude_shortcut_ok is true, it may
1368 * incorrectly return path_ignored (and never reaches
1369 * this part) instead of path_handled.
1371 if (contents)
1372 (*contents)++;
1373 if (check_only)
1374 break;
1375 dir_add_name(dir, path.buf, path.len);
1377 closedir(fdir);
1378 out:
1379 strbuf_release(&path);
1382 static int cmp_name(const void *p1, const void *p2)
1384 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1385 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1387 return cache_name_compare(e1->name, e1->len,
1388 e2->name, e2->len);
1391 static struct path_simplify *create_simplify(const char **pathspec)
1393 int nr, alloc = 0;
1394 struct path_simplify *simplify = NULL;
1396 if (!pathspec)
1397 return NULL;
1399 for (nr = 0 ; ; nr++) {
1400 const char *match;
1401 if (nr >= alloc) {
1402 alloc = alloc_nr(alloc);
1403 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
1405 match = *pathspec++;
1406 if (!match)
1407 break;
1408 simplify[nr].path = match;
1409 simplify[nr].len = simple_length(match);
1411 simplify[nr].path = NULL;
1412 simplify[nr].len = 0;
1413 return simplify;
1416 static void free_simplify(struct path_simplify *simplify)
1418 free(simplify);
1421 static int treat_leading_path(struct dir_struct *dir,
1422 const char *path, int len,
1423 const struct path_simplify *simplify)
1425 struct strbuf sb = STRBUF_INIT;
1426 int baselen, rc = 0;
1427 const char *cp;
1429 while (len && path[len - 1] == '/')
1430 len--;
1431 if (!len)
1432 return 1;
1433 baselen = 0;
1434 while (1) {
1435 cp = path + baselen + !!baselen;
1436 cp = memchr(cp, '/', path + len - cp);
1437 if (!cp)
1438 baselen = len;
1439 else
1440 baselen = cp - path;
1441 strbuf_setlen(&sb, 0);
1442 strbuf_add(&sb, path, baselen);
1443 if (!is_directory(sb.buf))
1444 break;
1445 if (simplify_away(sb.buf, sb.len, simplify))
1446 break;
1447 if (treat_one_path(dir, &sb, simplify,
1448 DT_DIR, NULL, 0) == path_ignored)
1449 break; /* do not recurse into it */
1450 if (len <= baselen) {
1451 rc = 1;
1452 break; /* finished checking */
1455 strbuf_release(&sb);
1456 return rc;
1459 int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
1461 struct path_simplify *simplify;
1463 if (has_symlink_leading_path(path, len))
1464 return dir->nr;
1466 simplify = create_simplify(pathspec);
1467 if (!len || treat_leading_path(dir, path, len, simplify))
1468 read_directory_recursive(dir, path, len, 0, simplify, NULL);
1469 free_simplify(simplify);
1470 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1471 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1472 return dir->nr;
1475 int file_exists(const char *f)
1477 struct stat sb;
1478 return lstat(f, &sb) == 0;
1482 * Given two normalized paths (a trailing slash is ok), if subdir is
1483 * outside dir, return -1. Otherwise return the offset in subdir that
1484 * can be used as relative path to dir.
1486 int dir_inside_of(const char *subdir, const char *dir)
1488 int offset = 0;
1490 assert(dir && subdir && *dir && *subdir);
1492 while (*dir && *subdir && *dir == *subdir) {
1493 dir++;
1494 subdir++;
1495 offset++;
1498 /* hel[p]/me vs hel[l]/yeah */
1499 if (*dir && *subdir)
1500 return -1;
1502 if (!*subdir)
1503 return !*dir ? offset : -1; /* same dir */
1505 /* foo/[b]ar vs foo/[] */
1506 if (is_dir_sep(dir[-1]))
1507 return is_dir_sep(subdir[-1]) ? offset : -1;
1509 /* foo[/]bar vs foo[] */
1510 return is_dir_sep(*subdir) ? offset + 1 : -1;
1513 int is_inside_dir(const char *dir)
1515 char cwd[PATH_MAX];
1516 if (!dir)
1517 return 0;
1518 if (!getcwd(cwd, sizeof(cwd)))
1519 die_errno("can't find the current directory");
1520 return dir_inside_of(cwd, dir) >= 0;
1523 int is_empty_dir(const char *path)
1525 DIR *dir = opendir(path);
1526 struct dirent *e;
1527 int ret = 1;
1529 if (!dir)
1530 return 0;
1532 while ((e = readdir(dir)) != NULL)
1533 if (!is_dot_or_dotdot(e->d_name)) {
1534 ret = 0;
1535 break;
1538 closedir(dir);
1539 return ret;
1542 static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
1544 DIR *dir;
1545 struct dirent *e;
1546 int ret = 0, original_len = path->len, len, kept_down = 0;
1547 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1548 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
1549 unsigned char submodule_head[20];
1551 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1552 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
1553 /* Do not descend and nuke a nested git work tree. */
1554 if (kept_up)
1555 *kept_up = 1;
1556 return 0;
1559 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
1560 dir = opendir(path->buf);
1561 if (!dir) {
1562 /* an empty dir could be removed even if it is unreadble */
1563 if (!keep_toplevel)
1564 return rmdir(path->buf);
1565 else
1566 return -1;
1568 if (path->buf[original_len - 1] != '/')
1569 strbuf_addch(path, '/');
1571 len = path->len;
1572 while ((e = readdir(dir)) != NULL) {
1573 struct stat st;
1574 if (is_dot_or_dotdot(e->d_name))
1575 continue;
1577 strbuf_setlen(path, len);
1578 strbuf_addstr(path, e->d_name);
1579 if (lstat(path->buf, &st))
1580 ; /* fall thru */
1581 else if (S_ISDIR(st.st_mode)) {
1582 if (!remove_dir_recurse(path, flag, &kept_down))
1583 continue; /* happy */
1584 } else if (!only_empty && !unlink(path->buf))
1585 continue; /* happy, too */
1587 /* path too long, stat fails, or non-directory still exists */
1588 ret = -1;
1589 break;
1591 closedir(dir);
1593 strbuf_setlen(path, original_len);
1594 if (!ret && !keep_toplevel && !kept_down)
1595 ret = rmdir(path->buf);
1596 else if (kept_up)
1598 * report the uplevel that it is not an error that we
1599 * did not rmdir() our directory.
1601 *kept_up = !ret;
1602 return ret;
1605 int remove_dir_recursively(struct strbuf *path, int flag)
1607 return remove_dir_recurse(path, flag, NULL);
1610 void setup_standard_excludes(struct dir_struct *dir)
1612 const char *path;
1613 char *xdg_path;
1615 dir->exclude_per_dir = ".gitignore";
1616 path = git_path("info/exclude");
1617 if (!excludes_file) {
1618 home_config_paths(NULL, &xdg_path, "ignore");
1619 excludes_file = xdg_path;
1621 if (!access_or_warn(path, R_OK))
1622 add_excludes_from_file(dir, path);
1623 if (excludes_file && !access_or_warn(excludes_file, R_OK))
1624 add_excludes_from_file(dir, excludes_file);
1627 int remove_path(const char *name)
1629 char *slash;
1631 if (unlink(name) && errno != ENOENT)
1632 return -1;
1634 slash = strrchr(name, '/');
1635 if (slash) {
1636 char *dirs = xstrdup(name);
1637 slash = dirs + (slash - name);
1638 do {
1639 *slash = '\0';
1640 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1641 free(dirs);
1643 return 0;
1646 static int pathspec_item_cmp(const void *a_, const void *b_)
1648 struct pathspec_item *a, *b;
1650 a = (struct pathspec_item *)a_;
1651 b = (struct pathspec_item *)b_;
1652 return strcmp(a->match, b->match);
1655 int init_pathspec(struct pathspec *pathspec, const char **paths)
1657 const char **p = paths;
1658 int i;
1660 memset(pathspec, 0, sizeof(*pathspec));
1661 if (!p)
1662 return 0;
1663 while (*p)
1664 p++;
1665 pathspec->raw = paths;
1666 pathspec->nr = p - paths;
1667 if (!pathspec->nr)
1668 return 0;
1670 pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1671 for (i = 0; i < pathspec->nr; i++) {
1672 struct pathspec_item *item = pathspec->items+i;
1673 const char *path = paths[i];
1675 item->match = path;
1676 item->len = strlen(path);
1677 item->flags = 0;
1678 if (limit_pathspec_to_literal()) {
1679 item->nowildcard_len = item->len;
1680 } else {
1681 item->nowildcard_len = simple_length(path);
1682 if (item->nowildcard_len < item->len) {
1683 pathspec->has_wildcard = 1;
1684 if (path[item->nowildcard_len] == '*' &&
1685 no_wildcard(path + item->nowildcard_len + 1))
1686 item->flags |= PATHSPEC_ONESTAR;
1691 qsort(pathspec->items, pathspec->nr,
1692 sizeof(struct pathspec_item), pathspec_item_cmp);
1694 return 0;
1697 void free_pathspec(struct pathspec *pathspec)
1699 free(pathspec->items);
1700 pathspec->items = NULL;
1703 int limit_pathspec_to_literal(void)
1705 static int flag = -1;
1706 if (flag < 0)
1707 flag = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
1708 return flag;
1712 * Frees memory within dir which was allocated for exclude lists and
1713 * the exclude_stack. Does not free dir itself.
1715 void clear_directory(struct dir_struct *dir)
1717 int i, j;
1718 struct exclude_list_group *group;
1719 struct exclude_list *el;
1720 struct exclude_stack *stk;
1722 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1723 group = &dir->exclude_list_group[i];
1724 for (j = 0; j < group->nr; j++) {
1725 el = &group->el[j];
1726 if (i == EXC_DIRS)
1727 free((char *)el->src);
1728 clear_exclude_list(el);
1730 free(group->el);
1733 stk = dir->exclude_stack;
1734 while (stk) {
1735 struct exclude_stack *prev = stk->prev;
1736 free(stk);
1737 stk = prev;