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"
16 struct path_simplify
{
22 * Tells read_directory_recursive how a file or directory should be treated.
23 * Values are ordered by significance, e.g. if a directory contains both
24 * excluded and untracked files, it is listed as untracked because
25 * path_untracked > path_excluded.
34 static enum path_treatment
read_directory_recursive(struct dir_struct
*dir
,
35 const char *path
, int len
,
36 int check_only
, const struct path_simplify
*simplify
);
37 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
39 /* helper string functions with support for the ignore_case flag */
40 int strcmp_icase(const char *a
, const char *b
)
42 return ignore_case ?
strcasecmp(a
, b
) : strcmp(a
, b
);
45 int strncmp_icase(const char *a
, const char *b
, size_t count
)
47 return ignore_case ?
strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
50 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
52 return wildmatch(pattern
, string
,
53 flags
| (ignore_case ? WM_CASEFOLD
: 0),
57 int git_fnmatch(const struct pathspec_item
*item
,
58 const char *pattern
, const char *string
,
62 if (ps_strncmp(item
, pattern
, string
, prefix
))
67 if (item
->flags
& PATHSPEC_ONESTAR
) {
68 int pattern_len
= strlen(++pattern
);
69 int string_len
= strlen(string
);
70 return string_len
< pattern_len
||
71 ps_strcmp(item
, pattern
,
72 string
+ string_len
- pattern_len
);
74 if (item
->magic
& PATHSPEC_GLOB
)
75 return wildmatch(pattern
, string
,
77 (item
->magic
& PATHSPEC_ICASE ? WM_CASEFOLD
: 0),
80 /* wildmatch has not learned no FNM_PATHNAME mode yet */
81 return wildmatch(pattern
, string
,
82 item
->magic
& PATHSPEC_ICASE ? WM_CASEFOLD
: 0,
86 static int fnmatch_icase_mem(const char *pattern
, int patternlen
,
87 const char *string
, int stringlen
,
91 struct strbuf pat_buf
= STRBUF_INIT
;
92 struct strbuf str_buf
= STRBUF_INIT
;
93 const char *use_pat
= pattern
;
94 const char *use_str
= string
;
96 if (pattern
[patternlen
]) {
97 strbuf_add(&pat_buf
, pattern
, patternlen
);
98 use_pat
= pat_buf
.buf
;
100 if (string
[stringlen
]) {
101 strbuf_add(&str_buf
, string
, stringlen
);
102 use_str
= str_buf
.buf
;
106 flags
|= WM_CASEFOLD
;
107 match_status
= wildmatch(use_pat
, use_str
, flags
, NULL
);
109 strbuf_release(&pat_buf
);
110 strbuf_release(&str_buf
);
115 static size_t common_prefix_len(const struct pathspec
*pathspec
)
121 * ":(icase)path" is treated as a pathspec full of
122 * wildcard. In other words, only prefix is considered common
123 * prefix. If the pathspec is abc/foo abc/bar, running in
124 * subdir xyz, the common prefix is still xyz, not xuz/abc as
127 GUARD_PATHSPEC(pathspec
,
135 for (n
= 0; n
< pathspec
->nr
; n
++) {
136 size_t i
= 0, len
= 0, item_len
;
137 if (pathspec
->items
[n
].magic
& PATHSPEC_EXCLUDE
)
139 if (pathspec
->items
[n
].magic
& PATHSPEC_ICASE
)
140 item_len
= pathspec
->items
[n
].prefix
;
142 item_len
= pathspec
->items
[n
].nowildcard_len
;
143 while (i
< item_len
&& (n
== 0 || i
< max
)) {
144 char c
= pathspec
->items
[n
].match
[i
];
145 if (c
!= pathspec
->items
[0].match
[i
])
151 if (n
== 0 || len
< max
) {
161 * Returns a copy of the longest leading path common among all
164 char *common_prefix(const struct pathspec
*pathspec
)
166 unsigned long len
= common_prefix_len(pathspec
);
168 return len ?
xmemdupz(pathspec
->items
[0].match
, len
) : NULL
;
171 int fill_directory(struct dir_struct
*dir
, const struct pathspec
*pathspec
)
176 * Calculate common prefix for the pathspec, and
177 * use that to optimize the directory walk
179 len
= common_prefix_len(pathspec
);
181 /* Read the directory and prune it */
182 read_directory(dir
, pathspec
->nr ? pathspec
->_raw
[0] : "", len
, pathspec
);
186 int within_depth(const char *name
, int namelen
,
187 int depth
, int max_depth
)
189 const char *cp
= name
, *cpe
= name
+ namelen
;
195 if (depth
> max_depth
)
201 #define DO_MATCH_EXCLUDE 1
202 #define DO_MATCH_DIRECTORY 2
205 * Does 'match' match the given name?
206 * A match is found if
208 * (1) the 'match' string is leading directory of 'name', or
209 * (2) the 'match' string is a wildcard and matches 'name', or
210 * (3) the 'match' string is exactly the same as 'name'.
212 * and the return value tells which case it was.
214 * It returns 0 when there is no match.
216 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
217 const char *name
, int namelen
, unsigned flags
)
219 /* name/namelen has prefix cut off by caller */
220 const char *match
= item
->match
+ prefix
;
221 int matchlen
= item
->len
- prefix
;
224 * The normal call pattern is:
225 * 1. prefix = common_prefix_len(ps);
226 * 2. prune something, or fill_directory
227 * 3. match_pathspec()
229 * 'prefix' at #1 may be shorter than the command's prefix and
230 * it's ok for #2 to match extra files. Those extras will be
233 * Suppose the pathspec is 'foo' and '../bar' running from
234 * subdir 'xyz'. The common prefix at #1 will be empty, thanks
235 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
236 * user does not want XYZ/foo, only the "foo" part should be
237 * case-insensitive. We need to filter out XYZ/foo here. In
238 * other words, we do not trust the caller on comparing the
239 * prefix part when :(icase) is involved. We do exact
240 * comparison ourselves.
242 * Normally the caller (common_prefix_len() in fact) does
243 * _exact_ matching on name[-prefix+1..-1] and we do not need
244 * to check that part. Be defensive and check it anyway, in
245 * case common_prefix_len is changed, or a new caller is
246 * introduced that does not use common_prefix_len.
248 * If the penalty turns out too high when prefix is really
249 * long, maybe change it to
250 * strncmp(match, name, item->prefix - prefix)
252 if (item
->prefix
&& (item
->magic
& PATHSPEC_ICASE
) &&
253 strncmp(item
->match
, name
- prefix
, item
->prefix
))
256 /* If the match was just the prefix, we matched */
258 return MATCHED_RECURSIVELY
;
260 if (matchlen
<= namelen
&& !ps_strncmp(item
, match
, name
, matchlen
)) {
261 if (matchlen
== namelen
)
262 return MATCHED_EXACTLY
;
264 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
265 return MATCHED_RECURSIVELY
;
266 } else if ((flags
& DO_MATCH_DIRECTORY
) &&
267 match
[matchlen
- 1] == '/' &&
268 namelen
== matchlen
- 1 &&
269 !ps_strncmp(item
, match
, name
, namelen
))
270 return MATCHED_EXACTLY
;
272 if (item
->nowildcard_len
< item
->len
&&
273 !git_fnmatch(item
, match
, name
,
274 item
->nowildcard_len
- prefix
))
275 return MATCHED_FNMATCH
;
281 * Given a name and a list of pathspecs, returns the nature of the
282 * closest (i.e. most specific) match of the name to any of the
285 * The caller typically calls this multiple times with the same
286 * pathspec and seen[] array but with different name/namelen
287 * (e.g. entries from the index) and is interested in seeing if and
288 * how each pathspec matches all the names it calls this function
289 * with. A mark is left in the seen[] array for each pathspec element
290 * indicating the closest type of match that element achieved, so if
291 * seen[n] remains zero after multiple invocations, that means the nth
292 * pathspec did not match any names, which could indicate that the
293 * user mistyped the nth pathspec.
295 static int do_match_pathspec(const struct pathspec
*ps
,
296 const char *name
, int namelen
,
297 int prefix
, char *seen
,
300 int i
, retval
= 0, exclude
= flags
& DO_MATCH_EXCLUDE
;
311 if (!ps
->recursive
||
312 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
314 return MATCHED_RECURSIVELY
;
316 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
317 return MATCHED_EXACTLY
;
325 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
328 if ((!exclude
&& ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
) ||
329 ( exclude
&& !(ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
)))
332 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
335 * Make exclude patterns optional and never report
336 * "pathspec ':(exclude)foo' matches no files"
338 if (seen
&& ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
)
339 seen
[i
] = MATCHED_FNMATCH
;
340 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
,
343 (ps
->magic
& PATHSPEC_MAXDEPTH
) &&
344 ps
->max_depth
!= -1 &&
345 how
&& how
!= MATCHED_FNMATCH
) {
346 int len
= ps
->items
[i
].len
;
347 if (name
[len
] == '/')
349 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
350 how
= MATCHED_EXACTLY
;
357 if (seen
&& seen
[i
] < how
)
364 int match_pathspec(const struct pathspec
*ps
,
365 const char *name
, int namelen
,
366 int prefix
, char *seen
, int is_dir
)
368 int positive
, negative
;
369 unsigned flags
= is_dir ? DO_MATCH_DIRECTORY
: 0;
370 positive
= do_match_pathspec(ps
, name
, namelen
,
371 prefix
, seen
, flags
);
372 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) || !positive
)
374 negative
= do_match_pathspec(ps
, name
, namelen
,
376 flags
| DO_MATCH_EXCLUDE
);
377 return negative ?
0 : positive
;
380 int report_path_error(const char *ps_matched
,
381 const struct pathspec
*pathspec
,
385 * Make sure all pathspec matched; otherwise it is an error.
387 struct strbuf sb
= STRBUF_INIT
;
389 for (num
= 0; num
< pathspec
->nr
; num
++) {
390 int other
, found_dup
;
395 * The caller might have fed identical pathspec
396 * twice. Do not barf on such a mistake.
397 * FIXME: parse_pathspec should have eliminated
398 * duplicate pathspec.
400 for (found_dup
= other
= 0;
401 !found_dup
&& other
< pathspec
->nr
;
403 if (other
== num
|| !ps_matched
[other
])
405 if (!strcmp(pathspec
->items
[other
].original
,
406 pathspec
->items
[num
].original
))
408 * Ok, we have a match already.
415 error("pathspec '%s' did not match any file(s) known to git.",
416 pathspec
->items
[num
].original
);
424 * Return the length of the "simple" part of a path match limiter.
426 int simple_length(const char *match
)
431 unsigned char c
= *match
++;
433 if (c
== '\0' || is_glob_special(c
))
438 int no_wildcard(const char *string
)
440 return string
[simple_length(string
)] == '\0';
443 void parse_exclude_pattern(const char **pattern
,
448 const char *p
= *pattern
;
453 *flags
|= EXC_FLAG_NEGATIVE
;
457 if (len
&& p
[len
- 1] == '/') {
459 *flags
|= EXC_FLAG_MUSTBEDIR
;
461 for (i
= 0; i
< len
; i
++) {
466 *flags
|= EXC_FLAG_NODIR
;
467 *nowildcardlen
= simple_length(p
);
469 * we should have excluded the trailing slash from 'p' too,
470 * but that's one more allocation. Instead just make sure
471 * nowildcardlen does not exceed real patternlen
473 if (*nowildcardlen
> len
)
474 *nowildcardlen
= len
;
475 if (*p
== '*' && no_wildcard(p
+ 1))
476 *flags
|= EXC_FLAG_ENDSWITH
;
481 void add_exclude(const char *string
, const char *base
,
482 int baselen
, struct exclude_list
*el
, int srcpos
)
489 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
490 if (flags
& EXC_FLAG_MUSTBEDIR
) {
492 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
494 memcpy(s
, string
, patternlen
);
495 s
[patternlen
] = '\0';
498 x
= xmalloc(sizeof(*x
));
501 x
->patternlen
= patternlen
;
502 x
->nowildcardlen
= nowildcardlen
;
504 x
->baselen
= baselen
;
507 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
508 el
->excludes
[el
->nr
++] = x
;
512 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
516 enum object_type type
;
520 pos
= cache_name_pos(path
, len
);
523 if (!ce_skip_worktree(active_cache
[pos
]))
525 data
= read_sha1_file(active_cache
[pos
]->sha1
, &type
, &sz
);
526 if (!data
|| type
!= OBJ_BLOB
) {
535 * Frees memory within el which was allocated for exclude patterns and
536 * the file buffer. Does not free el itself.
538 void clear_exclude_list(struct exclude_list
*el
)
542 for (i
= 0; i
< el
->nr
; i
++)
543 free(el
->excludes
[i
]);
552 static void trim_trailing_spaces(char *buf
)
554 char *p
, *last_space
= NULL
;
556 for (p
= buf
; *p
; p
++)
575 int add_excludes_from_file_to_list(const char *fname
,
578 struct exclude_list
*el
,
582 int fd
, i
, lineno
= 1;
586 fd
= open(fname
, O_RDONLY
);
587 if (fd
< 0 || fstat(fd
, &st
) < 0) {
589 warn_on_inaccessible(fname
);
593 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
599 if (buf
[size
-1] != '\n') {
600 buf
= xrealloc(buf
, size
+1);
604 size
= xsize_t(st
.st_size
);
609 buf
= xmalloc(size
+1);
610 if (read_in_full(fd
, buf
, size
) != size
) {
621 for (i
= 0; i
< size
; i
++) {
622 if (buf
[i
] == '\n') {
623 if (entry
!= buf
+ i
&& entry
[0] != '#') {
624 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
625 trim_trailing_spaces(entry
);
626 add_exclude(entry
, base
, baselen
, el
, lineno
);
635 struct exclude_list
*add_exclude_list(struct dir_struct
*dir
,
636 int group_type
, const char *src
)
638 struct exclude_list
*el
;
639 struct exclude_list_group
*group
;
641 group
= &dir
->exclude_list_group
[group_type
];
642 ALLOC_GROW(group
->el
, group
->nr
+ 1, group
->alloc
);
643 el
= &group
->el
[group
->nr
++];
644 memset(el
, 0, sizeof(*el
));
650 * Used to set up core.excludesfile and .git/info/exclude lists.
652 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
654 struct exclude_list
*el
;
655 el
= add_exclude_list(dir
, EXC_FILE
, fname
);
656 if (add_excludes_from_file_to_list(fname
, "", 0, el
, 0) < 0)
657 die("cannot use %s as an exclude file", fname
);
660 int match_basename(const char *basename
, int basenamelen
,
661 const char *pattern
, int prefix
, int patternlen
,
664 if (prefix
== patternlen
) {
665 if (patternlen
== basenamelen
&&
666 !strncmp_icase(pattern
, basename
, basenamelen
))
668 } else if (flags
& EXC_FLAG_ENDSWITH
) {
669 /* "*literal" matching against "fooliteral" */
670 if (patternlen
- 1 <= basenamelen
&&
671 !strncmp_icase(pattern
+ 1,
672 basename
+ basenamelen
- (patternlen
- 1),
676 if (fnmatch_icase_mem(pattern
, patternlen
,
677 basename
, basenamelen
,
684 int match_pathname(const char *pathname
, int pathlen
,
685 const char *base
, int baselen
,
686 const char *pattern
, int prefix
, int patternlen
,
693 * match with FNM_PATHNAME; the pattern has base implicitly
696 if (*pattern
== '/') {
703 * baselen does not count the trailing slash. base[] may or
704 * may not end with a trailing slash though.
706 if (pathlen
< baselen
+ 1 ||
707 (baselen
&& pathname
[baselen
] != '/') ||
708 strncmp_icase(pathname
, base
, baselen
))
711 namelen
= baselen ? pathlen
- baselen
- 1 : pathlen
;
712 name
= pathname
+ pathlen
- namelen
;
716 * if the non-wildcard part is longer than the
717 * remaining pathname, surely it cannot match.
719 if (prefix
> namelen
)
722 if (strncmp_icase(pattern
, name
, prefix
))
725 patternlen
-= prefix
;
730 * If the whole pattern did not have a wildcard,
731 * then our prefix match is all we need; we
732 * do not need to call fnmatch at all.
734 if (!patternlen
&& !namelen
)
737 * This can happen when we ignore some exclude rules
738 * on directories in other to see if negative rules
744 * The pattern of interest is "/abc". On the first
745 * try, we should match path "abc" with this pattern
746 * in the "if" statement right above, but the caller
749 * On the second try with paths within "abc",
750 * e.g. "abc/xyz", we come here and try to match it
753 if (!patternlen
&& namelen
&& *name
== '/')
757 return fnmatch_icase_mem(pattern
, patternlen
,
763 * Return non-zero if pathname is a directory and an ancestor of the
764 * literal path in a (negative) pattern. This is used to keep
765 * descending in "foo" and "foo/bar" when the pattern is
766 * "!foo/bar/.gitignore". "foo/notbar" will not be descended however.
768 static int match_neg_path(const char *pathname
, int pathlen
, int *dtype
,
769 const char *base
, int baselen
,
770 const char *pattern
, int prefix
, int patternlen
,
773 assert((flags
& EXC_FLAG_NEGATIVE
) && !(flags
& EXC_FLAG_NODIR
));
775 if (*dtype
== DT_UNKNOWN
)
776 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
777 if (*dtype
!= DT_DIR
)
780 if (*pattern
== '/') {
787 if (((pathlen
< baselen
&& base
[pathlen
] == '/') ||
788 pathlen
== baselen
) &&
789 !strncmp_icase(pathname
, base
, pathlen
))
791 pathname
+= baselen
+ 1;
792 pathlen
-= baselen
+ 1;
797 ((pathlen
< prefix
&& pattern
[pathlen
] == '/') &&
798 !strncmp_icase(pathname
, pattern
, pathlen
)))
805 * Scan the given exclude list in reverse to see whether pathname
806 * should be ignored. The first match (i.e. the last on the list), if
807 * any, determines the fate. Returns the exclude_list element which
808 * matched, or NULL for undecided.
810 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
812 const char *basename
,
814 struct exclude_list
*el
)
816 struct exclude
*exc
= NULL
; /* undecided */
817 int i
, matched_negative_path
= 0;
820 return NULL
; /* undefined */
822 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
823 struct exclude
*x
= el
->excludes
[i
];
824 const char *exclude
= x
->pattern
;
825 int prefix
= x
->nowildcardlen
;
827 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
828 if (*dtype
== DT_UNKNOWN
)
829 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
830 if (*dtype
!= DT_DIR
)
834 if (x
->flags
& EXC_FLAG_NODIR
) {
835 if (match_basename(basename
,
836 pathlen
- (basename
- pathname
),
837 exclude
, prefix
, x
->patternlen
,
845 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
846 if (match_pathname(pathname
, pathlen
,
847 x
->base
, x
->baselen ? x
->baselen
- 1 : 0,
848 exclude
, prefix
, x
->patternlen
, x
->flags
)) {
853 if ((x
->flags
& EXC_FLAG_NEGATIVE
) && !matched_negative_path
&&
854 match_neg_path(pathname
, pathlen
, dtype
, x
->base
,
855 x
->baselen ? x
->baselen
- 1 : 0,
856 exclude
, prefix
, x
->patternlen
, x
->flags
))
857 matched_negative_path
= 1;
860 !(exc
->flags
& EXC_FLAG_NEGATIVE
) &&
861 !(exc
->flags
& EXC_FLAG_NODIR
) &&
862 matched_negative_path
)
868 * Scan the list and let the last match determine the fate.
869 * Return 1 for exclude, 0 for include and -1 for undecided.
871 int is_excluded_from_list(const char *pathname
,
872 int pathlen
, const char *basename
, int *dtype
,
873 struct exclude_list
*el
)
875 struct exclude
*exclude
;
876 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
878 return exclude
->flags
& EXC_FLAG_NEGATIVE ?
0 : 1;
879 return -1; /* undecided */
882 static struct exclude
*last_exclude_matching_from_lists(struct dir_struct
*dir
,
883 const char *pathname
, int pathlen
, const char *basename
,
887 struct exclude_list_group
*group
;
888 struct exclude
*exclude
;
889 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
890 group
= &dir
->exclude_list_group
[i
];
891 for (j
= group
->nr
- 1; j
>= 0; j
--) {
892 exclude
= last_exclude_matching_from_list(
893 pathname
, pathlen
, basename
, dtype_p
,
903 * Loads the per-directory exclude list for the substring of base
904 * which has a char length of baselen.
906 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
908 struct exclude_list_group
*group
;
909 struct exclude_list
*el
;
910 struct exclude_stack
*stk
= NULL
;
913 group
= &dir
->exclude_list_group
[EXC_DIRS
];
916 * Pop the exclude lists from the EXCL_DIRS exclude_list_group
917 * which originate from directories not in the prefix of the
918 * path being checked.
920 while ((stk
= dir
->exclude_stack
) != NULL
) {
921 if (stk
->baselen
<= baselen
&&
922 !strncmp(dir
->basebuf
.buf
, base
, stk
->baselen
))
924 el
= &group
->el
[dir
->exclude_stack
->exclude_ix
];
925 dir
->exclude_stack
= stk
->prev
;
927 free((char *)el
->src
); /* see strbuf_detach() below */
928 clear_exclude_list(el
);
933 /* Skip traversing into sub directories if the parent is excluded */
938 * Lazy initialization. All call sites currently just
939 * memset(dir, 0, sizeof(*dir)) before use. Changing all of
940 * them seems lots of work for little benefit.
942 if (!dir
->basebuf
.buf
)
943 strbuf_init(&dir
->basebuf
, PATH_MAX
);
945 /* Read from the parent directories and push them down. */
946 current
= stk ? stk
->baselen
: -1;
947 strbuf_setlen(&dir
->basebuf
, current
< 0 ?
0 : current
);
948 while (current
< baselen
) {
951 stk
= xcalloc(1, sizeof(*stk
));
956 cp
= strchr(base
+ current
+ 1, '/');
958 die("oops in prep_exclude");
961 stk
->prev
= dir
->exclude_stack
;
962 stk
->baselen
= cp
- base
;
963 stk
->exclude_ix
= group
->nr
;
964 el
= add_exclude_list(dir
, EXC_DIRS
, NULL
);
965 strbuf_add(&dir
->basebuf
, base
+ current
, stk
->baselen
- current
);
966 assert(stk
->baselen
== dir
->basebuf
.len
);
968 /* Abort if the directory is excluded */
971 dir
->basebuf
.buf
[stk
->baselen
- 1] = 0;
972 dir
->exclude
= last_exclude_matching_from_lists(dir
,
973 dir
->basebuf
.buf
, stk
->baselen
- 1,
974 dir
->basebuf
.buf
+ current
, &dt
);
975 dir
->basebuf
.buf
[stk
->baselen
- 1] = '/';
977 dir
->exclude
->flags
& EXC_FLAG_NEGATIVE
)
980 dir
->exclude_stack
= stk
;
985 /* Try to read per-directory file */
986 if (dir
->exclude_per_dir
) {
988 * dir->basebuf gets reused by the traversal, but we
989 * need fname to remain unchanged to ensure the src
990 * member of each struct exclude correctly
991 * back-references its source file. Other invocations
992 * of add_exclude_list provide stable strings, so we
993 * strbuf_detach() and free() here in the caller.
995 struct strbuf sb
= STRBUF_INIT
;
996 strbuf_addbuf(&sb
, &dir
->basebuf
);
997 strbuf_addstr(&sb
, dir
->exclude_per_dir
);
998 el
->src
= strbuf_detach(&sb
, NULL
);
999 add_excludes_from_file_to_list(el
->src
, el
->src
,
1000 stk
->baselen
, el
, 1);
1002 dir
->exclude_stack
= stk
;
1003 current
= stk
->baselen
;
1005 strbuf_setlen(&dir
->basebuf
, baselen
);
1009 * Loads the exclude lists for the directory containing pathname, then
1010 * scans all exclude lists to determine whether pathname is excluded.
1011 * Returns the exclude_list element which matched, or NULL for
1014 struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
1015 const char *pathname
,
1018 int pathlen
= strlen(pathname
);
1019 const char *basename
= strrchr(pathname
, '/');
1020 basename
= (basename
) ? basename
+1 : pathname
;
1022 prep_exclude(dir
, pathname
, basename
-pathname
);
1025 return dir
->exclude
;
1027 return last_exclude_matching_from_lists(dir
, pathname
, pathlen
,
1032 * Loads the exclude lists for the directory containing pathname, then
1033 * scans all exclude lists to determine whether pathname is excluded.
1034 * Returns 1 if true, otherwise 0.
1036 int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
1038 struct exclude
*exclude
=
1039 last_exclude_matching(dir
, pathname
, dtype_p
);
1041 return exclude
->flags
& EXC_FLAG_NEGATIVE ?
0 : 1;
1045 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
1047 struct dir_entry
*ent
;
1049 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
1051 memcpy(ent
->name
, pathname
, len
);
1056 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
1058 if (cache_file_exists(pathname
, len
, ignore_case
))
1061 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
1062 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
1065 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
1067 if (!cache_name_is_other(pathname
, len
))
1070 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
1071 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
1075 index_nonexistent
= 0,
1081 * Do not use the alphabetically sorted index to look up
1082 * the directory name; instead, use the case insensitive
1085 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
1087 const struct cache_entry
*ce
= cache_dir_exists(dirname
, len
);
1088 unsigned char endchar
;
1091 return index_nonexistent
;
1092 endchar
= ce
->name
[len
];
1095 * The cache_entry structure returned will contain this dirname
1096 * and possibly additional path components.
1099 return index_directory
;
1102 * If there are no additional path components, then this cache_entry
1103 * represents a submodule. Submodules, despite being directories,
1104 * are stored in the cache without a closing slash.
1106 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
1107 return index_gitdir
;
1109 /* This should never be hit, but it exists just in case. */
1110 return index_nonexistent
;
1114 * The index sorts alphabetically by entry name, which
1115 * means that a gitlink sorts as '\0' at the end, while
1116 * a directory (which is defined not as an entry, but as
1117 * the files it contains) will sort with the '/' at the
1120 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
1125 return directory_exists_in_index_icase(dirname
, len
);
1127 pos
= cache_name_pos(dirname
, len
);
1130 while (pos
< active_nr
) {
1131 const struct cache_entry
*ce
= active_cache
[pos
++];
1132 unsigned char endchar
;
1134 if (strncmp(ce
->name
, dirname
, len
))
1136 endchar
= ce
->name
[len
];
1140 return index_directory
;
1141 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
1142 return index_gitdir
;
1144 return index_nonexistent
;
1148 * When we find a directory when traversing the filesystem, we
1149 * have three distinct cases:
1152 * - see it as a directory
1155 * and which one we choose depends on a combination of existing
1156 * git index contents and the flags passed into the directory
1157 * traversal routine.
1159 * Case 1: If we *already* have entries in the index under that
1160 * directory name, we always recurse into the directory to see
1163 * Case 2: If we *already* have that directory name as a gitlink,
1164 * we always continue to see it as a gitlink, regardless of whether
1165 * there is an actual git directory there or not (it might not
1166 * be checked out as a subproject!)
1168 * Case 3: if we didn't have it in the index previously, we
1169 * have a few sub-cases:
1171 * (a) if "show_other_directories" is true, we show it as
1172 * just a directory, unless "hide_empty_directories" is
1173 * also true, in which case we need to check if it contains any
1174 * untracked and / or ignored files.
1175 * (b) if it looks like a git directory, and we don't have
1176 * 'no_gitlinks' set we treat it as a gitlink, and show it
1178 * (c) otherwise, we recurse into it.
1180 static enum path_treatment
treat_directory(struct dir_struct
*dir
,
1181 const char *dirname
, int len
, int exclude
,
1182 const struct path_simplify
*simplify
)
1184 /* The "len-1" is to strip the final '/' */
1185 switch (directory_exists_in_index(dirname
, len
-1)) {
1186 case index_directory
:
1187 return path_recurse
;
1192 case index_nonexistent
:
1193 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1195 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
1196 unsigned char sha1
[20];
1197 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
1198 return path_untracked
;
1200 return path_recurse
;
1203 /* This is the "show_other_directories" case */
1205 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1206 return exclude ? path_excluded
: path_untracked
;
1208 return read_directory_recursive(dir
, dirname
, len
, 1, simplify
);
1212 * This is an inexact early pruning of any recursive directory
1213 * reading - if the path cannot possibly be in the pathspec,
1214 * return true, and we'll skip it early.
1216 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1220 const char *match
= simplify
->path
;
1221 int len
= simplify
->len
;
1227 if (!memcmp(path
, match
, len
))
1237 * This function tells us whether an excluded path matches a
1238 * list of "interesting" pathspecs. That is, whether a path matched
1239 * by any of the pathspecs could possibly be ignored by excluding
1240 * the specified path. This can happen if:
1242 * 1. the path is mentioned explicitly in the pathspec
1244 * 2. the path is a directory prefix of some element in the
1247 static int exclude_matches_pathspec(const char *path
, int len
,
1248 const struct path_simplify
*simplify
)
1251 for (; simplify
->path
; simplify
++) {
1252 if (len
== simplify
->len
1253 && !memcmp(path
, simplify
->path
, len
))
1255 if (len
< simplify
->len
1256 && simplify
->path
[len
] == '/'
1257 && !memcmp(path
, simplify
->path
, len
))
1264 static int get_index_dtype(const char *path
, int len
)
1267 const struct cache_entry
*ce
;
1269 ce
= cache_file_exists(path
, len
, 0);
1271 if (!ce_uptodate(ce
))
1273 if (S_ISGITLINK(ce
->ce_mode
))
1276 * Nobody actually cares about the
1277 * difference between DT_LNK and DT_REG
1282 /* Try to look it up as a directory */
1283 pos
= cache_name_pos(path
, len
);
1287 while (pos
< active_nr
) {
1288 ce
= active_cache
[pos
++];
1289 if (strncmp(ce
->name
, path
, len
))
1291 if (ce
->name
[len
] > '/')
1293 if (ce
->name
[len
] < '/')
1295 if (!ce_uptodate(ce
))
1296 break; /* continue? */
1302 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1304 int dtype
= de ?
DTYPE(de
) : DT_UNKNOWN
;
1307 if (dtype
!= DT_UNKNOWN
)
1309 dtype
= get_index_dtype(path
, len
);
1310 if (dtype
!= DT_UNKNOWN
)
1312 if (lstat(path
, &st
))
1314 if (S_ISREG(st
.st_mode
))
1316 if (S_ISDIR(st
.st_mode
))
1318 if (S_ISLNK(st
.st_mode
))
1323 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1324 struct strbuf
*path
,
1325 const struct path_simplify
*simplify
,
1326 int dtype
, struct dirent
*de
)
1329 int has_path_in_index
= !!cache_file_exists(path
->buf
, path
->len
, ignore_case
);
1331 if (dtype
== DT_UNKNOWN
)
1332 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1334 /* Always exclude indexed files */
1335 if (dtype
!= DT_DIR
&& has_path_in_index
)
1339 * When we are looking at a directory P in the working tree,
1340 * there are three cases:
1342 * (1) P exists in the index. Everything inside the directory P in
1343 * the working tree needs to go when P is checked out from the
1346 * (2) P does not exist in the index, but there is P/Q in the index.
1347 * We know P will stay a directory when we check out the contents
1348 * of the index, but we do not know yet if there is a directory
1349 * P/Q in the working tree to be killed, so we need to recurse.
1351 * (3) P does not exist in the index, and there is no P/Q in the index
1352 * to require P to be a directory, either. Only in this case, we
1353 * know that everything inside P will not be killed without
1356 if ((dir
->flags
& DIR_COLLECT_KILLED_ONLY
) &&
1357 (dtype
== DT_DIR
) &&
1358 !has_path_in_index
&&
1359 (directory_exists_in_index(path
->buf
, path
->len
) == index_nonexistent
))
1362 exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1365 * Excluded? If we don't explicitly want to show
1366 * ignored files, ignore it
1368 if (exclude
&& !(dir
->flags
& (DIR_SHOW_IGNORED
|DIR_SHOW_IGNORED_TOO
)))
1369 return path_excluded
;
1375 strbuf_addch(path
, '/');
1376 return treat_directory(dir
, path
->buf
, path
->len
, exclude
,
1380 return exclude ? path_excluded
: path_untracked
;
1384 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1386 struct strbuf
*path
,
1388 const struct path_simplify
*simplify
)
1392 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1394 strbuf_setlen(path
, baselen
);
1395 strbuf_addstr(path
, de
->d_name
);
1396 if (simplify_away(path
->buf
, path
->len
, simplify
))
1400 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1404 * Read a directory tree. We currently ignore anything but
1405 * directories, regular files and symlinks. That's because git
1406 * doesn't handle them at all yet. Maybe that will change some
1409 * Also, we ignore the name ".git" (even if it is not a directory).
1410 * That likely will not change.
1412 * Returns the most significant path_treatment value encountered in the scan.
1414 static enum path_treatment
read_directory_recursive(struct dir_struct
*dir
,
1415 const char *base
, int baselen
,
1417 const struct path_simplify
*simplify
)
1420 enum path_treatment state
, subdir_state
, dir_state
= path_none
;
1422 struct strbuf path
= STRBUF_INIT
;
1424 strbuf_add(&path
, base
, baselen
);
1426 fdir
= opendir(path
.len ? path
.buf
: ".");
1430 while ((de
= readdir(fdir
)) != NULL
) {
1431 /* check how the file or directory should be treated */
1432 state
= treat_path(dir
, de
, &path
, baselen
, simplify
);
1433 if (state
> dir_state
)
1436 /* recurse into subdir if instructed by treat_path */
1437 if (state
== path_recurse
) {
1438 subdir_state
= read_directory_recursive(dir
, path
.buf
,
1439 path
.len
, check_only
, simplify
);
1440 if (subdir_state
> dir_state
)
1441 dir_state
= subdir_state
;
1445 /* abort early if maximum state has been reached */
1446 if (dir_state
== path_untracked
)
1448 /* skip the dir_add_* part */
1452 /* add the path to the appropriate result list */
1455 if (dir
->flags
& DIR_SHOW_IGNORED
)
1456 dir_add_name(dir
, path
.buf
, path
.len
);
1457 else if ((dir
->flags
& DIR_SHOW_IGNORED_TOO
) ||
1458 ((dir
->flags
& DIR_COLLECT_IGNORED
) &&
1459 exclude_matches_pathspec(path
.buf
, path
.len
,
1461 dir_add_ignored(dir
, path
.buf
, path
.len
);
1464 case path_untracked
:
1465 if (!(dir
->flags
& DIR_SHOW_IGNORED
))
1466 dir_add_name(dir
, path
.buf
, path
.len
);
1475 strbuf_release(&path
);
1480 static int cmp_name(const void *p1
, const void *p2
)
1482 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1483 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1485 return name_compare(e1
->name
, e1
->len
, e2
->name
, e2
->len
);
1488 static struct path_simplify
*create_simplify(const char **pathspec
)
1491 struct path_simplify
*simplify
= NULL
;
1496 for (nr
= 0 ; ; nr
++) {
1498 ALLOC_GROW(simplify
, nr
+ 1, alloc
);
1499 match
= *pathspec
++;
1502 simplify
[nr
].path
= match
;
1503 simplify
[nr
].len
= simple_length(match
);
1505 simplify
[nr
].path
= NULL
;
1506 simplify
[nr
].len
= 0;
1510 static void free_simplify(struct path_simplify
*simplify
)
1515 static int treat_leading_path(struct dir_struct
*dir
,
1516 const char *path
, int len
,
1517 const struct path_simplify
*simplify
)
1519 struct strbuf sb
= STRBUF_INIT
;
1520 int baselen
, rc
= 0;
1522 int old_flags
= dir
->flags
;
1524 while (len
&& path
[len
- 1] == '/')
1529 dir
->flags
&= ~DIR_SHOW_OTHER_DIRECTORIES
;
1531 cp
= path
+ baselen
+ !!baselen
;
1532 cp
= memchr(cp
, '/', path
+ len
- cp
);
1536 baselen
= cp
- path
;
1537 strbuf_setlen(&sb
, 0);
1538 strbuf_add(&sb
, path
, baselen
);
1539 if (!is_directory(sb
.buf
))
1541 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1543 if (treat_one_path(dir
, &sb
, simplify
,
1544 DT_DIR
, NULL
) == path_none
)
1545 break; /* do not recurse into it */
1546 if (len
<= baselen
) {
1548 break; /* finished checking */
1551 strbuf_release(&sb
);
1552 dir
->flags
= old_flags
;
1556 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const struct pathspec
*pathspec
)
1558 struct path_simplify
*simplify
;
1561 * Check out create_simplify()
1564 GUARD_PATHSPEC(pathspec
,
1572 if (has_symlink_leading_path(path
, len
))
1576 * exclude patterns are treated like positive ones in
1577 * create_simplify. Usually exclude patterns should be a
1578 * subset of positive ones, which has no impacts on
1579 * create_simplify().
1581 simplify
= create_simplify(pathspec ? pathspec
->_raw
: NULL
);
1582 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1583 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1584 free_simplify(simplify
);
1585 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1586 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1590 int file_exists(const char *f
)
1593 return lstat(f
, &sb
) == 0;
1597 * Given two normalized paths (a trailing slash is ok), if subdir is
1598 * outside dir, return -1. Otherwise return the offset in subdir that
1599 * can be used as relative path to dir.
1601 int dir_inside_of(const char *subdir
, const char *dir
)
1605 assert(dir
&& subdir
&& *dir
&& *subdir
);
1607 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1613 /* hel[p]/me vs hel[l]/yeah */
1614 if (*dir
&& *subdir
)
1618 return !*dir ? offset
: -1; /* same dir */
1620 /* foo/[b]ar vs foo/[] */
1621 if (is_dir_sep(dir
[-1]))
1622 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1624 /* foo[/]bar vs foo[] */
1625 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1628 int is_inside_dir(const char *dir
)
1637 rc
= (dir_inside_of(cwd
, dir
) >= 0);
1642 int is_empty_dir(const char *path
)
1644 DIR *dir
= opendir(path
);
1651 while ((e
= readdir(dir
)) != NULL
)
1652 if (!is_dot_or_dotdot(e
->d_name
)) {
1661 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1665 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1666 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1667 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1668 unsigned char submodule_head
[20];
1670 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1671 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1672 /* Do not descend and nuke a nested git work tree. */
1678 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1679 dir
= opendir(path
->buf
);
1681 if (errno
== ENOENT
)
1682 return keep_toplevel ?
-1 : 0;
1683 else if (errno
== EACCES
&& !keep_toplevel
)
1685 * An empty dir could be removable even if it
1688 return rmdir(path
->buf
);
1692 if (path
->buf
[original_len
- 1] != '/')
1693 strbuf_addch(path
, '/');
1696 while ((e
= readdir(dir
)) != NULL
) {
1698 if (is_dot_or_dotdot(e
->d_name
))
1701 strbuf_setlen(path
, len
);
1702 strbuf_addstr(path
, e
->d_name
);
1703 if (lstat(path
->buf
, &st
)) {
1704 if (errno
== ENOENT
)
1706 * file disappeared, which is what we
1711 } else if (S_ISDIR(st
.st_mode
)) {
1712 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1713 continue; /* happy */
1714 } else if (!only_empty
&&
1715 (!unlink(path
->buf
) || errno
== ENOENT
)) {
1716 continue; /* happy, too */
1719 /* path too long, stat fails, or non-directory still exists */
1725 strbuf_setlen(path
, original_len
);
1726 if (!ret
&& !keep_toplevel
&& !kept_down
)
1727 ret
= (!rmdir(path
->buf
) || errno
== ENOENT
) ?
0 : -1;
1730 * report the uplevel that it is not an error that we
1731 * did not rmdir() our directory.
1737 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1739 return remove_dir_recurse(path
, flag
, NULL
);
1742 void setup_standard_excludes(struct dir_struct
*dir
)
1747 dir
->exclude_per_dir
= ".gitignore";
1748 path
= git_path("info/exclude");
1749 if (!excludes_file
) {
1750 home_config_paths(NULL
, &xdg_path
, "ignore");
1751 excludes_file
= xdg_path
;
1753 if (!access_or_warn(path
, R_OK
, 0))
1754 add_excludes_from_file(dir
, path
);
1755 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
, 0))
1756 add_excludes_from_file(dir
, excludes_file
);
1759 int remove_path(const char *name
)
1763 if (unlink(name
) && errno
!= ENOENT
&& errno
!= ENOTDIR
)
1766 slash
= strrchr(name
, '/');
1768 char *dirs
= xstrdup(name
);
1769 slash
= dirs
+ (slash
- name
);
1772 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1779 * Frees memory within dir which was allocated for exclude lists and
1780 * the exclude_stack. Does not free dir itself.
1782 void clear_directory(struct dir_struct
*dir
)
1785 struct exclude_list_group
*group
;
1786 struct exclude_list
*el
;
1787 struct exclude_stack
*stk
;
1789 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
1790 group
= &dir
->exclude_list_group
[i
];
1791 for (j
= 0; j
< group
->nr
; j
++) {
1794 free((char *)el
->src
);
1795 clear_exclude_list(el
);
1800 stk
= dir
->exclude_stack
;
1802 struct exclude_stack
*prev
= stk
->prev
;
1806 strbuf_release(&dir
->basebuf
);