2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
12 struct path_simplify
{
17 static 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 static int get_dtype(struct dirent
*de
, const char *path
);
22 int common_prefix(const char **pathspec
)
24 const char *path
, *slash
, *next
;
31 slash
= strrchr(path
, '/');
35 prefix
= slash
- path
+ 1;
36 while ((next
= *++pathspec
) != NULL
) {
37 int len
= strlen(next
);
38 if (len
>= prefix
&& !memcmp(path
, next
, prefix
))
44 if (next
[--len
] != '/')
46 if (memcmp(path
, next
, len
+1))
56 * Does 'match' matches the given name?
59 * (1) the 'match' string is leading directory of 'name', or
60 * (2) the 'match' string is a wildcard and matches 'name', or
61 * (3) the 'match' string is exactly the same as 'name'.
63 * and the return value tells which case it was.
65 * It returns 0 when there is no match.
67 static int match_one(const char *match
, const char *name
, int namelen
)
71 /* If the match was just the prefix, we matched */
72 matchlen
= strlen(match
);
74 return MATCHED_RECURSIVELY
;
77 * If we don't match the matchstring exactly,
78 * we need to match by fnmatch
80 if (strncmp(match
, name
, matchlen
))
81 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
84 return MATCHED_EXACTLY
;
85 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
86 return MATCHED_RECURSIVELY
;
91 * Given a name and a list of pathspecs, see if the name matches
92 * any of the pathspecs. The caller is also interested in seeing
93 * all pathspec matches some names it calls this function with
94 * (otherwise the user could have mistyped the unmatched pathspec),
95 * and a mark is left in seen[] array for pathspec element that
96 * actually matched anything.
98 int match_pathspec(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
106 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
108 if (retval
&& *seen
== MATCHED_EXACTLY
)
111 how
= match_one(match
, name
, namelen
);
122 static int no_wildcard(const char *string
)
124 return string
[strcspn(string
, "*?[{")] == '\0';
127 void add_exclude(const char *string
, const char *base
,
128 int baselen
, struct exclude_list
*which
)
135 if (*string
== '!') {
139 len
= strlen(string
);
140 if (len
&& string
[len
- 1] == '/') {
142 x
= xmalloc(sizeof(*x
) + len
);
144 memcpy(s
, string
, len
- 1);
148 flags
= EXC_FLAG_MUSTBEDIR
;
150 x
= xmalloc(sizeof(*x
));
153 x
->to_exclude
= to_exclude
;
154 x
->patternlen
= strlen(string
);
156 x
->baselen
= baselen
;
158 if (!strchr(string
, '/'))
159 x
->flags
|= EXC_FLAG_NODIR
;
160 if (no_wildcard(string
))
161 x
->flags
|= EXC_FLAG_NOWILDCARD
;
162 if (*string
== '*' && no_wildcard(string
+1))
163 x
->flags
|= EXC_FLAG_ENDSWITH
;
164 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
165 which
->excludes
[which
->nr
++] = x
;
168 static int add_excludes_from_file_1(const char *fname
,
172 struct exclude_list
*which
)
179 fd
= open(fname
, O_RDONLY
);
180 if (fd
< 0 || fstat(fd
, &st
) < 0)
182 size
= xsize_t(st
.st_size
);
187 buf
= xmalloc(size
+1);
188 if (read_in_full(fd
, buf
, size
) != size
)
199 for (i
= 0; i
< size
; i
++) {
200 if (buf
[i
] == '\n') {
201 if (entry
!= buf
+ i
&& entry
[0] != '#') {
202 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
203 add_exclude(entry
, base
, baselen
, which
);
216 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
218 if (add_excludes_from_file_1(fname
, "", 0, NULL
,
219 &dir
->exclude_list
[EXC_FILE
]) < 0)
220 die("cannot use %s as an exclude file", fname
);
223 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
225 struct exclude_list
*el
;
226 struct exclude_stack
*stk
= NULL
;
229 if ((!dir
->exclude_per_dir
) ||
230 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
231 return; /* too long a path -- ignore */
233 /* Pop the ones that are not the prefix of the path being checked. */
234 el
= &dir
->exclude_list
[EXC_DIRS
];
235 while ((stk
= dir
->exclude_stack
) != NULL
) {
236 if (stk
->baselen
<= baselen
&&
237 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
239 dir
->exclude_stack
= stk
->prev
;
240 while (stk
->exclude_ix
< el
->nr
)
241 free(el
->excludes
[--el
->nr
]);
246 /* Read from the parent directories and push them down. */
247 current
= stk
? stk
->baselen
: -1;
248 while (current
< baselen
) {
249 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
257 cp
= strchr(base
+ current
+ 1, '/');
259 die("oops in prep_exclude");
262 stk
->prev
= dir
->exclude_stack
;
263 stk
->baselen
= cp
- base
;
264 stk
->exclude_ix
= el
->nr
;
265 memcpy(dir
->basebuf
+ current
, base
+ current
,
266 stk
->baselen
- current
);
267 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
268 add_excludes_from_file_1(dir
->basebuf
,
269 dir
->basebuf
, stk
->baselen
,
271 dir
->exclude_stack
= stk
;
272 current
= stk
->baselen
;
274 dir
->basebuf
[baselen
] = '\0';
277 /* Scan the list and let the last match determines the fate.
278 * Return 1 for exclude, 0 for include and -1 for undecided.
280 static int excluded_1(const char *pathname
,
281 int pathlen
, const char *basename
, int *dtype
,
282 struct exclude_list
*el
)
287 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
288 struct exclude
*x
= el
->excludes
[i
];
289 const char *exclude
= x
->pattern
;
290 int to_exclude
= x
->to_exclude
;
292 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
293 if (*dtype
== DT_UNKNOWN
)
294 *dtype
= get_dtype(NULL
, pathname
);
295 if (*dtype
!= DT_DIR
)
299 if (x
->flags
& EXC_FLAG_NODIR
) {
301 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
302 if (!strcmp(exclude
, basename
))
304 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
305 if (x
->patternlen
- 1 <= pathlen
&&
306 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
309 if (fnmatch(exclude
, basename
, 0) == 0)
314 /* match with FNM_PATHNAME:
315 * exclude has base (baselen long) implicitly
318 int baselen
= x
->baselen
;
322 if (pathlen
< baselen
||
323 (baselen
&& pathname
[baselen
-1] != '/') ||
324 strncmp(pathname
, x
->base
, baselen
))
327 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
328 if (!strcmp(exclude
, pathname
+ baselen
))
331 if (fnmatch(exclude
, pathname
+baselen
,
338 return -1; /* undecided */
341 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
343 int pathlen
= strlen(pathname
);
345 const char *basename
= strrchr(pathname
, '/');
346 basename
= (basename
) ? basename
+1 : pathname
;
348 prep_exclude(dir
, pathname
, basename
-pathname
);
349 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
350 switch (excluded_1(pathname
, pathlen
, basename
,
351 dtype_p
, &dir
->exclude_list
[st
])) {
361 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
363 struct dir_entry
*ent
;
365 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
367 memcpy(ent
->name
, pathname
, len
);
372 struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
374 if (cache_name_exists(pathname
, len
, 0))
377 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
378 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
381 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
383 if (cache_name_pos(pathname
, len
) >= 0)
386 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
387 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
391 index_nonexistent
= 0,
397 * The index sorts alphabetically by entry name, which
398 * means that a gitlink sorts as '\0' at the end, while
399 * a directory (which is defined not as an entry, but as
400 * the files it contains) will sort with the '/' at the
403 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
405 int pos
= cache_name_pos(dirname
, len
);
408 while (pos
< active_nr
) {
409 struct cache_entry
*ce
= active_cache
[pos
++];
410 unsigned char endchar
;
412 if (strncmp(ce
->name
, dirname
, len
))
414 endchar
= ce
->name
[len
];
418 return index_directory
;
419 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
422 return index_nonexistent
;
426 * When we find a directory when traversing the filesystem, we
427 * have three distinct cases:
430 * - see it as a directory
433 * and which one we choose depends on a combination of existing
434 * git index contents and the flags passed into the directory
437 * Case 1: If we *already* have entries in the index under that
438 * directory name, we always recurse into the directory to see
441 * Case 2: If we *already* have that directory name as a gitlink,
442 * we always continue to see it as a gitlink, regardless of whether
443 * there is an actual git directory there or not (it might not
444 * be checked out as a subproject!)
446 * Case 3: if we didn't have it in the index previously, we
447 * have a few sub-cases:
449 * (a) if "show_other_directories" is true, we show it as
450 * just a directory, unless "hide_empty_directories" is
451 * also true and the directory is empty, in which case
452 * we just ignore it entirely.
453 * (b) if it looks like a git directory, and we don't have
454 * 'no_gitlinks' set we treat it as a gitlink, and show it
456 * (c) otherwise, we recurse into it.
458 enum directory_treatment
{
461 recurse_into_directory
,
464 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
465 const char *dirname
, int len
,
466 const struct path_simplify
*simplify
)
468 /* The "len-1" is to strip the final '/' */
469 switch (directory_exists_in_index(dirname
, len
-1)) {
470 case index_directory
:
471 return recurse_into_directory
;
474 if (dir
->show_other_directories
)
475 return ignore_directory
;
476 return show_directory
;
478 case index_nonexistent
:
479 if (dir
->show_other_directories
)
481 if (!dir
->no_gitlinks
) {
482 unsigned char sha1
[20];
483 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
484 return show_directory
;
486 return recurse_into_directory
;
489 /* This is the "show_other_directories" case */
490 if (!dir
->hide_empty_directories
)
491 return show_directory
;
492 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
493 return ignore_directory
;
494 return show_directory
;
498 * This is an inexact early pruning of any recursive directory
499 * reading - if the path cannot possibly be in the pathspec,
500 * return true, and we'll skip it early.
502 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
506 const char *match
= simplify
->path
;
507 int len
= simplify
->len
;
513 if (!memcmp(path
, match
, len
))
522 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
525 for (; simplify
->path
; simplify
++) {
526 if (len
== simplify
->len
527 && !memcmp(path
, simplify
->path
, len
))
534 static int get_dtype(struct dirent
*de
, const char *path
)
536 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
539 if (dtype
!= DT_UNKNOWN
)
541 if (lstat(path
, &st
))
543 if (S_ISREG(st
.st_mode
))
545 if (S_ISDIR(st
.st_mode
))
547 if (S_ISLNK(st
.st_mode
))
553 * Read a directory tree. We currently ignore anything but
554 * directories, regular files and symlinks. That's because git
555 * doesn't handle them at all yet. Maybe that will change some
558 * Also, we ignore the name ".git" (even if it is not a directory).
559 * That likely will not change.
561 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, int check_only
, const struct path_simplify
*simplify
)
563 DIR *fdir
= opendir(path
);
568 char fullname
[PATH_MAX
+ 1];
569 memcpy(fullname
, base
, baselen
);
571 while ((de
= readdir(fdir
)) != NULL
) {
575 if ((de
->d_name
[0] == '.') &&
576 (de
->d_name
[1] == 0 ||
577 !strcmp(de
->d_name
+ 1, ".") ||
578 !strcmp(de
->d_name
+ 1, "git")))
580 len
= strlen(de
->d_name
);
581 /* Ignore overly long pathnames! */
582 if (len
+ baselen
+ 8 > sizeof(fullname
))
584 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
585 if (simplify_away(fullname
, baselen
+ len
, simplify
))
589 exclude
= excluded(dir
, fullname
, &dtype
);
590 if (exclude
&& dir
->collect_ignored
591 && in_pathspec(fullname
, baselen
+ len
, simplify
))
592 dir_add_ignored(dir
, fullname
, baselen
+ len
);
595 * Excluded? If we don't explicitly want to show
596 * ignored files, ignore it
598 if (exclude
&& !dir
->show_ignored
)
601 if (dtype
== DT_UNKNOWN
)
602 dtype
= get_dtype(de
, fullname
);
605 * Do we want to see just the ignored files?
606 * We still need to recurse into directories,
607 * even if we don't ignore them, since the
608 * directory may contain files that we do..
610 if (!exclude
&& dir
->show_ignored
) {
619 memcpy(fullname
+ baselen
+ len
, "/", 2);
621 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
623 if (exclude
!= dir
->show_ignored
)
626 case recurse_into_directory
:
627 contents
+= read_directory_recursive(dir
,
628 fullname
, fullname
, baselen
+ len
, 0, simplify
);
630 case ignore_directory
:
642 dir_add_name(dir
, fullname
, baselen
+ len
);
651 static int cmp_name(const void *p1
, const void *p2
)
653 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
654 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
656 return cache_name_compare(e1
->name
, e1
->len
,
661 * Return the length of the "simple" part of a path match limiter.
663 static int simple_length(const char *match
)
665 const char special
[256] = {
667 ['\\'] = 1, ['*'] = 1,
673 unsigned char c
= *match
++;
680 static struct path_simplify
*create_simplify(const char **pathspec
)
683 struct path_simplify
*simplify
= NULL
;
688 for (nr
= 0 ; ; nr
++) {
691 alloc
= alloc_nr(alloc
);
692 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
697 simplify
[nr
].path
= match
;
698 simplify
[nr
].len
= simple_length(match
);
700 simplify
[nr
].path
= NULL
;
701 simplify
[nr
].len
= 0;
705 static void free_simplify(struct path_simplify
*simplify
)
710 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
712 struct path_simplify
*simplify
= create_simplify(pathspec
);
714 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
715 free_simplify(simplify
);
716 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
717 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
721 int file_exists(const char *f
)
724 return lstat(f
, &sb
) == 0;
728 * get_relative_cwd() gets the prefix of the current working directory
729 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
731 * As a convenience, it also returns NULL if 'dir' is already NULL. The
732 * reason for this behaviour is that it is natural for functions returning
733 * directory names to return NULL to say "this directory does not exist"
734 * or "this directory is invalid". These cases are usually handled the
735 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
736 * returns NULL for both of them.
738 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
739 * unifies the handling of "outside work tree" with "no work tree at all".
741 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
747 if (!getcwd(buffer
, size
))
748 die("can't find the current directory: %s", strerror(errno
));
750 if (!is_absolute_path(dir
))
751 dir
= make_absolute_path(dir
);
753 while (*dir
&& *dir
== *cwd
) {
764 int is_inside_dir(const char *dir
)
766 char buffer
[PATH_MAX
];
767 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
770 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
772 DIR *dir
= opendir(path
->buf
);
774 int ret
= 0, original_len
= path
->len
, len
;
778 if (path
->buf
[original_len
- 1] != '/')
779 strbuf_addch(path
, '/');
782 while ((e
= readdir(dir
)) != NULL
) {
784 if ((e
->d_name
[0] == '.') &&
785 ((e
->d_name
[1] == 0) ||
786 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
787 continue; /* "." and ".." */
789 strbuf_setlen(path
, len
);
790 strbuf_addstr(path
, e
->d_name
);
791 if (lstat(path
->buf
, &st
))
793 else if (S_ISDIR(st
.st_mode
)) {
794 if (!remove_dir_recursively(path
, only_empty
))
795 continue; /* happy */
796 } else if (!only_empty
&& !unlink(path
->buf
))
797 continue; /* happy, too */
799 /* path too long, stat fails, or non-directory still exists */
805 strbuf_setlen(path
, original_len
);
807 ret
= rmdir(path
->buf
);
811 void setup_standard_excludes(struct dir_struct
*dir
)
815 dir
->exclude_per_dir
= ".gitignore";
816 path
= git_path("info/exclude");
817 if (!access(path
, R_OK
))
818 add_excludes_from_file(dir
, path
);
819 if (excludes_file
&& !access(excludes_file
, R_OK
))
820 add_excludes_from_file(dir
, excludes_file
);