Allow cloning to an existing empty directory
[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
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 587
8ca12c0d
AP
588 if (is_dot_or_dotdot(de->d_name) ||
589 !strcmp(de->d_name, ".git"))
453ec4bd
LT
590 continue;
591 len = strlen(de->d_name);
5d5cea67
LT
592 /* Ignore overly long pathnames! */
593 if (len + baselen + 8 > sizeof(fullname))
594 continue;
453ec4bd 595 memcpy(fullname + baselen, de->d_name, len+1);
9fc42d60
LT
596 if (simplify_away(fullname, baselen + len, simplify))
597 continue;
b9916256 598
6831a88a
JH
599 dtype = DTYPE(de);
600 exclude = excluded(dir, fullname, &dtype);
e96980ef
JK
601 if (exclude && dir->collect_ignored
602 && in_pathspec(fullname, baselen + len, simplify))
2abd31b0 603 dir_add_ignored(dir, fullname, baselen + len);
07134421
LT
604
605 /*
606 * Excluded? If we don't explicitly want to show
607 * ignored files, ignore it
608 */
609 if (exclude && !dir->show_ignored)
610 continue;
611
6831a88a
JH
612 if (dtype == DT_UNKNOWN)
613 dtype = get_dtype(de, fullname);
07134421
LT
614
615 /*
616 * Do we want to see just the ignored files?
617 * We still need to recurse into directories,
618 * even if we don't ignore them, since the
619 * directory may contain files that we do..
620 */
621 if (!exclude && dir->show_ignored) {
622 if (dtype != DT_DIR)
c889763b 623 continue;
c889763b 624 }
453ec4bd 625
07134421 626 switch (dtype) {
453ec4bd
LT
627 default:
628 continue;
453ec4bd
LT
629 case DT_DIR:
630 memcpy(fullname + baselen + len, "/", 2);
631 len++;
09595258
LT
632 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
633 case show_directory:
b9916256
MS
634 if (exclude != dir->show_ignored)
635 continue;
453ec4bd 636 break;
09595258
LT
637 case recurse_into_directory:
638 contents += read_directory_recursive(dir,
639 fullname, fullname, baselen + len, 0, simplify);
640 continue;
641 case ignore_directory:
642 continue;
453ec4bd 643 }
09595258 644 break;
453ec4bd
LT
645 case DT_REG:
646 case DT_LNK:
647 break;
648 }
453ec4bd 649 contents++;
07ccbff8
JS
650 if (check_only)
651 goto exit_early;
652 else
4d06f8ac 653 dir_add_name(dir, fullname, baselen + len);
453ec4bd 654 }
07ccbff8 655exit_early:
453ec4bd 656 closedir(fdir);
453ec4bd
LT
657 }
658
659 return contents;
660}
661
662static int cmp_name(const void *p1, const void *p2)
663{
664 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
665 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
666
667 return cache_name_compare(e1->name, e1->len,
668 e2->name, e2->len);
669}
670
9fc42d60
LT
671/*
672 * Return the length of the "simple" part of a path match limiter.
673 */
674static int simple_length(const char *match)
453ec4bd 675{
9fc42d60
LT
676 int len = -1;
677
678 for (;;) {
679 unsigned char c = *match++;
680 len++;
63e8aea7 681 if (isspecial(c))
9fc42d60
LT
682 return len;
683 }
684}
685
686static struct path_simplify *create_simplify(const char **pathspec)
687{
688 int nr, alloc = 0;
689 struct path_simplify *simplify = NULL;
690
691 if (!pathspec)
692 return NULL;
693
694 for (nr = 0 ; ; nr++) {
695 const char *match;
696 if (nr >= alloc) {
697 alloc = alloc_nr(alloc);
698 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
699 }
700 match = *pathspec++;
701 if (!match)
702 break;
703 simplify[nr].path = match;
704 simplify[nr].len = simple_length(match);
705 }
706 simplify[nr].path = NULL;
707 simplify[nr].len = 0;
708 return simplify;
709}
710
711static void free_simplify(struct path_simplify *simplify)
712{
8e0f7003 713 free(simplify);
9fc42d60
LT
714}
715
716int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
717{
725b0605 718 struct path_simplify *simplify;
b4189aa8 719
725b0605
JH
720 if (has_symlink_leading_path(strlen(path), path))
721 return dir->nr;
722
723 simplify = create_simplify(pathspec);
9fc42d60
LT
724 read_directory_recursive(dir, path, base, baselen, 0, simplify);
725 free_simplify(simplify);
453ec4bd 726 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 727 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
728 return dir->nr;
729}
c91f0d92 730
686a4a06 731int file_exists(const char *f)
c91f0d92 732{
686a4a06 733 struct stat sb;
a50f9fc5 734 return lstat(f, &sb) == 0;
c91f0d92 735}
e6636747
JS
736
737/*
738 * get_relative_cwd() gets the prefix of the current working directory
739 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
740 *
741 * As a convenience, it also returns NULL if 'dir' is already NULL. The
742 * reason for this behaviour is that it is natural for functions returning
743 * directory names to return NULL to say "this directory does not exist"
744 * or "this directory is invalid". These cases are usually handled the
745 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
746 * returns NULL for both of them.
747 *
748 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
749 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
750 */
751char *get_relative_cwd(char *buffer, int size, const char *dir)
752{
753 char *cwd = buffer;
754
e6636747
JS
755 if (!dir)
756 return NULL;
757 if (!getcwd(buffer, size))
758 die("can't find the current directory: %s", strerror(errno));
759
760 if (!is_absolute_path(dir))
761 dir = make_absolute_path(dir);
762
763 while (*dir && *dir == *cwd) {
764 dir++;
765 cwd++;
766 }
767 if (*dir)
768 return NULL;
769 if (*cwd == '/')
770 return cwd + 1;
771 return cwd;
772}
773
774int is_inside_dir(const char *dir)
775{
776 char buffer[PATH_MAX];
777 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
778}
7155b727 779
55892d23
AP
780int is_empty_dir(const char *path)
781{
782 DIR *dir = opendir(path);
783 struct dirent *e;
784 int ret = 1;
785
786 if (!dir)
787 return 0;
788
789 while ((e = readdir(dir)) != NULL)
790 if (!is_dot_or_dotdot(e->d_name)) {
791 ret = 0;
792 break;
793 }
794
795 closedir(dir);
796 return ret;
797}
798
7155b727
JS
799int remove_dir_recursively(struct strbuf *path, int only_empty)
800{
801 DIR *dir = opendir(path->buf);
802 struct dirent *e;
803 int ret = 0, original_len = path->len, len;
804
805 if (!dir)
806 return -1;
807 if (path->buf[original_len - 1] != '/')
808 strbuf_addch(path, '/');
809
810 len = path->len;
811 while ((e = readdir(dir)) != NULL) {
812 struct stat st;
8ca12c0d
AP
813 if (is_dot_or_dotdot(e->d_name))
814 continue;
7155b727
JS
815
816 strbuf_setlen(path, len);
817 strbuf_addstr(path, e->d_name);
818 if (lstat(path->buf, &st))
819 ; /* fall thru */
820 else if (S_ISDIR(st.st_mode)) {
821 if (!remove_dir_recursively(path, only_empty))
822 continue; /* happy */
823 } else if (!only_empty && !unlink(path->buf))
824 continue; /* happy, too */
825
826 /* path too long, stat fails, or non-directory still exists */
827 ret = -1;
828 break;
829 }
830 closedir(dir);
831
832 strbuf_setlen(path, original_len);
833 if (!ret)
834 ret = rmdir(path->buf);
835 return ret;
836}
039bc64e
JH
837
838void setup_standard_excludes(struct dir_struct *dir)
839{
840 const char *path;
841
842 dir->exclude_per_dir = ".gitignore";
843 path = git_path("info/exclude");
844 if (!access(path, R_OK))
845 add_excludes_from_file(dir, path);
846 if (excludes_file && !access(excludes_file, R_OK))
847 add_excludes_from_file(dir, excludes_file);
848}
4a92d1bf
AR
849
850int remove_path(const char *name)
851{
852 char *slash;
853
854 if (unlink(name) && errno != ENOENT)
855 return -1;
856
857 slash = strrchr(name, '/');
858 if (slash) {
859 char *dirs = xstrdup(name);
860 slash = dirs + (slash - name);
861 do {
862 *slash = '\0';
863 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
864 free(dirs);
865 }
866 return 0;
867}
868