]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
Merge branch 'mh/maint-sendmail-cc-doc'
[thirdparty/git.git] / dir.c
CommitLineData
453ec4bd
LT
1/*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
4 *
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
453ec4bd
LT
8#include "cache.h"
9#include "dir.h"
09595258 10#include "refs.h"
453ec4bd 11
9fc42d60
LT
12struct path_simplify {
13 int len;
14 const char *path;
15};
16
09595258
LT
17static int read_directory_recursive(struct dir_struct *dir,
18 const char *path, const char *base, int baselen,
19 int check_only, const struct path_simplify *simplify);
6831a88a 20static int get_dtype(struct dirent *de, const char *path);
09595258 21
3c6a370b
LT
22int common_prefix(const char **pathspec)
23{
24 const char *path, *slash, *next;
25 int prefix;
26
27 if (!pathspec)
28 return 0;
29
30 path = *pathspec;
31 slash = strrchr(path, '/');
32 if (!slash)
33 return 0;
34
35 prefix = slash - path + 1;
36 while ((next = *++pathspec) != NULL) {
37 int len = strlen(next);
c7f34c18 38 if (len >= prefix && !memcmp(path, next, prefix))
3c6a370b 39 continue;
c7f34c18 40 len = prefix - 1;
3c6a370b
LT
41 for (;;) {
42 if (!len)
43 return 0;
44 if (next[--len] != '/')
45 continue;
46 if (memcmp(path, next, len+1))
47 continue;
48 prefix = len + 1;
49 break;
50 }
51 }
52 return prefix;
53}
54
e813d50e
JH
55/*
56 * Does 'match' matches the given name?
57 * A match is found if
58 *
59 * (1) the 'match' string is leading directory of 'name', or
60 * (2) the 'match' string is a wildcard and matches 'name', or
61 * (3) the 'match' string is exactly the same as 'name'.
62 *
63 * and the return value tells which case it was.
64 *
65 * It returns 0 when there is no match.
66 */
3c6a370b
LT
67static int match_one(const char *match, const char *name, int namelen)
68{
69 int matchlen;
70
71 /* If the match was just the prefix, we matched */
88ea8112 72 if (!*match)
e813d50e 73 return MATCHED_RECURSIVELY;
3c6a370b 74
88ea8112
LT
75 for (;;) {
76 unsigned char c1 = *match;
77 unsigned char c2 = *name;
63e8aea7 78 if (isspecial(c1))
88ea8112
LT
79 break;
80 if (c1 != c2)
81 return 0;
82 match++;
83 name++;
84 namelen--;
85 }
86
87
3c6a370b
LT
88 /*
89 * If we don't match the matchstring exactly,
90 * we need to match by fnmatch
91 */
88ea8112 92 matchlen = strlen(match);
3c6a370b 93 if (strncmp(match, name, matchlen))
e813d50e 94 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
3c6a370b 95
f2d0df71 96 if (namelen == matchlen)
e813d50e
JH
97 return MATCHED_EXACTLY;
98 if (match[matchlen-1] == '/' || name[matchlen] == '/')
99 return MATCHED_RECURSIVELY;
100 return 0;
3c6a370b
LT
101}
102
e813d50e
JH
103/*
104 * Given a name and a list of pathspecs, see if the name matches
105 * any of the pathspecs. The caller is also interested in seeing
106 * all pathspec matches some names it calls this function with
107 * (otherwise the user could have mistyped the unmatched pathspec),
108 * and a mark is left in seen[] array for pathspec element that
109 * actually matched anything.
110 */
3c6a370b
LT
111int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
112{
113 int retval;
114 const char *match;
115
116 name += prefix;
117 namelen -= prefix;
118
119 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
e813d50e
JH
120 int how;
121 if (retval && *seen == MATCHED_EXACTLY)
3c6a370b
LT
122 continue;
123 match += prefix;
e813d50e
JH
124 how = match_one(match, name, namelen);
125 if (how) {
126 if (retval < how)
127 retval = how;
128 if (*seen < how)
129 *seen = how;
3c6a370b
LT
130 }
131 }
132 return retval;
133}
134
68492fc7
LK
135static int no_wildcard(const char *string)
136{
137 return string[strcspn(string, "*?[{")] == '\0';
138}
139
453ec4bd
LT
140void add_exclude(const char *string, const char *base,
141 int baselen, struct exclude_list *which)
142{
d6b8fc30
JH
143 struct exclude *x;
144 size_t len;
145 int to_exclude = 1;
146 int flags = 0;
453ec4bd 147
68492fc7 148 if (*string == '!') {
d6b8fc30 149 to_exclude = 0;
68492fc7
LK
150 string++;
151 }
d6b8fc30
JH
152 len = strlen(string);
153 if (len && string[len - 1] == '/') {
154 char *s;
155 x = xmalloc(sizeof(*x) + len);
156 s = (char*)(x+1);
157 memcpy(s, string, len - 1);
158 s[len - 1] = '\0';
159 string = s;
160 x->pattern = s;
161 flags = EXC_FLAG_MUSTBEDIR;
162 } else {
163 x = xmalloc(sizeof(*x));
164 x->pattern = string;
165 }
166 x->to_exclude = to_exclude;
68492fc7 167 x->patternlen = strlen(string);
453ec4bd
LT
168 x->base = base;
169 x->baselen = baselen;
d6b8fc30 170 x->flags = flags;
68492fc7
LK
171 if (!strchr(string, '/'))
172 x->flags |= EXC_FLAG_NODIR;
173 if (no_wildcard(string))
174 x->flags |= EXC_FLAG_NOWILDCARD;
175 if (*string == '*' && no_wildcard(string+1))
176 x->flags |= EXC_FLAG_ENDSWITH;
686a4a06 177 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
453ec4bd
LT
178 which->excludes[which->nr++] = x;
179}
180
181static int add_excludes_from_file_1(const char *fname,
182 const char *base,
183 int baselen,
63d285c8 184 char **buf_p,
453ec4bd
LT
185 struct exclude_list *which)
186{
c470701a 187 struct stat st;
453ec4bd 188 int fd, i;
dc49cd76 189 size_t size;
453ec4bd
LT
190 char *buf, *entry;
191
192 fd = open(fname, O_RDONLY);
c470701a 193 if (fd < 0 || fstat(fd, &st) < 0)
453ec4bd 194 goto err;
dc49cd76 195 size = xsize_t(st.st_size);
453ec4bd
LT
196 if (size == 0) {
197 close(fd);
198 return 0;
199 }
200 buf = xmalloc(size+1);
93d26e4c 201 if (read_in_full(fd, buf, size) != size)
6ba78238
LH
202 {
203 free(buf);
453ec4bd 204 goto err;
6ba78238 205 }
453ec4bd
LT
206 close(fd);
207
63d285c8
JH
208 if (buf_p)
209 *buf_p = buf;
453ec4bd
LT
210 buf[size++] = '\n';
211 entry = buf;
212 for (i = 0; i < size; i++) {
213 if (buf[i] == '\n') {
214 if (entry != buf + i && entry[0] != '#') {
215 buf[i - (i && buf[i-1] == '\r')] = 0;
216 add_exclude(entry, base, baselen, which);
217 }
218 entry = buf + i + 1;
219 }
220 }
221 return 0;
222
223 err:
224 if (0 <= fd)
225 close(fd);
226 return -1;
227}
228
229void add_excludes_from_file(struct dir_struct *dir, const char *fname)
230{
63d285c8 231 if (add_excludes_from_file_1(fname, "", 0, NULL,
453ec4bd
LT
232 &dir->exclude_list[EXC_FILE]) < 0)
233 die("cannot use %s as an exclude file", fname);
234}
235
63d285c8 236static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 237{
63d285c8
JH
238 struct exclude_list *el;
239 struct exclude_stack *stk = NULL;
240 int current;
241
242 if ((!dir->exclude_per_dir) ||
243 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
244 return; /* too long a path -- ignore */
245
246 /* Pop the ones that are not the prefix of the path being checked. */
247 el = &dir->exclude_list[EXC_DIRS];
248 while ((stk = dir->exclude_stack) != NULL) {
249 if (stk->baselen <= baselen &&
250 !strncmp(dir->basebuf, base, stk->baselen))
251 break;
252 dir->exclude_stack = stk->prev;
253 while (stk->exclude_ix < el->nr)
254 free(el->excludes[--el->nr]);
255 free(stk->filebuf);
256 free(stk);
453ec4bd 257 }
453ec4bd 258
63d285c8
JH
259 /* Read from the parent directories and push them down. */
260 current = stk ? stk->baselen : -1;
261 while (current < baselen) {
262 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
263 const char *cp;
453ec4bd 264
63d285c8
JH
265 if (current < 0) {
266 cp = base;
267 current = 0;
268 }
269 else {
270 cp = strchr(base + current + 1, '/');
271 if (!cp)
272 die("oops in prep_exclude");
273 cp++;
274 }
275 stk->prev = dir->exclude_stack;
276 stk->baselen = cp - base;
277 stk->exclude_ix = el->nr;
278 memcpy(dir->basebuf + current, base + current,
279 stk->baselen - current);
280 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
281 add_excludes_from_file_1(dir->basebuf,
282 dir->basebuf, stk->baselen,
283 &stk->filebuf, el);
284 dir->exclude_stack = stk;
285 current = stk->baselen;
286 }
287 dir->basebuf[baselen] = '\0';
453ec4bd
LT
288}
289
290/* Scan the list and let the last match determines the fate.
291 * Return 1 for exclude, 0 for include and -1 for undecided.
292 */
293static int excluded_1(const char *pathname,
6831a88a 294 int pathlen, const char *basename, int *dtype,
453ec4bd
LT
295 struct exclude_list *el)
296{
297 int i;
298
299 if (el->nr) {
300 for (i = el->nr - 1; 0 <= i; i--) {
301 struct exclude *x = el->excludes[i];
302 const char *exclude = x->pattern;
68492fc7 303 int to_exclude = x->to_exclude;
453ec4bd 304
6831a88a
JH
305 if (x->flags & EXC_FLAG_MUSTBEDIR) {
306 if (*dtype == DT_UNKNOWN)
307 *dtype = get_dtype(NULL, pathname);
308 if (*dtype != DT_DIR)
309 continue;
310 }
d6b8fc30 311
68492fc7 312 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 313 /* match basename */
68492fc7
LK
314 if (x->flags & EXC_FLAG_NOWILDCARD) {
315 if (!strcmp(exclude, basename))
316 return to_exclude;
317 } else if (x->flags & EXC_FLAG_ENDSWITH) {
318 if (x->patternlen - 1 <= pathlen &&
319 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
320 return to_exclude;
321 } else {
322 if (fnmatch(exclude, basename, 0) == 0)
323 return to_exclude;
324 }
453ec4bd
LT
325 }
326 else {
327 /* match with FNM_PATHNAME:
328 * exclude has base (baselen long) implicitly
329 * in front of it.
330 */
331 int baselen = x->baselen;
332 if (*exclude == '/')
333 exclude++;
334
335 if (pathlen < baselen ||
336 (baselen && pathname[baselen-1] != '/') ||
337 strncmp(pathname, x->base, baselen))
338 continue;
339
68492fc7
LK
340 if (x->flags & EXC_FLAG_NOWILDCARD) {
341 if (!strcmp(exclude, pathname + baselen))
342 return to_exclude;
343 } else {
344 if (fnmatch(exclude, pathname+baselen,
345 FNM_PATHNAME) == 0)
346 return to_exclude;
347 }
453ec4bd
LT
348 }
349 }
350 }
351 return -1; /* undecided */
352}
353
6831a88a 354int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
355{
356 int pathlen = strlen(pathname);
357 int st;
68492fc7
LK
358 const char *basename = strrchr(pathname, '/');
359 basename = (basename) ? basename+1 : pathname;
453ec4bd 360
63d285c8 361 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 362 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
d6b8fc30 363 switch (excluded_1(pathname, pathlen, basename,
6831a88a 364 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
365 case 0:
366 return 0;
367 case 1:
368 return 1;
369 }
370 }
371 return 0;
372}
373
f3fa1838
JH
374static struct dir_entry *dir_entry_new(const char *pathname, int len)
375{
453ec4bd
LT
376 struct dir_entry *ent;
377
453ec4bd
LT
378 ent = xmalloc(sizeof(*ent) + len + 1);
379 ent->len = len;
380 memcpy(ent->name, pathname, len);
381 ent->name[len] = 0;
4d06f8ac 382 return ent;
453ec4bd
LT
383}
384
159b3212 385static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 386{
0a9b88b7 387 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
388 return NULL;
389
25fd2f7a 390 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
391 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
392}
393
159b3212 394static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0
JK
395{
396 if (cache_name_pos(pathname, len) >= 0)
397 return NULL;
398
25fd2f7a 399 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
400 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
401}
402
09595258
LT
403enum exist_status {
404 index_nonexistent = 0,
405 index_directory,
406 index_gitdir,
407};
408
409/*
410 * The index sorts alphabetically by entry name, which
411 * means that a gitlink sorts as '\0' at the end, while
412 * a directory (which is defined not as an entry, but as
413 * the files it contains) will sort with the '/' at the
414 * end.
415 */
416static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd
LT
417{
418 int pos = cache_name_pos(dirname, len);
09595258
LT
419 if (pos < 0)
420 pos = -pos-1;
421 while (pos < active_nr) {
422 struct cache_entry *ce = active_cache[pos++];
423 unsigned char endchar;
424
425 if (strncmp(ce->name, dirname, len))
426 break;
427 endchar = ce->name[len];
428 if (endchar > '/')
429 break;
430 if (endchar == '/')
431 return index_directory;
7a51ed66 432 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
433 return index_gitdir;
434 }
435 return index_nonexistent;
436}
437
438/*
439 * When we find a directory when traversing the filesystem, we
440 * have three distinct cases:
441 *
442 * - ignore it
443 * - see it as a directory
444 * - recurse into it
445 *
446 * and which one we choose depends on a combination of existing
447 * git index contents and the flags passed into the directory
448 * traversal routine.
449 *
450 * Case 1: If we *already* have entries in the index under that
451 * directory name, we always recurse into the directory to see
452 * all the files.
453 *
454 * Case 2: If we *already* have that directory name as a gitlink,
455 * we always continue to see it as a gitlink, regardless of whether
456 * there is an actual git directory there or not (it might not
457 * be checked out as a subproject!)
458 *
459 * Case 3: if we didn't have it in the index previously, we
460 * have a few sub-cases:
461 *
462 * (a) if "show_other_directories" is true, we show it as
463 * just a directory, unless "hide_empty_directories" is
464 * also true and the directory is empty, in which case
465 * we just ignore it entirely.
466 * (b) if it looks like a git directory, and we don't have
302b9282 467 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
468 * as a directory.
469 * (c) otherwise, we recurse into it.
470 */
471enum directory_treatment {
472 show_directory,
473 ignore_directory,
474 recurse_into_directory,
475};
476
477static enum directory_treatment treat_directory(struct dir_struct *dir,
478 const char *dirname, int len,
479 const struct path_simplify *simplify)
480{
481 /* The "len-1" is to strip the final '/' */
482 switch (directory_exists_in_index(dirname, len-1)) {
483 case index_directory:
484 return recurse_into_directory;
485
486 case index_gitdir:
ab22aed3
LT
487 if (dir->show_other_directories)
488 return ignore_directory;
09595258
LT
489 return show_directory;
490
491 case index_nonexistent:
492 if (dir->show_other_directories)
493 break;
302b9282 494 if (!dir->no_gitlinks) {
09595258
LT
495 unsigned char sha1[20];
496 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
497 return show_directory;
498 }
499 return recurse_into_directory;
500 }
501
502 /* This is the "show_other_directories" case */
503 if (!dir->hide_empty_directories)
504 return show_directory;
505 if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
506 return ignore_directory;
507 return show_directory;
453ec4bd
LT
508}
509
9fc42d60
LT
510/*
511 * This is an inexact early pruning of any recursive directory
512 * reading - if the path cannot possibly be in the pathspec,
513 * return true, and we'll skip it early.
514 */
515static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
516{
517 if (simplify) {
518 for (;;) {
519 const char *match = simplify->path;
520 int len = simplify->len;
521
522 if (!match)
523 break;
524 if (len > pathlen)
525 len = pathlen;
526 if (!memcmp(path, match, len))
527 return 0;
528 simplify++;
529 }
530 return 1;
531 }
532 return 0;
533}
534
e96980ef
JK
535static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
536{
537 if (simplify) {
538 for (; simplify->path; simplify++) {
539 if (len == simplify->len
540 && !memcmp(path, simplify->path, len))
541 return 1;
542 }
543 }
544 return 0;
545}
546
07134421
LT
547static int get_dtype(struct dirent *de, const char *path)
548{
6831a88a 549 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
550 struct stat st;
551
552 if (dtype != DT_UNKNOWN)
553 return dtype;
554 if (lstat(path, &st))
555 return dtype;
556 if (S_ISREG(st.st_mode))
557 return DT_REG;
558 if (S_ISDIR(st.st_mode))
559 return DT_DIR;
560 if (S_ISLNK(st.st_mode))
561 return DT_LNK;
562 return dtype;
563}
564
453ec4bd
LT
565/*
566 * Read a directory tree. We currently ignore anything but
567 * directories, regular files and symlinks. That's because git
568 * doesn't handle them at all yet. Maybe that will change some
569 * day.
570 *
571 * Also, we ignore the name ".git" (even if it is not a directory).
572 * That likely will not change.
573 */
9fc42d60 574static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
453ec4bd
LT
575{
576 DIR *fdir = opendir(path);
577 int contents = 0;
578
579 if (fdir) {
453ec4bd 580 struct dirent *de;
095c424d 581 char fullname[PATH_MAX + 1];
453ec4bd
LT
582 memcpy(fullname, base, baselen);
583
453ec4bd 584 while ((de = readdir(fdir)) != NULL) {
07134421 585 int len, dtype;
b9916256 586 int exclude;
453ec4bd
LT
587
588 if ((de->d_name[0] == '.') &&
589 (de->d_name[1] == 0 ||
590 !strcmp(de->d_name + 1, ".") ||
591 !strcmp(de->d_name + 1, "git")))
592 continue;
593 len = strlen(de->d_name);
5d5cea67
LT
594 /* Ignore overly long pathnames! */
595 if (len + baselen + 8 > sizeof(fullname))
596 continue;
453ec4bd 597 memcpy(fullname + baselen, de->d_name, len+1);
9fc42d60
LT
598 if (simplify_away(fullname, baselen + len, simplify))
599 continue;
b9916256 600
6831a88a
JH
601 dtype = DTYPE(de);
602 exclude = excluded(dir, fullname, &dtype);
e96980ef
JK
603 if (exclude && dir->collect_ignored
604 && in_pathspec(fullname, baselen + len, simplify))
2abd31b0 605 dir_add_ignored(dir, fullname, baselen + len);
07134421
LT
606
607 /*
608 * Excluded? If we don't explicitly want to show
609 * ignored files, ignore it
610 */
611 if (exclude && !dir->show_ignored)
612 continue;
613
6831a88a
JH
614 if (dtype == DT_UNKNOWN)
615 dtype = get_dtype(de, fullname);
07134421
LT
616
617 /*
618 * Do we want to see just the ignored files?
619 * We still need to recurse into directories,
620 * even if we don't ignore them, since the
621 * directory may contain files that we do..
622 */
623 if (!exclude && dir->show_ignored) {
624 if (dtype != DT_DIR)
c889763b 625 continue;
c889763b 626 }
453ec4bd 627
07134421 628 switch (dtype) {
453ec4bd
LT
629 default:
630 continue;
453ec4bd
LT
631 case DT_DIR:
632 memcpy(fullname + baselen + len, "/", 2);
633 len++;
09595258
LT
634 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
635 case show_directory:
b9916256
MS
636 if (exclude != dir->show_ignored)
637 continue;
453ec4bd 638 break;
09595258
LT
639 case recurse_into_directory:
640 contents += read_directory_recursive(dir,
641 fullname, fullname, baselen + len, 0, simplify);
642 continue;
643 case ignore_directory:
644 continue;
453ec4bd 645 }
09595258 646 break;
453ec4bd
LT
647 case DT_REG:
648 case DT_LNK:
649 break;
650 }
453ec4bd 651 contents++;
07ccbff8
JS
652 if (check_only)
653 goto exit_early;
654 else
4d06f8ac 655 dir_add_name(dir, fullname, baselen + len);
453ec4bd 656 }
07ccbff8 657exit_early:
453ec4bd 658 closedir(fdir);
453ec4bd
LT
659 }
660
661 return contents;
662}
663
664static int cmp_name(const void *p1, const void *p2)
665{
666 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
667 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
668
669 return cache_name_compare(e1->name, e1->len,
670 e2->name, e2->len);
671}
672
9fc42d60
LT
673/*
674 * Return the length of the "simple" part of a path match limiter.
675 */
676static int simple_length(const char *match)
453ec4bd 677{
9fc42d60
LT
678 int len = -1;
679
680 for (;;) {
681 unsigned char c = *match++;
682 len++;
63e8aea7 683 if (isspecial(c))
9fc42d60
LT
684 return len;
685 }
686}
687
688static struct path_simplify *create_simplify(const char **pathspec)
689{
690 int nr, alloc = 0;
691 struct path_simplify *simplify = NULL;
692
693 if (!pathspec)
694 return NULL;
695
696 for (nr = 0 ; ; nr++) {
697 const char *match;
698 if (nr >= alloc) {
699 alloc = alloc_nr(alloc);
700 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
701 }
702 match = *pathspec++;
703 if (!match)
704 break;
705 simplify[nr].path = match;
706 simplify[nr].len = simple_length(match);
707 }
708 simplify[nr].path = NULL;
709 simplify[nr].len = 0;
710 return simplify;
711}
712
713static void free_simplify(struct path_simplify *simplify)
714{
8e0f7003 715 free(simplify);
9fc42d60
LT
716}
717
718int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
719{
725b0605 720 struct path_simplify *simplify;
b4189aa8 721
725b0605
JH
722 if (has_symlink_leading_path(strlen(path), path))
723 return dir->nr;
724
725 simplify = create_simplify(pathspec);
9fc42d60
LT
726 read_directory_recursive(dir, path, base, baselen, 0, simplify);
727 free_simplify(simplify);
453ec4bd 728 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 729 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
730 return dir->nr;
731}
c91f0d92 732
686a4a06 733int file_exists(const char *f)
c91f0d92 734{
686a4a06 735 struct stat sb;
a50f9fc5 736 return lstat(f, &sb) == 0;
c91f0d92 737}
e6636747
JS
738
739/*
740 * get_relative_cwd() gets the prefix of the current working directory
741 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
742 *
743 * As a convenience, it also returns NULL if 'dir' is already NULL. The
744 * reason for this behaviour is that it is natural for functions returning
745 * directory names to return NULL to say "this directory does not exist"
746 * or "this directory is invalid". These cases are usually handled the
747 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
748 * returns NULL for both of them.
749 *
750 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
751 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
752 */
753char *get_relative_cwd(char *buffer, int size, const char *dir)
754{
755 char *cwd = buffer;
756
e6636747
JS
757 if (!dir)
758 return NULL;
759 if (!getcwd(buffer, size))
760 die("can't find the current directory: %s", strerror(errno));
761
762 if (!is_absolute_path(dir))
763 dir = make_absolute_path(dir);
764
765 while (*dir && *dir == *cwd) {
766 dir++;
767 cwd++;
768 }
769 if (*dir)
770 return NULL;
771 if (*cwd == '/')
772 return cwd + 1;
773 return cwd;
774}
775
776int is_inside_dir(const char *dir)
777{
778 char buffer[PATH_MAX];
779 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
780}
7155b727
JS
781
782int remove_dir_recursively(struct strbuf *path, int only_empty)
783{
784 DIR *dir = opendir(path->buf);
785 struct dirent *e;
786 int ret = 0, original_len = path->len, len;
787
788 if (!dir)
789 return -1;
790 if (path->buf[original_len - 1] != '/')
791 strbuf_addch(path, '/');
792
793 len = path->len;
794 while ((e = readdir(dir)) != NULL) {
795 struct stat st;
796 if ((e->d_name[0] == '.') &&
797 ((e->d_name[1] == 0) ||
798 ((e->d_name[1] == '.') && e->d_name[2] == 0)))
799 continue; /* "." and ".." */
800
801 strbuf_setlen(path, len);
802 strbuf_addstr(path, e->d_name);
803 if (lstat(path->buf, &st))
804 ; /* fall thru */
805 else if (S_ISDIR(st.st_mode)) {
806 if (!remove_dir_recursively(path, only_empty))
807 continue; /* happy */
808 } else if (!only_empty && !unlink(path->buf))
809 continue; /* happy, too */
810
811 /* path too long, stat fails, or non-directory still exists */
812 ret = -1;
813 break;
814 }
815 closedir(dir);
816
817 strbuf_setlen(path, original_len);
818 if (!ret)
819 ret = rmdir(path->buf);
820 return ret;
821}
039bc64e
JH
822
823void setup_standard_excludes(struct dir_struct *dir)
824{
825 const char *path;
826
827 dir->exclude_per_dir = ".gitignore";
828 path = git_path("info/exclude");
829 if (!access(path, R_OK))
830 add_excludes_from_file(dir, path);
831 if (excludes_file && !access(excludes_file, R_OK))
832 add_excludes_from_file(dir, excludes_file);
833}
4a92d1bf
AR
834
835int remove_path(const char *name)
836{
837 char *slash;
838
839 if (unlink(name) && errno != ENOENT)
840 return -1;
841
842 slash = strrchr(name, '/');
843 if (slash) {
844 char *dirs = xstrdup(name);
845 slash = dirs + (slash - name);
846 do {
847 *slash = '\0';
848 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
849 free(dirs);
850 }
851 return 0;
852}
853