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