]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
Merge branch 'rr/core-tutorial'
[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
dba2e203 17static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
09595258 18 int check_only, const struct path_simplify *simplify);
caa6b782 19static int get_dtype(struct dirent *de, const char *path, int len);
09595258 20
1d8842d9 21static int common_prefix(const char **pathspec)
3c6a370b
LT
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
1d8842d9
LT
54int fill_directory(struct dir_struct *dir, const char **pathspec)
55{
dba2e203
LT
56 const char *path;
57 int len;
1d8842d9
LT
58
59 /*
60 * Calculate common prefix for the pathspec, and
61 * use that to optimize the directory walk
62 */
dba2e203 63 len = common_prefix(pathspec);
1d8842d9 64 path = "";
1d8842d9 65
dba2e203
LT
66 if (len)
67 path = xmemdupz(*pathspec, len);
1d8842d9
LT
68
69 /* Read the directory and prune it */
dba2e203
LT
70 read_directory(dir, path, len, pathspec);
71 return len;
1d8842d9
LT
72}
73
e813d50e 74/*
2c5b0115 75 * Does 'match' match the given name?
e813d50e
JH
76 * A match is found if
77 *
78 * (1) the 'match' string is leading directory of 'name', or
79 * (2) the 'match' string is a wildcard and matches 'name', or
80 * (3) the 'match' string is exactly the same as 'name'.
81 *
82 * and the return value tells which case it was.
83 *
84 * It returns 0 when there is no match.
85 */
3c6a370b
LT
86static int match_one(const char *match, const char *name, int namelen)
87{
88 int matchlen;
89
90 /* If the match was just the prefix, we matched */
88ea8112 91 if (!*match)
e813d50e 92 return MATCHED_RECURSIVELY;
3c6a370b 93
88ea8112
LT
94 for (;;) {
95 unsigned char c1 = *match;
96 unsigned char c2 = *name;
8cc32992 97 if (c1 == '\0' || is_glob_special(c1))
88ea8112
LT
98 break;
99 if (c1 != c2)
100 return 0;
101 match++;
102 name++;
103 namelen--;
104 }
105
106
3c6a370b
LT
107 /*
108 * If we don't match the matchstring exactly,
109 * we need to match by fnmatch
110 */
88ea8112 111 matchlen = strlen(match);
3c6a370b 112 if (strncmp(match, name, matchlen))
e813d50e 113 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
3c6a370b 114
f2d0df71 115 if (namelen == matchlen)
e813d50e
JH
116 return MATCHED_EXACTLY;
117 if (match[matchlen-1] == '/' || name[matchlen] == '/')
118 return MATCHED_RECURSIVELY;
119 return 0;
3c6a370b
LT
120}
121
e813d50e
JH
122/*
123 * Given a name and a list of pathspecs, see if the name matches
124 * any of the pathspecs. The caller is also interested in seeing
125 * all pathspec matches some names it calls this function with
126 * (otherwise the user could have mistyped the unmatched pathspec),
127 * and a mark is left in seen[] array for pathspec element that
128 * actually matched anything.
129 */
0b50922a
CB
130int match_pathspec(const char **pathspec, const char *name, int namelen,
131 int prefix, char *seen)
3c6a370b 132{
0b50922a
CB
133 int i, retval = 0;
134
135 if (!pathspec)
136 return 1;
3c6a370b
LT
137
138 name += prefix;
139 namelen -= prefix;
140
0b50922a 141 for (i = 0; pathspec[i] != NULL; i++) {
e813d50e 142 int how;
0b50922a
CB
143 const char *match = pathspec[i] + prefix;
144 if (seen && seen[i] == MATCHED_EXACTLY)
3c6a370b 145 continue;
e813d50e
JH
146 how = match_one(match, name, namelen);
147 if (how) {
148 if (retval < how)
149 retval = how;
0b50922a
CB
150 if (seen && seen[i] < how)
151 seen[i] = how;
3c6a370b
LT
152 }
153 }
154 return retval;
155}
156
68492fc7
LK
157static int no_wildcard(const char *string)
158{
dd482eea 159 return string[strcspn(string, "*?[{\\")] == '\0';
68492fc7
LK
160}
161
453ec4bd
LT
162void add_exclude(const char *string, const char *base,
163 int baselen, struct exclude_list *which)
164{
d6b8fc30
JH
165 struct exclude *x;
166 size_t len;
167 int to_exclude = 1;
168 int flags = 0;
453ec4bd 169
68492fc7 170 if (*string == '!') {
d6b8fc30 171 to_exclude = 0;
68492fc7
LK
172 string++;
173 }
d6b8fc30
JH
174 len = strlen(string);
175 if (len && string[len - 1] == '/') {
176 char *s;
177 x = xmalloc(sizeof(*x) + len);
4b25d091 178 s = (char *)(x+1);
d6b8fc30
JH
179 memcpy(s, string, len - 1);
180 s[len - 1] = '\0';
181 string = s;
182 x->pattern = s;
183 flags = EXC_FLAG_MUSTBEDIR;
184 } else {
185 x = xmalloc(sizeof(*x));
186 x->pattern = string;
187 }
188 x->to_exclude = to_exclude;
68492fc7 189 x->patternlen = strlen(string);
453ec4bd
LT
190 x->base = base;
191 x->baselen = baselen;
d6b8fc30 192 x->flags = flags;
68492fc7
LK
193 if (!strchr(string, '/'))
194 x->flags |= EXC_FLAG_NODIR;
195 if (no_wildcard(string))
196 x->flags |= EXC_FLAG_NOWILDCARD;
197 if (*string == '*' && no_wildcard(string+1))
198 x->flags |= EXC_FLAG_ENDSWITH;
686a4a06 199 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
453ec4bd
LT
200 which->excludes[which->nr++] = x;
201}
202
c28b3d6e
NTND
203static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
204{
205 int pos, len;
206 unsigned long sz;
207 enum object_type type;
208 void *data;
209 struct index_state *istate = &the_index;
210
211 len = strlen(path);
212 pos = index_name_pos(istate, path, len);
213 if (pos < 0)
214 return NULL;
215 if (!ce_skip_worktree(istate->cache[pos]))
216 return NULL;
217 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
218 if (!data || type != OBJ_BLOB) {
219 free(data);
220 return NULL;
221 }
222 *size = xsize_t(sz);
223 return data;
224}
225
cb097534
NTND
226int add_excludes_from_file_to_list(const char *fname,
227 const char *base,
228 int baselen,
229 char **buf_p,
230 struct exclude_list *which,
231 int check_index)
453ec4bd 232{
c470701a 233 struct stat st;
453ec4bd 234 int fd, i;
dc49cd76 235 size_t size;
453ec4bd
LT
236 char *buf, *entry;
237
238 fd = open(fname, O_RDONLY);
c28b3d6e
NTND
239 if (fd < 0 || fstat(fd, &st) < 0) {
240 if (0 <= fd)
241 close(fd);
242 if (!check_index ||
243 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
244 return -1;
453ec4bd 245 }
c28b3d6e
NTND
246 else {
247 size = xsize_t(st.st_size);
248 if (size == 0) {
249 close(fd);
250 return 0;
251 }
252 buf = xmalloc(size);
253 if (read_in_full(fd, buf, size) != size) {
254 close(fd);
255 return -1;
256 }
257 close(fd);
6ba78238 258 }
453ec4bd 259
63d285c8
JH
260 if (buf_p)
261 *buf_p = buf;
453ec4bd 262 entry = buf;
b5041c5f
NTND
263 for (i = 0; i <= size; i++) {
264 if (i == size || buf[i] == '\n') {
453ec4bd
LT
265 if (entry != buf + i && entry[0] != '#') {
266 buf[i - (i && buf[i-1] == '\r')] = 0;
267 add_exclude(entry, base, baselen, which);
268 }
269 entry = buf + i + 1;
270 }
271 }
272 return 0;
453ec4bd
LT
273}
274
275void add_excludes_from_file(struct dir_struct *dir, const char *fname)
276{
cb097534
NTND
277 if (add_excludes_from_file_to_list(fname, "", 0, NULL,
278 &dir->exclude_list[EXC_FILE], 0) < 0)
453ec4bd
LT
279 die("cannot use %s as an exclude file", fname);
280}
281
63d285c8 282static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 283{
63d285c8
JH
284 struct exclude_list *el;
285 struct exclude_stack *stk = NULL;
286 int current;
287
288 if ((!dir->exclude_per_dir) ||
289 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
290 return; /* too long a path -- ignore */
291
292 /* Pop the ones that are not the prefix of the path being checked. */
293 el = &dir->exclude_list[EXC_DIRS];
294 while ((stk = dir->exclude_stack) != NULL) {
295 if (stk->baselen <= baselen &&
296 !strncmp(dir->basebuf, base, stk->baselen))
297 break;
298 dir->exclude_stack = stk->prev;
299 while (stk->exclude_ix < el->nr)
300 free(el->excludes[--el->nr]);
301 free(stk->filebuf);
302 free(stk);
453ec4bd 303 }
453ec4bd 304
63d285c8
JH
305 /* Read from the parent directories and push them down. */
306 current = stk ? stk->baselen : -1;
307 while (current < baselen) {
308 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
309 const char *cp;
453ec4bd 310
63d285c8
JH
311 if (current < 0) {
312 cp = base;
313 current = 0;
314 }
315 else {
316 cp = strchr(base + current + 1, '/');
317 if (!cp)
318 die("oops in prep_exclude");
319 cp++;
320 }
321 stk->prev = dir->exclude_stack;
322 stk->baselen = cp - base;
323 stk->exclude_ix = el->nr;
324 memcpy(dir->basebuf + current, base + current,
325 stk->baselen - current);
326 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
cb097534
NTND
327 add_excludes_from_file_to_list(dir->basebuf,
328 dir->basebuf, stk->baselen,
329 &stk->filebuf, el, 1);
63d285c8
JH
330 dir->exclude_stack = stk;
331 current = stk->baselen;
332 }
333 dir->basebuf[baselen] = '\0';
453ec4bd
LT
334}
335
2c5b0115 336/* Scan the list and let the last match determine the fate.
453ec4bd
LT
337 * Return 1 for exclude, 0 for include and -1 for undecided.
338 */
cb097534
NTND
339int excluded_from_list(const char *pathname,
340 int pathlen, const char *basename, int *dtype,
341 struct exclude_list *el)
453ec4bd
LT
342{
343 int i;
344
345 if (el->nr) {
346 for (i = el->nr - 1; 0 <= i; i--) {
347 struct exclude *x = el->excludes[i];
348 const char *exclude = x->pattern;
68492fc7 349 int to_exclude = x->to_exclude;
453ec4bd 350
6831a88a 351 if (x->flags & EXC_FLAG_MUSTBEDIR) {
c84de707
NTND
352 if (!dtype) {
353 if (!prefixcmp(pathname, exclude))
354 return to_exclude;
355 else
356 continue;
357 }
6831a88a 358 if (*dtype == DT_UNKNOWN)
caa6b782 359 *dtype = get_dtype(NULL, pathname, pathlen);
6831a88a
JH
360 if (*dtype != DT_DIR)
361 continue;
362 }
d6b8fc30 363
68492fc7 364 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 365 /* match basename */
68492fc7
LK
366 if (x->flags & EXC_FLAG_NOWILDCARD) {
367 if (!strcmp(exclude, basename))
368 return to_exclude;
369 } else if (x->flags & EXC_FLAG_ENDSWITH) {
370 if (x->patternlen - 1 <= pathlen &&
371 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
372 return to_exclude;
373 } else {
374 if (fnmatch(exclude, basename, 0) == 0)
375 return to_exclude;
376 }
453ec4bd
LT
377 }
378 else {
379 /* match with FNM_PATHNAME:
380 * exclude has base (baselen long) implicitly
381 * in front of it.
382 */
383 int baselen = x->baselen;
384 if (*exclude == '/')
385 exclude++;
386
387 if (pathlen < baselen ||
388 (baselen && pathname[baselen-1] != '/') ||
389 strncmp(pathname, x->base, baselen))
390 continue;
391
68492fc7
LK
392 if (x->flags & EXC_FLAG_NOWILDCARD) {
393 if (!strcmp(exclude, pathname + baselen))
394 return to_exclude;
395 } else {
396 if (fnmatch(exclude, pathname+baselen,
397 FNM_PATHNAME) == 0)
398 return to_exclude;
399 }
453ec4bd
LT
400 }
401 }
402 }
403 return -1; /* undecided */
404}
405
6831a88a 406int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
407{
408 int pathlen = strlen(pathname);
409 int st;
68492fc7
LK
410 const char *basename = strrchr(pathname, '/');
411 basename = (basename) ? basename+1 : pathname;
453ec4bd 412
63d285c8 413 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 414 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
cb097534
NTND
415 switch (excluded_from_list(pathname, pathlen, basename,
416 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
417 case 0:
418 return 0;
419 case 1:
420 return 1;
421 }
422 }
423 return 0;
424}
425
f3fa1838
JH
426static struct dir_entry *dir_entry_new(const char *pathname, int len)
427{
453ec4bd
LT
428 struct dir_entry *ent;
429
453ec4bd
LT
430 ent = xmalloc(sizeof(*ent) + len + 1);
431 ent->len = len;
432 memcpy(ent->name, pathname, len);
433 ent->name[len] = 0;
4d06f8ac 434 return ent;
453ec4bd
LT
435}
436
159b3212 437static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 438{
0a9b88b7 439 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
440 return NULL;
441
25fd2f7a 442 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
443 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
444}
445
159b3212 446static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 447{
6e4f981f 448 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
449 return NULL;
450
25fd2f7a 451 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
452 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
453}
454
09595258
LT
455enum exist_status {
456 index_nonexistent = 0,
457 index_directory,
458 index_gitdir,
459};
460
461/*
462 * The index sorts alphabetically by entry name, which
463 * means that a gitlink sorts as '\0' at the end, while
464 * a directory (which is defined not as an entry, but as
465 * the files it contains) will sort with the '/' at the
466 * end.
467 */
468static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd
LT
469{
470 int pos = cache_name_pos(dirname, len);
09595258
LT
471 if (pos < 0)
472 pos = -pos-1;
473 while (pos < active_nr) {
474 struct cache_entry *ce = active_cache[pos++];
475 unsigned char endchar;
476
477 if (strncmp(ce->name, dirname, len))
478 break;
479 endchar = ce->name[len];
480 if (endchar > '/')
481 break;
482 if (endchar == '/')
483 return index_directory;
7a51ed66 484 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
485 return index_gitdir;
486 }
487 return index_nonexistent;
488}
489
490/*
491 * When we find a directory when traversing the filesystem, we
492 * have three distinct cases:
493 *
494 * - ignore it
495 * - see it as a directory
496 * - recurse into it
497 *
498 * and which one we choose depends on a combination of existing
499 * git index contents and the flags passed into the directory
500 * traversal routine.
501 *
502 * Case 1: If we *already* have entries in the index under that
503 * directory name, we always recurse into the directory to see
504 * all the files.
505 *
506 * Case 2: If we *already* have that directory name as a gitlink,
507 * we always continue to see it as a gitlink, regardless of whether
508 * there is an actual git directory there or not (it might not
509 * be checked out as a subproject!)
510 *
511 * Case 3: if we didn't have it in the index previously, we
512 * have a few sub-cases:
513 *
514 * (a) if "show_other_directories" is true, we show it as
515 * just a directory, unless "hide_empty_directories" is
516 * also true and the directory is empty, in which case
517 * we just ignore it entirely.
518 * (b) if it looks like a git directory, and we don't have
302b9282 519 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
520 * as a directory.
521 * (c) otherwise, we recurse into it.
522 */
523enum directory_treatment {
524 show_directory,
525 ignore_directory,
526 recurse_into_directory,
527};
528
529static enum directory_treatment treat_directory(struct dir_struct *dir,
530 const char *dirname, int len,
531 const struct path_simplify *simplify)
532{
533 /* The "len-1" is to strip the final '/' */
534 switch (directory_exists_in_index(dirname, len-1)) {
535 case index_directory:
536 return recurse_into_directory;
537
538 case index_gitdir:
7c4c97c0 539 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
ab22aed3 540 return ignore_directory;
09595258
LT
541 return show_directory;
542
543 case index_nonexistent:
7c4c97c0 544 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 545 break;
7c4c97c0 546 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
547 unsigned char sha1[20];
548 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
549 return show_directory;
550 }
551 return recurse_into_directory;
552 }
553
554 /* This is the "show_other_directories" case */
7c4c97c0 555 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
09595258 556 return show_directory;
dba2e203 557 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
09595258
LT
558 return ignore_directory;
559 return show_directory;
453ec4bd
LT
560}
561
9fc42d60
LT
562/*
563 * This is an inexact early pruning of any recursive directory
564 * reading - if the path cannot possibly be in the pathspec,
565 * return true, and we'll skip it early.
566 */
567static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
568{
569 if (simplify) {
570 for (;;) {
571 const char *match = simplify->path;
572 int len = simplify->len;
573
574 if (!match)
575 break;
576 if (len > pathlen)
577 len = pathlen;
578 if (!memcmp(path, match, len))
579 return 0;
580 simplify++;
581 }
582 return 1;
583 }
584 return 0;
585}
586
e96980ef
JK
587static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
588{
589 if (simplify) {
590 for (; simplify->path; simplify++) {
591 if (len == simplify->len
592 && !memcmp(path, simplify->path, len))
593 return 1;
594 }
595 }
596 return 0;
597}
598
443e061a
LT
599static int get_index_dtype(const char *path, int len)
600{
601 int pos;
602 struct cache_entry *ce;
603
604 ce = cache_name_exists(path, len, 0);
605 if (ce) {
606 if (!ce_uptodate(ce))
607 return DT_UNKNOWN;
608 if (S_ISGITLINK(ce->ce_mode))
609 return DT_DIR;
610 /*
611 * Nobody actually cares about the
612 * difference between DT_LNK and DT_REG
613 */
614 return DT_REG;
615 }
616
617 /* Try to look it up as a directory */
618 pos = cache_name_pos(path, len);
619 if (pos >= 0)
620 return DT_UNKNOWN;
621 pos = -pos-1;
622 while (pos < active_nr) {
623 ce = active_cache[pos++];
624 if (strncmp(ce->name, path, len))
625 break;
626 if (ce->name[len] > '/')
627 break;
628 if (ce->name[len] < '/')
629 continue;
630 if (!ce_uptodate(ce))
631 break; /* continue? */
632 return DT_DIR;
633 }
634 return DT_UNKNOWN;
635}
636
caa6b782 637static int get_dtype(struct dirent *de, const char *path, int len)
07134421 638{
6831a88a 639 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
640 struct stat st;
641
642 if (dtype != DT_UNKNOWN)
643 return dtype;
443e061a
LT
644 dtype = get_index_dtype(path, len);
645 if (dtype != DT_UNKNOWN)
646 return dtype;
647 if (lstat(path, &st))
07134421
LT
648 return dtype;
649 if (S_ISREG(st.st_mode))
650 return DT_REG;
651 if (S_ISDIR(st.st_mode))
652 return DT_DIR;
653 if (S_ISLNK(st.st_mode))
654 return DT_LNK;
655 return dtype;
656}
657
53cc5356
JH
658enum path_treatment {
659 path_ignored,
660 path_handled,
661 path_recurse,
662};
663
16e2cfa9
JH
664static enum path_treatment treat_one_path(struct dir_struct *dir,
665 char *path, int *len,
666 const struct path_simplify *simplify,
667 int dtype, struct dirent *de)
53cc5356 668{
16e2cfa9 669 int exclude = excluded(dir, path, &dtype);
53cc5356
JH
670 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
671 && in_pathspec(path, *len, simplify))
672 dir_add_ignored(dir, path, *len);
673
674 /*
675 * Excluded? If we don't explicitly want to show
676 * ignored files, ignore it
677 */
678 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
679 return path_ignored;
680
681 if (dtype == DT_UNKNOWN)
682 dtype = get_dtype(de, path, *len);
683
684 /*
685 * Do we want to see just the ignored files?
686 * We still need to recurse into directories,
687 * even if we don't ignore them, since the
688 * directory may contain files that we do..
689 */
690 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
691 if (dtype != DT_DIR)
692 return path_ignored;
693 }
694
695 switch (dtype) {
696 default:
697 return path_ignored;
698 case DT_DIR:
699 memcpy(path + *len, "/", 2);
700 (*len)++;
701 switch (treat_directory(dir, path, *len, simplify)) {
702 case show_directory:
703 if (exclude != !!(dir->flags
704 & DIR_SHOW_IGNORED))
705 return path_ignored;
706 break;
707 case recurse_into_directory:
708 return path_recurse;
709 case ignore_directory:
710 return path_ignored;
711 }
712 break;
713 case DT_REG:
714 case DT_LNK:
715 break;
716 }
717 return path_handled;
718}
719
16e2cfa9
JH
720static enum path_treatment treat_path(struct dir_struct *dir,
721 struct dirent *de,
722 char *path, int path_max,
723 int baselen,
724 const struct path_simplify *simplify,
725 int *len)
726{
727 int dtype;
728
729 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
730 return path_ignored;
731 *len = strlen(de->d_name);
732 /* Ignore overly long pathnames! */
733 if (*len + baselen + 8 > path_max)
734 return path_ignored;
735 memcpy(path + baselen, de->d_name, *len + 1);
736 *len += baselen;
737 if (simplify_away(path, *len, simplify))
738 return path_ignored;
739
740 dtype = DTYPE(de);
741 return treat_one_path(dir, path, len, simplify, dtype, de);
742}
743
453ec4bd
LT
744/*
745 * Read a directory tree. We currently ignore anything but
746 * directories, regular files and symlinks. That's because git
747 * doesn't handle them at all yet. Maybe that will change some
748 * day.
749 *
750 * Also, we ignore the name ".git" (even if it is not a directory).
751 * That likely will not change.
752 */
53cc5356
JH
753static int read_directory_recursive(struct dir_struct *dir,
754 const char *base, int baselen,
755 int check_only,
756 const struct path_simplify *simplify)
453ec4bd 757{
dba2e203 758 DIR *fdir = opendir(*base ? base : ".");
453ec4bd
LT
759 int contents = 0;
760
761 if (fdir) {
453ec4bd 762 struct dirent *de;
dba2e203
LT
763 char path[PATH_MAX + 1];
764 memcpy(path, base, baselen);
453ec4bd 765
453ec4bd 766 while ((de = readdir(fdir)) != NULL) {
53cc5356
JH
767 int len;
768 switch (treat_path(dir, de, path, sizeof(path),
769 baselen, simplify, &len)) {
770 case path_recurse:
771 contents += read_directory_recursive
772 (dir, path, len, 0, simplify);
453ec4bd 773 continue;
53cc5356 774 case path_ignored:
5d5cea67 775 continue;
53cc5356 776 case path_handled:
453ec4bd
LT
777 break;
778 }
453ec4bd 779 contents++;
07ccbff8
JS
780 if (check_only)
781 goto exit_early;
782 else
dba2e203 783 dir_add_name(dir, path, len);
453ec4bd 784 }
07ccbff8 785exit_early:
453ec4bd 786 closedir(fdir);
453ec4bd
LT
787 }
788
789 return contents;
790}
791
792static int cmp_name(const void *p1, const void *p2)
793{
794 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
795 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
796
797 return cache_name_compare(e1->name, e1->len,
798 e2->name, e2->len);
799}
800
9fc42d60
LT
801/*
802 * Return the length of the "simple" part of a path match limiter.
803 */
804static int simple_length(const char *match)
453ec4bd 805{
9fc42d60
LT
806 int len = -1;
807
808 for (;;) {
809 unsigned char c = *match++;
810 len++;
8cc32992 811 if (c == '\0' || is_glob_special(c))
9fc42d60
LT
812 return len;
813 }
814}
815
816static struct path_simplify *create_simplify(const char **pathspec)
817{
818 int nr, alloc = 0;
819 struct path_simplify *simplify = NULL;
820
821 if (!pathspec)
822 return NULL;
823
824 for (nr = 0 ; ; nr++) {
825 const char *match;
826 if (nr >= alloc) {
827 alloc = alloc_nr(alloc);
828 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
829 }
830 match = *pathspec++;
831 if (!match)
832 break;
833 simplify[nr].path = match;
834 simplify[nr].len = simple_length(match);
835 }
836 simplify[nr].path = NULL;
837 simplify[nr].len = 0;
838 return simplify;
839}
840
841static void free_simplify(struct path_simplify *simplify)
842{
8e0f7003 843 free(simplify);
9fc42d60
LT
844}
845
48ffef96
JH
846static int treat_leading_path(struct dir_struct *dir,
847 const char *path, int len,
848 const struct path_simplify *simplify)
849{
850 char pathbuf[PATH_MAX];
851 int baselen, blen;
852 const char *cp;
853
854 while (len && path[len - 1] == '/')
855 len--;
856 if (!len)
857 return 1;
858 baselen = 0;
859 while (1) {
860 cp = path + baselen + !!baselen;
861 cp = memchr(cp, '/', path + len - cp);
862 if (!cp)
863 baselen = len;
864 else
865 baselen = cp - path;
866 memcpy(pathbuf, path, baselen);
867 pathbuf[baselen] = '\0';
868 if (!is_directory(pathbuf))
869 return 0;
870 if (simplify_away(pathbuf, baselen, simplify))
871 return 0;
872 blen = baselen;
873 if (treat_one_path(dir, pathbuf, &blen, simplify,
874 DT_DIR, NULL) == path_ignored)
875 return 0; /* do not recurse into it */
876 if (len <= baselen)
877 return 1; /* finished checking */
878 }
879}
880
dba2e203 881int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
9fc42d60 882{
725b0605 883 struct path_simplify *simplify;
b4189aa8 884
dba2e203 885 if (has_symlink_leading_path(path, len))
725b0605
JH
886 return dir->nr;
887
888 simplify = create_simplify(pathspec);
48ffef96
JH
889 if (!len || treat_leading_path(dir, path, len, simplify))
890 read_directory_recursive(dir, path, len, 0, simplify);
9fc42d60 891 free_simplify(simplify);
453ec4bd 892 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 893 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
894 return dir->nr;
895}
c91f0d92 896
686a4a06 897int file_exists(const char *f)
c91f0d92 898{
686a4a06 899 struct stat sb;
a50f9fc5 900 return lstat(f, &sb) == 0;
c91f0d92 901}
e6636747
JS
902
903/*
904 * get_relative_cwd() gets the prefix of the current working directory
905 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
906 *
907 * As a convenience, it also returns NULL if 'dir' is already NULL. The
908 * reason for this behaviour is that it is natural for functions returning
909 * directory names to return NULL to say "this directory does not exist"
910 * or "this directory is invalid". These cases are usually handled the
911 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
912 * returns NULL for both of them.
913 *
914 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
915 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
916 */
917char *get_relative_cwd(char *buffer, int size, const char *dir)
918{
919 char *cwd = buffer;
920
e6636747
JS
921 if (!dir)
922 return NULL;
923 if (!getcwd(buffer, size))
d824cbba 924 die_errno("can't find the current directory");
e6636747
JS
925
926 if (!is_absolute_path(dir))
927 dir = make_absolute_path(dir);
928
929 while (*dir && *dir == *cwd) {
930 dir++;
931 cwd++;
932 }
933 if (*dir)
934 return NULL;
935 if (*cwd == '/')
936 return cwd + 1;
937 return cwd;
938}
939
940int is_inside_dir(const char *dir)
941{
942 char buffer[PATH_MAX];
943 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
944}
7155b727 945
55892d23
AP
946int is_empty_dir(const char *path)
947{
948 DIR *dir = opendir(path);
949 struct dirent *e;
950 int ret = 1;
951
952 if (!dir)
953 return 0;
954
955 while ((e = readdir(dir)) != NULL)
956 if (!is_dot_or_dotdot(e->d_name)) {
957 ret = 0;
958 break;
959 }
960
961 closedir(dir);
962 return ret;
963}
964
a0f4afbe 965int remove_dir_recursively(struct strbuf *path, int flag)
7155b727 966{
a0f4afbe 967 DIR *dir;
7155b727
JS
968 struct dirent *e;
969 int ret = 0, original_len = path->len, len;
a0f4afbe
JH
970 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
971 unsigned char submodule_head[20];
7155b727 972
a0f4afbe
JH
973 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
974 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
975 /* Do not descend and nuke a nested git work tree. */
976 return 0;
977
978 dir = opendir(path->buf);
7155b727
JS
979 if (!dir)
980 return -1;
981 if (path->buf[original_len - 1] != '/')
982 strbuf_addch(path, '/');
983
984 len = path->len;
985 while ((e = readdir(dir)) != NULL) {
986 struct stat st;
8ca12c0d
AP
987 if (is_dot_or_dotdot(e->d_name))
988 continue;
7155b727
JS
989
990 strbuf_setlen(path, len);
991 strbuf_addstr(path, e->d_name);
992 if (lstat(path->buf, &st))
993 ; /* fall thru */
994 else if (S_ISDIR(st.st_mode)) {
995 if (!remove_dir_recursively(path, only_empty))
996 continue; /* happy */
997 } else if (!only_empty && !unlink(path->buf))
998 continue; /* happy, too */
999
1000 /* path too long, stat fails, or non-directory still exists */
1001 ret = -1;
1002 break;
1003 }
1004 closedir(dir);
1005
1006 strbuf_setlen(path, original_len);
1007 if (!ret)
1008 ret = rmdir(path->buf);
1009 return ret;
1010}
039bc64e
JH
1011
1012void setup_standard_excludes(struct dir_struct *dir)
1013{
1014 const char *path;
1015
1016 dir->exclude_per_dir = ".gitignore";
1017 path = git_path("info/exclude");
1018 if (!access(path, R_OK))
1019 add_excludes_from_file(dir, path);
1020 if (excludes_file && !access(excludes_file, R_OK))
1021 add_excludes_from_file(dir, excludes_file);
1022}
4a92d1bf
AR
1023
1024int remove_path(const char *name)
1025{
1026 char *slash;
1027
1028 if (unlink(name) && errno != ENOENT)
1029 return -1;
1030
1031 slash = strrchr(name, '/');
1032 if (slash) {
1033 char *dirs = xstrdup(name);
1034 slash = dirs + (slash - name);
1035 do {
1036 *slash = '\0';
1037 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
1038 free(dirs);
1039 }
1040 return 0;
1041}
1042