]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
tree_entry_interesting(): optimize wildcard matching when base is matched
[thirdparty/git.git] / dir.c
CommitLineData
453ec4bd
LT
1/*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
4 *
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
453ec4bd
LT
8#include "cache.h"
9#include "dir.h"
09595258 10#include "refs.h"
453ec4bd 11
9fc42d60
LT
12struct path_simplify {
13 int len;
14 const char *path;
15};
16
dba2e203 17static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
09595258 18 int check_only, const struct path_simplify *simplify);
caa6b782 19static int get_dtype(struct dirent *de, const char *path, int len);
09595258 20
8cf2a84e
JJ
21/* helper string functions with support for the ignore_case flag */
22int strcmp_icase(const char *a, const char *b)
23{
24 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
25}
26
27int strncmp_icase(const char *a, const char *b, size_t count)
28{
29 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
30}
31
32int fnmatch_icase(const char *pattern, const char *string, int flags)
33{
34 return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
35}
36
1d8842d9 37static int common_prefix(const char **pathspec)
3c6a370b
LT
38{
39 const char *path, *slash, *next;
40 int prefix;
41
42 if (!pathspec)
43 return 0;
44
45 path = *pathspec;
46 slash = strrchr(path, '/');
47 if (!slash)
48 return 0;
49
42f9852f
JH
50 /*
51 * The first 'prefix' characters of 'path' are common leading
52 * path components among the pathspecs we have seen so far,
53 * including the trailing slash.
54 */
3c6a370b
LT
55 prefix = slash - path + 1;
56 while ((next = *++pathspec) != NULL) {
42f9852f
JH
57 int len, last_matching_slash = -1;
58 for (len = 0; len < prefix && next[len] == path[len]; len++)
59 if (next[len] == '/')
60 last_matching_slash = len;
61 if (len == prefix)
3c6a370b 62 continue;
42f9852f
JH
63 if (last_matching_slash < 0)
64 return 0;
65 prefix = last_matching_slash + 1;
3c6a370b
LT
66 }
67 return prefix;
68}
69
1d8842d9
LT
70int fill_directory(struct dir_struct *dir, const char **pathspec)
71{
dba2e203
LT
72 const char *path;
73 int len;
1d8842d9
LT
74
75 /*
76 * Calculate common prefix for the pathspec, and
77 * use that to optimize the directory walk
78 */
dba2e203 79 len = common_prefix(pathspec);
1d8842d9 80 path = "";
1d8842d9 81
dba2e203
LT
82 if (len)
83 path = xmemdupz(*pathspec, len);
1d8842d9
LT
84
85 /* Read the directory and prune it */
dba2e203
LT
86 read_directory(dir, path, len, pathspec);
87 return len;
1d8842d9
LT
88}
89
bc96cc87
NTND
90int within_depth(const char *name, int namelen,
91 int depth, int max_depth)
92{
93 const char *cp = name, *cpe = name + namelen;
94
95 while (cp < cpe) {
96 if (*cp++ != '/')
97 continue;
98 depth++;
99 if (depth > max_depth)
100 return 0;
101 }
102 return 1;
103}
104
e813d50e 105/*
2c5b0115 106 * Does 'match' match the given name?
e813d50e
JH
107 * A match is found if
108 *
109 * (1) the 'match' string is leading directory of 'name', or
110 * (2) the 'match' string is a wildcard and matches 'name', or
111 * (3) the 'match' string is exactly the same as 'name'.
112 *
113 * and the return value tells which case it was.
114 *
115 * It returns 0 when there is no match.
116 */
3c6a370b
LT
117static int match_one(const char *match, const char *name, int namelen)
118{
119 int matchlen;
120
121 /* If the match was just the prefix, we matched */
88ea8112 122 if (!*match)
e813d50e 123 return MATCHED_RECURSIVELY;
3c6a370b 124
21444f18
JJ
125 if (ignore_case) {
126 for (;;) {
127 unsigned char c1 = tolower(*match);
128 unsigned char c2 = tolower(*name);
129 if (c1 == '\0' || is_glob_special(c1))
130 break;
131 if (c1 != c2)
132 return 0;
133 match++;
134 name++;
135 namelen--;
136 }
137 } else {
138 for (;;) {
139 unsigned char c1 = *match;
140 unsigned char c2 = *name;
141 if (c1 == '\0' || is_glob_special(c1))
142 break;
143 if (c1 != c2)
144 return 0;
145 match++;
146 name++;
147 namelen--;
148 }
88ea8112
LT
149 }
150
151
3c6a370b
LT
152 /*
153 * If we don't match the matchstring exactly,
154 * we need to match by fnmatch
155 */
88ea8112 156 matchlen = strlen(match);
21444f18
JJ
157 if (strncmp_icase(match, name, matchlen))
158 return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
3c6a370b 159
f2d0df71 160 if (namelen == matchlen)
e813d50e
JH
161 return MATCHED_EXACTLY;
162 if (match[matchlen-1] == '/' || name[matchlen] == '/')
163 return MATCHED_RECURSIVELY;
164 return 0;
3c6a370b
LT
165}
166
e813d50e
JH
167/*
168 * Given a name and a list of pathspecs, see if the name matches
169 * any of the pathspecs. The caller is also interested in seeing
170 * all pathspec matches some names it calls this function with
171 * (otherwise the user could have mistyped the unmatched pathspec),
172 * and a mark is left in seen[] array for pathspec element that
173 * actually matched anything.
174 */
0b50922a
CB
175int match_pathspec(const char **pathspec, const char *name, int namelen,
176 int prefix, char *seen)
3c6a370b 177{
0b50922a
CB
178 int i, retval = 0;
179
180 if (!pathspec)
181 return 1;
3c6a370b
LT
182
183 name += prefix;
184 namelen -= prefix;
185
0b50922a 186 for (i = 0; pathspec[i] != NULL; i++) {
e813d50e 187 int how;
0b50922a
CB
188 const char *match = pathspec[i] + prefix;
189 if (seen && seen[i] == MATCHED_EXACTLY)
3c6a370b 190 continue;
e813d50e
JH
191 how = match_one(match, name, namelen);
192 if (how) {
193 if (retval < how)
194 retval = how;
0b50922a
CB
195 if (seen && seen[i] < how)
196 seen[i] = how;
3c6a370b
LT
197 }
198 }
199 return retval;
200}
201
68492fc7
LK
202static int no_wildcard(const char *string)
203{
dd482eea 204 return string[strcspn(string, "*?[{\\")] == '\0';
68492fc7
LK
205}
206
453ec4bd
LT
207void add_exclude(const char *string, const char *base,
208 int baselen, struct exclude_list *which)
209{
d6b8fc30
JH
210 struct exclude *x;
211 size_t len;
212 int to_exclude = 1;
213 int flags = 0;
453ec4bd 214
68492fc7 215 if (*string == '!') {
d6b8fc30 216 to_exclude = 0;
68492fc7
LK
217 string++;
218 }
d6b8fc30
JH
219 len = strlen(string);
220 if (len && string[len - 1] == '/') {
221 char *s;
222 x = xmalloc(sizeof(*x) + len);
4b25d091 223 s = (char *)(x+1);
d6b8fc30
JH
224 memcpy(s, string, len - 1);
225 s[len - 1] = '\0';
226 string = s;
227 x->pattern = s;
228 flags = EXC_FLAG_MUSTBEDIR;
229 } else {
230 x = xmalloc(sizeof(*x));
231 x->pattern = string;
232 }
233 x->to_exclude = to_exclude;
68492fc7 234 x->patternlen = strlen(string);
453ec4bd
LT
235 x->base = base;
236 x->baselen = baselen;
d6b8fc30 237 x->flags = flags;
68492fc7
LK
238 if (!strchr(string, '/'))
239 x->flags |= EXC_FLAG_NODIR;
240 if (no_wildcard(string))
241 x->flags |= EXC_FLAG_NOWILDCARD;
242 if (*string == '*' && no_wildcard(string+1))
243 x->flags |= EXC_FLAG_ENDSWITH;
686a4a06 244 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
453ec4bd
LT
245 which->excludes[which->nr++] = x;
246}
247
c28b3d6e
NTND
248static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
249{
250 int pos, len;
251 unsigned long sz;
252 enum object_type type;
253 void *data;
254 struct index_state *istate = &the_index;
255
256 len = strlen(path);
257 pos = index_name_pos(istate, path, len);
258 if (pos < 0)
259 return NULL;
260 if (!ce_skip_worktree(istate->cache[pos]))
261 return NULL;
262 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
263 if (!data || type != OBJ_BLOB) {
264 free(data);
265 return NULL;
266 }
267 *size = xsize_t(sz);
268 return data;
269}
270
0fd0e241
NTND
271void free_excludes(struct exclude_list *el)
272{
273 int i;
274
275 for (i = 0; i < el->nr; i++)
276 free(el->excludes[i]);
277 free(el->excludes);
278
279 el->nr = 0;
280 el->excludes = NULL;
281}
282
cb097534
NTND
283int add_excludes_from_file_to_list(const char *fname,
284 const char *base,
285 int baselen,
286 char **buf_p,
287 struct exclude_list *which,
288 int check_index)
453ec4bd 289{
c470701a 290 struct stat st;
453ec4bd 291 int fd, i;
9d14017a 292 size_t size = 0;
453ec4bd
LT
293 char *buf, *entry;
294
295 fd = open(fname, O_RDONLY);
c28b3d6e
NTND
296 if (fd < 0 || fstat(fd, &st) < 0) {
297 if (0 <= fd)
298 close(fd);
299 if (!check_index ||
300 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
301 return -1;
45d76f17
NTND
302 if (size == 0) {
303 free(buf);
304 return 0;
305 }
306 if (buf[size-1] != '\n') {
307 buf = xrealloc(buf, size+1);
308 buf[size++] = '\n';
309 }
453ec4bd 310 }
c28b3d6e
NTND
311 else {
312 size = xsize_t(st.st_size);
313 if (size == 0) {
314 close(fd);
315 return 0;
316 }
45d76f17 317 buf = xmalloc(size+1);
c28b3d6e 318 if (read_in_full(fd, buf, size) != size) {
45d76f17 319 free(buf);
c28b3d6e
NTND
320 close(fd);
321 return -1;
322 }
45d76f17 323 buf[size++] = '\n';
c28b3d6e 324 close(fd);
6ba78238 325 }
453ec4bd 326
63d285c8
JH
327 if (buf_p)
328 *buf_p = buf;
453ec4bd 329 entry = buf;
45d76f17
NTND
330 for (i = 0; i < size; i++) {
331 if (buf[i] == '\n') {
453ec4bd
LT
332 if (entry != buf + i && entry[0] != '#') {
333 buf[i - (i && buf[i-1] == '\r')] = 0;
334 add_exclude(entry, base, baselen, which);
335 }
336 entry = buf + i + 1;
337 }
338 }
339 return 0;
453ec4bd
LT
340}
341
342void add_excludes_from_file(struct dir_struct *dir, const char *fname)
343{
cb097534
NTND
344 if (add_excludes_from_file_to_list(fname, "", 0, NULL,
345 &dir->exclude_list[EXC_FILE], 0) < 0)
453ec4bd
LT
346 die("cannot use %s as an exclude file", fname);
347}
348
63d285c8 349static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 350{
63d285c8
JH
351 struct exclude_list *el;
352 struct exclude_stack *stk = NULL;
353 int current;
354
355 if ((!dir->exclude_per_dir) ||
356 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
357 return; /* too long a path -- ignore */
358
359 /* Pop the ones that are not the prefix of the path being checked. */
360 el = &dir->exclude_list[EXC_DIRS];
361 while ((stk = dir->exclude_stack) != NULL) {
362 if (stk->baselen <= baselen &&
363 !strncmp(dir->basebuf, base, stk->baselen))
364 break;
365 dir->exclude_stack = stk->prev;
366 while (stk->exclude_ix < el->nr)
367 free(el->excludes[--el->nr]);
368 free(stk->filebuf);
369 free(stk);
453ec4bd 370 }
453ec4bd 371
63d285c8
JH
372 /* Read from the parent directories and push them down. */
373 current = stk ? stk->baselen : -1;
374 while (current < baselen) {
375 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
376 const char *cp;
453ec4bd 377
63d285c8
JH
378 if (current < 0) {
379 cp = base;
380 current = 0;
381 }
382 else {
383 cp = strchr(base + current + 1, '/');
384 if (!cp)
385 die("oops in prep_exclude");
386 cp++;
387 }
388 stk->prev = dir->exclude_stack;
389 stk->baselen = cp - base;
390 stk->exclude_ix = el->nr;
391 memcpy(dir->basebuf + current, base + current,
392 stk->baselen - current);
393 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
cb097534
NTND
394 add_excludes_from_file_to_list(dir->basebuf,
395 dir->basebuf, stk->baselen,
396 &stk->filebuf, el, 1);
63d285c8
JH
397 dir->exclude_stack = stk;
398 current = stk->baselen;
399 }
400 dir->basebuf[baselen] = '\0';
453ec4bd
LT
401}
402
2c5b0115 403/* Scan the list and let the last match determine the fate.
453ec4bd
LT
404 * Return 1 for exclude, 0 for include and -1 for undecided.
405 */
cb097534
NTND
406int excluded_from_list(const char *pathname,
407 int pathlen, const char *basename, int *dtype,
408 struct exclude_list *el)
453ec4bd
LT
409{
410 int i;
411
412 if (el->nr) {
413 for (i = el->nr - 1; 0 <= i; i--) {
414 struct exclude *x = el->excludes[i];
415 const char *exclude = x->pattern;
68492fc7 416 int to_exclude = x->to_exclude;
453ec4bd 417
6831a88a
JH
418 if (x->flags & EXC_FLAG_MUSTBEDIR) {
419 if (*dtype == DT_UNKNOWN)
caa6b782 420 *dtype = get_dtype(NULL, pathname, pathlen);
6831a88a
JH
421 if (*dtype != DT_DIR)
422 continue;
423 }
d6b8fc30 424
68492fc7 425 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 426 /* match basename */
68492fc7 427 if (x->flags & EXC_FLAG_NOWILDCARD) {
10d4b02b 428 if (!strcmp_icase(exclude, basename))
68492fc7
LK
429 return to_exclude;
430 } else if (x->flags & EXC_FLAG_ENDSWITH) {
431 if (x->patternlen - 1 <= pathlen &&
10d4b02b 432 !strcmp_icase(exclude + 1, pathname + pathlen - x->patternlen + 1))
68492fc7
LK
433 return to_exclude;
434 } else {
10d4b02b 435 if (fnmatch_icase(exclude, basename, 0) == 0)
68492fc7
LK
436 return to_exclude;
437 }
453ec4bd
LT
438 }
439 else {
440 /* match with FNM_PATHNAME:
441 * exclude has base (baselen long) implicitly
442 * in front of it.
443 */
444 int baselen = x->baselen;
445 if (*exclude == '/')
446 exclude++;
447
448 if (pathlen < baselen ||
449 (baselen && pathname[baselen-1] != '/') ||
10d4b02b 450 strncmp_icase(pathname, x->base, baselen))
453ec4bd
LT
451 continue;
452
68492fc7 453 if (x->flags & EXC_FLAG_NOWILDCARD) {
10d4b02b 454 if (!strcmp_icase(exclude, pathname + baselen))
68492fc7
LK
455 return to_exclude;
456 } else {
10d4b02b 457 if (fnmatch_icase(exclude, pathname+baselen,
68492fc7
LK
458 FNM_PATHNAME) == 0)
459 return to_exclude;
460 }
453ec4bd
LT
461 }
462 }
463 }
464 return -1; /* undecided */
465}
466
6831a88a 467int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
468{
469 int pathlen = strlen(pathname);
470 int st;
68492fc7
LK
471 const char *basename = strrchr(pathname, '/');
472 basename = (basename) ? basename+1 : pathname;
453ec4bd 473
63d285c8 474 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 475 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
cb097534
NTND
476 switch (excluded_from_list(pathname, pathlen, basename,
477 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
478 case 0:
479 return 0;
480 case 1:
481 return 1;
482 }
483 }
484 return 0;
485}
486
f3fa1838
JH
487static struct dir_entry *dir_entry_new(const char *pathname, int len)
488{
453ec4bd
LT
489 struct dir_entry *ent;
490
453ec4bd
LT
491 ent = xmalloc(sizeof(*ent) + len + 1);
492 ent->len = len;
493 memcpy(ent->name, pathname, len);
494 ent->name[len] = 0;
4d06f8ac 495 return ent;
453ec4bd
LT
496}
497
159b3212 498static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 499{
0a9b88b7 500 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
501 return NULL;
502
25fd2f7a 503 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
504 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
505}
506
108da0db 507struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 508{
6e4f981f 509 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
510 return NULL;
511
25fd2f7a 512 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
513 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
514}
515
09595258
LT
516enum exist_status {
517 index_nonexistent = 0,
518 index_directory,
4b05548f 519 index_gitdir
09595258
LT
520};
521
5102c617
JJ
522/*
523 * Do not use the alphabetically stored index to look up
524 * the directory name; instead, use the case insensitive
525 * name hash.
526 */
527static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
528{
529 struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
530 unsigned char endchar;
531
532 if (!ce)
533 return index_nonexistent;
534 endchar = ce->name[len];
535
536 /*
537 * The cache_entry structure returned will contain this dirname
538 * and possibly additional path components.
539 */
540 if (endchar == '/')
541 return index_directory;
542
543 /*
544 * If there are no additional path components, then this cache_entry
545 * represents a submodule. Submodules, despite being directories,
546 * are stored in the cache without a closing slash.
547 */
548 if (!endchar && S_ISGITLINK(ce->ce_mode))
549 return index_gitdir;
550
551 /* This should never be hit, but it exists just in case. */
552 return index_nonexistent;
553}
554
09595258
LT
555/*
556 * The index sorts alphabetically by entry name, which
557 * means that a gitlink sorts as '\0' at the end, while
558 * a directory (which is defined not as an entry, but as
559 * the files it contains) will sort with the '/' at the
560 * end.
561 */
562static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd 563{
5102c617
JJ
564 int pos;
565
566 if (ignore_case)
567 return directory_exists_in_index_icase(dirname, len);
568
569 pos = cache_name_pos(dirname, len);
09595258
LT
570 if (pos < 0)
571 pos = -pos-1;
572 while (pos < active_nr) {
573 struct cache_entry *ce = active_cache[pos++];
574 unsigned char endchar;
575
576 if (strncmp(ce->name, dirname, len))
577 break;
578 endchar = ce->name[len];
579 if (endchar > '/')
580 break;
581 if (endchar == '/')
582 return index_directory;
7a51ed66 583 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
584 return index_gitdir;
585 }
586 return index_nonexistent;
587}
588
589/*
590 * When we find a directory when traversing the filesystem, we
591 * have three distinct cases:
592 *
593 * - ignore it
594 * - see it as a directory
595 * - recurse into it
596 *
597 * and which one we choose depends on a combination of existing
598 * git index contents and the flags passed into the directory
599 * traversal routine.
600 *
601 * Case 1: If we *already* have entries in the index under that
602 * directory name, we always recurse into the directory to see
603 * all the files.
604 *
605 * Case 2: If we *already* have that directory name as a gitlink,
606 * we always continue to see it as a gitlink, regardless of whether
607 * there is an actual git directory there or not (it might not
608 * be checked out as a subproject!)
609 *
610 * Case 3: if we didn't have it in the index previously, we
611 * have a few sub-cases:
612 *
613 * (a) if "show_other_directories" is true, we show it as
614 * just a directory, unless "hide_empty_directories" is
615 * also true and the directory is empty, in which case
616 * we just ignore it entirely.
617 * (b) if it looks like a git directory, and we don't have
302b9282 618 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
619 * as a directory.
620 * (c) otherwise, we recurse into it.
621 */
622enum directory_treatment {
623 show_directory,
624 ignore_directory,
4b05548f 625 recurse_into_directory
09595258
LT
626};
627
628static enum directory_treatment treat_directory(struct dir_struct *dir,
629 const char *dirname, int len,
630 const struct path_simplify *simplify)
631{
632 /* The "len-1" is to strip the final '/' */
633 switch (directory_exists_in_index(dirname, len-1)) {
634 case index_directory:
635 return recurse_into_directory;
636
637 case index_gitdir:
7c4c97c0 638 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
ab22aed3 639 return ignore_directory;
09595258
LT
640 return show_directory;
641
642 case index_nonexistent:
7c4c97c0 643 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 644 break;
7c4c97c0 645 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
646 unsigned char sha1[20];
647 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
648 return show_directory;
649 }
650 return recurse_into_directory;
651 }
652
653 /* This is the "show_other_directories" case */
7c4c97c0 654 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
09595258 655 return show_directory;
dba2e203 656 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
09595258
LT
657 return ignore_directory;
658 return show_directory;
453ec4bd
LT
659}
660
9fc42d60
LT
661/*
662 * This is an inexact early pruning of any recursive directory
663 * reading - if the path cannot possibly be in the pathspec,
664 * return true, and we'll skip it early.
665 */
666static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
667{
668 if (simplify) {
669 for (;;) {
670 const char *match = simplify->path;
671 int len = simplify->len;
672
673 if (!match)
674 break;
675 if (len > pathlen)
676 len = pathlen;
677 if (!memcmp(path, match, len))
678 return 0;
679 simplify++;
680 }
681 return 1;
682 }
683 return 0;
684}
685
29209cbe
JK
686/*
687 * This function tells us whether an excluded path matches a
688 * list of "interesting" pathspecs. That is, whether a path matched
689 * by any of the pathspecs could possibly be ignored by excluding
690 * the specified path. This can happen if:
691 *
692 * 1. the path is mentioned explicitly in the pathspec
693 *
694 * 2. the path is a directory prefix of some element in the
695 * pathspec
696 */
697static int exclude_matches_pathspec(const char *path, int len,
698 const struct path_simplify *simplify)
e96980ef
JK
699{
700 if (simplify) {
701 for (; simplify->path; simplify++) {
702 if (len == simplify->len
703 && !memcmp(path, simplify->path, len))
704 return 1;
29209cbe
JK
705 if (len < simplify->len
706 && simplify->path[len] == '/'
707 && !memcmp(path, simplify->path, len))
708 return 1;
e96980ef
JK
709 }
710 }
711 return 0;
712}
713
443e061a
LT
714static int get_index_dtype(const char *path, int len)
715{
716 int pos;
717 struct cache_entry *ce;
718
719 ce = cache_name_exists(path, len, 0);
720 if (ce) {
721 if (!ce_uptodate(ce))
722 return DT_UNKNOWN;
723 if (S_ISGITLINK(ce->ce_mode))
724 return DT_DIR;
725 /*
726 * Nobody actually cares about the
727 * difference between DT_LNK and DT_REG
728 */
729 return DT_REG;
730 }
731
732 /* Try to look it up as a directory */
733 pos = cache_name_pos(path, len);
734 if (pos >= 0)
735 return DT_UNKNOWN;
736 pos = -pos-1;
737 while (pos < active_nr) {
738 ce = active_cache[pos++];
739 if (strncmp(ce->name, path, len))
740 break;
741 if (ce->name[len] > '/')
742 break;
743 if (ce->name[len] < '/')
744 continue;
745 if (!ce_uptodate(ce))
746 break; /* continue? */
747 return DT_DIR;
748 }
749 return DT_UNKNOWN;
750}
751
caa6b782 752static int get_dtype(struct dirent *de, const char *path, int len)
07134421 753{
6831a88a 754 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
755 struct stat st;
756
757 if (dtype != DT_UNKNOWN)
758 return dtype;
443e061a
LT
759 dtype = get_index_dtype(path, len);
760 if (dtype != DT_UNKNOWN)
761 return dtype;
762 if (lstat(path, &st))
07134421
LT
763 return dtype;
764 if (S_ISREG(st.st_mode))
765 return DT_REG;
766 if (S_ISDIR(st.st_mode))
767 return DT_DIR;
768 if (S_ISLNK(st.st_mode))
769 return DT_LNK;
770 return dtype;
771}
772
53cc5356
JH
773enum path_treatment {
774 path_ignored,
775 path_handled,
4b05548f 776 path_recurse
53cc5356
JH
777};
778
16e2cfa9
JH
779static enum path_treatment treat_one_path(struct dir_struct *dir,
780 char *path, int *len,
781 const struct path_simplify *simplify,
782 int dtype, struct dirent *de)
53cc5356 783{
16e2cfa9 784 int exclude = excluded(dir, path, &dtype);
53cc5356 785 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
29209cbe 786 && exclude_matches_pathspec(path, *len, simplify))
53cc5356
JH
787 dir_add_ignored(dir, path, *len);
788
789 /*
790 * Excluded? If we don't explicitly want to show
791 * ignored files, ignore it
792 */
793 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
794 return path_ignored;
795
796 if (dtype == DT_UNKNOWN)
797 dtype = get_dtype(de, path, *len);
798
799 /*
800 * Do we want to see just the ignored files?
801 * We still need to recurse into directories,
802 * even if we don't ignore them, since the
803 * directory may contain files that we do..
804 */
805 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
806 if (dtype != DT_DIR)
807 return path_ignored;
808 }
809
810 switch (dtype) {
811 default:
812 return path_ignored;
813 case DT_DIR:
814 memcpy(path + *len, "/", 2);
815 (*len)++;
816 switch (treat_directory(dir, path, *len, simplify)) {
817 case show_directory:
818 if (exclude != !!(dir->flags
819 & DIR_SHOW_IGNORED))
820 return path_ignored;
821 break;
822 case recurse_into_directory:
823 return path_recurse;
824 case ignore_directory:
825 return path_ignored;
826 }
827 break;
828 case DT_REG:
829 case DT_LNK:
830 break;
831 }
832 return path_handled;
833}
834
16e2cfa9
JH
835static enum path_treatment treat_path(struct dir_struct *dir,
836 struct dirent *de,
837 char *path, int path_max,
838 int baselen,
839 const struct path_simplify *simplify,
840 int *len)
841{
842 int dtype;
843
844 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
845 return path_ignored;
846 *len = strlen(de->d_name);
847 /* Ignore overly long pathnames! */
848 if (*len + baselen + 8 > path_max)
849 return path_ignored;
850 memcpy(path + baselen, de->d_name, *len + 1);
851 *len += baselen;
852 if (simplify_away(path, *len, simplify))
853 return path_ignored;
854
855 dtype = DTYPE(de);
856 return treat_one_path(dir, path, len, simplify, dtype, de);
857}
858
453ec4bd
LT
859/*
860 * Read a directory tree. We currently ignore anything but
861 * directories, regular files and symlinks. That's because git
862 * doesn't handle them at all yet. Maybe that will change some
863 * day.
864 *
865 * Also, we ignore the name ".git" (even if it is not a directory).
866 * That likely will not change.
867 */
53cc5356
JH
868static int read_directory_recursive(struct dir_struct *dir,
869 const char *base, int baselen,
870 int check_only,
871 const struct path_simplify *simplify)
453ec4bd 872{
dba2e203 873 DIR *fdir = opendir(*base ? base : ".");
453ec4bd
LT
874 int contents = 0;
875
876 if (fdir) {
453ec4bd 877 struct dirent *de;
dba2e203
LT
878 char path[PATH_MAX + 1];
879 memcpy(path, base, baselen);
453ec4bd 880
453ec4bd 881 while ((de = readdir(fdir)) != NULL) {
53cc5356
JH
882 int len;
883 switch (treat_path(dir, de, path, sizeof(path),
884 baselen, simplify, &len)) {
885 case path_recurse:
886 contents += read_directory_recursive
887 (dir, path, len, 0, simplify);
453ec4bd 888 continue;
53cc5356 889 case path_ignored:
5d5cea67 890 continue;
53cc5356 891 case path_handled:
453ec4bd
LT
892 break;
893 }
453ec4bd 894 contents++;
07ccbff8
JS
895 if (check_only)
896 goto exit_early;
897 else
dba2e203 898 dir_add_name(dir, path, len);
453ec4bd 899 }
07ccbff8 900exit_early:
453ec4bd 901 closedir(fdir);
453ec4bd
LT
902 }
903
904 return contents;
905}
906
907static int cmp_name(const void *p1, const void *p2)
908{
909 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
910 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
911
912 return cache_name_compare(e1->name, e1->len,
913 e2->name, e2->len);
914}
915
9fc42d60
LT
916/*
917 * Return the length of the "simple" part of a path match limiter.
918 */
919static int simple_length(const char *match)
453ec4bd 920{
9fc42d60
LT
921 int len = -1;
922
923 for (;;) {
924 unsigned char c = *match++;
925 len++;
8cc32992 926 if (c == '\0' || is_glob_special(c))
9fc42d60
LT
927 return len;
928 }
929}
930
931static struct path_simplify *create_simplify(const char **pathspec)
932{
933 int nr, alloc = 0;
934 struct path_simplify *simplify = NULL;
935
936 if (!pathspec)
937 return NULL;
938
939 for (nr = 0 ; ; nr++) {
940 const char *match;
941 if (nr >= alloc) {
942 alloc = alloc_nr(alloc);
943 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
944 }
945 match = *pathspec++;
946 if (!match)
947 break;
948 simplify[nr].path = match;
949 simplify[nr].len = simple_length(match);
950 }
951 simplify[nr].path = NULL;
952 simplify[nr].len = 0;
953 return simplify;
954}
955
956static void free_simplify(struct path_simplify *simplify)
957{
8e0f7003 958 free(simplify);
9fc42d60
LT
959}
960
48ffef96
JH
961static int treat_leading_path(struct dir_struct *dir,
962 const char *path, int len,
963 const struct path_simplify *simplify)
964{
965 char pathbuf[PATH_MAX];
966 int baselen, blen;
967 const char *cp;
968
969 while (len && path[len - 1] == '/')
970 len--;
971 if (!len)
972 return 1;
973 baselen = 0;
974 while (1) {
975 cp = path + baselen + !!baselen;
976 cp = memchr(cp, '/', path + len - cp);
977 if (!cp)
978 baselen = len;
979 else
980 baselen = cp - path;
981 memcpy(pathbuf, path, baselen);
982 pathbuf[baselen] = '\0';
983 if (!is_directory(pathbuf))
984 return 0;
985 if (simplify_away(pathbuf, baselen, simplify))
986 return 0;
987 blen = baselen;
988 if (treat_one_path(dir, pathbuf, &blen, simplify,
989 DT_DIR, NULL) == path_ignored)
990 return 0; /* do not recurse into it */
991 if (len <= baselen)
992 return 1; /* finished checking */
993 }
994}
995
dba2e203 996int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
9fc42d60 997{
725b0605 998 struct path_simplify *simplify;
b4189aa8 999
dba2e203 1000 if (has_symlink_leading_path(path, len))
725b0605
JH
1001 return dir->nr;
1002
1003 simplify = create_simplify(pathspec);
48ffef96
JH
1004 if (!len || treat_leading_path(dir, path, len, simplify))
1005 read_directory_recursive(dir, path, len, 0, simplify);
9fc42d60 1006 free_simplify(simplify);
453ec4bd 1007 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 1008 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
1009 return dir->nr;
1010}
c91f0d92 1011
686a4a06 1012int file_exists(const char *f)
c91f0d92 1013{
686a4a06 1014 struct stat sb;
a50f9fc5 1015 return lstat(f, &sb) == 0;
c91f0d92 1016}
e6636747
JS
1017
1018/*
1019 * get_relative_cwd() gets the prefix of the current working directory
1020 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
1021 *
1022 * As a convenience, it also returns NULL if 'dir' is already NULL. The
1023 * reason for this behaviour is that it is natural for functions returning
1024 * directory names to return NULL to say "this directory does not exist"
1025 * or "this directory is invalid". These cases are usually handled the
1026 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1027 * returns NULL for both of them.
1028 *
1029 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1030 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
1031 */
1032char *get_relative_cwd(char *buffer, int size, const char *dir)
1033{
1034 char *cwd = buffer;
1035
e6636747
JS
1036 if (!dir)
1037 return NULL;
1038 if (!getcwd(buffer, size))
d824cbba 1039 die_errno("can't find the current directory");
e6636747
JS
1040
1041 if (!is_absolute_path(dir))
1042 dir = make_absolute_path(dir);
1043
1044 while (*dir && *dir == *cwd) {
1045 dir++;
1046 cwd++;
1047 }
1048 if (*dir)
1049 return NULL;
490544b1
CB
1050 switch (*cwd) {
1051 case '\0':
1052 return cwd;
1053 case '/':
e6636747 1054 return cwd + 1;
490544b1 1055 default:
fbbb4e19
NTND
1056 /*
1057 * dir can end with a path separator when it's root
1058 * directory. Return proper prefix in that case.
1059 */
1060 if (dir[-1] == '/')
1061 return cwd;
490544b1
CB
1062 return NULL;
1063 }
e6636747
JS
1064}
1065
1066int is_inside_dir(const char *dir)
1067{
1068 char buffer[PATH_MAX];
1069 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1070}
7155b727 1071
55892d23
AP
1072int is_empty_dir(const char *path)
1073{
1074 DIR *dir = opendir(path);
1075 struct dirent *e;
1076 int ret = 1;
1077
1078 if (!dir)
1079 return 0;
1080
1081 while ((e = readdir(dir)) != NULL)
1082 if (!is_dot_or_dotdot(e->d_name)) {
1083 ret = 0;
1084 break;
1085 }
1086
1087 closedir(dir);
1088 return ret;
1089}
1090
a0f4afbe 1091int remove_dir_recursively(struct strbuf *path, int flag)
7155b727 1092{
a0f4afbe 1093 DIR *dir;
7155b727
JS
1094 struct dirent *e;
1095 int ret = 0, original_len = path->len, len;
a0f4afbe
JH
1096 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1097 unsigned char submodule_head[20];
7155b727 1098
a0f4afbe
JH
1099 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1100 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
1101 /* Do not descend and nuke a nested git work tree. */
1102 return 0;
1103
1104 dir = opendir(path->buf);
7155b727
JS
1105 if (!dir)
1106 return -1;
1107 if (path->buf[original_len - 1] != '/')
1108 strbuf_addch(path, '/');
1109
1110 len = path->len;
1111 while ((e = readdir(dir)) != NULL) {
1112 struct stat st;
8ca12c0d
AP
1113 if (is_dot_or_dotdot(e->d_name))
1114 continue;
7155b727
JS
1115
1116 strbuf_setlen(path, len);
1117 strbuf_addstr(path, e->d_name);
1118 if (lstat(path->buf, &st))
1119 ; /* fall thru */
1120 else if (S_ISDIR(st.st_mode)) {
1121 if (!remove_dir_recursively(path, only_empty))
1122 continue; /* happy */
1123 } else if (!only_empty && !unlink(path->buf))
1124 continue; /* happy, too */
1125
1126 /* path too long, stat fails, or non-directory still exists */
1127 ret = -1;
1128 break;
1129 }
1130 closedir(dir);
1131
1132 strbuf_setlen(path, original_len);
1133 if (!ret)
1134 ret = rmdir(path->buf);
1135 return ret;
1136}
039bc64e
JH
1137
1138void setup_standard_excludes(struct dir_struct *dir)
1139{
1140 const char *path;
1141
1142 dir->exclude_per_dir = ".gitignore";
1143 path = git_path("info/exclude");
1144 if (!access(path, R_OK))
1145 add_excludes_from_file(dir, path);
1146 if (excludes_file && !access(excludes_file, R_OK))
1147 add_excludes_from_file(dir, excludes_file);
1148}
4a92d1bf
AR
1149
1150int remove_path(const char *name)
1151{
1152 char *slash;
1153
1154 if (unlink(name) && errno != ENOENT)
1155 return -1;
1156
1157 slash = strrchr(name, '/');
1158 if (slash) {
1159 char *dirs = xstrdup(name);
1160 slash = dirs + (slash - name);
1161 do {
1162 *slash = '\0';
3fc0d131 1163 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
4a92d1bf
AR
1164 free(dirs);
1165 }
1166 return 0;
1167}
1168
86e4ca69
NTND
1169static int pathspec_item_cmp(const void *a_, const void *b_)
1170{
1171 struct pathspec_item *a, *b;
1172
1173 a = (struct pathspec_item *)a_;
1174 b = (struct pathspec_item *)b_;
1175 return strcmp(a->match, b->match);
1176}
1177
0602f3e9
NTND
1178int init_pathspec(struct pathspec *pathspec, const char **paths)
1179{
1180 const char **p = paths;
1181 int i;
1182
1183 memset(pathspec, 0, sizeof(*pathspec));
1184 if (!p)
1185 return 0;
1186 while (*p)
1187 p++;
1188 pathspec->raw = paths;
1189 pathspec->nr = p - paths;
1190 if (!pathspec->nr)
1191 return 0;
1192
1193 pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1194 for (i = 0; i < pathspec->nr; i++) {
1195 struct pathspec_item *item = pathspec->items+i;
1196 const char *path = paths[i];
1197
1198 item->match = path;
1199 item->len = strlen(path);
d38f2809
NTND
1200 item->has_wildcard = !no_wildcard(path);
1201 if (item->has_wildcard)
1202 pathspec->has_wildcard = 1;
0602f3e9 1203 }
86e4ca69
NTND
1204
1205 qsort(pathspec->items, pathspec->nr,
1206 sizeof(struct pathspec_item), pathspec_item_cmp);
1207
0602f3e9
NTND
1208 return 0;
1209}
1210
1211void free_pathspec(struct pathspec *pathspec)
1212{
1213 free(pathspec->items);
1214 pathspec->items = NULL;
1215}