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
, const char *path
, int len
,
18 int check_only
, const struct path_simplify
*simplify
);
19 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
21 static int common_prefix(const char **pathspec
)
23 const char *path
, *slash
, *next
;
30 slash
= strrchr(path
, '/');
34 prefix
= slash
- path
+ 1;
35 while ((next
= *++pathspec
) != NULL
) {
36 int len
= strlen(next
);
37 if (len
>= prefix
&& !memcmp(path
, next
, prefix
))
43 if (next
[--len
] != '/')
45 if (memcmp(path
, next
, len
+1))
54 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
60 * Calculate common prefix for the pathspec, and
61 * use that to optimize the directory walk
63 len
= common_prefix(pathspec
);
67 path
= xmemdupz(*pathspec
, len
);
69 /* Read the directory and prune it */
70 read_directory(dir
, path
, len
, pathspec
);
75 * Does 'match' match the given name?
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'.
82 * and the return value tells which case it was.
84 * It returns 0 when there is no match.
86 static int match_one(const char *match
, const char *name
, int namelen
)
90 /* If the match was just the prefix, we matched */
92 return MATCHED_RECURSIVELY
;
95 unsigned char c1
= *match
;
96 unsigned char c2
= *name
;
97 if (c1
== '\0' || is_glob_special(c1
))
108 * If we don't match the matchstring exactly,
109 * we need to match by fnmatch
111 matchlen
= strlen(match
);
112 if (strncmp(match
, name
, matchlen
))
113 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
115 if (namelen
== matchlen
)
116 return MATCHED_EXACTLY
;
117 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
118 return MATCHED_RECURSIVELY
;
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.
130 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
131 int prefix
, char *seen
)
141 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
143 const char *match
= pathspec
[i
] + prefix
;
144 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
146 how
= match_one(match
, name
, namelen
);
150 if (seen
&& seen
[i
] < how
)
157 static int no_wildcard(const char *string
)
159 return string
[strcspn(string
, "*?[{\\")] == '\0';
162 void add_exclude(const char *string
, const char *base
,
163 int baselen
, struct exclude_list
*which
)
170 if (*string
== '!') {
174 len
= strlen(string
);
175 if (len
&& string
[len
- 1] == '/') {
177 x
= xmalloc(sizeof(*x
) + len
);
179 memcpy(s
, string
, len
- 1);
183 flags
= EXC_FLAG_MUSTBEDIR
;
185 x
= xmalloc(sizeof(*x
));
188 x
->to_exclude
= to_exclude
;
189 x
->patternlen
= strlen(string
);
191 x
->baselen
= baselen
;
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
;
199 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
200 which
->excludes
[which
->nr
++] = x
;
203 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
207 enum object_type type
;
209 struct index_state
*istate
= &the_index
;
212 pos
= index_name_pos(istate
, path
, len
);
215 if (!ce_skip_worktree(istate
->cache
[pos
]))
217 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
218 if (!data
|| type
!= OBJ_BLOB
) {
226 int add_excludes_from_file_to_list(const char *fname
,
230 struct exclude_list
*which
,
238 fd
= open(fname
, O_RDONLY
);
239 if (fd
< 0 || fstat(fd
, &st
) < 0) {
243 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
247 size
= xsize_t(st
.st_size
);
253 if (read_in_full(fd
, buf
, size
) != size
) {
263 for (i
= 0; i
<= size
; i
++) {
264 if (i
== size
|| buf
[i
] == '\n') {
265 if (entry
!= buf
+ i
&& entry
[0] != '#') {
266 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
267 add_exclude(entry
, base
, baselen
, which
);
275 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
277 if (add_excludes_from_file_to_list(fname
, "", 0, NULL
,
278 &dir
->exclude_list
[EXC_FILE
], 0) < 0)
279 die("cannot use %s as an exclude file", fname
);
282 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
284 struct exclude_list
*el
;
285 struct exclude_stack
*stk
= NULL
;
288 if ((!dir
->exclude_per_dir
) ||
289 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
290 return; /* too long a path -- ignore */
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
))
298 dir
->exclude_stack
= stk
->prev
;
299 while (stk
->exclude_ix
< el
->nr
)
300 free(el
->excludes
[--el
->nr
]);
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
));
316 cp
= strchr(base
+ current
+ 1, '/');
318 die("oops in prep_exclude");
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
);
327 add_excludes_from_file_to_list(dir
->basebuf
,
328 dir
->basebuf
, stk
->baselen
,
329 &stk
->filebuf
, el
, 1);
330 dir
->exclude_stack
= stk
;
331 current
= stk
->baselen
;
333 dir
->basebuf
[baselen
] = '\0';
336 /* Scan the list and let the last match determine the fate.
337 * Return 1 for exclude, 0 for include and -1 for undecided.
339 int excluded_from_list(const char *pathname
,
340 int pathlen
, const char *basename
, int *dtype
,
341 struct exclude_list
*el
)
346 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
347 struct exclude
*x
= el
->excludes
[i
];
348 const char *exclude
= x
->pattern
;
349 int to_exclude
= x
->to_exclude
;
351 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
353 if (!prefixcmp(pathname
, exclude
))
358 if (*dtype
== DT_UNKNOWN
)
359 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
360 if (*dtype
!= DT_DIR
)
364 if (x
->flags
& EXC_FLAG_NODIR
) {
366 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
367 if (!strcmp(exclude
, basename
))
369 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
370 if (x
->patternlen
- 1 <= pathlen
&&
371 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
374 if (fnmatch(exclude
, basename
, 0) == 0)
379 /* match with FNM_PATHNAME:
380 * exclude has base (baselen long) implicitly
383 int baselen
= x
->baselen
;
387 if (pathlen
< baselen
||
388 (baselen
&& pathname
[baselen
-1] != '/') ||
389 strncmp(pathname
, x
->base
, baselen
))
392 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
393 if (!strcmp(exclude
, pathname
+ baselen
))
396 if (fnmatch(exclude
, pathname
+baselen
,
403 return -1; /* undecided */
406 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
408 int pathlen
= strlen(pathname
);
410 const char *basename
= strrchr(pathname
, '/');
411 basename
= (basename
) ? basename
+1 : pathname
;
413 prep_exclude(dir
, pathname
, basename
-pathname
);
414 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
415 switch (excluded_from_list(pathname
, pathlen
, basename
,
416 dtype_p
, &dir
->exclude_list
[st
])) {
426 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
428 struct dir_entry
*ent
;
430 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
432 memcpy(ent
->name
, pathname
, len
);
437 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
439 if (cache_name_exists(pathname
, len
, ignore_case
))
442 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
443 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
446 static struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
448 if (!cache_name_is_other(pathname
, len
))
451 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
452 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
456 index_nonexistent
= 0,
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
468 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
470 int pos
= cache_name_pos(dirname
, len
);
473 while (pos
< active_nr
) {
474 struct cache_entry
*ce
= active_cache
[pos
++];
475 unsigned char endchar
;
477 if (strncmp(ce
->name
, dirname
, len
))
479 endchar
= ce
->name
[len
];
483 return index_directory
;
484 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
487 return index_nonexistent
;
491 * When we find a directory when traversing the filesystem, we
492 * have three distinct cases:
495 * - see it as a directory
498 * and which one we choose depends on a combination of existing
499 * git index contents and the flags passed into the directory
502 * Case 1: If we *already* have entries in the index under that
503 * directory name, we always recurse into the directory to see
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!)
511 * Case 3: if we didn't have it in the index previously, we
512 * have a few sub-cases:
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
519 * 'no_gitlinks' set we treat it as a gitlink, and show it
521 * (c) otherwise, we recurse into it.
523 enum directory_treatment
{
526 recurse_into_directory
,
529 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
530 const char *dirname
, int len
,
531 const struct path_simplify
*simplify
)
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
;
539 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
540 return ignore_directory
;
541 return show_directory
;
543 case index_nonexistent
:
544 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
546 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
547 unsigned char sha1
[20];
548 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
549 return show_directory
;
551 return recurse_into_directory
;
554 /* This is the "show_other_directories" case */
555 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
556 return show_directory
;
557 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
558 return ignore_directory
;
559 return show_directory
;
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.
567 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
571 const char *match
= simplify
->path
;
572 int len
= simplify
->len
;
578 if (!memcmp(path
, match
, len
))
587 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
590 for (; simplify
->path
; simplify
++) {
591 if (len
== simplify
->len
592 && !memcmp(path
, simplify
->path
, len
))
599 static int get_index_dtype(const char *path
, int len
)
602 struct cache_entry
*ce
;
604 ce
= cache_name_exists(path
, len
, 0);
606 if (!ce_uptodate(ce
))
608 if (S_ISGITLINK(ce
->ce_mode
))
611 * Nobody actually cares about the
612 * difference between DT_LNK and DT_REG
617 /* Try to look it up as a directory */
618 pos
= cache_name_pos(path
, len
);
622 while (pos
< active_nr
) {
623 ce
= active_cache
[pos
++];
624 if (strncmp(ce
->name
, path
, len
))
626 if (ce
->name
[len
] > '/')
628 if (ce
->name
[len
] < '/')
630 if (!ce_uptodate(ce
))
631 break; /* continue? */
637 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
639 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
642 if (dtype
!= DT_UNKNOWN
)
644 dtype
= get_index_dtype(path
, len
);
645 if (dtype
!= DT_UNKNOWN
)
647 if (lstat(path
, &st
))
649 if (S_ISREG(st
.st_mode
))
651 if (S_ISDIR(st
.st_mode
))
653 if (S_ISLNK(st
.st_mode
))
659 * Read a directory tree. We currently ignore anything but
660 * directories, regular files and symlinks. That's because git
661 * doesn't handle them at all yet. Maybe that will change some
664 * Also, we ignore the name ".git" (even if it is not a directory).
665 * That likely will not change.
667 static int read_directory_recursive(struct dir_struct
*dir
, const char *base
, int baselen
, int check_only
, const struct path_simplify
*simplify
)
669 DIR *fdir
= opendir(*base
? base
: ".");
674 char path
[PATH_MAX
+ 1];
675 memcpy(path
, base
, baselen
);
677 while ((de
= readdir(fdir
)) != NULL
) {
681 if (is_dot_or_dotdot(de
->d_name
) ||
682 !strcmp(de
->d_name
, ".git"))
684 len
= strlen(de
->d_name
);
685 /* Ignore overly long pathnames! */
686 if (len
+ baselen
+ 8 > sizeof(path
))
688 memcpy(path
+ baselen
, de
->d_name
, len
+1);
690 if (simplify_away(path
, len
, simplify
))
694 exclude
= excluded(dir
, path
, &dtype
);
695 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
696 && in_pathspec(path
, len
, simplify
))
697 dir_add_ignored(dir
, path
,len
);
700 * Excluded? If we don't explicitly want to show
701 * ignored files, ignore it
703 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
706 if (dtype
== DT_UNKNOWN
)
707 dtype
= get_dtype(de
, path
, len
);
710 * Do we want to see just the ignored files?
711 * We still need to recurse into directories,
712 * even if we don't ignore them, since the
713 * directory may contain files that we do..
715 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
724 memcpy(path
+ len
, "/", 2);
726 switch (treat_directory(dir
, path
, len
, simplify
)) {
728 if (exclude
!= !!(dir
->flags
732 case recurse_into_directory
:
733 contents
+= read_directory_recursive(dir
,
734 path
, len
, 0, simplify
);
736 case ignore_directory
:
748 dir_add_name(dir
, path
, len
);
757 static int cmp_name(const void *p1
, const void *p2
)
759 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
760 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
762 return cache_name_compare(e1
->name
, e1
->len
,
767 * Return the length of the "simple" part of a path match limiter.
769 static int simple_length(const char *match
)
774 unsigned char c
= *match
++;
776 if (c
== '\0' || is_glob_special(c
))
781 static struct path_simplify
*create_simplify(const char **pathspec
)
784 struct path_simplify
*simplify
= NULL
;
789 for (nr
= 0 ; ; nr
++) {
792 alloc
= alloc_nr(alloc
);
793 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
798 simplify
[nr
].path
= match
;
799 simplify
[nr
].len
= simple_length(match
);
801 simplify
[nr
].path
= NULL
;
802 simplify
[nr
].len
= 0;
806 static void free_simplify(struct path_simplify
*simplify
)
811 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
813 struct path_simplify
*simplify
;
815 if (has_symlink_leading_path(path
, len
))
818 simplify
= create_simplify(pathspec
);
819 read_directory_recursive(dir
, path
, len
, 0, simplify
);
820 free_simplify(simplify
);
821 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
822 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
826 int file_exists(const char *f
)
829 return lstat(f
, &sb
) == 0;
833 * get_relative_cwd() gets the prefix of the current working directory
834 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
836 * As a convenience, it also returns NULL if 'dir' is already NULL. The
837 * reason for this behaviour is that it is natural for functions returning
838 * directory names to return NULL to say "this directory does not exist"
839 * or "this directory is invalid". These cases are usually handled the
840 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
841 * returns NULL for both of them.
843 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
844 * unifies the handling of "outside work tree" with "no work tree at all".
846 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
852 if (!getcwd(buffer
, size
))
853 die_errno("can't find the current directory");
855 if (!is_absolute_path(dir
))
856 dir
= make_absolute_path(dir
);
858 while (*dir
&& *dir
== *cwd
) {
869 int is_inside_dir(const char *dir
)
871 char buffer
[PATH_MAX
];
872 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
875 int is_empty_dir(const char *path
)
877 DIR *dir
= opendir(path
);
884 while ((e
= readdir(dir
)) != NULL
)
885 if (!is_dot_or_dotdot(e
->d_name
)) {
894 int remove_dir_recursively(struct strbuf
*path
, int flag
)
898 int ret
= 0, original_len
= path
->len
, len
;
899 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
900 unsigned char submodule_head
[20];
902 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
903 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
))
904 /* Do not descend and nuke a nested git work tree. */
907 dir
= opendir(path
->buf
);
910 if (path
->buf
[original_len
- 1] != '/')
911 strbuf_addch(path
, '/');
914 while ((e
= readdir(dir
)) != NULL
) {
916 if (is_dot_or_dotdot(e
->d_name
))
919 strbuf_setlen(path
, len
);
920 strbuf_addstr(path
, e
->d_name
);
921 if (lstat(path
->buf
, &st
))
923 else if (S_ISDIR(st
.st_mode
)) {
924 if (!remove_dir_recursively(path
, only_empty
))
925 continue; /* happy */
926 } else if (!only_empty
&& !unlink(path
->buf
))
927 continue; /* happy, too */
929 /* path too long, stat fails, or non-directory still exists */
935 strbuf_setlen(path
, original_len
);
937 ret
= rmdir(path
->buf
);
941 void setup_standard_excludes(struct dir_struct
*dir
)
945 dir
->exclude_per_dir
= ".gitignore";
946 path
= git_path("info/exclude");
947 if (!access(path
, R_OK
))
948 add_excludes_from_file(dir
, path
);
949 if (excludes_file
&& !access(excludes_file
, R_OK
))
950 add_excludes_from_file(dir
, excludes_file
);
953 int remove_path(const char *name
)
957 if (unlink(name
) && errno
!= ENOENT
)
960 slash
= strrchr(name
, '/');
962 char *dirs
= xstrdup(name
);
963 slash
= dirs
+ (slash
- name
);
966 } while (rmdir(dirs
) && (slash
= strrchr(dirs
, '/')));