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