2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * See Documentation/technical/api-directory-listing.txt
7 * Copyright (C) Linus Torvalds, 2005-2006
8 * Junio Hamano, 2005-2006
13 #include "wildmatch.h"
15 struct path_simplify
{
20 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, int len
,
21 int check_only
, const struct path_simplify
*simplify
);
22 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
24 /* helper string functions with support for the ignore_case flag */
25 int strcmp_icase(const char *a
, const char *b
)
27 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
30 int strncmp_icase(const char *a
, const char *b
, size_t count
)
32 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
35 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
37 return fnmatch(pattern
, string
, flags
| (ignore_case
? FNM_CASEFOLD
: 0));
40 inline int git_fnmatch(const char *pattern
, const char *string
,
41 int flags
, int prefix
)
44 if (flags
& GFNM_PATHNAME
)
45 fnm_flags
|= FNM_PATHNAME
;
47 if (strncmp(pattern
, string
, prefix
))
52 if (flags
& GFNM_ONESTAR
) {
53 int pattern_len
= strlen(++pattern
);
54 int string_len
= strlen(string
);
55 return string_len
< pattern_len
||
57 string
+ string_len
- pattern_len
);
59 return fnmatch(pattern
, string
, fnm_flags
);
62 static size_t common_prefix_len(const char **pathspec
)
64 const char *n
, *first
;
66 int literal
= limit_pathspec_to_literal();
72 while ((n
= *pathspec
++)) {
74 for (i
= 0; first
== n
|| i
< max
; i
++) {
76 if (!c
|| c
!= first
[i
] || (!literal
&& is_glob_special(c
)))
81 if (first
== n
|| len
< max
) {
91 * Returns a copy of the longest leading path common among all
94 char *common_prefix(const char **pathspec
)
96 unsigned long len
= common_prefix_len(pathspec
);
98 return len
? xmemdupz(*pathspec
, len
) : NULL
;
101 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
106 * Calculate common prefix for the pathspec, and
107 * use that to optimize the directory walk
109 len
= common_prefix_len(pathspec
);
111 /* Read the directory and prune it */
112 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
116 int within_depth(const char *name
, int namelen
,
117 int depth
, int max_depth
)
119 const char *cp
= name
, *cpe
= name
+ namelen
;
125 if (depth
> max_depth
)
132 * Does 'match' match the given name?
133 * A match is found if
135 * (1) the 'match' string is leading directory of 'name', or
136 * (2) the 'match' string is a wildcard and matches 'name', or
137 * (3) the 'match' string is exactly the same as 'name'.
139 * and the return value tells which case it was.
141 * It returns 0 when there is no match.
143 static int match_one(const char *match
, const char *name
, int namelen
)
146 int literal
= limit_pathspec_to_literal();
148 /* If the match was just the prefix, we matched */
150 return MATCHED_RECURSIVELY
;
154 unsigned char c1
= tolower(*match
);
155 unsigned char c2
= tolower(*name
);
156 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
166 unsigned char c1
= *match
;
167 unsigned char c2
= *name
;
168 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
179 * If we don't match the matchstring exactly,
180 * we need to match by fnmatch
182 matchlen
= strlen(match
);
183 if (strncmp_icase(match
, name
, matchlen
)) {
186 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
189 if (namelen
== matchlen
)
190 return MATCHED_EXACTLY
;
191 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
192 return MATCHED_RECURSIVELY
;
197 * Given a name and a list of pathspecs, see if the name matches
198 * any of the pathspecs. The caller is also interested in seeing
199 * all pathspec matches some names it calls this function with
200 * (otherwise the user could have mistyped the unmatched pathspec),
201 * and a mark is left in seen[] array for pathspec element that
202 * actually matched anything.
204 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
205 int prefix
, char *seen
)
215 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
217 const char *match
= pathspec
[i
] + prefix
;
218 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
220 how
= match_one(match
, name
, namelen
);
224 if (seen
&& seen
[i
] < how
)
232 * Does 'match' match the given name?
233 * A match is found if
235 * (1) the 'match' string is leading directory of 'name', or
236 * (2) the 'match' string is a wildcard and matches 'name', or
237 * (3) the 'match' string is exactly the same as 'name'.
239 * and the return value tells which case it was.
241 * It returns 0 when there is no match.
243 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
244 const char *name
, int namelen
)
246 /* name/namelen has prefix cut off by caller */
247 const char *match
= item
->match
+ prefix
;
248 int matchlen
= item
->len
- prefix
;
250 /* If the match was just the prefix, we matched */
252 return MATCHED_RECURSIVELY
;
254 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
255 if (matchlen
== namelen
)
256 return MATCHED_EXACTLY
;
258 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
259 return MATCHED_RECURSIVELY
;
262 if (item
->nowildcard_len
< item
->len
&&
263 !git_fnmatch(match
, name
,
264 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
265 item
->nowildcard_len
- prefix
))
266 return MATCHED_FNMATCH
;
272 * Given a name and a list of pathspecs, see if the name matches
273 * any of the pathspecs. The caller is also interested in seeing
274 * all pathspec matches some names it calls this function with
275 * (otherwise the user could have mistyped the unmatched pathspec),
276 * and a mark is left in seen[] array for pathspec element that
277 * actually matched anything.
279 int match_pathspec_depth(const struct pathspec
*ps
,
280 const char *name
, int namelen
,
281 int prefix
, char *seen
)
286 if (!ps
->recursive
|| ps
->max_depth
== -1)
287 return MATCHED_RECURSIVELY
;
289 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
290 return MATCHED_EXACTLY
;
298 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
300 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
302 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
303 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
304 how
&& how
!= MATCHED_FNMATCH
) {
305 int len
= ps
->items
[i
].len
;
306 if (name
[len
] == '/')
308 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
309 how
= MATCHED_EXACTLY
;
316 if (seen
&& seen
[i
] < how
)
324 * Return the length of the "simple" part of a path match limiter.
326 static int simple_length(const char *match
)
331 unsigned char c
= *match
++;
333 if (c
== '\0' || is_glob_special(c
))
338 static int no_wildcard(const char *string
)
340 return string
[simple_length(string
)] == '\0';
343 void parse_exclude_pattern(const char **pattern
,
348 const char *p
= *pattern
;
353 *flags
|= EXC_FLAG_NEGATIVE
;
357 if (len
&& p
[len
- 1] == '/') {
359 *flags
|= EXC_FLAG_MUSTBEDIR
;
361 for (i
= 0; i
< len
; i
++) {
366 *flags
|= EXC_FLAG_NODIR
;
367 *nowildcardlen
= simple_length(p
);
369 * we should have excluded the trailing slash from 'p' too,
370 * but that's one more allocation. Instead just make sure
371 * nowildcardlen does not exceed real patternlen
373 if (*nowildcardlen
> len
)
374 *nowildcardlen
= len
;
375 if (*p
== '*' && no_wildcard(p
+ 1))
376 *flags
|= EXC_FLAG_ENDSWITH
;
381 void add_exclude(const char *string
, const char *base
,
382 int baselen
, struct exclude_list
*el
)
389 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
390 if (flags
& EXC_FLAG_MUSTBEDIR
) {
392 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
394 memcpy(s
, string
, patternlen
);
395 s
[patternlen
] = '\0';
398 x
= xmalloc(sizeof(*x
));
401 x
->patternlen
= patternlen
;
402 x
->nowildcardlen
= nowildcardlen
;
404 x
->baselen
= baselen
;
406 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
407 el
->excludes
[el
->nr
++] = x
;
410 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
414 enum object_type type
;
416 struct index_state
*istate
= &the_index
;
419 pos
= index_name_pos(istate
, path
, len
);
422 if (!ce_skip_worktree(istate
->cache
[pos
]))
424 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
425 if (!data
|| type
!= OBJ_BLOB
) {
434 * Frees memory within el which was allocated for exclude patterns and
435 * the file buffer. Does not free el itself.
437 void clear_exclude_list(struct exclude_list
*el
)
441 for (i
= 0; i
< el
->nr
; i
++)
442 free(el
->excludes
[i
]);
449 int add_excludes_from_file_to_list(const char *fname
,
453 struct exclude_list
*el
,
461 fd
= open(fname
, O_RDONLY
);
462 if (fd
< 0 || fstat(fd
, &st
) < 0) {
464 warn_on_inaccessible(fname
);
468 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
474 if (buf
[size
-1] != '\n') {
475 buf
= xrealloc(buf
, size
+1);
480 size
= xsize_t(st
.st_size
);
485 buf
= xmalloc(size
+1);
486 if (read_in_full(fd
, buf
, size
) != size
) {
498 for (i
= 0; i
< size
; i
++) {
499 if (buf
[i
] == '\n') {
500 if (entry
!= buf
+ i
&& entry
[0] != '#') {
501 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
502 add_exclude(entry
, base
, baselen
, el
);
510 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
512 if (add_excludes_from_file_to_list(fname
, "", 0, NULL
,
513 &dir
->exclude_list
[EXC_FILE
], 0) < 0)
514 die("cannot use %s as an exclude file", fname
);
518 * Loads the per-directory exclude list for the substring of base
519 * which has a char length of baselen.
521 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
523 struct exclude_list
*el
;
524 struct exclude_stack
*stk
= NULL
;
527 if ((!dir
->exclude_per_dir
) ||
528 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
529 return; /* too long a path -- ignore */
531 /* Pop the directories that are not the prefix of the path being checked. */
532 el
= &dir
->exclude_list
[EXC_DIRS
];
533 while ((stk
= dir
->exclude_stack
) != NULL
) {
534 if (stk
->baselen
<= baselen
&&
535 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
537 dir
->exclude_stack
= stk
->prev
;
538 while (stk
->exclude_ix
< el
->nr
)
539 free(el
->excludes
[--el
->nr
]);
544 /* Read from the parent directories and push them down. */
545 current
= stk
? stk
->baselen
: -1;
546 while (current
< baselen
) {
547 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
555 cp
= strchr(base
+ current
+ 1, '/');
557 die("oops in prep_exclude");
560 stk
->prev
= dir
->exclude_stack
;
561 stk
->baselen
= cp
- base
;
562 stk
->exclude_ix
= el
->nr
;
563 memcpy(dir
->basebuf
+ current
, base
+ current
,
564 stk
->baselen
- current
);
565 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
566 add_excludes_from_file_to_list(dir
->basebuf
,
567 dir
->basebuf
, stk
->baselen
,
568 &stk
->filebuf
, el
, 1);
569 dir
->exclude_stack
= stk
;
570 current
= stk
->baselen
;
572 dir
->basebuf
[baselen
] = '\0';
575 int match_basename(const char *basename
, int basenamelen
,
576 const char *pattern
, int prefix
, int patternlen
,
579 if (prefix
== patternlen
) {
580 if (!strcmp_icase(pattern
, basename
))
582 } else if (flags
& EXC_FLAG_ENDSWITH
) {
583 if (patternlen
- 1 <= basenamelen
&&
584 !strcmp_icase(pattern
+ 1,
585 basename
+ basenamelen
- patternlen
+ 1))
588 if (fnmatch_icase(pattern
, basename
, 0) == 0)
594 int match_pathname(const char *pathname
, int pathlen
,
595 const char *base
, int baselen
,
596 const char *pattern
, int prefix
, int patternlen
,
603 * match with FNM_PATHNAME; the pattern has base implicitly
606 if (*pattern
== '/') {
612 * baselen does not count the trailing slash. base[] may or
613 * may not end with a trailing slash though.
615 if (pathlen
< baselen
+ 1 ||
616 (baselen
&& pathname
[baselen
] != '/') ||
617 strncmp_icase(pathname
, base
, baselen
))
620 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
621 name
= pathname
+ pathlen
- namelen
;
625 * if the non-wildcard part is longer than the
626 * remaining pathname, surely it cannot match.
628 if (prefix
> namelen
)
631 if (strncmp_icase(pattern
, name
, prefix
))
638 return wildmatch(pattern
, name
,
639 ignore_case
? FNM_CASEFOLD
: 0) == 0;
643 * Scan the given exclude list in reverse to see whether pathname
644 * should be ignored. The first match (i.e. the last on the list), if
645 * any, determines the fate. Returns the exclude_list element which
646 * matched, or NULL for undecided.
648 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
650 const char *basename
,
652 struct exclude_list
*el
)
657 return NULL
; /* undefined */
659 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
660 struct exclude
*x
= el
->excludes
[i
];
661 const char *exclude
= x
->pattern
;
662 int prefix
= x
->nowildcardlen
;
664 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
665 if (*dtype
== DT_UNKNOWN
)
666 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
667 if (*dtype
!= DT_DIR
)
671 if (x
->flags
& EXC_FLAG_NODIR
) {
672 if (match_basename(basename
,
673 pathlen
- (basename
- pathname
),
674 exclude
, prefix
, x
->patternlen
,
680 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
681 if (match_pathname(pathname
, pathlen
,
682 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
683 exclude
, prefix
, x
->patternlen
, x
->flags
))
686 return NULL
; /* undecided */
690 * Scan the list and let the last match determine the fate.
691 * Return 1 for exclude, 0 for include and -1 for undecided.
693 int is_excluded_from_list(const char *pathname
,
694 int pathlen
, const char *basename
, int *dtype
,
695 struct exclude_list
*el
)
697 struct exclude
*exclude
;
698 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
700 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
701 return -1; /* undecided */
705 * Loads the exclude lists for the directory containing pathname, then
706 * scans all exclude lists to determine whether pathname is excluded.
707 * Returns the exclude_list element which matched, or NULL for
710 static struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
711 const char *pathname
,
714 int pathlen
= strlen(pathname
);
716 struct exclude
*exclude
;
717 const char *basename
= strrchr(pathname
, '/');
718 basename
= (basename
) ? basename
+1 : pathname
;
720 prep_exclude(dir
, pathname
, basename
-pathname
);
721 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
722 exclude
= last_exclude_matching_from_list(
723 pathname
, pathlen
, basename
, dtype_p
,
724 &dir
->exclude_list
[st
]);
732 * Loads the exclude lists for the directory containing pathname, then
733 * scans all exclude lists to determine whether pathname is excluded.
734 * Returns 1 if true, otherwise 0.
736 static int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
738 struct exclude
*exclude
=
739 last_exclude_matching(dir
, pathname
, dtype_p
);
741 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
745 void path_exclude_check_init(struct path_exclude_check
*check
,
746 struct dir_struct
*dir
)
749 check
->exclude
= NULL
;
750 strbuf_init(&check
->path
, 256);
753 void path_exclude_check_clear(struct path_exclude_check
*check
)
755 strbuf_release(&check
->path
);
759 * For each subdirectory in name, starting with the top-most, checks
760 * to see if that subdirectory is excluded, and if so, returns the
761 * corresponding exclude structure. Otherwise, checks whether name
762 * itself (which is presumably a file) is excluded.
764 * A path to a directory known to be excluded is left in check->path to
765 * optimize for repeated checks for files in the same excluded directory.
767 struct exclude
*last_exclude_matching_path(struct path_exclude_check
*check
,
768 const char *name
, int namelen
,
772 struct strbuf
*path
= &check
->path
;
773 struct exclude
*exclude
;
776 * we allow the caller to pass namelen as an optimization; it
777 * must match the length of the name, as we eventually call
778 * is_excluded() on the whole name string.
781 namelen
= strlen(name
);
784 * If path is non-empty, and name is equal to path or a
785 * subdirectory of path, name should be excluded, because
786 * it's inside a directory which is already known to be
787 * excluded and was previously left in check->path.
790 path
->len
<= namelen
&&
791 !memcmp(name
, path
->buf
, path
->len
) &&
792 (!name
[path
->len
] || name
[path
->len
] == '/'))
793 return check
->exclude
;
795 strbuf_setlen(path
, 0);
796 for (i
= 0; name
[i
]; i
++) {
801 exclude
= last_exclude_matching(check
->dir
,
804 check
->exclude
= exclude
;
808 strbuf_addch(path
, ch
);
811 /* An entry in the index; cannot be a directory with subentries */
812 strbuf_setlen(path
, 0);
814 return last_exclude_matching(check
->dir
, name
, dtype
);
818 * Is this name excluded? This is for a caller like show_files() that
819 * do not honor directory hierarchy and iterate through paths that are
820 * possibly in an ignored directory.
822 int is_path_excluded(struct path_exclude_check
*check
,
823 const char *name
, int namelen
, int *dtype
)
825 struct exclude
*exclude
=
826 last_exclude_matching_path(check
, name
, namelen
, dtype
);
828 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
832 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
834 struct dir_entry
*ent
;
836 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
838 memcpy(ent
->name
, pathname
, len
);
843 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
845 if (cache_name_exists(pathname
, len
, ignore_case
))
848 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
849 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
852 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
854 if (!cache_name_is_other(pathname
, len
))
857 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
858 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
862 index_nonexistent
= 0,
868 * Do not use the alphabetically stored index to look up
869 * the directory name; instead, use the case insensitive
872 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
874 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
875 unsigned char endchar
;
878 return index_nonexistent
;
879 endchar
= ce
->name
[len
];
882 * The cache_entry structure returned will contain this dirname
883 * and possibly additional path components.
886 return index_directory
;
889 * If there are no additional path components, then this cache_entry
890 * represents a submodule. Submodules, despite being directories,
891 * are stored in the cache without a closing slash.
893 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
896 /* This should never be hit, but it exists just in case. */
897 return index_nonexistent
;
901 * The index sorts alphabetically by entry name, which
902 * means that a gitlink sorts as '\0' at the end, while
903 * a directory (which is defined not as an entry, but as
904 * the files it contains) will sort with the '/' at the
907 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
912 return directory_exists_in_index_icase(dirname
, len
);
914 pos
= cache_name_pos(dirname
, len
);
917 while (pos
< active_nr
) {
918 struct cache_entry
*ce
= active_cache
[pos
++];
919 unsigned char endchar
;
921 if (strncmp(ce
->name
, dirname
, len
))
923 endchar
= ce
->name
[len
];
927 return index_directory
;
928 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
931 return index_nonexistent
;
935 * When we find a directory when traversing the filesystem, we
936 * have three distinct cases:
939 * - see it as a directory
942 * and which one we choose depends on a combination of existing
943 * git index contents and the flags passed into the directory
946 * Case 1: If we *already* have entries in the index under that
947 * directory name, we always recurse into the directory to see
950 * Case 2: If we *already* have that directory name as a gitlink,
951 * we always continue to see it as a gitlink, regardless of whether
952 * there is an actual git directory there or not (it might not
953 * be checked out as a subproject!)
955 * Case 3: if we didn't have it in the index previously, we
956 * have a few sub-cases:
958 * (a) if "show_other_directories" is true, we show it as
959 * just a directory, unless "hide_empty_directories" is
960 * also true and the directory is empty, in which case
961 * we just ignore it entirely.
962 * (b) if it looks like a git directory, and we don't have
963 * 'no_gitlinks' set we treat it as a gitlink, and show it
965 * (c) otherwise, we recurse into it.
967 enum directory_treatment
{
970 recurse_into_directory
973 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
974 const char *dirname
, int len
,
975 const struct path_simplify
*simplify
)
977 /* The "len-1" is to strip the final '/' */
978 switch (directory_exists_in_index(dirname
, len
-1)) {
979 case index_directory
:
980 return recurse_into_directory
;
983 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
984 return ignore_directory
;
985 return show_directory
;
987 case index_nonexistent
:
988 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
990 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
991 unsigned char sha1
[20];
992 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
993 return show_directory
;
995 return recurse_into_directory
;
998 /* This is the "show_other_directories" case */
999 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1000 return show_directory
;
1001 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
1002 return ignore_directory
;
1003 return show_directory
;
1007 * This is an inexact early pruning of any recursive directory
1008 * reading - if the path cannot possibly be in the pathspec,
1009 * return true, and we'll skip it early.
1011 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1015 const char *match
= simplify
->path
;
1016 int len
= simplify
->len
;
1022 if (!memcmp(path
, match
, len
))
1032 * This function tells us whether an excluded path matches a
1033 * list of "interesting" pathspecs. That is, whether a path matched
1034 * by any of the pathspecs could possibly be ignored by excluding
1035 * the specified path. This can happen if:
1037 * 1. the path is mentioned explicitly in the pathspec
1039 * 2. the path is a directory prefix of some element in the
1042 static int exclude_matches_pathspec(const char *path
, int len
,
1043 const struct path_simplify
*simplify
)
1046 for (; simplify
->path
; simplify
++) {
1047 if (len
== simplify
->len
1048 && !memcmp(path
, simplify
->path
, len
))
1050 if (len
< simplify
->len
1051 && simplify
->path
[len
] == '/'
1052 && !memcmp(path
, simplify
->path
, len
))
1059 static int get_index_dtype(const char *path
, int len
)
1062 struct cache_entry
*ce
;
1064 ce
= cache_name_exists(path
, len
, 0);
1066 if (!ce_uptodate(ce
))
1068 if (S_ISGITLINK(ce
->ce_mode
))
1071 * Nobody actually cares about the
1072 * difference between DT_LNK and DT_REG
1077 /* Try to look it up as a directory */
1078 pos
= cache_name_pos(path
, len
);
1082 while (pos
< active_nr
) {
1083 ce
= active_cache
[pos
++];
1084 if (strncmp(ce
->name
, path
, len
))
1086 if (ce
->name
[len
] > '/')
1088 if (ce
->name
[len
] < '/')
1090 if (!ce_uptodate(ce
))
1091 break; /* continue? */
1097 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1099 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1102 if (dtype
!= DT_UNKNOWN
)
1104 dtype
= get_index_dtype(path
, len
);
1105 if (dtype
!= DT_UNKNOWN
)
1107 if (lstat(path
, &st
))
1109 if (S_ISREG(st
.st_mode
))
1111 if (S_ISDIR(st
.st_mode
))
1113 if (S_ISLNK(st
.st_mode
))
1118 enum path_treatment
{
1124 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1125 struct strbuf
*path
,
1126 const struct path_simplify
*simplify
,
1127 int dtype
, struct dirent
*de
)
1129 int exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1130 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1131 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1132 dir_add_ignored(dir
, path
->buf
, path
->len
);
1135 * Excluded? If we don't explicitly want to show
1136 * ignored files, ignore it
1138 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1139 return path_ignored
;
1141 if (dtype
== DT_UNKNOWN
)
1142 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1145 * Do we want to see just the ignored files?
1146 * We still need to recurse into directories,
1147 * even if we don't ignore them, since the
1148 * directory may contain files that we do..
1150 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
1151 if (dtype
!= DT_DIR
)
1152 return path_ignored
;
1157 return path_ignored
;
1159 strbuf_addch(path
, '/');
1160 switch (treat_directory(dir
, path
->buf
, path
->len
, simplify
)) {
1161 case show_directory
:
1162 if (exclude
!= !!(dir
->flags
1163 & DIR_SHOW_IGNORED
))
1164 return path_ignored
;
1166 case recurse_into_directory
:
1167 return path_recurse
;
1168 case ignore_directory
:
1169 return path_ignored
;
1176 return path_handled
;
1179 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1181 struct strbuf
*path
,
1183 const struct path_simplify
*simplify
)
1187 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1188 return path_ignored
;
1189 strbuf_setlen(path
, baselen
);
1190 strbuf_addstr(path
, de
->d_name
);
1191 if (simplify_away(path
->buf
, path
->len
, simplify
))
1192 return path_ignored
;
1195 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1199 * Read a directory tree. We currently ignore anything but
1200 * directories, regular files and symlinks. That's because git
1201 * doesn't handle them at all yet. Maybe that will change some
1204 * Also, we ignore the name ".git" (even if it is not a directory).
1205 * That likely will not change.
1207 static int read_directory_recursive(struct dir_struct
*dir
,
1208 const char *base
, int baselen
,
1210 const struct path_simplify
*simplify
)
1215 struct strbuf path
= STRBUF_INIT
;
1217 strbuf_add(&path
, base
, baselen
);
1219 fdir
= opendir(path
.len
? path
.buf
: ".");
1223 while ((de
= readdir(fdir
)) != NULL
) {
1224 switch (treat_path(dir
, de
, &path
, baselen
, simplify
)) {
1226 contents
+= read_directory_recursive(dir
, path
.buf
,
1238 dir_add_name(dir
, path
.buf
, path
.len
);
1242 strbuf_release(&path
);
1247 static int cmp_name(const void *p1
, const void *p2
)
1249 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1250 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1252 return cache_name_compare(e1
->name
, e1
->len
,
1256 static struct path_simplify
*create_simplify(const char **pathspec
)
1259 struct path_simplify
*simplify
= NULL
;
1264 for (nr
= 0 ; ; nr
++) {
1267 alloc
= alloc_nr(alloc
);
1268 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1270 match
= *pathspec
++;
1273 simplify
[nr
].path
= match
;
1274 simplify
[nr
].len
= simple_length(match
);
1276 simplify
[nr
].path
= NULL
;
1277 simplify
[nr
].len
= 0;
1281 static void free_simplify(struct path_simplify
*simplify
)
1286 static int treat_leading_path(struct dir_struct
*dir
,
1287 const char *path
, int len
,
1288 const struct path_simplify
*simplify
)
1290 struct strbuf sb
= STRBUF_INIT
;
1291 int baselen
, rc
= 0;
1294 while (len
&& path
[len
- 1] == '/')
1300 cp
= path
+ baselen
+ !!baselen
;
1301 cp
= memchr(cp
, '/', path
+ len
- cp
);
1305 baselen
= cp
- path
;
1306 strbuf_setlen(&sb
, 0);
1307 strbuf_add(&sb
, path
, baselen
);
1308 if (!is_directory(sb
.buf
))
1310 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1312 if (treat_one_path(dir
, &sb
, simplify
,
1313 DT_DIR
, NULL
) == path_ignored
)
1314 break; /* do not recurse into it */
1315 if (len
<= baselen
) {
1317 break; /* finished checking */
1320 strbuf_release(&sb
);
1324 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1326 struct path_simplify
*simplify
;
1328 if (has_symlink_leading_path(path
, len
))
1331 simplify
= create_simplify(pathspec
);
1332 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1333 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1334 free_simplify(simplify
);
1335 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1336 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1340 int file_exists(const char *f
)
1343 return lstat(f
, &sb
) == 0;
1347 * Given two normalized paths (a trailing slash is ok), if subdir is
1348 * outside dir, return -1. Otherwise return the offset in subdir that
1349 * can be used as relative path to dir.
1351 int dir_inside_of(const char *subdir
, const char *dir
)
1355 assert(dir
&& subdir
&& *dir
&& *subdir
);
1357 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1363 /* hel[p]/me vs hel[l]/yeah */
1364 if (*dir
&& *subdir
)
1368 return !*dir
? offset
: -1; /* same dir */
1370 /* foo/[b]ar vs foo/[] */
1371 if (is_dir_sep(dir
[-1]))
1372 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1374 /* foo[/]bar vs foo[] */
1375 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1378 int is_inside_dir(const char *dir
)
1383 if (!getcwd(cwd
, sizeof(cwd
)))
1384 die_errno("can't find the current directory");
1385 return dir_inside_of(cwd
, dir
) >= 0;
1388 int is_empty_dir(const char *path
)
1390 DIR *dir
= opendir(path
);
1397 while ((e
= readdir(dir
)) != NULL
)
1398 if (!is_dot_or_dotdot(e
->d_name
)) {
1407 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1411 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1412 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1413 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1414 unsigned char submodule_head
[20];
1416 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1417 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1418 /* Do not descend and nuke a nested git work tree. */
1424 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1425 dir
= opendir(path
->buf
);
1427 /* an empty dir could be removed even if it is unreadble */
1429 return rmdir(path
->buf
);
1433 if (path
->buf
[original_len
- 1] != '/')
1434 strbuf_addch(path
, '/');
1437 while ((e
= readdir(dir
)) != NULL
) {
1439 if (is_dot_or_dotdot(e
->d_name
))
1442 strbuf_setlen(path
, len
);
1443 strbuf_addstr(path
, e
->d_name
);
1444 if (lstat(path
->buf
, &st
))
1446 else if (S_ISDIR(st
.st_mode
)) {
1447 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1448 continue; /* happy */
1449 } else if (!only_empty
&& !unlink(path
->buf
))
1450 continue; /* happy, too */
1452 /* path too long, stat fails, or non-directory still exists */
1458 strbuf_setlen(path
, original_len
);
1459 if (!ret
&& !keep_toplevel
&& !kept_down
)
1460 ret
= rmdir(path
->buf
);
1463 * report the uplevel that it is not an error that we
1464 * did not rmdir() our directory.
1470 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1472 return remove_dir_recurse(path
, flag
, NULL
);
1475 void setup_standard_excludes(struct dir_struct
*dir
)
1480 dir
->exclude_per_dir
= ".gitignore";
1481 path
= git_path("info/exclude");
1482 if (!excludes_file
) {
1483 home_config_paths(NULL
, &xdg_path
, "ignore");
1484 excludes_file
= xdg_path
;
1486 if (!access_or_warn(path
, R_OK
))
1487 add_excludes_from_file(dir
, path
);
1488 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
))
1489 add_excludes_from_file(dir
, excludes_file
);
1492 int remove_path(const char *name
)
1496 if (unlink(name
) && errno
!= ENOENT
)
1499 slash
= strrchr(name
, '/');
1501 char *dirs
= xstrdup(name
);
1502 slash
= dirs
+ (slash
- name
);
1505 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1511 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1513 struct pathspec_item
*a
, *b
;
1515 a
= (struct pathspec_item
*)a_
;
1516 b
= (struct pathspec_item
*)b_
;
1517 return strcmp(a
->match
, b
->match
);
1520 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1522 const char **p
= paths
;
1525 memset(pathspec
, 0, sizeof(*pathspec
));
1530 pathspec
->raw
= paths
;
1531 pathspec
->nr
= p
- paths
;
1535 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1536 for (i
= 0; i
< pathspec
->nr
; i
++) {
1537 struct pathspec_item
*item
= pathspec
->items
+i
;
1538 const char *path
= paths
[i
];
1541 item
->len
= strlen(path
);
1543 if (limit_pathspec_to_literal()) {
1544 item
->nowildcard_len
= item
->len
;
1546 item
->nowildcard_len
= simple_length(path
);
1547 if (item
->nowildcard_len
< item
->len
) {
1548 pathspec
->has_wildcard
= 1;
1549 if (path
[item
->nowildcard_len
] == '*' &&
1550 no_wildcard(path
+ item
->nowildcard_len
+ 1))
1551 item
->flags
|= PATHSPEC_ONESTAR
;
1556 qsort(pathspec
->items
, pathspec
->nr
,
1557 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1562 void free_pathspec(struct pathspec
*pathspec
)
1564 free(pathspec
->items
);
1565 pathspec
->items
= NULL
;
1568 int limit_pathspec_to_literal(void)
1570 static int flag
= -1;
1572 flag
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);