]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
untracked cache: invalidate dirs recursively if .gitignore changes
[thirdparty/git.git] / dir.c
CommitLineData
453ec4bd
LT
1/*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
4 *
95a68344
AS
5 * See Documentation/technical/api-directory-listing.txt
6 *
453ec4bd
LT
7 * Copyright (C) Linus Torvalds, 2005-2006
8 * Junio Hamano, 2005-2006
9 */
453ec4bd
LT
10#include "cache.h"
11#include "dir.h"
09595258 12#include "refs.h"
237ec6e4 13#include "wildmatch.h"
64acde94 14#include "pathspec.h"
453ec4bd 15
9fc42d60
LT
16struct path_simplify {
17 int len;
18 const char *path;
19};
20
defd7c7b
KB
21/*
22 * Tells read_directory_recursive how a file or directory should be treated.
23 * Values are ordered by significance, e.g. if a directory contains both
24 * excluded and untracked files, it is listed as untracked because
25 * path_untracked > path_excluded.
26 */
27enum path_treatment {
28 path_none = 0,
29 path_recurse,
30 path_excluded,
31 path_untracked
32};
33
34static enum path_treatment read_directory_recursive(struct dir_struct *dir,
0dcb8d7f 35 const char *path, int len, struct untracked_cache_dir *untracked,
09595258 36 int check_only, const struct path_simplify *simplify);
caa6b782 37static int get_dtype(struct dirent *de, const char *path, int len);
09595258 38
8cf2a84e
JJ
39/* helper string functions with support for the ignore_case flag */
40int strcmp_icase(const char *a, const char *b)
41{
42 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
43}
44
45int strncmp_icase(const char *a, const char *b, size_t count)
46{
47 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
48}
49
50int fnmatch_icase(const char *pattern, const char *string, int flags)
51{
eb07894f
NTND
52 return wildmatch(pattern, string,
53 flags | (ignore_case ? WM_CASEFOLD : 0),
54 NULL);
8cf2a84e
JJ
55}
56
1f26ce61
CB
57int git_fnmatch(const struct pathspec_item *item,
58 const char *pattern, const char *string,
59 int prefix)
5d74762d 60{
5d74762d 61 if (prefix > 0) {
93d93537 62 if (ps_strncmp(item, pattern, string, prefix))
eb07894f 63 return WM_NOMATCH;
5d74762d
NTND
64 pattern += prefix;
65 string += prefix;
66 }
bd30c2e4 67 if (item->flags & PATHSPEC_ONESTAR) {
8c6abbcd
NTND
68 int pattern_len = strlen(++pattern);
69 int string_len = strlen(string);
70 return string_len < pattern_len ||
93d93537
NTND
71 ps_strcmp(item, pattern,
72 string + string_len - pattern_len);
8c6abbcd 73 }
bd30c2e4 74 if (item->magic & PATHSPEC_GLOB)
93d93537
NTND
75 return wildmatch(pattern, string,
76 WM_PATHNAME |
77 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
78 NULL);
bd30c2e4
NTND
79 else
80 /* wildmatch has not learned no FNM_PATHNAME mode yet */
eb07894f
NTND
81 return wildmatch(pattern, string,
82 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0,
83 NULL);
5d74762d
NTND
84}
85
0b6e56df
JH
86static int fnmatch_icase_mem(const char *pattern, int patternlen,
87 const char *string, int stringlen,
88 int flags)
89{
90 int match_status;
91 struct strbuf pat_buf = STRBUF_INIT;
92 struct strbuf str_buf = STRBUF_INIT;
93 const char *use_pat = pattern;
94 const char *use_str = string;
95
96 if (pattern[patternlen]) {
97 strbuf_add(&pat_buf, pattern, patternlen);
98 use_pat = pat_buf.buf;
99 }
100 if (string[stringlen]) {
101 strbuf_add(&str_buf, string, stringlen);
102 use_str = str_buf.buf;
103 }
104
f30366b2
JH
105 if (ignore_case)
106 flags |= WM_CASEFOLD;
107 match_status = wildmatch(use_pat, use_str, flags, NULL);
0b6e56df
JH
108
109 strbuf_release(&pat_buf);
110 strbuf_release(&str_buf);
111
112 return match_status;
113}
114
827f4d6c 115static size_t common_prefix_len(const struct pathspec *pathspec)
3c6a370b 116{
827f4d6c 117 int n;
4a085b16 118 size_t max = 0;
3c6a370b 119
93d93537
NTND
120 /*
121 * ":(icase)path" is treated as a pathspec full of
122 * wildcard. In other words, only prefix is considered common
123 * prefix. If the pathspec is abc/foo abc/bar, running in
124 * subdir xyz, the common prefix is still xyz, not xuz/abc as
125 * in non-:(icase).
126 */
5c6933d2
NTND
127 GUARD_PATHSPEC(pathspec,
128 PATHSPEC_FROMTOP |
129 PATHSPEC_MAXDEPTH |
bd30c2e4 130 PATHSPEC_LITERAL |
93d93537 131 PATHSPEC_GLOB |
ef79b1f8
NTND
132 PATHSPEC_ICASE |
133 PATHSPEC_EXCLUDE);
4a085b16 134
827f4d6c 135 for (n = 0; n < pathspec->nr; n++) {
93d93537 136 size_t i = 0, len = 0, item_len;
ef79b1f8
NTND
137 if (pathspec->items[n].magic & PATHSPEC_EXCLUDE)
138 continue;
93d93537
NTND
139 if (pathspec->items[n].magic & PATHSPEC_ICASE)
140 item_len = pathspec->items[n].prefix;
141 else
142 item_len = pathspec->items[n].nowildcard_len;
143 while (i < item_len && (n == 0 || i < max)) {
827f4d6c
NTND
144 char c = pathspec->items[n].match[i];
145 if (c != pathspec->items[0].match[i])
4a085b16
JH
146 break;
147 if (c == '/')
148 len = i + 1;
827f4d6c 149 i++;
4a085b16 150 }
827f4d6c 151 if (n == 0 || len < max) {
4a085b16
JH
152 max = len;
153 if (!max)
154 break;
155 }
3c6a370b 156 }
4a085b16 157 return max;
3c6a370b
LT
158}
159
f950eb95
CB
160/*
161 * Returns a copy of the longest leading path common among all
162 * pathspecs.
163 */
827f4d6c 164char *common_prefix(const struct pathspec *pathspec)
f950eb95
CB
165{
166 unsigned long len = common_prefix_len(pathspec);
167
827f4d6c 168 return len ? xmemdupz(pathspec->items[0].match, len) : NULL;
f950eb95
CB
169}
170
7327d3d1 171int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
1d8842d9 172{
4a085b16 173 size_t len;
1d8842d9
LT
174
175 /*
176 * Calculate common prefix for the pathspec, and
177 * use that to optimize the directory walk
178 */
4a085b16 179 len = common_prefix_len(pathspec);
1d8842d9
LT
180
181 /* Read the directory and prune it */
b3920bbd 182 read_directory(dir, pathspec->nr ? pathspec->_raw[0] : "", len, pathspec);
dba2e203 183 return len;
1d8842d9
LT
184}
185
bc96cc87
NTND
186int within_depth(const char *name, int namelen,
187 int depth, int max_depth)
188{
189 const char *cp = name, *cpe = name + namelen;
190
191 while (cp < cpe) {
192 if (*cp++ != '/')
193 continue;
194 depth++;
195 if (depth > max_depth)
196 return 0;
197 }
198 return 1;
199}
200
42b0874a 201#define DO_MATCH_EXCLUDE 1
68690fdd 202#define DO_MATCH_DIRECTORY 2
42b0874a 203
61cf2820
NTND
204/*
205 * Does 'match' match the given name?
206 * A match is found if
207 *
208 * (1) the 'match' string is leading directory of 'name', or
209 * (2) the 'match' string is a wildcard and matches 'name', or
210 * (3) the 'match' string is exactly the same as 'name'.
211 *
212 * and the return value tells which case it was.
213 *
214 * It returns 0 when there is no match.
215 */
216static int match_pathspec_item(const struct pathspec_item *item, int prefix,
42b0874a 217 const char *name, int namelen, unsigned flags)
61cf2820
NTND
218{
219 /* name/namelen has prefix cut off by caller */
220 const char *match = item->match + prefix;
221 int matchlen = item->len - prefix;
222
93d93537
NTND
223 /*
224 * The normal call pattern is:
225 * 1. prefix = common_prefix_len(ps);
226 * 2. prune something, or fill_directory
854b0959 227 * 3. match_pathspec()
93d93537
NTND
228 *
229 * 'prefix' at #1 may be shorter than the command's prefix and
230 * it's ok for #2 to match extra files. Those extras will be
231 * trimmed at #3.
232 *
233 * Suppose the pathspec is 'foo' and '../bar' running from
234 * subdir 'xyz'. The common prefix at #1 will be empty, thanks
235 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
236 * user does not want XYZ/foo, only the "foo" part should be
237 * case-insensitive. We need to filter out XYZ/foo here. In
238 * other words, we do not trust the caller on comparing the
239 * prefix part when :(icase) is involved. We do exact
240 * comparison ourselves.
241 *
242 * Normally the caller (common_prefix_len() in fact) does
243 * _exact_ matching on name[-prefix+1..-1] and we do not need
244 * to check that part. Be defensive and check it anyway, in
245 * case common_prefix_len is changed, or a new caller is
246 * introduced that does not use common_prefix_len.
247 *
248 * If the penalty turns out too high when prefix is really
249 * long, maybe change it to
250 * strncmp(match, name, item->prefix - prefix)
251 */
252 if (item->prefix && (item->magic & PATHSPEC_ICASE) &&
253 strncmp(item->match, name - prefix, item->prefix))
254 return 0;
255
61cf2820
NTND
256 /* If the match was just the prefix, we matched */
257 if (!*match)
258 return MATCHED_RECURSIVELY;
259
93d93537 260 if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) {
61cf2820
NTND
261 if (matchlen == namelen)
262 return MATCHED_EXACTLY;
263
264 if (match[matchlen-1] == '/' || name[matchlen] == '/')
265 return MATCHED_RECURSIVELY;
68690fdd
NTND
266 } else if ((flags & DO_MATCH_DIRECTORY) &&
267 match[matchlen - 1] == '/' &&
268 namelen == matchlen - 1 &&
269 !ps_strncmp(item, match, name, namelen))
270 return MATCHED_EXACTLY;
61cf2820 271
5d74762d 272 if (item->nowildcard_len < item->len &&
bd30c2e4 273 !git_fnmatch(item, match, name,
8c6abbcd 274 item->nowildcard_len - prefix))
61cf2820
NTND
275 return MATCHED_FNMATCH;
276
277 return 0;
278}
279
280/*
52ed1894
AS
281 * Given a name and a list of pathspecs, returns the nature of the
282 * closest (i.e. most specific) match of the name to any of the
283 * pathspecs.
284 *
285 * The caller typically calls this multiple times with the same
286 * pathspec and seen[] array but with different name/namelen
287 * (e.g. entries from the index) and is interested in seeing if and
288 * how each pathspec matches all the names it calls this function
289 * with. A mark is left in the seen[] array for each pathspec element
290 * indicating the closest type of match that element achieved, so if
291 * seen[n] remains zero after multiple invocations, that means the nth
292 * pathspec did not match any names, which could indicate that the
293 * user mistyped the nth pathspec.
61cf2820 294 */
854b0959
NTND
295static int do_match_pathspec(const struct pathspec *ps,
296 const char *name, int namelen,
297 int prefix, char *seen,
42b0874a 298 unsigned flags)
61cf2820 299{
42b0874a 300 int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE;
61cf2820 301
5c6933d2
NTND
302 GUARD_PATHSPEC(ps,
303 PATHSPEC_FROMTOP |
304 PATHSPEC_MAXDEPTH |
bd30c2e4 305 PATHSPEC_LITERAL |
93d93537 306 PATHSPEC_GLOB |
ef79b1f8
NTND
307 PATHSPEC_ICASE |
308 PATHSPEC_EXCLUDE);
8f4f8f45 309
61cf2820 310 if (!ps->nr) {
6330a171
NTND
311 if (!ps->recursive ||
312 !(ps->magic & PATHSPEC_MAXDEPTH) ||
313 ps->max_depth == -1)
61cf2820
NTND
314 return MATCHED_RECURSIVELY;
315
316 if (within_depth(name, namelen, 0, ps->max_depth))
317 return MATCHED_EXACTLY;
318 else
319 return 0;
320 }
321
322 name += prefix;
323 namelen -= prefix;
324
325 for (i = ps->nr - 1; i >= 0; i--) {
326 int how;
ef79b1f8
NTND
327
328 if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) ||
329 ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE)))
330 continue;
331
61cf2820
NTND
332 if (seen && seen[i] == MATCHED_EXACTLY)
333 continue;
ef79b1f8
NTND
334 /*
335 * Make exclude patterns optional and never report
336 * "pathspec ':(exclude)foo' matches no files"
337 */
338 if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE)
339 seen[i] = MATCHED_FNMATCH;
42b0874a
NTND
340 how = match_pathspec_item(ps->items+i, prefix, name,
341 namelen, flags);
6330a171
NTND
342 if (ps->recursive &&
343 (ps->magic & PATHSPEC_MAXDEPTH) &&
344 ps->max_depth != -1 &&
61cf2820
NTND
345 how && how != MATCHED_FNMATCH) {
346 int len = ps->items[i].len;
347 if (name[len] == '/')
348 len++;
349 if (within_depth(name+len, namelen-len, 0, ps->max_depth))
350 how = MATCHED_EXACTLY;
351 else
352 how = 0;
353 }
354 if (how) {
355 if (retval < how)
356 retval = how;
357 if (seen && seen[i] < how)
358 seen[i] = how;
359 }
360 }
361 return retval;
362}
363
854b0959
NTND
364int match_pathspec(const struct pathspec *ps,
365 const char *name, int namelen,
ae8d0824 366 int prefix, char *seen, int is_dir)
ef79b1f8
NTND
367{
368 int positive, negative;
ae8d0824 369 unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
42b0874a
NTND
370 positive = do_match_pathspec(ps, name, namelen,
371 prefix, seen, flags);
ef79b1f8
NTND
372 if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
373 return positive;
42b0874a
NTND
374 negative = do_match_pathspec(ps, name, namelen,
375 prefix, seen,
376 flags | DO_MATCH_EXCLUDE);
ef79b1f8
NTND
377 return negative ? 0 : positive;
378}
379
fcd631ed
NTND
380/*
381 * Return the length of the "simple" part of a path match limiter.
382 */
87323bda 383int simple_length(const char *match)
fcd631ed
NTND
384{
385 int len = -1;
386
387 for (;;) {
388 unsigned char c = *match++;
389 len++;
390 if (c == '\0' || is_glob_special(c))
391 return len;
392 }
393}
394
87323bda 395int no_wildcard(const char *string)
68492fc7 396{
fcd631ed 397 return string[simple_length(string)] == '\0';
68492fc7
LK
398}
399
82dce998
NTND
400void parse_exclude_pattern(const char **pattern,
401 int *patternlen,
402 int *flags,
403 int *nowildcardlen)
84460eec
NTND
404{
405 const char *p = *pattern;
406 size_t i, len;
407
408 *flags = 0;
409 if (*p == '!') {
410 *flags |= EXC_FLAG_NEGATIVE;
411 p++;
412 }
413 len = strlen(p);
414 if (len && p[len - 1] == '/') {
415 len--;
416 *flags |= EXC_FLAG_MUSTBEDIR;
417 }
418 for (i = 0; i < len; i++) {
419 if (p[i] == '/')
420 break;
421 }
422 if (i == len)
423 *flags |= EXC_FLAG_NODIR;
424 *nowildcardlen = simple_length(p);
425 /*
426 * we should have excluded the trailing slash from 'p' too,
427 * but that's one more allocation. Instead just make sure
428 * nowildcardlen does not exceed real patternlen
429 */
430 if (*nowildcardlen > len)
431 *nowildcardlen = len;
432 if (*p == '*' && no_wildcard(p + 1))
433 *flags |= EXC_FLAG_ENDSWITH;
434 *pattern = p;
435 *patternlen = len;
436}
437
453ec4bd 438void add_exclude(const char *string, const char *base,
c04318e4 439 int baselen, struct exclude_list *el, int srcpos)
453ec4bd 440{
d6b8fc30 441 struct exclude *x;
84460eec
NTND
442 int patternlen;
443 int flags;
444 int nowildcardlen;
453ec4bd 445
84460eec
NTND
446 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
447 if (flags & EXC_FLAG_MUSTBEDIR) {
d6b8fc30 448 char *s;
84460eec 449 x = xmalloc(sizeof(*x) + patternlen + 1);
4b25d091 450 s = (char *)(x+1);
84460eec
NTND
451 memcpy(s, string, patternlen);
452 s[patternlen] = '\0';
d6b8fc30 453 x->pattern = s;
d6b8fc30
JH
454 } else {
455 x = xmalloc(sizeof(*x));
456 x->pattern = string;
457 }
84460eec
NTND
458 x->patternlen = patternlen;
459 x->nowildcardlen = nowildcardlen;
453ec4bd
LT
460 x->base = base;
461 x->baselen = baselen;
d6b8fc30 462 x->flags = flags;
c04318e4 463 x->srcpos = srcpos;
840fc334
AS
464 ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
465 el->excludes[el->nr++] = x;
c04318e4 466 x->el = el;
453ec4bd
LT
467}
468
55fe6f51
NTND
469static void *read_skip_worktree_file_from_index(const char *path, size_t *size,
470 struct sha1_stat *sha1_stat)
c28b3d6e
NTND
471{
472 int pos, len;
473 unsigned long sz;
474 enum object_type type;
475 void *data;
c28b3d6e
NTND
476
477 len = strlen(path);
71261027 478 pos = cache_name_pos(path, len);
c28b3d6e
NTND
479 if (pos < 0)
480 return NULL;
71261027 481 if (!ce_skip_worktree(active_cache[pos]))
c28b3d6e 482 return NULL;
71261027 483 data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
c28b3d6e
NTND
484 if (!data || type != OBJ_BLOB) {
485 free(data);
486 return NULL;
487 }
488 *size = xsize_t(sz);
55fe6f51
NTND
489 if (sha1_stat) {
490 memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
491 hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
492 }
c28b3d6e
NTND
493 return data;
494}
495
f6198812
AS
496/*
497 * Frees memory within el which was allocated for exclude patterns and
498 * the file buffer. Does not free el itself.
499 */
500void clear_exclude_list(struct exclude_list *el)
0fd0e241
NTND
501{
502 int i;
503
504 for (i = 0; i < el->nr; i++)
505 free(el->excludes[i]);
506 free(el->excludes);
c082df24 507 free(el->filebuf);
0fd0e241
NTND
508
509 el->nr = 0;
510 el->excludes = NULL;
c082df24 511 el->filebuf = NULL;
0fd0e241
NTND
512}
513
7e2e4b37 514static void trim_trailing_spaces(char *buf)
16402b99 515{
e61a6c1d
PB
516 char *p, *last_space = NULL;
517
518 for (p = buf; *p; p++)
519 switch (*p) {
520 case ' ':
521 if (!last_space)
522 last_space = p;
523 break;
524 case '\\':
525 p++;
526 if (!*p)
527 return;
528 /* fallthrough */
529 default:
530 last_space = NULL;
531 }
532
533 if (last_space)
534 *last_space = '\0';
16402b99
NTND
535}
536
0dcb8d7f
NTND
537/*
538 * Given a subdirectory name and "dir" of the current directory,
539 * search the subdir in "dir" and return it, or create a new one if it
540 * does not exist in "dir".
541 *
542 * If "name" has the trailing slash, it'll be excluded in the search.
543 */
544static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
545 struct untracked_cache_dir *dir,
546 const char *name, int len)
547{
548 int first, last;
549 struct untracked_cache_dir *d;
550 if (!dir)
551 return NULL;
552 if (len && name[len - 1] == '/')
553 len--;
554 first = 0;
555 last = dir->dirs_nr;
556 while (last > first) {
557 int cmp, next = (last + first) >> 1;
558 d = dir->dirs[next];
559 cmp = strncmp(name, d->name, len);
560 if (!cmp && strlen(d->name) > len)
561 cmp = -1;
562 if (!cmp)
563 return d;
564 if (cmp < 0) {
565 last = next;
566 continue;
567 }
568 first = next+1;
569 }
570
571 uc->dir_created++;
572 d = xmalloc(sizeof(*d) + len + 1);
573 memset(d, 0, sizeof(*d));
574 memcpy(d->name, name, len);
575 d->name[len] = '\0';
576
577 ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
578 memmove(dir->dirs + first + 1, dir->dirs + first,
579 (dir->dirs_nr - first) * sizeof(*dir->dirs));
580 dir->dirs_nr++;
581 dir->dirs[first] = d;
582 return d;
583}
584
ccad261f
NTND
585static void do_invalidate_gitignore(struct untracked_cache_dir *dir)
586{
587 int i;
588 dir->valid = 0;
589 dir->untracked_nr = 0;
590 for (i = 0; i < dir->dirs_nr; i++)
591 do_invalidate_gitignore(dir->dirs[i]);
592}
593
594static void invalidate_gitignore(struct untracked_cache *uc,
595 struct untracked_cache_dir *dir)
596{
597 uc->gitignore_invalidated++;
598 do_invalidate_gitignore(dir);
599}
600
55fe6f51
NTND
601/*
602 * Given a file with name "fname", read it (either from disk, or from
603 * the index if "check_index" is non-zero), parse it and store the
604 * exclude rules in "el".
605 *
606 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
607 * stat data from disk (only valid if add_excludes returns zero). If
608 * ss_valid is non-zero, "ss" must contain good value as input.
609 */
610static int add_excludes(const char *fname, const char *base, int baselen,
611 struct exclude_list *el, int check_index,
612 struct sha1_stat *sha1_stat)
453ec4bd 613{
c470701a 614 struct stat st;
c04318e4 615 int fd, i, lineno = 1;
9d14017a 616 size_t size = 0;
453ec4bd
LT
617 char *buf, *entry;
618
619 fd = open(fname, O_RDONLY);
c28b3d6e 620 if (fd < 0 || fstat(fd, &st) < 0) {
69660731 621 if (errno != ENOENT)
55b38a48 622 warn_on_inaccessible(fname);
c28b3d6e
NTND
623 if (0 <= fd)
624 close(fd);
625 if (!check_index ||
55fe6f51 626 (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
c28b3d6e 627 return -1;
45d76f17
NTND
628 if (size == 0) {
629 free(buf);
630 return 0;
631 }
632 if (buf[size-1] != '\n') {
633 buf = xrealloc(buf, size+1);
634 buf[size++] = '\n';
635 }
d961baa8 636 } else {
c28b3d6e
NTND
637 size = xsize_t(st.st_size);
638 if (size == 0) {
55fe6f51
NTND
639 if (sha1_stat) {
640 fill_stat_data(&sha1_stat->stat, &st);
641 hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
642 sha1_stat->valid = 1;
643 }
c28b3d6e
NTND
644 close(fd);
645 return 0;
646 }
45d76f17 647 buf = xmalloc(size+1);
c28b3d6e 648 if (read_in_full(fd, buf, size) != size) {
45d76f17 649 free(buf);
c28b3d6e
NTND
650 close(fd);
651 return -1;
652 }
45d76f17 653 buf[size++] = '\n';
c28b3d6e 654 close(fd);
55fe6f51
NTND
655 if (sha1_stat) {
656 int pos;
657 if (sha1_stat->valid &&
658 !match_stat_data(&sha1_stat->stat, &st))
659 ; /* no content change, ss->sha1 still good */
660 else if (check_index &&
661 (pos = cache_name_pos(fname, strlen(fname))) >= 0 &&
662 !ce_stage(active_cache[pos]) &&
663 ce_uptodate(active_cache[pos]) &&
664 !would_convert_to_git(fname))
665 hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
666 else
667 hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
668 fill_stat_data(&sha1_stat->stat, &st);
669 sha1_stat->valid = 1;
670 }
6ba78238 671 }
453ec4bd 672
c082df24 673 el->filebuf = buf;
453ec4bd 674 entry = buf;
45d76f17
NTND
675 for (i = 0; i < size; i++) {
676 if (buf[i] == '\n') {
453ec4bd
LT
677 if (entry != buf + i && entry[0] != '#') {
678 buf[i - (i && buf[i-1] == '\r')] = 0;
7e2e4b37 679 trim_trailing_spaces(entry);
c04318e4 680 add_exclude(entry, base, baselen, el, lineno);
453ec4bd 681 }
c04318e4 682 lineno++;
453ec4bd
LT
683 entry = buf + i + 1;
684 }
685 }
686 return 0;
453ec4bd
LT
687}
688
55fe6f51
NTND
689int add_excludes_from_file_to_list(const char *fname, const char *base,
690 int baselen, struct exclude_list *el,
691 int check_index)
692{
693 return add_excludes(fname, base, baselen, el, check_index, NULL);
694}
695
c04318e4
AS
696struct exclude_list *add_exclude_list(struct dir_struct *dir,
697 int group_type, const char *src)
c082df24
AS
698{
699 struct exclude_list *el;
700 struct exclude_list_group *group;
701
702 group = &dir->exclude_list_group[group_type];
703 ALLOC_GROW(group->el, group->nr + 1, group->alloc);
704 el = &group->el[group->nr++];
705 memset(el, 0, sizeof(*el));
c04318e4 706 el->src = src;
c082df24
AS
707 return el;
708}
709
710/*
711 * Used to set up core.excludesfile and .git/info/exclude lists.
712 */
0dcb8d7f
NTND
713static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
714 struct sha1_stat *sha1_stat)
453ec4bd 715{
c082df24 716 struct exclude_list *el;
ccad261f
NTND
717 /*
718 * catch setup_standard_excludes() that's called before
719 * dir->untracked is assigned. That function behaves
720 * differently when dir->untracked is non-NULL.
721 */
722 if (!dir->untracked)
723 dir->unmanaged_exclude_files++;
c04318e4 724 el = add_exclude_list(dir, EXC_FILE, fname);
0dcb8d7f 725 if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0)
453ec4bd
LT
726 die("cannot use %s as an exclude file", fname);
727}
728
0dcb8d7f
NTND
729void add_excludes_from_file(struct dir_struct *dir, const char *fname)
730{
ccad261f 731 dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
0dcb8d7f
NTND
732 add_excludes_from_file_1(dir, fname, NULL);
733}
734
82dce998
NTND
735int match_basename(const char *basename, int basenamelen,
736 const char *pattern, int prefix, int patternlen,
737 int flags)
593cb880
NTND
738{
739 if (prefix == patternlen) {
0b6e56df
JH
740 if (patternlen == basenamelen &&
741 !strncmp_icase(pattern, basename, basenamelen))
593cb880
NTND
742 return 1;
743 } else if (flags & EXC_FLAG_ENDSWITH) {
0b6e56df 744 /* "*literal" matching against "fooliteral" */
593cb880 745 if (patternlen - 1 <= basenamelen &&
0b6e56df
JH
746 !strncmp_icase(pattern + 1,
747 basename + basenamelen - (patternlen - 1),
748 patternlen - 1))
593cb880
NTND
749 return 1;
750 } else {
0b6e56df
JH
751 if (fnmatch_icase_mem(pattern, patternlen,
752 basename, basenamelen,
753 0) == 0)
593cb880
NTND
754 return 1;
755 }
756 return 0;
757}
758
82dce998
NTND
759int match_pathname(const char *pathname, int pathlen,
760 const char *base, int baselen,
761 const char *pattern, int prefix, int patternlen,
762 int flags)
b5592632
NTND
763{
764 const char *name;
765 int namelen;
766
767 /*
768 * match with FNM_PATHNAME; the pattern has base implicitly
769 * in front of it.
770 */
771 if (*pattern == '/') {
772 pattern++;
982ac873 773 patternlen--;
b5592632
NTND
774 prefix--;
775 }
776
777 /*
778 * baselen does not count the trailing slash. base[] may or
779 * may not end with a trailing slash though.
780 */
781 if (pathlen < baselen + 1 ||
782 (baselen && pathname[baselen] != '/') ||
783 strncmp_icase(pathname, base, baselen))
784 return 0;
785
786 namelen = baselen ? pathlen - baselen - 1 : pathlen;
787 name = pathname + pathlen - namelen;
788
789 if (prefix) {
790 /*
791 * if the non-wildcard part is longer than the
792 * remaining pathname, surely it cannot match.
793 */
794 if (prefix > namelen)
795 return 0;
796
797 if (strncmp_icase(pattern, name, prefix))
798 return 0;
799 pattern += prefix;
ab3aebc1 800 patternlen -= prefix;
b5592632
NTND
801 name += prefix;
802 namelen -= prefix;
ab3aebc1
JK
803
804 /*
805 * If the whole pattern did not have a wildcard,
806 * then our prefix match is all we need; we
807 * do not need to call fnmatch at all.
808 */
809 if (!patternlen && !namelen)
810 return 1;
b5592632
NTND
811 }
812
ab3aebc1
JK
813 return fnmatch_icase_mem(pattern, patternlen,
814 name, namelen,
f30366b2 815 WM_PATHNAME) == 0;
b5592632
NTND
816}
817
578cd7c3
AS
818/*
819 * Scan the given exclude list in reverse to see whether pathname
820 * should be ignored. The first match (i.e. the last on the list), if
821 * any, determines the fate. Returns the exclude_list element which
822 * matched, or NULL for undecided.
453ec4bd 823 */
578cd7c3
AS
824static struct exclude *last_exclude_matching_from_list(const char *pathname,
825 int pathlen,
826 const char *basename,
827 int *dtype,
828 struct exclude_list *el)
453ec4bd
LT
829{
830 int i;
831
35a94d44 832 if (!el->nr)
578cd7c3 833 return NULL; /* undefined */
d6b8fc30 834
35a94d44
NTND
835 for (i = el->nr - 1; 0 <= i; i--) {
836 struct exclude *x = el->excludes[i];
b5592632 837 const char *exclude = x->pattern;
b5592632 838 int prefix = x->nowildcardlen;
35a94d44
NTND
839
840 if (x->flags & EXC_FLAG_MUSTBEDIR) {
841 if (*dtype == DT_UNKNOWN)
842 *dtype = get_dtype(NULL, pathname, pathlen);
843 if (*dtype != DT_DIR)
844 continue;
845 }
d6b8fc30 846
35a94d44 847 if (x->flags & EXC_FLAG_NODIR) {
593cb880
NTND
848 if (match_basename(basename,
849 pathlen - (basename - pathname),
850 exclude, prefix, x->patternlen,
851 x->flags))
578cd7c3 852 return x;
35a94d44
NTND
853 continue;
854 }
855
b5592632
NTND
856 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
857 if (match_pathname(pathname, pathlen,
858 x->base, x->baselen ? x->baselen - 1 : 0,
859 exclude, prefix, x->patternlen, x->flags))
578cd7c3 860 return x;
453ec4bd 861 }
578cd7c3
AS
862 return NULL; /* undecided */
863}
864
865/*
866 * Scan the list and let the last match determine the fate.
867 * Return 1 for exclude, 0 for include and -1 for undecided.
868 */
869int is_excluded_from_list(const char *pathname,
870 int pathlen, const char *basename, int *dtype,
871 struct exclude_list *el)
872{
873 struct exclude *exclude;
874 exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
875 if (exclude)
876 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
453ec4bd
LT
877 return -1; /* undecided */
878}
879
46aa2f95
KB
880static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
881 const char *pathname, int pathlen, const char *basename,
882 int *dtype_p)
883{
884 int i, j;
885 struct exclude_list_group *group;
886 struct exclude *exclude;
887 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
888 group = &dir->exclude_list_group[i];
889 for (j = group->nr - 1; j >= 0; j--) {
890 exclude = last_exclude_matching_from_list(
891 pathname, pathlen, basename, dtype_p,
892 &group->el[j]);
893 if (exclude)
894 return exclude;
895 }
896 }
897 return NULL;
898}
899
6cd5c582
KB
900/*
901 * Loads the per-directory exclude list for the substring of base
902 * which has a char length of baselen.
903 */
904static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
905{
906 struct exclude_list_group *group;
907 struct exclude_list *el;
908 struct exclude_stack *stk = NULL;
0dcb8d7f 909 struct untracked_cache_dir *untracked;
6cd5c582
KB
910 int current;
911
6cd5c582
KB
912 group = &dir->exclude_list_group[EXC_DIRS];
913
d961baa8
NTND
914 /*
915 * Pop the exclude lists from the EXCL_DIRS exclude_list_group
6cd5c582 916 * which originate from directories not in the prefix of the
d961baa8
NTND
917 * path being checked.
918 */
6cd5c582
KB
919 while ((stk = dir->exclude_stack) != NULL) {
920 if (stk->baselen <= baselen &&
aceb9429 921 !strncmp(dir->basebuf.buf, base, stk->baselen))
6cd5c582
KB
922 break;
923 el = &group->el[dir->exclude_stack->exclude_ix];
924 dir->exclude_stack = stk->prev;
95c6f271 925 dir->exclude = NULL;
aceb9429 926 free((char *)el->src); /* see strbuf_detach() below */
6cd5c582
KB
927 clear_exclude_list(el);
928 free(stk);
929 group->nr--;
930 }
931
95c6f271
KB
932 /* Skip traversing into sub directories if the parent is excluded */
933 if (dir->exclude)
934 return;
935
aceb9429
NTND
936 /*
937 * Lazy initialization. All call sites currently just
938 * memset(dir, 0, sizeof(*dir)) before use. Changing all of
939 * them seems lots of work for little benefit.
940 */
941 if (!dir->basebuf.buf)
942 strbuf_init(&dir->basebuf, PATH_MAX);
943
6cd5c582
KB
944 /* Read from the parent directories and push them down. */
945 current = stk ? stk->baselen : -1;
aceb9429 946 strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
0dcb8d7f
NTND
947 if (dir->untracked)
948 untracked = stk ? stk->ucd : dir->untracked->root;
949 else
950 untracked = NULL;
951
6cd5c582 952 while (current < baselen) {
6cd5c582 953 const char *cp;
0dcb8d7f 954 struct sha1_stat sha1_stat;
6cd5c582 955
03e11a71 956 stk = xcalloc(1, sizeof(*stk));
6cd5c582
KB
957 if (current < 0) {
958 cp = base;
959 current = 0;
d961baa8 960 } else {
6cd5c582
KB
961 cp = strchr(base + current + 1, '/');
962 if (!cp)
963 die("oops in prep_exclude");
964 cp++;
0dcb8d7f
NTND
965 untracked =
966 lookup_untracked(dir->untracked, untracked,
967 base + current,
968 cp - base - current);
6cd5c582
KB
969 }
970 stk->prev = dir->exclude_stack;
971 stk->baselen = cp - base;
95c6f271 972 stk->exclude_ix = group->nr;
0dcb8d7f 973 stk->ucd = untracked;
95c6f271 974 el = add_exclude_list(dir, EXC_DIRS, NULL);
aceb9429
NTND
975 strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
976 assert(stk->baselen == dir->basebuf.len);
95c6f271
KB
977
978 /* Abort if the directory is excluded */
979 if (stk->baselen) {
980 int dt = DT_DIR;
aceb9429 981 dir->basebuf.buf[stk->baselen - 1] = 0;
95c6f271 982 dir->exclude = last_exclude_matching_from_lists(dir,
aceb9429
NTND
983 dir->basebuf.buf, stk->baselen - 1,
984 dir->basebuf.buf + current, &dt);
985 dir->basebuf.buf[stk->baselen - 1] = '/';
c3c327de
KB
986 if (dir->exclude &&
987 dir->exclude->flags & EXC_FLAG_NEGATIVE)
988 dir->exclude = NULL;
95c6f271 989 if (dir->exclude) {
95c6f271
KB
990 dir->exclude_stack = stk;
991 return;
992 }
993 }
994
aceb9429 995 /* Try to read per-directory file */
0dcb8d7f
NTND
996 hashclr(sha1_stat.sha1);
997 sha1_stat.valid = 0;
aceb9429 998 if (dir->exclude_per_dir) {
95c6f271
KB
999 /*
1000 * dir->basebuf gets reused by the traversal, but we
1001 * need fname to remain unchanged to ensure the src
1002 * member of each struct exclude correctly
1003 * back-references its source file. Other invocations
1004 * of add_exclude_list provide stable strings, so we
aceb9429 1005 * strbuf_detach() and free() here in the caller.
95c6f271 1006 */
aceb9429
NTND
1007 struct strbuf sb = STRBUF_INIT;
1008 strbuf_addbuf(&sb, &dir->basebuf);
1009 strbuf_addstr(&sb, dir->exclude_per_dir);
1010 el->src = strbuf_detach(&sb, NULL);
0dcb8d7f
NTND
1011 add_excludes(el->src, el->src, stk->baselen, el, 1,
1012 untracked ? &sha1_stat : NULL);
1013 }
5ebf79ad
NTND
1014 /*
1015 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1016 * will first be called in valid_cached_dir() then maybe many
1017 * times more in last_exclude_matching(). When the cache is
1018 * used, last_exclude_matching() will not be called and
1019 * reading .gitignore content will be a waste.
1020 *
1021 * So when it's called by valid_cached_dir() and we can get
1022 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1023 * modified on work tree), we could delay reading the
1024 * .gitignore content until we absolutely need it in
1025 * last_exclude_matching(). Be careful about ignore rule
1026 * order, though, if you do that.
1027 */
1028 if (untracked &&
1029 hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1030 invalidate_gitignore(dir->untracked, untracked);
0dcb8d7f 1031 hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
95c6f271 1032 }
6cd5c582
KB
1033 dir->exclude_stack = stk;
1034 current = stk->baselen;
1035 }
aceb9429 1036 strbuf_setlen(&dir->basebuf, baselen);
6cd5c582
KB
1037}
1038
f4cd69a6
AS
1039/*
1040 * Loads the exclude lists for the directory containing pathname, then
1041 * scans all exclude lists to determine whether pathname is excluded.
1042 * Returns the exclude_list element which matched, or NULL for
1043 * undecided.
1044 */
b07bc8c8 1045struct exclude *last_exclude_matching(struct dir_struct *dir,
f4cd69a6
AS
1046 const char *pathname,
1047 int *dtype_p)
453ec4bd
LT
1048{
1049 int pathlen = strlen(pathname);
68492fc7
LK
1050 const char *basename = strrchr(pathname, '/');
1051 basename = (basename) ? basename+1 : pathname;
453ec4bd 1052
63d285c8 1053 prep_exclude(dir, pathname, basename-pathname);
c082df24 1054
95c6f271
KB
1055 if (dir->exclude)
1056 return dir->exclude;
1057
46aa2f95
KB
1058 return last_exclude_matching_from_lists(dir, pathname, pathlen,
1059 basename, dtype_p);
f4cd69a6
AS
1060}
1061
1062/*
1063 * Loads the exclude lists for the directory containing pathname, then
1064 * scans all exclude lists to determine whether pathname is excluded.
1065 * Returns 1 if true, otherwise 0.
1066 */
b07bc8c8 1067int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
f4cd69a6
AS
1068{
1069 struct exclude *exclude =
1070 last_exclude_matching(dir, pathname, dtype_p);
1071 if (exclude)
1072 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
453ec4bd
LT
1073 return 0;
1074}
1075
f3fa1838
JH
1076static struct dir_entry *dir_entry_new(const char *pathname, int len)
1077{
453ec4bd
LT
1078 struct dir_entry *ent;
1079
453ec4bd
LT
1080 ent = xmalloc(sizeof(*ent) + len + 1);
1081 ent->len = len;
1082 memcpy(ent->name, pathname, len);
1083 ent->name[len] = 0;
4d06f8ac 1084 return ent;
453ec4bd
LT
1085}
1086
159b3212 1087static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 1088{
ebbd7439 1089 if (cache_file_exists(pathname, len, ignore_case))
6815e569
JK
1090 return NULL;
1091
25fd2f7a 1092 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
1093 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1094}
1095
108da0db 1096struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 1097{
6e4f981f 1098 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
1099 return NULL;
1100
25fd2f7a 1101 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
1102 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1103}
1104
09595258
LT
1105enum exist_status {
1106 index_nonexistent = 0,
1107 index_directory,
4b05548f 1108 index_gitdir
09595258
LT
1109};
1110
5102c617 1111/*
71261027 1112 * Do not use the alphabetically sorted index to look up
5102c617 1113 * the directory name; instead, use the case insensitive
ebbd7439 1114 * directory hash.
5102c617
JJ
1115 */
1116static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
1117{
d28eec26 1118 const struct cache_entry *ce = cache_dir_exists(dirname, len);
5102c617
JJ
1119 unsigned char endchar;
1120
1121 if (!ce)
1122 return index_nonexistent;
1123 endchar = ce->name[len];
1124
1125 /*
1126 * The cache_entry structure returned will contain this dirname
1127 * and possibly additional path components.
1128 */
1129 if (endchar == '/')
1130 return index_directory;
1131
1132 /*
1133 * If there are no additional path components, then this cache_entry
1134 * represents a submodule. Submodules, despite being directories,
1135 * are stored in the cache without a closing slash.
1136 */
1137 if (!endchar && S_ISGITLINK(ce->ce_mode))
1138 return index_gitdir;
1139
1140 /* This should never be hit, but it exists just in case. */
1141 return index_nonexistent;
1142}
1143
09595258
LT
1144/*
1145 * The index sorts alphabetically by entry name, which
1146 * means that a gitlink sorts as '\0' at the end, while
1147 * a directory (which is defined not as an entry, but as
1148 * the files it contains) will sort with the '/' at the
1149 * end.
1150 */
1151static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd 1152{
5102c617
JJ
1153 int pos;
1154
1155 if (ignore_case)
1156 return directory_exists_in_index_icase(dirname, len);
1157
1158 pos = cache_name_pos(dirname, len);
09595258
LT
1159 if (pos < 0)
1160 pos = -pos-1;
1161 while (pos < active_nr) {
9c5e6c80 1162 const struct cache_entry *ce = active_cache[pos++];
09595258
LT
1163 unsigned char endchar;
1164
1165 if (strncmp(ce->name, dirname, len))
1166 break;
1167 endchar = ce->name[len];
1168 if (endchar > '/')
1169 break;
1170 if (endchar == '/')
1171 return index_directory;
7a51ed66 1172 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
1173 return index_gitdir;
1174 }
1175 return index_nonexistent;
1176}
1177
1178/*
1179 * When we find a directory when traversing the filesystem, we
1180 * have three distinct cases:
1181 *
1182 * - ignore it
1183 * - see it as a directory
1184 * - recurse into it
1185 *
1186 * and which one we choose depends on a combination of existing
1187 * git index contents and the flags passed into the directory
1188 * traversal routine.
1189 *
1190 * Case 1: If we *already* have entries in the index under that
5bd8e2d8
KB
1191 * directory name, we always recurse into the directory to see
1192 * all the files.
09595258
LT
1193 *
1194 * Case 2: If we *already* have that directory name as a gitlink,
1195 * we always continue to see it as a gitlink, regardless of whether
1196 * there is an actual git directory there or not (it might not
1197 * be checked out as a subproject!)
1198 *
1199 * Case 3: if we didn't have it in the index previously, we
1200 * have a few sub-cases:
1201 *
1202 * (a) if "show_other_directories" is true, we show it as
1203 * just a directory, unless "hide_empty_directories" is
defd7c7b
KB
1204 * also true, in which case we need to check if it contains any
1205 * untracked and / or ignored files.
09595258 1206 * (b) if it looks like a git directory, and we don't have
302b9282 1207 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
1208 * as a directory.
1209 * (c) otherwise, we recurse into it.
1210 */
defd7c7b 1211static enum path_treatment treat_directory(struct dir_struct *dir,
0dcb8d7f 1212 struct untracked_cache_dir *untracked,
721ac4ed 1213 const char *dirname, int len, int exclude,
09595258
LT
1214 const struct path_simplify *simplify)
1215{
1216 /* The "len-1" is to strip the final '/' */
1217 switch (directory_exists_in_index(dirname, len-1)) {
1218 case index_directory:
defd7c7b 1219 return path_recurse;
09595258
LT
1220
1221 case index_gitdir:
26c986e1 1222 return path_none;
09595258
LT
1223
1224 case index_nonexistent:
7c4c97c0 1225 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 1226 break;
7c4c97c0 1227 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
1228 unsigned char sha1[20];
1229 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
defd7c7b 1230 return path_untracked;
09595258 1231 }
defd7c7b 1232 return path_recurse;
09595258
LT
1233 }
1234
1235 /* This is the "show_other_directories" case */
721ac4ed 1236
184d2a8e 1237 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
defd7c7b
KB
1238 return exclude ? path_excluded : path_untracked;
1239
0dcb8d7f
NTND
1240 untracked = lookup_untracked(dir->untracked, untracked, dirname, len);
1241 return read_directory_recursive(dir, dirname, len,
1242 untracked, 1, simplify);
453ec4bd
LT
1243}
1244
9fc42d60
LT
1245/*
1246 * This is an inexact early pruning of any recursive directory
1247 * reading - if the path cannot possibly be in the pathspec,
1248 * return true, and we'll skip it early.
1249 */
1250static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
1251{
1252 if (simplify) {
1253 for (;;) {
1254 const char *match = simplify->path;
1255 int len = simplify->len;
1256
1257 if (!match)
1258 break;
1259 if (len > pathlen)
1260 len = pathlen;
1261 if (!memcmp(path, match, len))
1262 return 0;
1263 simplify++;
1264 }
1265 return 1;
1266 }
1267 return 0;
1268}
1269
29209cbe
JK
1270/*
1271 * This function tells us whether an excluded path matches a
1272 * list of "interesting" pathspecs. That is, whether a path matched
1273 * by any of the pathspecs could possibly be ignored by excluding
1274 * the specified path. This can happen if:
1275 *
1276 * 1. the path is mentioned explicitly in the pathspec
1277 *
1278 * 2. the path is a directory prefix of some element in the
1279 * pathspec
1280 */
1281static int exclude_matches_pathspec(const char *path, int len,
1282 const struct path_simplify *simplify)
e96980ef
JK
1283{
1284 if (simplify) {
1285 for (; simplify->path; simplify++) {
1286 if (len == simplify->len
1287 && !memcmp(path, simplify->path, len))
1288 return 1;
29209cbe
JK
1289 if (len < simplify->len
1290 && simplify->path[len] == '/'
1291 && !memcmp(path, simplify->path, len))
1292 return 1;
e96980ef
JK
1293 }
1294 }
1295 return 0;
1296}
1297
443e061a
LT
1298static int get_index_dtype(const char *path, int len)
1299{
1300 int pos;
9c5e6c80 1301 const struct cache_entry *ce;
443e061a 1302
ebbd7439 1303 ce = cache_file_exists(path, len, 0);
443e061a
LT
1304 if (ce) {
1305 if (!ce_uptodate(ce))
1306 return DT_UNKNOWN;
1307 if (S_ISGITLINK(ce->ce_mode))
1308 return DT_DIR;
1309 /*
1310 * Nobody actually cares about the
1311 * difference between DT_LNK and DT_REG
1312 */
1313 return DT_REG;
1314 }
1315
1316 /* Try to look it up as a directory */
1317 pos = cache_name_pos(path, len);
1318 if (pos >= 0)
1319 return DT_UNKNOWN;
1320 pos = -pos-1;
1321 while (pos < active_nr) {
1322 ce = active_cache[pos++];
1323 if (strncmp(ce->name, path, len))
1324 break;
1325 if (ce->name[len] > '/')
1326 break;
1327 if (ce->name[len] < '/')
1328 continue;
1329 if (!ce_uptodate(ce))
1330 break; /* continue? */
1331 return DT_DIR;
1332 }
1333 return DT_UNKNOWN;
1334}
1335
caa6b782 1336static int get_dtype(struct dirent *de, const char *path, int len)
07134421 1337{
6831a88a 1338 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
1339 struct stat st;
1340
1341 if (dtype != DT_UNKNOWN)
1342 return dtype;
443e061a
LT
1343 dtype = get_index_dtype(path, len);
1344 if (dtype != DT_UNKNOWN)
1345 return dtype;
1346 if (lstat(path, &st))
07134421
LT
1347 return dtype;
1348 if (S_ISREG(st.st_mode))
1349 return DT_REG;
1350 if (S_ISDIR(st.st_mode))
1351 return DT_DIR;
1352 if (S_ISLNK(st.st_mode))
1353 return DT_LNK;
1354 return dtype;
1355}
1356
16e2cfa9 1357static enum path_treatment treat_one_path(struct dir_struct *dir,
0dcb8d7f 1358 struct untracked_cache_dir *untracked,
49dc2cc2 1359 struct strbuf *path,
16e2cfa9
JH
1360 const struct path_simplify *simplify,
1361 int dtype, struct dirent *de)
53cc5356 1362{
8aaf8d77 1363 int exclude;
ebbd7439 1364 int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);
2eac2a4c 1365
8aaf8d77
KB
1366 if (dtype == DT_UNKNOWN)
1367 dtype = get_dtype(de, path->buf, path->len);
1368
1369 /* Always exclude indexed files */
2eac2a4c 1370 if (dtype != DT_DIR && has_path_in_index)
defd7c7b 1371 return path_none;
8aaf8d77 1372
2eac2a4c
JH
1373 /*
1374 * When we are looking at a directory P in the working tree,
1375 * there are three cases:
1376 *
1377 * (1) P exists in the index. Everything inside the directory P in
1378 * the working tree needs to go when P is checked out from the
1379 * index.
1380 *
1381 * (2) P does not exist in the index, but there is P/Q in the index.
1382 * We know P will stay a directory when we check out the contents
1383 * of the index, but we do not know yet if there is a directory
1384 * P/Q in the working tree to be killed, so we need to recurse.
1385 *
1386 * (3) P does not exist in the index, and there is no P/Q in the index
1387 * to require P to be a directory, either. Only in this case, we
1388 * know that everything inside P will not be killed without
1389 * recursing.
1390 */
1391 if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
1392 (dtype == DT_DIR) &&
de372b1b
ES
1393 !has_path_in_index &&
1394 (directory_exists_in_index(path->buf, path->len) == index_nonexistent))
1395 return path_none;
8aaf8d77
KB
1396
1397 exclude = is_excluded(dir, path->buf, &dtype);
53cc5356
JH
1398
1399 /*
1400 * Excluded? If we don't explicitly want to show
1401 * ignored files, ignore it
1402 */
0aaf62b6 1403 if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))
defd7c7b 1404 return path_excluded;
53cc5356 1405
53cc5356
JH
1406 switch (dtype) {
1407 default:
defd7c7b 1408 return path_none;
53cc5356 1409 case DT_DIR:
49dc2cc2 1410 strbuf_addch(path, '/');
0dcb8d7f 1411 return treat_directory(dir, untracked, path->buf, path->len, exclude,
defd7c7b 1412 simplify);
53cc5356
JH
1413 case DT_REG:
1414 case DT_LNK:
defd7c7b 1415 return exclude ? path_excluded : path_untracked;
53cc5356 1416 }
53cc5356
JH
1417}
1418
16e2cfa9 1419static enum path_treatment treat_path(struct dir_struct *dir,
0dcb8d7f 1420 struct untracked_cache_dir *untracked,
16e2cfa9 1421 struct dirent *de,
49dc2cc2 1422 struct strbuf *path,
16e2cfa9 1423 int baselen,
49dc2cc2 1424 const struct path_simplify *simplify)
16e2cfa9
JH
1425{
1426 int dtype;
1427
1428 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
defd7c7b 1429 return path_none;
49dc2cc2
RS
1430 strbuf_setlen(path, baselen);
1431 strbuf_addstr(path, de->d_name);
1432 if (simplify_away(path->buf, path->len, simplify))
defd7c7b 1433 return path_none;
16e2cfa9
JH
1434
1435 dtype = DTYPE(de);
0dcb8d7f
NTND
1436 return treat_one_path(dir, untracked, path, simplify, dtype, de);
1437}
1438
1439static void add_untracked(struct untracked_cache_dir *dir, const char *name)
1440{
1441 if (!dir)
1442 return;
1443 ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,
1444 dir->untracked_alloc);
1445 dir->untracked[dir->untracked_nr++] = xstrdup(name);
16e2cfa9
JH
1446}
1447
453ec4bd
LT
1448/*
1449 * Read a directory tree. We currently ignore anything but
1450 * directories, regular files and symlinks. That's because git
1451 * doesn't handle them at all yet. Maybe that will change some
1452 * day.
1453 *
1454 * Also, we ignore the name ".git" (even if it is not a directory).
1455 * That likely will not change.
defd7c7b
KB
1456 *
1457 * Returns the most significant path_treatment value encountered in the scan.
453ec4bd 1458 */
defd7c7b 1459static enum path_treatment read_directory_recursive(struct dir_struct *dir,
53cc5356 1460 const char *base, int baselen,
0dcb8d7f 1461 struct untracked_cache_dir *untracked, int check_only,
53cc5356 1462 const struct path_simplify *simplify)
453ec4bd 1463{
1528d247 1464 DIR *fdir;
defd7c7b 1465 enum path_treatment state, subdir_state, dir_state = path_none;
02cb6753 1466 struct dirent *de;
bef36921 1467 struct strbuf path = STRBUF_INIT;
453ec4bd 1468
bef36921 1469 strbuf_add(&path, base, baselen);
02cb6753 1470
1528d247
RS
1471 fdir = opendir(path.len ? path.buf : ".");
1472 if (!fdir)
1473 goto out;
1474
0dcb8d7f
NTND
1475 if (untracked)
1476 untracked->check_only = !!check_only;
1477
02cb6753 1478 while ((de = readdir(fdir)) != NULL) {
defd7c7b 1479 /* check how the file or directory should be treated */
0dcb8d7f
NTND
1480 state = treat_path(dir, untracked, de, &path, baselen, simplify);
1481
defd7c7b
KB
1482 if (state > dir_state)
1483 dir_state = state;
1484
1485 /* recurse into subdir if instructed by treat_path */
1486 if (state == path_recurse) {
0dcb8d7f
NTND
1487 struct untracked_cache_dir *ud;
1488 ud = lookup_untracked(dir->untracked, untracked,
1489 path.buf + baselen,
1490 path.len - baselen);
1491 subdir_state =
1492 read_directory_recursive(dir, path.buf, path.len,
1493 ud, check_only, simplify);
defd7c7b
KB
1494 if (subdir_state > dir_state)
1495 dir_state = subdir_state;
1496 }
1497
1498 if (check_only) {
1499 /* abort early if maximum state has been reached */
0dcb8d7f
NTND
1500 if (dir_state == path_untracked) {
1501 if (untracked)
1502 add_untracked(untracked, path.buf + baselen);
defd7c7b 1503 break;
0dcb8d7f 1504 }
defd7c7b 1505 /* skip the dir_add_* part */
02cb6753 1506 continue;
453ec4bd 1507 }
defd7c7b
KB
1508
1509 /* add the path to the appropriate result list */
1510 switch (state) {
1511 case path_excluded:
1512 if (dir->flags & DIR_SHOW_IGNORED)
1513 dir_add_name(dir, path.buf, path.len);
0aaf62b6
KB
1514 else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||
1515 ((dir->flags & DIR_COLLECT_IGNORED) &&
1516 exclude_matches_pathspec(path.buf, path.len,
1517 simplify)))
1518 dir_add_ignored(dir, path.buf, path.len);
defd7c7b
KB
1519 break;
1520
1521 case path_untracked:
0dcb8d7f
NTND
1522 if (dir->flags & DIR_SHOW_IGNORED)
1523 break;
1524 dir_add_name(dir, path.buf, path.len);
1525 if (untracked)
1526 add_untracked(untracked, path.buf + baselen);
1528d247 1527 break;
defd7c7b
KB
1528
1529 default:
1530 break;
1531 }
453ec4bd 1532 }
02cb6753 1533 closedir(fdir);
1528d247 1534 out:
bef36921 1535 strbuf_release(&path);
453ec4bd 1536
defd7c7b 1537 return dir_state;
453ec4bd
LT
1538}
1539
1540static int cmp_name(const void *p1, const void *p2)
1541{
1542 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1543 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1544
ccdd4a0f 1545 return name_compare(e1->name, e1->len, e2->name, e2->len);
453ec4bd
LT
1546}
1547
9fc42d60
LT
1548static struct path_simplify *create_simplify(const char **pathspec)
1549{
1550 int nr, alloc = 0;
1551 struct path_simplify *simplify = NULL;
1552
1553 if (!pathspec)
1554 return NULL;
1555
1556 for (nr = 0 ; ; nr++) {
1557 const char *match;
9af49f82 1558 ALLOC_GROW(simplify, nr + 1, alloc);
9fc42d60
LT
1559 match = *pathspec++;
1560 if (!match)
1561 break;
1562 simplify[nr].path = match;
1563 simplify[nr].len = simple_length(match);
1564 }
1565 simplify[nr].path = NULL;
1566 simplify[nr].len = 0;
1567 return simplify;
1568}
1569
1570static void free_simplify(struct path_simplify *simplify)
1571{
8e0f7003 1572 free(simplify);
9fc42d60
LT
1573}
1574
48ffef96
JH
1575static int treat_leading_path(struct dir_struct *dir,
1576 const char *path, int len,
1577 const struct path_simplify *simplify)
1578{
49dc2cc2
RS
1579 struct strbuf sb = STRBUF_INIT;
1580 int baselen, rc = 0;
48ffef96 1581 const char *cp;
be8a84c5 1582 int old_flags = dir->flags;
48ffef96
JH
1583
1584 while (len && path[len - 1] == '/')
1585 len--;
1586 if (!len)
1587 return 1;
1588 baselen = 0;
be8a84c5 1589 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
48ffef96
JH
1590 while (1) {
1591 cp = path + baselen + !!baselen;
1592 cp = memchr(cp, '/', path + len - cp);
1593 if (!cp)
1594 baselen = len;
1595 else
1596 baselen = cp - path;
49dc2cc2
RS
1597 strbuf_setlen(&sb, 0);
1598 strbuf_add(&sb, path, baselen);
1599 if (!is_directory(sb.buf))
1600 break;
1601 if (simplify_away(sb.buf, sb.len, simplify))
1602 break;
0dcb8d7f 1603 if (treat_one_path(dir, NULL, &sb, simplify,
defd7c7b 1604 DT_DIR, NULL) == path_none)
49dc2cc2
RS
1605 break; /* do not recurse into it */
1606 if (len <= baselen) {
1607 rc = 1;
1608 break; /* finished checking */
1609 }
48ffef96 1610 }
49dc2cc2 1611 strbuf_release(&sb);
be8a84c5 1612 dir->flags = old_flags;
49dc2cc2 1613 return rc;
48ffef96
JH
1614}
1615
ccad261f
NTND
1616static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,
1617 int base_len,
1618 const struct pathspec *pathspec)
1619{
1620 struct untracked_cache_dir *root;
1621
1622 if (!dir->untracked)
1623 return NULL;
1624
1625 /*
1626 * We only support $GIT_DIR/info/exclude and core.excludesfile
1627 * as the global ignore rule files. Any other additions
1628 * (e.g. from command line) invalidate the cache. This
1629 * condition also catches running setup_standard_excludes()
1630 * before setting dir->untracked!
1631 */
1632 if (dir->unmanaged_exclude_files)
1633 return NULL;
1634
1635 /*
1636 * Optimize for the main use case only: whole-tree git
1637 * status. More work involved in treat_leading_path() if we
1638 * use cache on just a subset of the worktree. pathspec
1639 * support could make the matter even worse.
1640 */
1641 if (base_len || (pathspec && pathspec->nr))
1642 return NULL;
1643
1644 /* Different set of flags may produce different results */
1645 if (dir->flags != dir->untracked->dir_flags ||
1646 /*
1647 * See treat_directory(), case index_nonexistent. Without
1648 * this flag, we may need to also cache .git file content
1649 * for the resolve_gitlink_ref() call, which we don't.
1650 */
1651 !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||
1652 /* We don't support collecting ignore files */
1653 (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |
1654 DIR_COLLECT_IGNORED)))
1655 return NULL;
1656
1657 /*
1658 * If we use .gitignore in the cache and now you change it to
1659 * .gitexclude, everything will go wrong.
1660 */
1661 if (dir->exclude_per_dir != dir->untracked->exclude_per_dir &&
1662 strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))
1663 return NULL;
1664
1665 /*
1666 * EXC_CMDL is not considered in the cache. If people set it,
1667 * skip the cache.
1668 */
1669 if (dir->exclude_list_group[EXC_CMDL].nr)
1670 return NULL;
1671
1672 if (!dir->untracked->root) {
1673 const int len = sizeof(*dir->untracked->root);
1674 dir->untracked->root = xmalloc(len);
1675 memset(dir->untracked->root, 0, len);
1676 }
1677
1678 /* Validate $GIT_DIR/info/exclude and core.excludesfile */
1679 root = dir->untracked->root;
1680 if (hashcmp(dir->ss_info_exclude.sha1,
1681 dir->untracked->ss_info_exclude.sha1)) {
1682 invalidate_gitignore(dir->untracked, root);
1683 dir->untracked->ss_info_exclude = dir->ss_info_exclude;
1684 }
1685 if (hashcmp(dir->ss_excludes_file.sha1,
1686 dir->untracked->ss_excludes_file.sha1)) {
1687 invalidate_gitignore(dir->untracked, root);
1688 dir->untracked->ss_excludes_file = dir->ss_excludes_file;
1689 }
1690 return root;
1691}
1692
7327d3d1 1693int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)
9fc42d60 1694{
725b0605 1695 struct path_simplify *simplify;
ccad261f 1696 struct untracked_cache_dir *untracked;
b4189aa8 1697
7327d3d1
NTND
1698 /*
1699 * Check out create_simplify()
1700 */
1701 if (pathspec)
5c6933d2
NTND
1702 GUARD_PATHSPEC(pathspec,
1703 PATHSPEC_FROMTOP |
1704 PATHSPEC_MAXDEPTH |
bd30c2e4 1705 PATHSPEC_LITERAL |
93d93537 1706 PATHSPEC_GLOB |
ef79b1f8
NTND
1707 PATHSPEC_ICASE |
1708 PATHSPEC_EXCLUDE);
7327d3d1 1709
dba2e203 1710 if (has_symlink_leading_path(path, len))
725b0605
JH
1711 return dir->nr;
1712
ef79b1f8
NTND
1713 /*
1714 * exclude patterns are treated like positive ones in
1715 * create_simplify. Usually exclude patterns should be a
1716 * subset of positive ones, which has no impacts on
1717 * create_simplify().
1718 */
b3920bbd 1719 simplify = create_simplify(pathspec ? pathspec->_raw : NULL);
ccad261f
NTND
1720 untracked = validate_untracked_cache(dir, len, pathspec);
1721 if (!untracked)
1722 /*
1723 * make sure untracked cache code path is disabled,
1724 * e.g. prep_exclude()
1725 */
1726 dir->untracked = NULL;
48ffef96 1727 if (!len || treat_leading_path(dir, path, len, simplify))
ccad261f 1728 read_directory_recursive(dir, path, len, untracked, 0, simplify);
9fc42d60 1729 free_simplify(simplify);
453ec4bd 1730 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 1731 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
1732 return dir->nr;
1733}
c91f0d92 1734
686a4a06 1735int file_exists(const char *f)
c91f0d92 1736{
686a4a06 1737 struct stat sb;
a50f9fc5 1738 return lstat(f, &sb) == 0;
c91f0d92 1739}
e6636747
JS
1740
1741/*
9b125da4
NTND
1742 * Given two normalized paths (a trailing slash is ok), if subdir is
1743 * outside dir, return -1. Otherwise return the offset in subdir that
1744 * can be used as relative path to dir.
e6636747 1745 */
9b125da4 1746int dir_inside_of(const char *subdir, const char *dir)
e6636747 1747{
9b125da4 1748 int offset = 0;
e6636747 1749
9b125da4 1750 assert(dir && subdir && *dir && *subdir);
e6636747 1751
9b125da4 1752 while (*dir && *subdir && *dir == *subdir) {
e6636747 1753 dir++;
9b125da4
NTND
1754 subdir++;
1755 offset++;
490544b1 1756 }
9b125da4
NTND
1757
1758 /* hel[p]/me vs hel[l]/yeah */
1759 if (*dir && *subdir)
1760 return -1;
1761
1762 if (!*subdir)
1763 return !*dir ? offset : -1; /* same dir */
1764
1765 /* foo/[b]ar vs foo/[] */
1766 if (is_dir_sep(dir[-1]))
1767 return is_dir_sep(subdir[-1]) ? offset : -1;
1768
1769 /* foo[/]bar vs foo[] */
1770 return is_dir_sep(*subdir) ? offset + 1 : -1;
e6636747
JS
1771}
1772
1773int is_inside_dir(const char *dir)
1774{
56b9f6e7
RS
1775 char *cwd;
1776 int rc;
1777
b892913d
NTND
1778 if (!dir)
1779 return 0;
56b9f6e7
RS
1780
1781 cwd = xgetcwd();
1782 rc = (dir_inside_of(cwd, dir) >= 0);
1783 free(cwd);
1784 return rc;
e6636747 1785}
7155b727 1786
55892d23
AP
1787int is_empty_dir(const char *path)
1788{
1789 DIR *dir = opendir(path);
1790 struct dirent *e;
1791 int ret = 1;
1792
1793 if (!dir)
1794 return 0;
1795
1796 while ((e = readdir(dir)) != NULL)
1797 if (!is_dot_or_dotdot(e->d_name)) {
1798 ret = 0;
1799 break;
1800 }
1801
1802 closedir(dir);
1803 return ret;
1804}
1805
ae2f203e 1806static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
7155b727 1807{
a0f4afbe 1808 DIR *dir;
7155b727 1809 struct dirent *e;
ae2f203e 1810 int ret = 0, original_len = path->len, len, kept_down = 0;
a0f4afbe 1811 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
c844a803 1812 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
a0f4afbe 1813 unsigned char submodule_head[20];
7155b727 1814
a0f4afbe 1815 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
ae2f203e 1816 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
a0f4afbe 1817 /* Do not descend and nuke a nested git work tree. */
ae2f203e
JH
1818 if (kept_up)
1819 *kept_up = 1;
a0f4afbe 1820 return 0;
ae2f203e 1821 }
a0f4afbe 1822
ae2f203e 1823 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
a0f4afbe 1824 dir = opendir(path->buf);
c844a803 1825 if (!dir) {
863808cd
MH
1826 if (errno == ENOENT)
1827 return keep_toplevel ? -1 : 0;
1828 else if (errno == EACCES && !keep_toplevel)
ecb2c282
MH
1829 /*
1830 * An empty dir could be removable even if it
1831 * is unreadable:
1832 */
c844a803
JH
1833 return rmdir(path->buf);
1834 else
1835 return -1;
1836 }
7155b727
JS
1837 if (path->buf[original_len - 1] != '/')
1838 strbuf_addch(path, '/');
1839
1840 len = path->len;
1841 while ((e = readdir(dir)) != NULL) {
1842 struct stat st;
8ca12c0d
AP
1843 if (is_dot_or_dotdot(e->d_name))
1844 continue;
7155b727
JS
1845
1846 strbuf_setlen(path, len);
1847 strbuf_addstr(path, e->d_name);
863808cd
MH
1848 if (lstat(path->buf, &st)) {
1849 if (errno == ENOENT)
1850 /*
1851 * file disappeared, which is what we
1852 * wanted anyway
1853 */
1854 continue;
1855 /* fall thru */
1856 } else if (S_ISDIR(st.st_mode)) {
ae2f203e 1857 if (!remove_dir_recurse(path, flag, &kept_down))
7155b727 1858 continue; /* happy */
863808cd
MH
1859 } else if (!only_empty &&
1860 (!unlink(path->buf) || errno == ENOENT)) {
7155b727 1861 continue; /* happy, too */
863808cd 1862 }
7155b727
JS
1863
1864 /* path too long, stat fails, or non-directory still exists */
1865 ret = -1;
1866 break;
1867 }
1868 closedir(dir);
1869
1870 strbuf_setlen(path, original_len);
ae2f203e 1871 if (!ret && !keep_toplevel && !kept_down)
863808cd 1872 ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
ae2f203e
JH
1873 else if (kept_up)
1874 /*
1875 * report the uplevel that it is not an error that we
1876 * did not rmdir() our directory.
1877 */
1878 *kept_up = !ret;
7155b727
JS
1879 return ret;
1880}
039bc64e 1881
ae2f203e
JH
1882int remove_dir_recursively(struct strbuf *path, int flag)
1883{
1884 return remove_dir_recurse(path, flag, NULL);
1885}
1886
039bc64e
JH
1887void setup_standard_excludes(struct dir_struct *dir)
1888{
1889 const char *path;
dc79687e 1890 char *xdg_path;
039bc64e
JH
1891
1892 dir->exclude_per_dir = ".gitignore";
1893 path = git_path("info/exclude");
dc79687e
HKNN
1894 if (!excludes_file) {
1895 home_config_paths(NULL, &xdg_path, "ignore");
1896 excludes_file = xdg_path;
1897 }
4698c8fe 1898 if (!access_or_warn(path, R_OK, 0))
0dcb8d7f
NTND
1899 add_excludes_from_file_1(dir, path,
1900 dir->untracked ? &dir->ss_info_exclude : NULL);
4698c8fe 1901 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
0dcb8d7f
NTND
1902 add_excludes_from_file_1(dir, excludes_file,
1903 dir->untracked ? &dir->ss_excludes_file : NULL);
039bc64e 1904}
4a92d1bf
AR
1905
1906int remove_path(const char *name)
1907{
1908 char *slash;
1909
9a6728d4 1910 if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
4a92d1bf
AR
1911 return -1;
1912
1913 slash = strrchr(name, '/');
1914 if (slash) {
1915 char *dirs = xstrdup(name);
1916 slash = dirs + (slash - name);
1917 do {
1918 *slash = '\0';
3fc0d131 1919 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
4a92d1bf
AR
1920 free(dirs);
1921 }
1922 return 0;
1923}
1924
270be816
AS
1925/*
1926 * Frees memory within dir which was allocated for exclude lists and
1927 * the exclude_stack. Does not free dir itself.
1928 */
1929void clear_directory(struct dir_struct *dir)
1930{
1931 int i, j;
1932 struct exclude_list_group *group;
1933 struct exclude_list *el;
1934 struct exclude_stack *stk;
1935
1936 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1937 group = &dir->exclude_list_group[i];
1938 for (j = 0; j < group->nr; j++) {
1939 el = &group->el[j];
1940 if (i == EXC_DIRS)
1941 free((char *)el->src);
1942 clear_exclude_list(el);
1943 }
1944 free(group->el);
1945 }
1946
1947 stk = dir->exclude_stack;
1948 while (stk) {
1949 struct exclude_stack *prev = stk->prev;
1950 free(stk);
1951 stk = prev;
1952 }
aceb9429 1953 strbuf_release(&dir->basebuf);
270be816 1954}