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 static 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))
55 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
57 const char *path
, *base
;
61 * Calculate common prefix for the pathspec, and
62 * use that to optimize the directory walk
64 baselen
= common_prefix(pathspec
);
69 path
= base
= xmemdupz(*pathspec
, baselen
);
71 /* Read the directory and prune it */
72 read_directory(dir
, path
, base
, baselen
, pathspec
);
77 * Does 'match' match the given name?
80 * (1) the 'match' string is leading directory of 'name', or
81 * (2) the 'match' string is a wildcard and matches 'name', or
82 * (3) the 'match' string is exactly the same as 'name'.
84 * and the return value tells which case it was.
86 * It returns 0 when there is no match.
88 static int match_one(const char *match
, const char *name
, int namelen
)
92 /* If the match was just the prefix, we matched */
94 return MATCHED_RECURSIVELY
;
97 unsigned char c1
= *match
;
98 unsigned char c2
= *name
;
99 if (c1
== '\0' || is_glob_special(c1
))
110 * If we don't match the matchstring exactly,
111 * we need to match by fnmatch
113 matchlen
= strlen(match
);
114 if (strncmp(match
, name
, matchlen
))
115 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
117 if (namelen
== matchlen
)
118 return MATCHED_EXACTLY
;
119 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
120 return MATCHED_RECURSIVELY
;
125 * Given a name and a list of pathspecs, see if the name matches
126 * any of the pathspecs. The caller is also interested in seeing
127 * all pathspec matches some names it calls this function with
128 * (otherwise the user could have mistyped the unmatched pathspec),
129 * and a mark is left in seen[] array for pathspec element that
130 * actually matched anything.
132 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
133 int prefix
, char *seen
)
143 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
145 const char *match
= pathspec
[i
] + prefix
;
146 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
148 how
= match_one(match
, name
, namelen
);
152 if (seen
&& seen
[i
] < how
)
159 static int no_wildcard(const char *string
)
161 return string
[strcspn(string
, "*?[{\\")] == '\0';
164 void add_exclude(const char *string
, const char *base
,
165 int baselen
, struct exclude_list
*which
)
172 if (*string
== '!') {
176 len
= strlen(string
);
177 if (len
&& string
[len
- 1] == '/') {
179 x
= xmalloc(sizeof(*x
) + len
);
181 memcpy(s
, string
, len
- 1);
185 flags
= EXC_FLAG_MUSTBEDIR
;
187 x
= xmalloc(sizeof(*x
));
190 x
->to_exclude
= to_exclude
;
191 x
->patternlen
= strlen(string
);
193 x
->baselen
= baselen
;
195 if (!strchr(string
, '/'))
196 x
->flags
|= EXC_FLAG_NODIR
;
197 if (no_wildcard(string
))
198 x
->flags
|= EXC_FLAG_NOWILDCARD
;
199 if (*string
== '*' && no_wildcard(string
+1))
200 x
->flags
|= EXC_FLAG_ENDSWITH
;
201 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
202 which
->excludes
[which
->nr
++] = x
;
205 static int add_excludes_from_file_1(const char *fname
,
209 struct exclude_list
*which
)
216 fd
= open(fname
, O_RDONLY
);
217 if (fd
< 0 || fstat(fd
, &st
) < 0)
219 size
= xsize_t(st
.st_size
);
224 buf
= xmalloc(size
+1);
225 if (read_in_full(fd
, buf
, size
) != size
)
236 for (i
= 0; i
< size
; i
++) {
237 if (buf
[i
] == '\n') {
238 if (entry
!= buf
+ i
&& entry
[0] != '#') {
239 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
240 add_exclude(entry
, base
, baselen
, which
);
253 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
255 if (add_excludes_from_file_1(fname
, "", 0, NULL
,
256 &dir
->exclude_list
[EXC_FILE
]) < 0)
257 die("cannot use %s as an exclude file", fname
);
260 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
262 struct exclude_list
*el
;
263 struct exclude_stack
*stk
= NULL
;
266 if ((!dir
->exclude_per_dir
) ||
267 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
268 return; /* too long a path -- ignore */
270 /* Pop the ones that are not the prefix of the path being checked. */
271 el
= &dir
->exclude_list
[EXC_DIRS
];
272 while ((stk
= dir
->exclude_stack
) != NULL
) {
273 if (stk
->baselen
<= baselen
&&
274 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
276 dir
->exclude_stack
= stk
->prev
;
277 while (stk
->exclude_ix
< el
->nr
)
278 free(el
->excludes
[--el
->nr
]);
283 /* Read from the parent directories and push them down. */
284 current
= stk
? stk
->baselen
: -1;
285 while (current
< baselen
) {
286 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
294 cp
= strchr(base
+ current
+ 1, '/');
296 die("oops in prep_exclude");
299 stk
->prev
= dir
->exclude_stack
;
300 stk
->baselen
= cp
- base
;
301 stk
->exclude_ix
= el
->nr
;
302 memcpy(dir
->basebuf
+ current
, base
+ current
,
303 stk
->baselen
- current
);
304 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
305 add_excludes_from_file_1(dir
->basebuf
,
306 dir
->basebuf
, stk
->baselen
,
308 dir
->exclude_stack
= stk
;
309 current
= stk
->baselen
;
311 dir
->basebuf
[baselen
] = '\0';
314 /* Scan the list and let the last match determine the fate.
315 * Return 1 for exclude, 0 for include and -1 for undecided.
317 static int excluded_1(const char *pathname
,
318 int pathlen
, const char *basename
, int *dtype
,
319 struct exclude_list
*el
)
324 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
325 struct exclude
*x
= el
->excludes
[i
];
326 const char *exclude
= x
->pattern
;
327 int to_exclude
= x
->to_exclude
;
329 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
330 if (*dtype
== DT_UNKNOWN
)
331 *dtype
= get_dtype(NULL
, pathname
);
332 if (*dtype
!= DT_DIR
)
336 if (x
->flags
& EXC_FLAG_NODIR
) {
338 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
339 if (!strcmp(exclude
, basename
))
341 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
342 if (x
->patternlen
- 1 <= pathlen
&&
343 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
346 if (fnmatch(exclude
, basename
, 0) == 0)
351 /* match with FNM_PATHNAME:
352 * exclude has base (baselen long) implicitly
355 int baselen
= x
->baselen
;
359 if (pathlen
< baselen
||
360 (baselen
&& pathname
[baselen
-1] != '/') ||
361 strncmp(pathname
, x
->base
, baselen
))
364 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
365 if (!strcmp(exclude
, pathname
+ baselen
))
368 if (fnmatch(exclude
, pathname
+baselen
,
375 return -1; /* undecided */
378 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
380 int pathlen
= strlen(pathname
);
382 const char *basename
= strrchr(pathname
, '/');
383 basename
= (basename
) ? basename
+1 : pathname
;
385 prep_exclude(dir
, pathname
, basename
-pathname
);
386 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
387 switch (excluded_1(pathname
, pathlen
, basename
,
388 dtype_p
, &dir
->exclude_list
[st
])) {
398 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
400 struct dir_entry
*ent
;
402 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
404 memcpy(ent
->name
, pathname
, len
);
409 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
411 if (cache_name_exists(pathname
, len
, ignore_case
))
414 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
415 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
418 static struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
420 if (!cache_name_is_other(pathname
, len
))
423 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
424 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
428 index_nonexistent
= 0,
434 * The index sorts alphabetically by entry name, which
435 * means that a gitlink sorts as '\0' at the end, while
436 * a directory (which is defined not as an entry, but as
437 * the files it contains) will sort with the '/' at the
440 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
442 int pos
= cache_name_pos(dirname
, len
);
445 while (pos
< active_nr
) {
446 struct cache_entry
*ce
= active_cache
[pos
++];
447 unsigned char endchar
;
449 if (strncmp(ce
->name
, dirname
, len
))
451 endchar
= ce
->name
[len
];
455 return index_directory
;
456 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
459 return index_nonexistent
;
463 * When we find a directory when traversing the filesystem, we
464 * have three distinct cases:
467 * - see it as a directory
470 * and which one we choose depends on a combination of existing
471 * git index contents and the flags passed into the directory
474 * Case 1: If we *already* have entries in the index under that
475 * directory name, we always recurse into the directory to see
478 * Case 2: If we *already* have that directory name as a gitlink,
479 * we always continue to see it as a gitlink, regardless of whether
480 * there is an actual git directory there or not (it might not
481 * be checked out as a subproject!)
483 * Case 3: if we didn't have it in the index previously, we
484 * have a few sub-cases:
486 * (a) if "show_other_directories" is true, we show it as
487 * just a directory, unless "hide_empty_directories" is
488 * also true and the directory is empty, in which case
489 * we just ignore it entirely.
490 * (b) if it looks like a git directory, and we don't have
491 * 'no_gitlinks' set we treat it as a gitlink, and show it
493 * (c) otherwise, we recurse into it.
495 enum directory_treatment
{
498 recurse_into_directory
,
501 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
502 const char *dirname
, int len
,
503 const struct path_simplify
*simplify
)
505 /* The "len-1" is to strip the final '/' */
506 switch (directory_exists_in_index(dirname
, len
-1)) {
507 case index_directory
:
508 return recurse_into_directory
;
511 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
512 return ignore_directory
;
513 return show_directory
;
515 case index_nonexistent
:
516 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
518 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
519 unsigned char sha1
[20];
520 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
521 return show_directory
;
523 return recurse_into_directory
;
526 /* This is the "show_other_directories" case */
527 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
528 return show_directory
;
529 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
530 return ignore_directory
;
531 return show_directory
;
535 * This is an inexact early pruning of any recursive directory
536 * reading - if the path cannot possibly be in the pathspec,
537 * return true, and we'll skip it early.
539 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
543 const char *match
= simplify
->path
;
544 int len
= simplify
->len
;
550 if (!memcmp(path
, match
, len
))
559 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
562 for (; simplify
->path
; simplify
++) {
563 if (len
== simplify
->len
564 && !memcmp(path
, simplify
->path
, len
))
571 static int get_dtype(struct dirent
*de
, const char *path
)
573 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
576 if (dtype
!= DT_UNKNOWN
)
578 if (lstat(path
, &st
))
580 if (S_ISREG(st
.st_mode
))
582 if (S_ISDIR(st
.st_mode
))
584 if (S_ISLNK(st
.st_mode
))
590 * Read a directory tree. We currently ignore anything but
591 * directories, regular files and symlinks. That's because git
592 * doesn't handle them at all yet. Maybe that will change some
595 * Also, we ignore the name ".git" (even if it is not a directory).
596 * That likely will not change.
598 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
)
600 DIR *fdir
= opendir(*path
? path
: ".");
605 char fullname
[PATH_MAX
+ 1];
606 memcpy(fullname
, base
, baselen
);
608 while ((de
= readdir(fdir
)) != NULL
) {
612 if (is_dot_or_dotdot(de
->d_name
) ||
613 !strcmp(de
->d_name
, ".git"))
615 len
= strlen(de
->d_name
);
616 /* Ignore overly long pathnames! */
617 if (len
+ baselen
+ 8 > sizeof(fullname
))
619 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
620 if (simplify_away(fullname
, baselen
+ len
, simplify
))
624 exclude
= excluded(dir
, fullname
, &dtype
);
625 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
626 && in_pathspec(fullname
, baselen
+ len
, simplify
))
627 dir_add_ignored(dir
, fullname
, baselen
+ len
);
630 * Excluded? If we don't explicitly want to show
631 * ignored files, ignore it
633 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
636 if (dtype
== DT_UNKNOWN
)
637 dtype
= get_dtype(de
, fullname
);
640 * Do we want to see just the ignored files?
641 * We still need to recurse into directories,
642 * even if we don't ignore them, since the
643 * directory may contain files that we do..
645 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
654 memcpy(fullname
+ baselen
+ len
, "/", 2);
656 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
658 if (exclude
!= !!(dir
->flags
662 case recurse_into_directory
:
663 contents
+= read_directory_recursive(dir
,
664 fullname
, fullname
, baselen
+ len
, 0, simplify
);
666 case ignore_directory
:
678 dir_add_name(dir
, fullname
, baselen
+ len
);
687 static int cmp_name(const void *p1
, const void *p2
)
689 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
690 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
692 return cache_name_compare(e1
->name
, e1
->len
,
697 * Return the length of the "simple" part of a path match limiter.
699 static int simple_length(const char *match
)
704 unsigned char c
= *match
++;
706 if (c
== '\0' || is_glob_special(c
))
711 static struct path_simplify
*create_simplify(const char **pathspec
)
714 struct path_simplify
*simplify
= NULL
;
719 for (nr
= 0 ; ; nr
++) {
722 alloc
= alloc_nr(alloc
);
723 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
728 simplify
[nr
].path
= match
;
729 simplify
[nr
].len
= simple_length(match
);
731 simplify
[nr
].path
= NULL
;
732 simplify
[nr
].len
= 0;
736 static void free_simplify(struct path_simplify
*simplify
)
741 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
743 struct path_simplify
*simplify
;
745 if (has_symlink_leading_path(path
, strlen(path
)))
748 simplify
= create_simplify(pathspec
);
749 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
750 free_simplify(simplify
);
751 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
752 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
756 int file_exists(const char *f
)
759 return lstat(f
, &sb
) == 0;
763 * get_relative_cwd() gets the prefix of the current working directory
764 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
766 * As a convenience, it also returns NULL if 'dir' is already NULL. The
767 * reason for this behaviour is that it is natural for functions returning
768 * directory names to return NULL to say "this directory does not exist"
769 * or "this directory is invalid". These cases are usually handled the
770 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
771 * returns NULL for both of them.
773 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
774 * unifies the handling of "outside work tree" with "no work tree at all".
776 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
782 if (!getcwd(buffer
, size
))
783 die_errno("can't find the current directory");
785 if (!is_absolute_path(dir
))
786 dir
= make_absolute_path(dir
);
788 while (*dir
&& *dir
== *cwd
) {
799 int is_inside_dir(const char *dir
)
801 char buffer
[PATH_MAX
];
802 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
805 int is_empty_dir(const char *path
)
807 DIR *dir
= opendir(path
);
814 while ((e
= readdir(dir
)) != NULL
)
815 if (!is_dot_or_dotdot(e
->d_name
)) {
824 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
826 DIR *dir
= opendir(path
->buf
);
828 int ret
= 0, original_len
= path
->len
, len
;
832 if (path
->buf
[original_len
- 1] != '/')
833 strbuf_addch(path
, '/');
836 while ((e
= readdir(dir
)) != NULL
) {
838 if (is_dot_or_dotdot(e
->d_name
))
841 strbuf_setlen(path
, len
);
842 strbuf_addstr(path
, e
->d_name
);
843 if (lstat(path
->buf
, &st
))
845 else if (S_ISDIR(st
.st_mode
)) {
846 if (!remove_dir_recursively(path
, only_empty
))
847 continue; /* happy */
848 } else if (!only_empty
&& !unlink(path
->buf
))
849 continue; /* happy, too */
851 /* path too long, stat fails, or non-directory still exists */
857 strbuf_setlen(path
, original_len
);
859 ret
= rmdir(path
->buf
);
863 void setup_standard_excludes(struct dir_struct
*dir
)
867 dir
->exclude_per_dir
= ".gitignore";
868 path
= git_path("info/exclude");
869 if (!access(path
, R_OK
))
870 add_excludes_from_file(dir
, path
);
871 if (excludes_file
&& !access(excludes_file
, R_OK
))
872 add_excludes_from_file(dir
, excludes_file
);
875 int remove_path(const char *name
)
879 if (unlink(name
) && errno
!= ENOENT
)
882 slash
= strrchr(name
, '/');
884 char *dirs
= xstrdup(name
);
885 slash
= dirs
+ (slash
- name
);
888 } while (rmdir(dirs
) && (slash
= strrchr(dirs
, '/')));