unpack-trees(): carry skip-worktree bit over in merged_entry()
[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
34 prefix = slash - path + 1;
35 while ((next = *++pathspec) != NULL) {
36 int len = strlen(next);
c7f34c18 37 if (len >= prefix && !memcmp(path, next, prefix))
3c6a370b 38 continue;
c7f34c18 39 len = prefix - 1;
3c6a370b
LT
40 for (;;) {
41 if (!len)
42 return 0;
43 if (next[--len] != '/')
44 continue;
45 if (memcmp(path, next, len+1))
46 continue;
47 prefix = len + 1;
48 break;
49 }
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
453ec4bd
LT
226static int add_excludes_from_file_1(const char *fname,
227 const char *base,
228 int baselen,
63d285c8 229 char **buf_p,
c28b3d6e
NTND
230 struct exclude_list *which,
231 int check_index)
453ec4bd 232{
c470701a 233 struct stat st;
453ec4bd 234 int fd, i;
dc49cd76 235 size_t size;
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;
453ec4bd 245 }
c28b3d6e
NTND
246 else {
247 size = xsize_t(st.st_size);
248 if (size == 0) {
249 close(fd);
250 return 0;
251 }
252 buf = xmalloc(size);
253 if (read_in_full(fd, buf, size) != size) {
254 close(fd);
255 return -1;
256 }
257 close(fd);
6ba78238 258 }
453ec4bd 259
63d285c8
JH
260 if (buf_p)
261 *buf_p = buf;
453ec4bd 262 entry = buf;
b5041c5f
NTND
263 for (i = 0; i <= size; i++) {
264 if (i == size || buf[i] == '\n') {
453ec4bd
LT
265 if (entry != buf + i && entry[0] != '#') {
266 buf[i - (i && buf[i-1] == '\r')] = 0;
267 add_exclude(entry, base, baselen, which);
268 }
269 entry = buf + i + 1;
270 }
271 }
272 return 0;
453ec4bd
LT
273}
274
275void add_excludes_from_file(struct dir_struct *dir, const char *fname)
276{
63d285c8 277 if (add_excludes_from_file_1(fname, "", 0, NULL,
c28b3d6e 278 &dir->exclude_list[EXC_FILE], 0) < 0)
453ec4bd
LT
279 die("cannot use %s as an exclude file", fname);
280}
281
63d285c8 282static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 283{
63d285c8
JH
284 struct exclude_list *el;
285 struct exclude_stack *stk = NULL;
286 int current;
287
288 if ((!dir->exclude_per_dir) ||
289 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
290 return; /* too long a path -- ignore */
291
292 /* Pop the ones that are not the prefix of the path being checked. */
293 el = &dir->exclude_list[EXC_DIRS];
294 while ((stk = dir->exclude_stack) != NULL) {
295 if (stk->baselen <= baselen &&
296 !strncmp(dir->basebuf, base, stk->baselen))
297 break;
298 dir->exclude_stack = stk->prev;
299 while (stk->exclude_ix < el->nr)
300 free(el->excludes[--el->nr]);
301 free(stk->filebuf);
302 free(stk);
453ec4bd 303 }
453ec4bd 304
63d285c8
JH
305 /* Read from the parent directories and push them down. */
306 current = stk ? stk->baselen : -1;
307 while (current < baselen) {
308 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
309 const char *cp;
453ec4bd 310
63d285c8
JH
311 if (current < 0) {
312 cp = base;
313 current = 0;
314 }
315 else {
316 cp = strchr(base + current + 1, '/');
317 if (!cp)
318 die("oops in prep_exclude");
319 cp++;
320 }
321 stk->prev = dir->exclude_stack;
322 stk->baselen = cp - base;
323 stk->exclude_ix = el->nr;
324 memcpy(dir->basebuf + current, base + current,
325 stk->baselen - current);
326 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
327 add_excludes_from_file_1(dir->basebuf,
328 dir->basebuf, stk->baselen,
c28b3d6e 329 &stk->filebuf, el, 1);
63d285c8
JH
330 dir->exclude_stack = stk;
331 current = stk->baselen;
332 }
333 dir->basebuf[baselen] = '\0';
453ec4bd
LT
334}
335
2c5b0115 336/* Scan the list and let the last match determine the fate.
453ec4bd
LT
337 * Return 1 for exclude, 0 for include and -1 for undecided.
338 */
339static int excluded_1(const char *pathname,
6831a88a 340 int pathlen, const char *basename, int *dtype,
453ec4bd
LT
341 struct exclude_list *el)
342{
343 int i;
344
345 if (el->nr) {
346 for (i = el->nr - 1; 0 <= i; i--) {
347 struct exclude *x = el->excludes[i];
348 const char *exclude = x->pattern;
68492fc7 349 int to_exclude = x->to_exclude;
453ec4bd 350
6831a88a
JH
351 if (x->flags & EXC_FLAG_MUSTBEDIR) {
352 if (*dtype == DT_UNKNOWN)
caa6b782 353 *dtype = get_dtype(NULL, pathname, pathlen);
6831a88a
JH
354 if (*dtype != DT_DIR)
355 continue;
356 }
d6b8fc30 357
68492fc7 358 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 359 /* match basename */
68492fc7
LK
360 if (x->flags & EXC_FLAG_NOWILDCARD) {
361 if (!strcmp(exclude, basename))
362 return to_exclude;
363 } else if (x->flags & EXC_FLAG_ENDSWITH) {
364 if (x->patternlen - 1 <= pathlen &&
365 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
366 return to_exclude;
367 } else {
368 if (fnmatch(exclude, basename, 0) == 0)
369 return to_exclude;
370 }
453ec4bd
LT
371 }
372 else {
373 /* match with FNM_PATHNAME:
374 * exclude has base (baselen long) implicitly
375 * in front of it.
376 */
377 int baselen = x->baselen;
378 if (*exclude == '/')
379 exclude++;
380
381 if (pathlen < baselen ||
382 (baselen && pathname[baselen-1] != '/') ||
383 strncmp(pathname, x->base, baselen))
384 continue;
385
68492fc7
LK
386 if (x->flags & EXC_FLAG_NOWILDCARD) {
387 if (!strcmp(exclude, pathname + baselen))
388 return to_exclude;
389 } else {
390 if (fnmatch(exclude, pathname+baselen,
391 FNM_PATHNAME) == 0)
392 return to_exclude;
393 }
453ec4bd
LT
394 }
395 }
396 }
397 return -1; /* undecided */
398}
399
6831a88a 400int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
401{
402 int pathlen = strlen(pathname);
403 int st;
68492fc7
LK
404 const char *basename = strrchr(pathname, '/');
405 basename = (basename) ? basename+1 : pathname;
453ec4bd 406
63d285c8 407 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 408 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
d6b8fc30 409 switch (excluded_1(pathname, pathlen, basename,
6831a88a 410 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
411 case 0:
412 return 0;
413 case 1:
414 return 1;
415 }
416 }
417 return 0;
418}
419
f3fa1838
JH
420static struct dir_entry *dir_entry_new(const char *pathname, int len)
421{
453ec4bd
LT
422 struct dir_entry *ent;
423
453ec4bd
LT
424 ent = xmalloc(sizeof(*ent) + len + 1);
425 ent->len = len;
426 memcpy(ent->name, pathname, len);
427 ent->name[len] = 0;
4d06f8ac 428 return ent;
453ec4bd
LT
429}
430
159b3212 431static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 432{
0a9b88b7 433 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
434 return NULL;
435
25fd2f7a 436 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
437 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
438}
439
159b3212 440static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 441{
6e4f981f 442 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
443 return NULL;
444
25fd2f7a 445 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
446 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
447}
448
09595258
LT
449enum exist_status {
450 index_nonexistent = 0,
451 index_directory,
452 index_gitdir,
453};
454
455/*
456 * The index sorts alphabetically by entry name, which
457 * means that a gitlink sorts as '\0' at the end, while
458 * a directory (which is defined not as an entry, but as
459 * the files it contains) will sort with the '/' at the
460 * end.
461 */
462static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd
LT
463{
464 int pos = cache_name_pos(dirname, len);
09595258
LT
465 if (pos < 0)
466 pos = -pos-1;
467 while (pos < active_nr) {
468 struct cache_entry *ce = active_cache[pos++];
469 unsigned char endchar;
470
471 if (strncmp(ce->name, dirname, len))
472 break;
473 endchar = ce->name[len];
474 if (endchar > '/')
475 break;
476 if (endchar == '/')
477 return index_directory;
7a51ed66 478 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
479 return index_gitdir;
480 }
481 return index_nonexistent;
482}
483
484/*
485 * When we find a directory when traversing the filesystem, we
486 * have three distinct cases:
487 *
488 * - ignore it
489 * - see it as a directory
490 * - recurse into it
491 *
492 * and which one we choose depends on a combination of existing
493 * git index contents and the flags passed into the directory
494 * traversal routine.
495 *
496 * Case 1: If we *already* have entries in the index under that
497 * directory name, we always recurse into the directory to see
498 * all the files.
499 *
500 * Case 2: If we *already* have that directory name as a gitlink,
501 * we always continue to see it as a gitlink, regardless of whether
502 * there is an actual git directory there or not (it might not
503 * be checked out as a subproject!)
504 *
505 * Case 3: if we didn't have it in the index previously, we
506 * have a few sub-cases:
507 *
508 * (a) if "show_other_directories" is true, we show it as
509 * just a directory, unless "hide_empty_directories" is
510 * also true and the directory is empty, in which case
511 * we just ignore it entirely.
512 * (b) if it looks like a git directory, and we don't have
302b9282 513 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
514 * as a directory.
515 * (c) otherwise, we recurse into it.
516 */
517enum directory_treatment {
518 show_directory,
519 ignore_directory,
520 recurse_into_directory,
521};
522
523static enum directory_treatment treat_directory(struct dir_struct *dir,
524 const char *dirname, int len,
525 const struct path_simplify *simplify)
526{
527 /* The "len-1" is to strip the final '/' */
528 switch (directory_exists_in_index(dirname, len-1)) {
529 case index_directory:
530 return recurse_into_directory;
531
532 case index_gitdir:
7c4c97c0 533 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
ab22aed3 534 return ignore_directory;
09595258
LT
535 return show_directory;
536
537 case index_nonexistent:
7c4c97c0 538 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 539 break;
7c4c97c0 540 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
541 unsigned char sha1[20];
542 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
543 return show_directory;
544 }
545 return recurse_into_directory;
546 }
547
548 /* This is the "show_other_directories" case */
7c4c97c0 549 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
09595258 550 return show_directory;
dba2e203 551 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
09595258
LT
552 return ignore_directory;
553 return show_directory;
453ec4bd
LT
554}
555
9fc42d60
LT
556/*
557 * This is an inexact early pruning of any recursive directory
558 * reading - if the path cannot possibly be in the pathspec,
559 * return true, and we'll skip it early.
560 */
561static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
562{
563 if (simplify) {
564 for (;;) {
565 const char *match = simplify->path;
566 int len = simplify->len;
567
568 if (!match)
569 break;
570 if (len > pathlen)
571 len = pathlen;
572 if (!memcmp(path, match, len))
573 return 0;
574 simplify++;
575 }
576 return 1;
577 }
578 return 0;
579}
580
e96980ef
JK
581static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
582{
583 if (simplify) {
584 for (; simplify->path; simplify++) {
585 if (len == simplify->len
586 && !memcmp(path, simplify->path, len))
587 return 1;
588 }
589 }
590 return 0;
591}
592
443e061a
LT
593static int get_index_dtype(const char *path, int len)
594{
595 int pos;
596 struct cache_entry *ce;
597
598 ce = cache_name_exists(path, len, 0);
599 if (ce) {
600 if (!ce_uptodate(ce))
601 return DT_UNKNOWN;
602 if (S_ISGITLINK(ce->ce_mode))
603 return DT_DIR;
604 /*
605 * Nobody actually cares about the
606 * difference between DT_LNK and DT_REG
607 */
608 return DT_REG;
609 }
610
611 /* Try to look it up as a directory */
612 pos = cache_name_pos(path, len);
613 if (pos >= 0)
614 return DT_UNKNOWN;
615 pos = -pos-1;
616 while (pos < active_nr) {
617 ce = active_cache[pos++];
618 if (strncmp(ce->name, path, len))
619 break;
620 if (ce->name[len] > '/')
621 break;
622 if (ce->name[len] < '/')
623 continue;
624 if (!ce_uptodate(ce))
625 break; /* continue? */
626 return DT_DIR;
627 }
628 return DT_UNKNOWN;
629}
630
caa6b782 631static int get_dtype(struct dirent *de, const char *path, int len)
07134421 632{
6831a88a 633 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
634 struct stat st;
635
636 if (dtype != DT_UNKNOWN)
637 return dtype;
443e061a
LT
638 dtype = get_index_dtype(path, len);
639 if (dtype != DT_UNKNOWN)
640 return dtype;
641 if (lstat(path, &st))
07134421
LT
642 return dtype;
643 if (S_ISREG(st.st_mode))
644 return DT_REG;
645 if (S_ISDIR(st.st_mode))
646 return DT_DIR;
647 if (S_ISLNK(st.st_mode))
648 return DT_LNK;
649 return dtype;
650}
651
453ec4bd
LT
652/*
653 * Read a directory tree. We currently ignore anything but
654 * directories, regular files and symlinks. That's because git
655 * doesn't handle them at all yet. Maybe that will change some
656 * day.
657 *
658 * Also, we ignore the name ".git" (even if it is not a directory).
659 * That likely will not change.
660 */
dba2e203 661static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
453ec4bd 662{
dba2e203 663 DIR *fdir = opendir(*base ? base : ".");
453ec4bd
LT
664 int contents = 0;
665
666 if (fdir) {
453ec4bd 667 struct dirent *de;
dba2e203
LT
668 char path[PATH_MAX + 1];
669 memcpy(path, base, baselen);
453ec4bd 670
453ec4bd 671 while ((de = readdir(fdir)) != NULL) {
07134421 672 int len, dtype;
b9916256 673 int exclude;
453ec4bd 674
8ca12c0d
AP
675 if (is_dot_or_dotdot(de->d_name) ||
676 !strcmp(de->d_name, ".git"))
453ec4bd
LT
677 continue;
678 len = strlen(de->d_name);
5d5cea67 679 /* Ignore overly long pathnames! */
dba2e203 680 if (len + baselen + 8 > sizeof(path))
5d5cea67 681 continue;
dba2e203
LT
682 memcpy(path + baselen, de->d_name, len+1);
683 len = baselen + len;
684 if (simplify_away(path, len, simplify))
9fc42d60 685 continue;
b9916256 686
6831a88a 687 dtype = DTYPE(de);
dba2e203 688 exclude = excluded(dir, path, &dtype);
7c4c97c0 689 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
dba2e203
LT
690 && in_pathspec(path, len, simplify))
691 dir_add_ignored(dir, path,len);
07134421
LT
692
693 /*
694 * Excluded? If we don't explicitly want to show
695 * ignored files, ignore it
696 */
7c4c97c0 697 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
07134421
LT
698 continue;
699
6831a88a 700 if (dtype == DT_UNKNOWN)
caa6b782 701 dtype = get_dtype(de, path, len);
07134421
LT
702
703 /*
704 * Do we want to see just the ignored files?
705 * We still need to recurse into directories,
706 * even if we don't ignore them, since the
707 * directory may contain files that we do..
708 */
7c4c97c0 709 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
07134421 710 if (dtype != DT_DIR)
c889763b 711 continue;
c889763b 712 }
453ec4bd 713
07134421 714 switch (dtype) {
453ec4bd
LT
715 default:
716 continue;
453ec4bd 717 case DT_DIR:
dba2e203 718 memcpy(path + len, "/", 2);
453ec4bd 719 len++;
dba2e203 720 switch (treat_directory(dir, path, len, simplify)) {
09595258 721 case show_directory:
7c4c97c0
JS
722 if (exclude != !!(dir->flags
723 & DIR_SHOW_IGNORED))
b9916256 724 continue;
453ec4bd 725 break;
09595258
LT
726 case recurse_into_directory:
727 contents += read_directory_recursive(dir,
dba2e203 728 path, len, 0, simplify);
09595258
LT
729 continue;
730 case ignore_directory:
731 continue;
453ec4bd 732 }
09595258 733 break;
453ec4bd
LT
734 case DT_REG:
735 case DT_LNK:
736 break;
737 }
453ec4bd 738 contents++;
07ccbff8
JS
739 if (check_only)
740 goto exit_early;
741 else
dba2e203 742 dir_add_name(dir, path, len);
453ec4bd 743 }
07ccbff8 744exit_early:
453ec4bd 745 closedir(fdir);
453ec4bd
LT
746 }
747
748 return contents;
749}
750
751static int cmp_name(const void *p1, const void *p2)
752{
753 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
754 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
755
756 return cache_name_compare(e1->name, e1->len,
757 e2->name, e2->len);
758}
759
9fc42d60
LT
760/*
761 * Return the length of the "simple" part of a path match limiter.
762 */
763static int simple_length(const char *match)
453ec4bd 764{
9fc42d60
LT
765 int len = -1;
766
767 for (;;) {
768 unsigned char c = *match++;
769 len++;
8cc32992 770 if (c == '\0' || is_glob_special(c))
9fc42d60
LT
771 return len;
772 }
773}
774
775static struct path_simplify *create_simplify(const char **pathspec)
776{
777 int nr, alloc = 0;
778 struct path_simplify *simplify = NULL;
779
780 if (!pathspec)
781 return NULL;
782
783 for (nr = 0 ; ; nr++) {
784 const char *match;
785 if (nr >= alloc) {
786 alloc = alloc_nr(alloc);
787 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
788 }
789 match = *pathspec++;
790 if (!match)
791 break;
792 simplify[nr].path = match;
793 simplify[nr].len = simple_length(match);
794 }
795 simplify[nr].path = NULL;
796 simplify[nr].len = 0;
797 return simplify;
798}
799
800static void free_simplify(struct path_simplify *simplify)
801{
8e0f7003 802 free(simplify);
9fc42d60
LT
803}
804
dba2e203 805int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
9fc42d60 806{
725b0605 807 struct path_simplify *simplify;
b4189aa8 808
dba2e203 809 if (has_symlink_leading_path(path, len))
725b0605
JH
810 return dir->nr;
811
812 simplify = create_simplify(pathspec);
dba2e203 813 read_directory_recursive(dir, path, len, 0, simplify);
9fc42d60 814 free_simplify(simplify);
453ec4bd 815 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 816 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
817 return dir->nr;
818}
c91f0d92 819
686a4a06 820int file_exists(const char *f)
c91f0d92 821{
686a4a06 822 struct stat sb;
a50f9fc5 823 return lstat(f, &sb) == 0;
c91f0d92 824}
e6636747
JS
825
826/*
827 * get_relative_cwd() gets the prefix of the current working directory
828 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
829 *
830 * As a convenience, it also returns NULL if 'dir' is already NULL. The
831 * reason for this behaviour is that it is natural for functions returning
832 * directory names to return NULL to say "this directory does not exist"
833 * or "this directory is invalid". These cases are usually handled the
834 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
835 * returns NULL for both of them.
836 *
837 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
838 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
839 */
840char *get_relative_cwd(char *buffer, int size, const char *dir)
841{
842 char *cwd = buffer;
843
e6636747
JS
844 if (!dir)
845 return NULL;
846 if (!getcwd(buffer, size))
d824cbba 847 die_errno("can't find the current directory");
e6636747
JS
848
849 if (!is_absolute_path(dir))
850 dir = make_absolute_path(dir);
851
852 while (*dir && *dir == *cwd) {
853 dir++;
854 cwd++;
855 }
856 if (*dir)
857 return NULL;
858 if (*cwd == '/')
859 return cwd + 1;
860 return cwd;
861}
862
863int is_inside_dir(const char *dir)
864{
865 char buffer[PATH_MAX];
866 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
867}
7155b727 868
55892d23
AP
869int is_empty_dir(const char *path)
870{
871 DIR *dir = opendir(path);
872 struct dirent *e;
873 int ret = 1;
874
875 if (!dir)
876 return 0;
877
878 while ((e = readdir(dir)) != NULL)
879 if (!is_dot_or_dotdot(e->d_name)) {
880 ret = 0;
881 break;
882 }
883
884 closedir(dir);
885 return ret;
886}
887
7155b727
JS
888int remove_dir_recursively(struct strbuf *path, int only_empty)
889{
890 DIR *dir = opendir(path->buf);
891 struct dirent *e;
892 int ret = 0, original_len = path->len, len;
893
894 if (!dir)
895 return -1;
896 if (path->buf[original_len - 1] != '/')
897 strbuf_addch(path, '/');
898
899 len = path->len;
900 while ((e = readdir(dir)) != NULL) {
901 struct stat st;
8ca12c0d
AP
902 if (is_dot_or_dotdot(e->d_name))
903 continue;
7155b727
JS
904
905 strbuf_setlen(path, len);
906 strbuf_addstr(path, e->d_name);
907 if (lstat(path->buf, &st))
908 ; /* fall thru */
909 else if (S_ISDIR(st.st_mode)) {
910 if (!remove_dir_recursively(path, only_empty))
911 continue; /* happy */
912 } else if (!only_empty && !unlink(path->buf))
913 continue; /* happy, too */
914
915 /* path too long, stat fails, or non-directory still exists */
916 ret = -1;
917 break;
918 }
919 closedir(dir);
920
921 strbuf_setlen(path, original_len);
922 if (!ret)
923 ret = rmdir(path->buf);
924 return ret;
925}
039bc64e
JH
926
927void setup_standard_excludes(struct dir_struct *dir)
928{
929 const char *path;
930
931 dir->exclude_per_dir = ".gitignore";
932 path = git_path("info/exclude");
933 if (!access(path, R_OK))
934 add_excludes_from_file(dir, path);
935 if (excludes_file && !access(excludes_file, R_OK))
936 add_excludes_from_file(dir, excludes_file);
937}
4a92d1bf
AR
938
939int remove_path(const char *name)
940{
941 char *slash;
942
943 if (unlink(name) && errno != ENOENT)
944 return -1;
945
946 slash = strrchr(name, '/');
947 if (slash) {
948 char *dirs = xstrdup(name);
949 slash = dirs + (slash - name);
950 do {
951 *slash = '\0';
952 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
953 free(dirs);
954 }
955 return 0;
956}
957