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