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