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