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