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