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