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