untracked cache: record .gitignore information and dir hierarchy
[git/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
55fe6f51
NTND
585/*
586 * Given a file with name "fname", read it (either from disk, or from
587 * the index if "check_index" is non-zero), parse it and store the
588 * exclude rules in "el".
589 *
590 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
591 * stat data from disk (only valid if add_excludes returns zero). If
592 * ss_valid is non-zero, "ss" must contain good value as input.
593 */
594static int add_excludes(const char *fname, const char *base, int baselen,
595 struct exclude_list *el, int check_index,
596 struct sha1_stat *sha1_stat)
453ec4bd 597{
c470701a 598 struct stat st;
c04318e4 599 int fd, i, lineno = 1;
9d14017a 600 size_t size = 0;
453ec4bd
LT
601 char *buf, *entry;
602
603 fd = open(fname, O_RDONLY);
c28b3d6e 604 if (fd < 0 || fstat(fd, &st) < 0) {
69660731 605 if (errno != ENOENT)
55b38a48 606 warn_on_inaccessible(fname);
c28b3d6e
NTND
607 if (0 <= fd)
608 close(fd);
609 if (!check_index ||
55fe6f51 610 (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
c28b3d6e 611 return -1;
45d76f17
NTND
612 if (size == 0) {
613 free(buf);
614 return 0;
615 }
616 if (buf[size-1] != '\n') {
617 buf = xrealloc(buf, size+1);
618 buf[size++] = '\n';
619 }
d961baa8 620 } else {
c28b3d6e
NTND
621 size = xsize_t(st.st_size);
622 if (size == 0) {
55fe6f51
NTND
623 if (sha1_stat) {
624 fill_stat_data(&sha1_stat->stat, &st);
625 hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
626 sha1_stat->valid = 1;
627 }
c28b3d6e
NTND
628 close(fd);
629 return 0;
630 }
45d76f17 631 buf = xmalloc(size+1);
c28b3d6e 632 if (read_in_full(fd, buf, size) != size) {
45d76f17 633 free(buf);
c28b3d6e
NTND
634 close(fd);
635 return -1;
636 }
45d76f17 637 buf[size++] = '\n';
c28b3d6e 638 close(fd);
55fe6f51
NTND
639 if (sha1_stat) {
640 int pos;
641 if (sha1_stat->valid &&
642 !match_stat_data(&sha1_stat->stat, &st))
643 ; /* no content change, ss->sha1 still good */
644 else if (check_index &&
645 (pos = cache_name_pos(fname, strlen(fname))) >= 0 &&
646 !ce_stage(active_cache[pos]) &&
647 ce_uptodate(active_cache[pos]) &&
648 !would_convert_to_git(fname))
649 hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
650 else
651 hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
652 fill_stat_data(&sha1_stat->stat, &st);
653 sha1_stat->valid = 1;
654 }
6ba78238 655 }
453ec4bd 656
c082df24 657 el->filebuf = buf;
453ec4bd 658 entry = buf;
45d76f17
NTND
659 for (i = 0; i < size; i++) {
660 if (buf[i] == '\n') {
453ec4bd
LT
661 if (entry != buf + i && entry[0] != '#') {
662 buf[i - (i && buf[i-1] == '\r')] = 0;
7e2e4b37 663 trim_trailing_spaces(entry);
c04318e4 664 add_exclude(entry, base, baselen, el, lineno);
453ec4bd 665 }
c04318e4 666 lineno++;
453ec4bd
LT
667 entry = buf + i + 1;
668 }
669 }
670 return 0;
453ec4bd
LT
671}
672
55fe6f51
NTND
673int add_excludes_from_file_to_list(const char *fname, const char *base,
674 int baselen, struct exclude_list *el,
675 int check_index)
676{
677 return add_excludes(fname, base, baselen, el, check_index, NULL);
678}
679
c04318e4
AS
680struct exclude_list *add_exclude_list(struct dir_struct *dir,
681 int group_type, const char *src)
c082df24
AS
682{
683 struct exclude_list *el;
684 struct exclude_list_group *group;
685
686 group = &dir->exclude_list_group[group_type];
687 ALLOC_GROW(group->el, group->nr + 1, group->alloc);
688 el = &group->el[group->nr++];
689 memset(el, 0, sizeof(*el));
c04318e4 690 el->src = src;
c082df24
AS
691 return el;
692}
693
694/*
695 * Used to set up core.excludesfile and .git/info/exclude lists.
696 */
0dcb8d7f
NTND
697static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
698 struct sha1_stat *sha1_stat)
453ec4bd 699{
c082df24 700 struct exclude_list *el;
c04318e4 701 el = add_exclude_list(dir, EXC_FILE, fname);
0dcb8d7f 702 if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0)
453ec4bd
LT
703 die("cannot use %s as an exclude file", fname);
704}
705
0dcb8d7f
NTND
706void add_excludes_from_file(struct dir_struct *dir, const char *fname)
707{
708 add_excludes_from_file_1(dir, fname, NULL);
709}
710
82dce998
NTND
711int match_basename(const char *basename, int basenamelen,
712 const char *pattern, int prefix, int patternlen,
713 int flags)
593cb880
NTND
714{
715 if (prefix == patternlen) {
0b6e56df
JH
716 if (patternlen == basenamelen &&
717 !strncmp_icase(pattern, basename, basenamelen))
593cb880
NTND
718 return 1;
719 } else if (flags & EXC_FLAG_ENDSWITH) {
0b6e56df 720 /* "*literal" matching against "fooliteral" */
593cb880 721 if (patternlen - 1 <= basenamelen &&
0b6e56df
JH
722 !strncmp_icase(pattern + 1,
723 basename + basenamelen - (patternlen - 1),
724 patternlen - 1))
593cb880
NTND
725 return 1;
726 } else {
0b6e56df
JH
727 if (fnmatch_icase_mem(pattern, patternlen,
728 basename, basenamelen,
729 0) == 0)
593cb880
NTND
730 return 1;
731 }
732 return 0;
733}
734
82dce998
NTND
735int match_pathname(const char *pathname, int pathlen,
736 const char *base, int baselen,
737 const char *pattern, int prefix, int patternlen,
738 int flags)
b5592632
NTND
739{
740 const char *name;
741 int namelen;
742
743 /*
744 * match with FNM_PATHNAME; the pattern has base implicitly
745 * in front of it.
746 */
747 if (*pattern == '/') {
748 pattern++;
982ac873 749 patternlen--;
b5592632
NTND
750 prefix--;
751 }
752
753 /*
754 * baselen does not count the trailing slash. base[] may or
755 * may not end with a trailing slash though.
756 */
757 if (pathlen < baselen + 1 ||
758 (baselen && pathname[baselen] != '/') ||
759 strncmp_icase(pathname, base, baselen))
760 return 0;
761
762 namelen = baselen ? pathlen - baselen - 1 : pathlen;
763 name = pathname + pathlen - namelen;
764
765 if (prefix) {
766 /*
767 * if the non-wildcard part is longer than the
768 * remaining pathname, surely it cannot match.
769 */
770 if (prefix > namelen)
771 return 0;
772
773 if (strncmp_icase(pattern, name, prefix))
774 return 0;
775 pattern += prefix;
ab3aebc1 776 patternlen -= prefix;
b5592632
NTND
777 name += prefix;
778 namelen -= prefix;
ab3aebc1
JK
779
780 /*
781 * If the whole pattern did not have a wildcard,
782 * then our prefix match is all we need; we
783 * do not need to call fnmatch at all.
784 */
785 if (!patternlen && !namelen)
786 return 1;
b5592632
NTND
787 }
788
ab3aebc1
JK
789 return fnmatch_icase_mem(pattern, patternlen,
790 name, namelen,
f30366b2 791 WM_PATHNAME) == 0;
b5592632
NTND
792}
793
578cd7c3
AS
794/*
795 * Scan the given exclude list in reverse to see whether pathname
796 * should be ignored. The first match (i.e. the last on the list), if
797 * any, determines the fate. Returns the exclude_list element which
798 * matched, or NULL for undecided.
453ec4bd 799 */
578cd7c3
AS
800static struct exclude *last_exclude_matching_from_list(const char *pathname,
801 int pathlen,
802 const char *basename,
803 int *dtype,
804 struct exclude_list *el)
453ec4bd
LT
805{
806 int i;
807
35a94d44 808 if (!el->nr)
578cd7c3 809 return NULL; /* undefined */
d6b8fc30 810
35a94d44
NTND
811 for (i = el->nr - 1; 0 <= i; i--) {
812 struct exclude *x = el->excludes[i];
b5592632 813 const char *exclude = x->pattern;
b5592632 814 int prefix = x->nowildcardlen;
35a94d44
NTND
815
816 if (x->flags & EXC_FLAG_MUSTBEDIR) {
817 if (*dtype == DT_UNKNOWN)
818 *dtype = get_dtype(NULL, pathname, pathlen);
819 if (*dtype != DT_DIR)
820 continue;
821 }
d6b8fc30 822
35a94d44 823 if (x->flags & EXC_FLAG_NODIR) {
593cb880
NTND
824 if (match_basename(basename,
825 pathlen - (basename - pathname),
826 exclude, prefix, x->patternlen,
827 x->flags))
578cd7c3 828 return x;
35a94d44
NTND
829 continue;
830 }
831
b5592632
NTND
832 assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
833 if (match_pathname(pathname, pathlen,
834 x->base, x->baselen ? x->baselen - 1 : 0,
835 exclude, prefix, x->patternlen, x->flags))
578cd7c3 836 return x;
453ec4bd 837 }
578cd7c3
AS
838 return NULL; /* undecided */
839}
840
841/*
842 * Scan the list and let the last match determine the fate.
843 * Return 1 for exclude, 0 for include and -1 for undecided.
844 */
845int is_excluded_from_list(const char *pathname,
846 int pathlen, const char *basename, int *dtype,
847 struct exclude_list *el)
848{
849 struct exclude *exclude;
850 exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
851 if (exclude)
852 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
453ec4bd
LT
853 return -1; /* undecided */
854}
855
46aa2f95
KB
856static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
857 const char *pathname, int pathlen, const char *basename,
858 int *dtype_p)
859{
860 int i, j;
861 struct exclude_list_group *group;
862 struct exclude *exclude;
863 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
864 group = &dir->exclude_list_group[i];
865 for (j = group->nr - 1; j >= 0; j--) {
866 exclude = last_exclude_matching_from_list(
867 pathname, pathlen, basename, dtype_p,
868 &group->el[j]);
869 if (exclude)
870 return exclude;
871 }
872 }
873 return NULL;
874}
875
6cd5c582
KB
876/*
877 * Loads the per-directory exclude list for the substring of base
878 * which has a char length of baselen.
879 */
880static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
881{
882 struct exclude_list_group *group;
883 struct exclude_list *el;
884 struct exclude_stack *stk = NULL;
0dcb8d7f 885 struct untracked_cache_dir *untracked;
6cd5c582
KB
886 int current;
887
6cd5c582
KB
888 group = &dir->exclude_list_group[EXC_DIRS];
889
d961baa8
NTND
890 /*
891 * Pop the exclude lists from the EXCL_DIRS exclude_list_group
6cd5c582 892 * which originate from directories not in the prefix of the
d961baa8
NTND
893 * path being checked.
894 */
6cd5c582
KB
895 while ((stk = dir->exclude_stack) != NULL) {
896 if (stk->baselen <= baselen &&
aceb9429 897 !strncmp(dir->basebuf.buf, base, stk->baselen))
6cd5c582
KB
898 break;
899 el = &group->el[dir->exclude_stack->exclude_ix];
900 dir->exclude_stack = stk->prev;
95c6f271 901 dir->exclude = NULL;
aceb9429 902 free((char *)el->src); /* see strbuf_detach() below */
6cd5c582
KB
903 clear_exclude_list(el);
904 free(stk);
905 group->nr--;
906 }
907
95c6f271
KB
908 /* Skip traversing into sub directories if the parent is excluded */
909 if (dir->exclude)
910 return;
911
aceb9429
NTND
912 /*
913 * Lazy initialization. All call sites currently just
914 * memset(dir, 0, sizeof(*dir)) before use. Changing all of
915 * them seems lots of work for little benefit.
916 */
917 if (!dir->basebuf.buf)
918 strbuf_init(&dir->basebuf, PATH_MAX);
919
6cd5c582
KB
920 /* Read from the parent directories and push them down. */
921 current = stk ? stk->baselen : -1;
aceb9429 922 strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
0dcb8d7f
NTND
923 if (dir->untracked)
924 untracked = stk ? stk->ucd : dir->untracked->root;
925 else
926 untracked = NULL;
927
6cd5c582 928 while (current < baselen) {
6cd5c582 929 const char *cp;
0dcb8d7f 930 struct sha1_stat sha1_stat;
6cd5c582 931
03e11a71 932 stk = xcalloc(1, sizeof(*stk));
6cd5c582
KB
933 if (current < 0) {
934 cp = base;
935 current = 0;
d961baa8 936 } else {
6cd5c582
KB
937 cp = strchr(base + current + 1, '/');
938 if (!cp)
939 die("oops in prep_exclude");
940 cp++;
0dcb8d7f
NTND
941 untracked =
942 lookup_untracked(dir->untracked, untracked,
943 base + current,
944 cp - base - current);
6cd5c582
KB
945 }
946 stk->prev = dir->exclude_stack;
947 stk->baselen = cp - base;
95c6f271 948 stk->exclude_ix = group->nr;
0dcb8d7f 949 stk->ucd = untracked;
95c6f271 950 el = add_exclude_list(dir, EXC_DIRS, NULL);
aceb9429
NTND
951 strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
952 assert(stk->baselen == dir->basebuf.len);
95c6f271
KB
953
954 /* Abort if the directory is excluded */
955 if (stk->baselen) {
956 int dt = DT_DIR;
aceb9429 957 dir->basebuf.buf[stk->baselen - 1] = 0;
95c6f271 958 dir->exclude = last_exclude_matching_from_lists(dir,
aceb9429
NTND
959 dir->basebuf.buf, stk->baselen - 1,
960 dir->basebuf.buf + current, &dt);
961 dir->basebuf.buf[stk->baselen - 1] = '/';
c3c327de
KB
962 if (dir->exclude &&
963 dir->exclude->flags & EXC_FLAG_NEGATIVE)
964 dir->exclude = NULL;
95c6f271 965 if (dir->exclude) {
95c6f271
KB
966 dir->exclude_stack = stk;
967 return;
968 }
969 }
970
aceb9429 971 /* Try to read per-directory file */
0dcb8d7f
NTND
972 hashclr(sha1_stat.sha1);
973 sha1_stat.valid = 0;
aceb9429 974 if (dir->exclude_per_dir) {
95c6f271
KB
975 /*
976 * dir->basebuf gets reused by the traversal, but we
977 * need fname to remain unchanged to ensure the src
978 * member of each struct exclude correctly
979 * back-references its source file. Other invocations
980 * of add_exclude_list provide stable strings, so we
aceb9429 981 * strbuf_detach() and free() here in the caller.
95c6f271 982 */
aceb9429
NTND
983 struct strbuf sb = STRBUF_INIT;
984 strbuf_addbuf(&sb, &dir->basebuf);
985 strbuf_addstr(&sb, dir->exclude_per_dir);
986 el->src = strbuf_detach(&sb, NULL);
0dcb8d7f
NTND
987 add_excludes(el->src, el->src, stk->baselen, el, 1,
988 untracked ? &sha1_stat : NULL);
989 }
990 if (untracked) {
991 hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
95c6f271 992 }
6cd5c582
KB
993 dir->exclude_stack = stk;
994 current = stk->baselen;
995 }
aceb9429 996 strbuf_setlen(&dir->basebuf, baselen);
6cd5c582
KB
997}
998
f4cd69a6
AS
999/*
1000 * Loads the exclude lists for the directory containing pathname, then
1001 * scans all exclude lists to determine whether pathname is excluded.
1002 * Returns the exclude_list element which matched, or NULL for
1003 * undecided.
1004 */
b07bc8c8 1005struct exclude *last_exclude_matching(struct dir_struct *dir,
f4cd69a6
AS
1006 const char *pathname,
1007 int *dtype_p)
453ec4bd
LT
1008{
1009 int pathlen = strlen(pathname);
68492fc7
LK
1010 const char *basename = strrchr(pathname, '/');
1011 basename = (basename) ? basename+1 : pathname;
453ec4bd 1012
63d285c8 1013 prep_exclude(dir, pathname, basename-pathname);
c082df24 1014
95c6f271
KB
1015 if (dir->exclude)
1016 return dir->exclude;
1017
46aa2f95
KB
1018 return last_exclude_matching_from_lists(dir, pathname, pathlen,
1019 basename, dtype_p);
f4cd69a6
AS
1020}
1021
1022/*
1023 * Loads the exclude lists for the directory containing pathname, then
1024 * scans all exclude lists to determine whether pathname is excluded.
1025 * Returns 1 if true, otherwise 0.
1026 */
b07bc8c8 1027int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
f4cd69a6
AS
1028{
1029 struct exclude *exclude =
1030 last_exclude_matching(dir, pathname, dtype_p);
1031 if (exclude)
1032 return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
453ec4bd
LT
1033 return 0;
1034}
1035
f3fa1838
JH
1036static struct dir_entry *dir_entry_new(const char *pathname, int len)
1037{
453ec4bd
LT
1038 struct dir_entry *ent;
1039
453ec4bd
LT
1040 ent = xmalloc(sizeof(*ent) + len + 1);
1041 ent->len = len;
1042 memcpy(ent->name, pathname, len);
1043 ent->name[len] = 0;
4d06f8ac 1044 return ent;
453ec4bd
LT
1045}
1046
159b3212 1047static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 1048{
ebbd7439 1049 if (cache_file_exists(pathname, len, ignore_case))
6815e569
JK
1050 return NULL;
1051
25fd2f7a 1052 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
1053 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1054}
1055
108da0db 1056struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 1057{
6e4f981f 1058 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
1059 return NULL;
1060
25fd2f7a 1061 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
1062 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1063}
1064
09595258
LT
1065enum exist_status {
1066 index_nonexistent = 0,
1067 index_directory,
4b05548f 1068 index_gitdir
09595258
LT
1069};
1070
5102c617 1071/*
71261027 1072 * Do not use the alphabetically sorted index to look up
5102c617 1073 * the directory name; instead, use the case insensitive
ebbd7439 1074 * directory hash.
5102c617
JJ
1075 */
1076static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
1077{
d28eec26 1078 const struct cache_entry *ce = cache_dir_exists(dirname, len);
5102c617
JJ
1079 unsigned char endchar;
1080
1081 if (!ce)
1082 return index_nonexistent;
1083 endchar = ce->name[len];
1084
1085 /*
1086 * The cache_entry structure returned will contain this dirname
1087 * and possibly additional path components.
1088 */
1089 if (endchar == '/')
1090 return index_directory;
1091
1092 /*
1093 * If there are no additional path components, then this cache_entry
1094 * represents a submodule. Submodules, despite being directories,
1095 * are stored in the cache without a closing slash.
1096 */
1097 if (!endchar && S_ISGITLINK(ce->ce_mode))
1098 return index_gitdir;
1099
1100 /* This should never be hit, but it exists just in case. */
1101 return index_nonexistent;
1102}
1103
09595258
LT
1104/*
1105 * The index sorts alphabetically by entry name, which
1106 * means that a gitlink sorts as '\0' at the end, while
1107 * a directory (which is defined not as an entry, but as
1108 * the files it contains) will sort with the '/' at the
1109 * end.
1110 */
1111static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd 1112{
5102c617
JJ
1113 int pos;
1114
1115 if (ignore_case)
1116 return directory_exists_in_index_icase(dirname, len);
1117
1118 pos = cache_name_pos(dirname, len);
09595258
LT
1119 if (pos < 0)
1120 pos = -pos-1;
1121 while (pos < active_nr) {
9c5e6c80 1122 const struct cache_entry *ce = active_cache[pos++];
09595258
LT
1123 unsigned char endchar;
1124
1125 if (strncmp(ce->name, dirname, len))
1126 break;
1127 endchar = ce->name[len];
1128 if (endchar > '/')
1129 break;
1130 if (endchar == '/')
1131 return index_directory;
7a51ed66 1132 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
1133 return index_gitdir;
1134 }
1135 return index_nonexistent;
1136}
1137
1138/*
1139 * When we find a directory when traversing the filesystem, we
1140 * have three distinct cases:
1141 *
1142 * - ignore it
1143 * - see it as a directory
1144 * - recurse into it
1145 *
1146 * and which one we choose depends on a combination of existing
1147 * git index contents and the flags passed into the directory
1148 * traversal routine.
1149 *
1150 * Case 1: If we *already* have entries in the index under that
5bd8e2d8
KB
1151 * directory name, we always recurse into the directory to see
1152 * all the files.
09595258
LT
1153 *
1154 * Case 2: If we *already* have that directory name as a gitlink,
1155 * we always continue to see it as a gitlink, regardless of whether
1156 * there is an actual git directory there or not (it might not
1157 * be checked out as a subproject!)
1158 *
1159 * Case 3: if we didn't have it in the index previously, we
1160 * have a few sub-cases:
1161 *
1162 * (a) if "show_other_directories" is true, we show it as
1163 * just a directory, unless "hide_empty_directories" is
defd7c7b
KB
1164 * also true, in which case we need to check if it contains any
1165 * untracked and / or ignored files.
09595258 1166 * (b) if it looks like a git directory, and we don't have
302b9282 1167 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
1168 * as a directory.
1169 * (c) otherwise, we recurse into it.
1170 */
defd7c7b 1171static enum path_treatment treat_directory(struct dir_struct *dir,
0dcb8d7f 1172 struct untracked_cache_dir *untracked,
721ac4ed 1173 const char *dirname, int len, int exclude,
09595258
LT
1174 const struct path_simplify *simplify)
1175{
1176 /* The "len-1" is to strip the final '/' */
1177 switch (directory_exists_in_index(dirname, len-1)) {
1178 case index_directory:
defd7c7b 1179 return path_recurse;
09595258
LT
1180
1181 case index_gitdir:
26c986e1 1182 return path_none;
09595258
LT
1183
1184 case index_nonexistent:
7c4c97c0 1185 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 1186 break;
7c4c97c0 1187 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
1188 unsigned char sha1[20];
1189 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
defd7c7b 1190 return path_untracked;
09595258 1191 }
defd7c7b 1192 return path_recurse;
09595258
LT
1193 }
1194
1195 /* This is the "show_other_directories" case */
721ac4ed 1196
184d2a8e 1197 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
defd7c7b
KB
1198 return exclude ? path_excluded : path_untracked;
1199
0dcb8d7f
NTND
1200 untracked = lookup_untracked(dir->untracked, untracked, dirname, len);
1201 return read_directory_recursive(dir, dirname, len,
1202 untracked, 1, simplify);
453ec4bd
LT
1203}
1204
9fc42d60
LT
1205/*
1206 * This is an inexact early pruning of any recursive directory
1207 * reading - if the path cannot possibly be in the pathspec,
1208 * return true, and we'll skip it early.
1209 */
1210static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
1211{
1212 if (simplify) {
1213 for (;;) {
1214 const char *match = simplify->path;
1215 int len = simplify->len;
1216
1217 if (!match)
1218 break;
1219 if (len > pathlen)
1220 len = pathlen;
1221 if (!memcmp(path, match, len))
1222 return 0;
1223 simplify++;
1224 }
1225 return 1;
1226 }
1227 return 0;
1228}
1229
29209cbe
JK
1230/*
1231 * This function tells us whether an excluded path matches a
1232 * list of "interesting" pathspecs. That is, whether a path matched
1233 * by any of the pathspecs could possibly be ignored by excluding
1234 * the specified path. This can happen if:
1235 *
1236 * 1. the path is mentioned explicitly in the pathspec
1237 *
1238 * 2. the path is a directory prefix of some element in the
1239 * pathspec
1240 */
1241static int exclude_matches_pathspec(const char *path, int len,
1242 const struct path_simplify *simplify)
e96980ef
JK
1243{
1244 if (simplify) {
1245 for (; simplify->path; simplify++) {
1246 if (len == simplify->len
1247 && !memcmp(path, simplify->path, len))
1248 return 1;
29209cbe
JK
1249 if (len < simplify->len
1250 && simplify->path[len] == '/'
1251 && !memcmp(path, simplify->path, len))
1252 return 1;
e96980ef
JK
1253 }
1254 }
1255 return 0;
1256}
1257
443e061a
LT
1258static int get_index_dtype(const char *path, int len)
1259{
1260 int pos;
9c5e6c80 1261 const struct cache_entry *ce;
443e061a 1262
ebbd7439 1263 ce = cache_file_exists(path, len, 0);
443e061a
LT
1264 if (ce) {
1265 if (!ce_uptodate(ce))
1266 return DT_UNKNOWN;
1267 if (S_ISGITLINK(ce->ce_mode))
1268 return DT_DIR;
1269 /*
1270 * Nobody actually cares about the
1271 * difference between DT_LNK and DT_REG
1272 */
1273 return DT_REG;
1274 }
1275
1276 /* Try to look it up as a directory */
1277 pos = cache_name_pos(path, len);
1278 if (pos >= 0)
1279 return DT_UNKNOWN;
1280 pos = -pos-1;
1281 while (pos < active_nr) {
1282 ce = active_cache[pos++];
1283 if (strncmp(ce->name, path, len))
1284 break;
1285 if (ce->name[len] > '/')
1286 break;
1287 if (ce->name[len] < '/')
1288 continue;
1289 if (!ce_uptodate(ce))
1290 break; /* continue? */
1291 return DT_DIR;
1292 }
1293 return DT_UNKNOWN;
1294}
1295
caa6b782 1296static int get_dtype(struct dirent *de, const char *path, int len)
07134421 1297{
6831a88a 1298 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
1299 struct stat st;
1300
1301 if (dtype != DT_UNKNOWN)
1302 return dtype;
443e061a
LT
1303 dtype = get_index_dtype(path, len);
1304 if (dtype != DT_UNKNOWN)
1305 return dtype;
1306 if (lstat(path, &st))
07134421
LT
1307 return dtype;
1308 if (S_ISREG(st.st_mode))
1309 return DT_REG;
1310 if (S_ISDIR(st.st_mode))
1311 return DT_DIR;
1312 if (S_ISLNK(st.st_mode))
1313 return DT_LNK;
1314 return dtype;
1315}
1316
16e2cfa9 1317static enum path_treatment treat_one_path(struct dir_struct *dir,
0dcb8d7f 1318 struct untracked_cache_dir *untracked,
49dc2cc2 1319 struct strbuf *path,
16e2cfa9
JH
1320 const struct path_simplify *simplify,
1321 int dtype, struct dirent *de)
53cc5356 1322{
8aaf8d77 1323 int exclude;
ebbd7439 1324 int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);
2eac2a4c 1325
8aaf8d77
KB
1326 if (dtype == DT_UNKNOWN)
1327 dtype = get_dtype(de, path->buf, path->len);
1328
1329 /* Always exclude indexed files */
2eac2a4c 1330 if (dtype != DT_DIR && has_path_in_index)
defd7c7b 1331 return path_none;
8aaf8d77 1332
2eac2a4c
JH
1333 /*
1334 * When we are looking at a directory P in the working tree,
1335 * there are three cases:
1336 *
1337 * (1) P exists in the index. Everything inside the directory P in
1338 * the working tree needs to go when P is checked out from the
1339 * index.
1340 *
1341 * (2) P does not exist in the index, but there is P/Q in the index.
1342 * We know P will stay a directory when we check out the contents
1343 * of the index, but we do not know yet if there is a directory
1344 * P/Q in the working tree to be killed, so we need to recurse.
1345 *
1346 * (3) P does not exist in the index, and there is no P/Q in the index
1347 * to require P to be a directory, either. Only in this case, we
1348 * know that everything inside P will not be killed without
1349 * recursing.
1350 */
1351 if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
1352 (dtype == DT_DIR) &&
de372b1b
ES
1353 !has_path_in_index &&
1354 (directory_exists_in_index(path->buf, path->len) == index_nonexistent))
1355 return path_none;
8aaf8d77
KB
1356
1357 exclude = is_excluded(dir, path->buf, &dtype);
53cc5356
JH
1358
1359 /*
1360 * Excluded? If we don't explicitly want to show
1361 * ignored files, ignore it
1362 */
0aaf62b6 1363 if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))
defd7c7b 1364 return path_excluded;
53cc5356 1365
53cc5356
JH
1366 switch (dtype) {
1367 default:
defd7c7b 1368 return path_none;
53cc5356 1369 case DT_DIR:
49dc2cc2 1370 strbuf_addch(path, '/');
0dcb8d7f 1371 return treat_directory(dir, untracked, path->buf, path->len, exclude,
defd7c7b 1372 simplify);
53cc5356
JH
1373 case DT_REG:
1374 case DT_LNK:
defd7c7b 1375 return exclude ? path_excluded : path_untracked;
53cc5356 1376 }
53cc5356
JH
1377}
1378
16e2cfa9 1379static enum path_treatment treat_path(struct dir_struct *dir,
0dcb8d7f 1380 struct untracked_cache_dir *untracked,
16e2cfa9 1381 struct dirent *de,
49dc2cc2 1382 struct strbuf *path,
16e2cfa9 1383 int baselen,
49dc2cc2 1384 const struct path_simplify *simplify)
16e2cfa9
JH
1385{
1386 int dtype;
1387
1388 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
defd7c7b 1389 return path_none;
49dc2cc2
RS
1390 strbuf_setlen(path, baselen);
1391 strbuf_addstr(path, de->d_name);
1392 if (simplify_away(path->buf, path->len, simplify))
defd7c7b 1393 return path_none;
16e2cfa9
JH
1394
1395 dtype = DTYPE(de);
0dcb8d7f
NTND
1396 return treat_one_path(dir, untracked, path, simplify, dtype, de);
1397}
1398
1399static void add_untracked(struct untracked_cache_dir *dir, const char *name)
1400{
1401 if (!dir)
1402 return;
1403 ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,
1404 dir->untracked_alloc);
1405 dir->untracked[dir->untracked_nr++] = xstrdup(name);
16e2cfa9
JH
1406}
1407
453ec4bd
LT
1408/*
1409 * Read a directory tree. We currently ignore anything but
1410 * directories, regular files and symlinks. That's because git
1411 * doesn't handle them at all yet. Maybe that will change some
1412 * day.
1413 *
1414 * Also, we ignore the name ".git" (even if it is not a directory).
1415 * That likely will not change.
defd7c7b
KB
1416 *
1417 * Returns the most significant path_treatment value encountered in the scan.
453ec4bd 1418 */
defd7c7b 1419static enum path_treatment read_directory_recursive(struct dir_struct *dir,
53cc5356 1420 const char *base, int baselen,
0dcb8d7f 1421 struct untracked_cache_dir *untracked, int check_only,
53cc5356 1422 const struct path_simplify *simplify)
453ec4bd 1423{
1528d247 1424 DIR *fdir;
defd7c7b 1425 enum path_treatment state, subdir_state, dir_state = path_none;
02cb6753 1426 struct dirent *de;
bef36921 1427 struct strbuf path = STRBUF_INIT;
453ec4bd 1428
bef36921 1429 strbuf_add(&path, base, baselen);
02cb6753 1430
1528d247
RS
1431 fdir = opendir(path.len ? path.buf : ".");
1432 if (!fdir)
1433 goto out;
1434
0dcb8d7f
NTND
1435 if (untracked)
1436 untracked->check_only = !!check_only;
1437
02cb6753 1438 while ((de = readdir(fdir)) != NULL) {
defd7c7b 1439 /* check how the file or directory should be treated */
0dcb8d7f
NTND
1440 state = treat_path(dir, untracked, de, &path, baselen, simplify);
1441
defd7c7b
KB
1442 if (state > dir_state)
1443 dir_state = state;
1444
1445 /* recurse into subdir if instructed by treat_path */
1446 if (state == path_recurse) {
0dcb8d7f
NTND
1447 struct untracked_cache_dir *ud;
1448 ud = lookup_untracked(dir->untracked, untracked,
1449 path.buf + baselen,
1450 path.len - baselen);
1451 subdir_state =
1452 read_directory_recursive(dir, path.buf, path.len,
1453 ud, check_only, simplify);
defd7c7b
KB
1454 if (subdir_state > dir_state)
1455 dir_state = subdir_state;
1456 }
1457
1458 if (check_only) {
1459 /* abort early if maximum state has been reached */
0dcb8d7f
NTND
1460 if (dir_state == path_untracked) {
1461 if (untracked)
1462 add_untracked(untracked, path.buf + baselen);
defd7c7b 1463 break;
0dcb8d7f 1464 }
defd7c7b 1465 /* skip the dir_add_* part */
02cb6753 1466 continue;
453ec4bd 1467 }
defd7c7b
KB
1468
1469 /* add the path to the appropriate result list */
1470 switch (state) {
1471 case path_excluded:
1472 if (dir->flags & DIR_SHOW_IGNORED)
1473 dir_add_name(dir, path.buf, path.len);
0aaf62b6
KB
1474 else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||
1475 ((dir->flags & DIR_COLLECT_IGNORED) &&
1476 exclude_matches_pathspec(path.buf, path.len,
1477 simplify)))
1478 dir_add_ignored(dir, path.buf, path.len);
defd7c7b
KB
1479 break;
1480
1481 case path_untracked:
0dcb8d7f
NTND
1482 if (dir->flags & DIR_SHOW_IGNORED)
1483 break;
1484 dir_add_name(dir, path.buf, path.len);
1485 if (untracked)
1486 add_untracked(untracked, path.buf + baselen);
1528d247 1487 break;
defd7c7b
KB
1488
1489 default:
1490 break;
1491 }
453ec4bd 1492 }
02cb6753 1493 closedir(fdir);
1528d247 1494 out:
bef36921 1495 strbuf_release(&path);
453ec4bd 1496
defd7c7b 1497 return dir_state;
453ec4bd
LT
1498}
1499
1500static int cmp_name(const void *p1, const void *p2)
1501{
1502 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1503 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1504
ccdd4a0f 1505 return name_compare(e1->name, e1->len, e2->name, e2->len);
453ec4bd
LT
1506}
1507
9fc42d60
LT
1508static struct path_simplify *create_simplify(const char **pathspec)
1509{
1510 int nr, alloc = 0;
1511 struct path_simplify *simplify = NULL;
1512
1513 if (!pathspec)
1514 return NULL;
1515
1516 for (nr = 0 ; ; nr++) {
1517 const char *match;
9af49f82 1518 ALLOC_GROW(simplify, nr + 1, alloc);
9fc42d60
LT
1519 match = *pathspec++;
1520 if (!match)
1521 break;
1522 simplify[nr].path = match;
1523 simplify[nr].len = simple_length(match);
1524 }
1525 simplify[nr].path = NULL;
1526 simplify[nr].len = 0;
1527 return simplify;
1528}
1529
1530static void free_simplify(struct path_simplify *simplify)
1531{
8e0f7003 1532 free(simplify);
9fc42d60
LT
1533}
1534
48ffef96
JH
1535static int treat_leading_path(struct dir_struct *dir,
1536 const char *path, int len,
1537 const struct path_simplify *simplify)
1538{
49dc2cc2
RS
1539 struct strbuf sb = STRBUF_INIT;
1540 int baselen, rc = 0;
48ffef96 1541 const char *cp;
be8a84c5 1542 int old_flags = dir->flags;
48ffef96
JH
1543
1544 while (len && path[len - 1] == '/')
1545 len--;
1546 if (!len)
1547 return 1;
1548 baselen = 0;
be8a84c5 1549 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
48ffef96
JH
1550 while (1) {
1551 cp = path + baselen + !!baselen;
1552 cp = memchr(cp, '/', path + len - cp);
1553 if (!cp)
1554 baselen = len;
1555 else
1556 baselen = cp - path;
49dc2cc2
RS
1557 strbuf_setlen(&sb, 0);
1558 strbuf_add(&sb, path, baselen);
1559 if (!is_directory(sb.buf))
1560 break;
1561 if (simplify_away(sb.buf, sb.len, simplify))
1562 break;
0dcb8d7f 1563 if (treat_one_path(dir, NULL, &sb, simplify,
defd7c7b 1564 DT_DIR, NULL) == path_none)
49dc2cc2
RS
1565 break; /* do not recurse into it */
1566 if (len <= baselen) {
1567 rc = 1;
1568 break; /* finished checking */
1569 }
48ffef96 1570 }
49dc2cc2 1571 strbuf_release(&sb);
be8a84c5 1572 dir->flags = old_flags;
49dc2cc2 1573 return rc;
48ffef96
JH
1574}
1575
7327d3d1 1576int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)
9fc42d60 1577{
725b0605 1578 struct path_simplify *simplify;
b4189aa8 1579
7327d3d1
NTND
1580 /*
1581 * Check out create_simplify()
1582 */
1583 if (pathspec)
5c6933d2
NTND
1584 GUARD_PATHSPEC(pathspec,
1585 PATHSPEC_FROMTOP |
1586 PATHSPEC_MAXDEPTH |
bd30c2e4 1587 PATHSPEC_LITERAL |
93d93537 1588 PATHSPEC_GLOB |
ef79b1f8
NTND
1589 PATHSPEC_ICASE |
1590 PATHSPEC_EXCLUDE);
7327d3d1 1591
dba2e203 1592 if (has_symlink_leading_path(path, len))
725b0605
JH
1593 return dir->nr;
1594
ef79b1f8
NTND
1595 /*
1596 * exclude patterns are treated like positive ones in
1597 * create_simplify. Usually exclude patterns should be a
1598 * subset of positive ones, which has no impacts on
1599 * create_simplify().
1600 */
b3920bbd 1601 simplify = create_simplify(pathspec ? pathspec->_raw : NULL);
48ffef96 1602 if (!len || treat_leading_path(dir, path, len, simplify))
0dcb8d7f
NTND
1603 read_directory_recursive(dir, path, len,
1604 dir->untracked ? dir->untracked->root : NULL,
1605 0, simplify);
9fc42d60 1606 free_simplify(simplify);
453ec4bd 1607 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 1608 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
1609 return dir->nr;
1610}
c91f0d92 1611
686a4a06 1612int file_exists(const char *f)
c91f0d92 1613{
686a4a06 1614 struct stat sb;
a50f9fc5 1615 return lstat(f, &sb) == 0;
c91f0d92 1616}
e6636747
JS
1617
1618/*
9b125da4
NTND
1619 * Given two normalized paths (a trailing slash is ok), if subdir is
1620 * outside dir, return -1. Otherwise return the offset in subdir that
1621 * can be used as relative path to dir.
e6636747 1622 */
9b125da4 1623int dir_inside_of(const char *subdir, const char *dir)
e6636747 1624{
9b125da4 1625 int offset = 0;
e6636747 1626
9b125da4 1627 assert(dir && subdir && *dir && *subdir);
e6636747 1628
9b125da4 1629 while (*dir && *subdir && *dir == *subdir) {
e6636747 1630 dir++;
9b125da4
NTND
1631 subdir++;
1632 offset++;
490544b1 1633 }
9b125da4
NTND
1634
1635 /* hel[p]/me vs hel[l]/yeah */
1636 if (*dir && *subdir)
1637 return -1;
1638
1639 if (!*subdir)
1640 return !*dir ? offset : -1; /* same dir */
1641
1642 /* foo/[b]ar vs foo/[] */
1643 if (is_dir_sep(dir[-1]))
1644 return is_dir_sep(subdir[-1]) ? offset : -1;
1645
1646 /* foo[/]bar vs foo[] */
1647 return is_dir_sep(*subdir) ? offset + 1 : -1;
e6636747
JS
1648}
1649
1650int is_inside_dir(const char *dir)
1651{
56b9f6e7
RS
1652 char *cwd;
1653 int rc;
1654
b892913d
NTND
1655 if (!dir)
1656 return 0;
56b9f6e7
RS
1657
1658 cwd = xgetcwd();
1659 rc = (dir_inside_of(cwd, dir) >= 0);
1660 free(cwd);
1661 return rc;
e6636747 1662}
7155b727 1663
55892d23
AP
1664int is_empty_dir(const char *path)
1665{
1666 DIR *dir = opendir(path);
1667 struct dirent *e;
1668 int ret = 1;
1669
1670 if (!dir)
1671 return 0;
1672
1673 while ((e = readdir(dir)) != NULL)
1674 if (!is_dot_or_dotdot(e->d_name)) {
1675 ret = 0;
1676 break;
1677 }
1678
1679 closedir(dir);
1680 return ret;
1681}
1682
ae2f203e 1683static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
7155b727 1684{
a0f4afbe 1685 DIR *dir;
7155b727 1686 struct dirent *e;
ae2f203e 1687 int ret = 0, original_len = path->len, len, kept_down = 0;
a0f4afbe 1688 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
c844a803 1689 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
a0f4afbe 1690 unsigned char submodule_head[20];
7155b727 1691
a0f4afbe 1692 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
ae2f203e 1693 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
a0f4afbe 1694 /* Do not descend and nuke a nested git work tree. */
ae2f203e
JH
1695 if (kept_up)
1696 *kept_up = 1;
a0f4afbe 1697 return 0;
ae2f203e 1698 }
a0f4afbe 1699
ae2f203e 1700 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
a0f4afbe 1701 dir = opendir(path->buf);
c844a803 1702 if (!dir) {
863808cd
MH
1703 if (errno == ENOENT)
1704 return keep_toplevel ? -1 : 0;
1705 else if (errno == EACCES && !keep_toplevel)
ecb2c282
MH
1706 /*
1707 * An empty dir could be removable even if it
1708 * is unreadable:
1709 */
c844a803
JH
1710 return rmdir(path->buf);
1711 else
1712 return -1;
1713 }
7155b727
JS
1714 if (path->buf[original_len - 1] != '/')
1715 strbuf_addch(path, '/');
1716
1717 len = path->len;
1718 while ((e = readdir(dir)) != NULL) {
1719 struct stat st;
8ca12c0d
AP
1720 if (is_dot_or_dotdot(e->d_name))
1721 continue;
7155b727
JS
1722
1723 strbuf_setlen(path, len);
1724 strbuf_addstr(path, e->d_name);
863808cd
MH
1725 if (lstat(path->buf, &st)) {
1726 if (errno == ENOENT)
1727 /*
1728 * file disappeared, which is what we
1729 * wanted anyway
1730 */
1731 continue;
1732 /* fall thru */
1733 } else if (S_ISDIR(st.st_mode)) {
ae2f203e 1734 if (!remove_dir_recurse(path, flag, &kept_down))
7155b727 1735 continue; /* happy */
863808cd
MH
1736 } else if (!only_empty &&
1737 (!unlink(path->buf) || errno == ENOENT)) {
7155b727 1738 continue; /* happy, too */
863808cd 1739 }
7155b727
JS
1740
1741 /* path too long, stat fails, or non-directory still exists */
1742 ret = -1;
1743 break;
1744 }
1745 closedir(dir);
1746
1747 strbuf_setlen(path, original_len);
ae2f203e 1748 if (!ret && !keep_toplevel && !kept_down)
863808cd 1749 ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
ae2f203e
JH
1750 else if (kept_up)
1751 /*
1752 * report the uplevel that it is not an error that we
1753 * did not rmdir() our directory.
1754 */
1755 *kept_up = !ret;
7155b727
JS
1756 return ret;
1757}
039bc64e 1758
ae2f203e
JH
1759int remove_dir_recursively(struct strbuf *path, int flag)
1760{
1761 return remove_dir_recurse(path, flag, NULL);
1762}
1763
039bc64e
JH
1764void setup_standard_excludes(struct dir_struct *dir)
1765{
1766 const char *path;
dc79687e 1767 char *xdg_path;
039bc64e
JH
1768
1769 dir->exclude_per_dir = ".gitignore";
1770 path = git_path("info/exclude");
dc79687e
HKNN
1771 if (!excludes_file) {
1772 home_config_paths(NULL, &xdg_path, "ignore");
1773 excludes_file = xdg_path;
1774 }
4698c8fe 1775 if (!access_or_warn(path, R_OK, 0))
0dcb8d7f
NTND
1776 add_excludes_from_file_1(dir, path,
1777 dir->untracked ? &dir->ss_info_exclude : NULL);
4698c8fe 1778 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
0dcb8d7f
NTND
1779 add_excludes_from_file_1(dir, excludes_file,
1780 dir->untracked ? &dir->ss_excludes_file : NULL);
039bc64e 1781}
4a92d1bf
AR
1782
1783int remove_path(const char *name)
1784{
1785 char *slash;
1786
9a6728d4 1787 if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
4a92d1bf
AR
1788 return -1;
1789
1790 slash = strrchr(name, '/');
1791 if (slash) {
1792 char *dirs = xstrdup(name);
1793 slash = dirs + (slash - name);
1794 do {
1795 *slash = '\0';
3fc0d131 1796 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
4a92d1bf
AR
1797 free(dirs);
1798 }
1799 return 0;
1800}
1801
270be816
AS
1802/*
1803 * Frees memory within dir which was allocated for exclude lists and
1804 * the exclude_stack. Does not free dir itself.
1805 */
1806void clear_directory(struct dir_struct *dir)
1807{
1808 int i, j;
1809 struct exclude_list_group *group;
1810 struct exclude_list *el;
1811 struct exclude_stack *stk;
1812
1813 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1814 group = &dir->exclude_list_group[i];
1815 for (j = 0; j < group->nr; j++) {
1816 el = &group->el[j];
1817 if (i == EXC_DIRS)
1818 free((char *)el->src);
1819 clear_exclude_list(el);
1820 }
1821 free(group->el);
1822 }
1823
1824 stk = dir->exclude_stack;
1825 while (stk) {
1826 struct exclude_stack *prev = stk->prev;
1827 free(stk);
1828 stk = prev;
1829 }
aceb9429 1830 strbuf_release(&dir->basebuf);
270be816 1831}