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