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 #include "ewah/ewok.h"
18 struct path_simplify
{
24 * Tells read_directory_recursive how a file or directory should be treated.
25 * Values are ordered by significance, e.g. if a directory contains both
26 * excluded and untracked files, it is listed as untracked because
27 * path_untracked > path_excluded.
37 * Support data structure for our opendir/readdir/closedir wrappers
41 struct untracked_cache_dir
*untracked
;
47 struct untracked_cache_dir
*ucd
;
50 static enum path_treatment
read_directory_recursive(struct dir_struct
*dir
,
51 const char *path
, int len
, struct untracked_cache_dir
*untracked
,
52 int check_only
, const struct path_simplify
*simplify
);
53 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
55 /* helper string functions with support for the ignore_case flag */
56 int strcmp_icase(const char *a
, const char *b
)
58 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
61 int strncmp_icase(const char *a
, const char *b
, size_t count
)
63 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
66 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
68 return wildmatch(pattern
, string
,
69 flags
| (ignore_case
? WM_CASEFOLD
: 0),
73 int git_fnmatch(const struct pathspec_item
*item
,
74 const char *pattern
, const char *string
,
78 if (ps_strncmp(item
, pattern
, string
, prefix
))
83 if (item
->flags
& PATHSPEC_ONESTAR
) {
84 int pattern_len
= strlen(++pattern
);
85 int string_len
= strlen(string
);
86 return string_len
< pattern_len
||
87 ps_strcmp(item
, pattern
,
88 string
+ string_len
- pattern_len
);
90 if (item
->magic
& PATHSPEC_GLOB
)
91 return wildmatch(pattern
, string
,
93 (item
->magic
& PATHSPEC_ICASE
? WM_CASEFOLD
: 0),
96 /* wildmatch has not learned no FNM_PATHNAME mode yet */
97 return wildmatch(pattern
, string
,
98 item
->magic
& PATHSPEC_ICASE
? WM_CASEFOLD
: 0,
102 static int fnmatch_icase_mem(const char *pattern
, int patternlen
,
103 const char *string
, int stringlen
,
107 struct strbuf pat_buf
= STRBUF_INIT
;
108 struct strbuf str_buf
= STRBUF_INIT
;
109 const char *use_pat
= pattern
;
110 const char *use_str
= string
;
112 if (pattern
[patternlen
]) {
113 strbuf_add(&pat_buf
, pattern
, patternlen
);
114 use_pat
= pat_buf
.buf
;
116 if (string
[stringlen
]) {
117 strbuf_add(&str_buf
, string
, stringlen
);
118 use_str
= str_buf
.buf
;
122 flags
|= WM_CASEFOLD
;
123 match_status
= wildmatch(use_pat
, use_str
, flags
, NULL
);
125 strbuf_release(&pat_buf
);
126 strbuf_release(&str_buf
);
131 static size_t common_prefix_len(const struct pathspec
*pathspec
)
137 * ":(icase)path" is treated as a pathspec full of
138 * wildcard. In other words, only prefix is considered common
139 * prefix. If the pathspec is abc/foo abc/bar, running in
140 * subdir xyz, the common prefix is still xyz, not xuz/abc as
143 GUARD_PATHSPEC(pathspec
,
151 for (n
= 0; n
< pathspec
->nr
; n
++) {
152 size_t i
= 0, len
= 0, item_len
;
153 if (pathspec
->items
[n
].magic
& PATHSPEC_EXCLUDE
)
155 if (pathspec
->items
[n
].magic
& PATHSPEC_ICASE
)
156 item_len
= pathspec
->items
[n
].prefix
;
158 item_len
= pathspec
->items
[n
].nowildcard_len
;
159 while (i
< item_len
&& (n
== 0 || i
< max
)) {
160 char c
= pathspec
->items
[n
].match
[i
];
161 if (c
!= pathspec
->items
[0].match
[i
])
167 if (n
== 0 || len
< max
) {
177 * Returns a copy of the longest leading path common among all
180 char *common_prefix(const struct pathspec
*pathspec
)
182 unsigned long len
= common_prefix_len(pathspec
);
184 return len
? xmemdupz(pathspec
->items
[0].match
, len
) : NULL
;
187 int fill_directory(struct dir_struct
*dir
, const struct pathspec
*pathspec
)
192 * Calculate common prefix for the pathspec, and
193 * use that to optimize the directory walk
195 len
= common_prefix_len(pathspec
);
197 /* Read the directory and prune it */
198 read_directory(dir
, pathspec
->nr
? pathspec
->_raw
[0] : "", len
, pathspec
);
202 int within_depth(const char *name
, int namelen
,
203 int depth
, int max_depth
)
205 const char *cp
= name
, *cpe
= name
+ namelen
;
211 if (depth
> max_depth
)
217 #define DO_MATCH_EXCLUDE 1
218 #define DO_MATCH_DIRECTORY 2
221 * Does 'match' match the given name?
222 * A match is found if
224 * (1) the 'match' string is leading directory of 'name', or
225 * (2) the 'match' string is a wildcard and matches 'name', or
226 * (3) the 'match' string is exactly the same as 'name'.
228 * and the return value tells which case it was.
230 * It returns 0 when there is no match.
232 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
233 const char *name
, int namelen
, unsigned flags
)
235 /* name/namelen has prefix cut off by caller */
236 const char *match
= item
->match
+ prefix
;
237 int matchlen
= item
->len
- prefix
;
240 * The normal call pattern is:
241 * 1. prefix = common_prefix_len(ps);
242 * 2. prune something, or fill_directory
243 * 3. match_pathspec()
245 * 'prefix' at #1 may be shorter than the command's prefix and
246 * it's ok for #2 to match extra files. Those extras will be
249 * Suppose the pathspec is 'foo' and '../bar' running from
250 * subdir 'xyz'. The common prefix at #1 will be empty, thanks
251 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
252 * user does not want XYZ/foo, only the "foo" part should be
253 * case-insensitive. We need to filter out XYZ/foo here. In
254 * other words, we do not trust the caller on comparing the
255 * prefix part when :(icase) is involved. We do exact
256 * comparison ourselves.
258 * Normally the caller (common_prefix_len() in fact) does
259 * _exact_ matching on name[-prefix+1..-1] and we do not need
260 * to check that part. Be defensive and check it anyway, in
261 * case common_prefix_len is changed, or a new caller is
262 * introduced that does not use common_prefix_len.
264 * If the penalty turns out too high when prefix is really
265 * long, maybe change it to
266 * strncmp(match, name, item->prefix - prefix)
268 if (item
->prefix
&& (item
->magic
& PATHSPEC_ICASE
) &&
269 strncmp(item
->match
, name
- prefix
, item
->prefix
))
272 /* If the match was just the prefix, we matched */
274 return MATCHED_RECURSIVELY
;
276 if (matchlen
<= namelen
&& !ps_strncmp(item
, match
, name
, matchlen
)) {
277 if (matchlen
== namelen
)
278 return MATCHED_EXACTLY
;
280 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
281 return MATCHED_RECURSIVELY
;
282 } else if ((flags
& DO_MATCH_DIRECTORY
) &&
283 match
[matchlen
- 1] == '/' &&
284 namelen
== matchlen
- 1 &&
285 !ps_strncmp(item
, match
, name
, namelen
))
286 return MATCHED_EXACTLY
;
288 if (item
->nowildcard_len
< item
->len
&&
289 !git_fnmatch(item
, match
, name
,
290 item
->nowildcard_len
- prefix
))
291 return MATCHED_FNMATCH
;
297 * Given a name and a list of pathspecs, returns the nature of the
298 * closest (i.e. most specific) match of the name to any of the
301 * The caller typically calls this multiple times with the same
302 * pathspec and seen[] array but with different name/namelen
303 * (e.g. entries from the index) and is interested in seeing if and
304 * how each pathspec matches all the names it calls this function
305 * with. A mark is left in the seen[] array for each pathspec element
306 * indicating the closest type of match that element achieved, so if
307 * seen[n] remains zero after multiple invocations, that means the nth
308 * pathspec did not match any names, which could indicate that the
309 * user mistyped the nth pathspec.
311 static int do_match_pathspec(const struct pathspec
*ps
,
312 const char *name
, int namelen
,
313 int prefix
, char *seen
,
316 int i
, retval
= 0, exclude
= flags
& DO_MATCH_EXCLUDE
;
327 if (!ps
->recursive
||
328 !(ps
->magic
& PATHSPEC_MAXDEPTH
) ||
330 return MATCHED_RECURSIVELY
;
332 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
333 return MATCHED_EXACTLY
;
341 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
344 if ((!exclude
&& ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
) ||
345 ( exclude
&& !(ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
)))
348 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
351 * Make exclude patterns optional and never report
352 * "pathspec ':(exclude)foo' matches no files"
354 if (seen
&& ps
->items
[i
].magic
& PATHSPEC_EXCLUDE
)
355 seen
[i
] = MATCHED_FNMATCH
;
356 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
,
359 (ps
->magic
& PATHSPEC_MAXDEPTH
) &&
360 ps
->max_depth
!= -1 &&
361 how
&& how
!= MATCHED_FNMATCH
) {
362 int len
= ps
->items
[i
].len
;
363 if (name
[len
] == '/')
365 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
366 how
= MATCHED_EXACTLY
;
373 if (seen
&& seen
[i
] < how
)
380 int match_pathspec(const struct pathspec
*ps
,
381 const char *name
, int namelen
,
382 int prefix
, char *seen
, int is_dir
)
384 int positive
, negative
;
385 unsigned flags
= is_dir
? DO_MATCH_DIRECTORY
: 0;
386 positive
= do_match_pathspec(ps
, name
, namelen
,
387 prefix
, seen
, flags
);
388 if (!(ps
->magic
& PATHSPEC_EXCLUDE
) || !positive
)
390 negative
= do_match_pathspec(ps
, name
, namelen
,
392 flags
| DO_MATCH_EXCLUDE
);
393 return negative
? 0 : positive
;
397 * Return the length of the "simple" part of a path match limiter.
399 int simple_length(const char *match
)
404 unsigned char c
= *match
++;
406 if (c
== '\0' || is_glob_special(c
))
411 int no_wildcard(const char *string
)
413 return string
[simple_length(string
)] == '\0';
416 void parse_exclude_pattern(const char **pattern
,
421 const char *p
= *pattern
;
426 *flags
|= EXC_FLAG_NEGATIVE
;
430 if (len
&& p
[len
- 1] == '/') {
432 *flags
|= EXC_FLAG_MUSTBEDIR
;
434 for (i
= 0; i
< len
; i
++) {
439 *flags
|= EXC_FLAG_NODIR
;
440 *nowildcardlen
= simple_length(p
);
442 * we should have excluded the trailing slash from 'p' too,
443 * but that's one more allocation. Instead just make sure
444 * nowildcardlen does not exceed real patternlen
446 if (*nowildcardlen
> len
)
447 *nowildcardlen
= len
;
448 if (*p
== '*' && no_wildcard(p
+ 1))
449 *flags
|= EXC_FLAG_ENDSWITH
;
454 void add_exclude(const char *string
, const char *base
,
455 int baselen
, struct exclude_list
*el
, int srcpos
)
462 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
463 if (flags
& EXC_FLAG_MUSTBEDIR
) {
465 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
467 memcpy(s
, string
, patternlen
);
468 s
[patternlen
] = '\0';
471 x
= xmalloc(sizeof(*x
));
474 x
->patternlen
= patternlen
;
475 x
->nowildcardlen
= nowildcardlen
;
477 x
->baselen
= baselen
;
480 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
481 el
->excludes
[el
->nr
++] = x
;
485 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
,
486 struct sha1_stat
*sha1_stat
)
490 enum object_type type
;
494 pos
= cache_name_pos(path
, len
);
497 if (!ce_skip_worktree(active_cache
[pos
]))
499 data
= read_sha1_file(active_cache
[pos
]->sha1
, &type
, &sz
);
500 if (!data
|| type
!= OBJ_BLOB
) {
506 memset(&sha1_stat
->stat
, 0, sizeof(sha1_stat
->stat
));
507 hashcpy(sha1_stat
->sha1
, active_cache
[pos
]->sha1
);
513 * Frees memory within el which was allocated for exclude patterns and
514 * the file buffer. Does not free el itself.
516 void clear_exclude_list(struct exclude_list
*el
)
520 for (i
= 0; i
< el
->nr
; i
++)
521 free(el
->excludes
[i
]);
530 static void trim_trailing_spaces(char *buf
)
532 char *p
, *last_space
= NULL
;
534 for (p
= buf
; *p
; p
++)
554 * Given a subdirectory name and "dir" of the current directory,
555 * search the subdir in "dir" and return it, or create a new one if it
556 * does not exist in "dir".
558 * If "name" has the trailing slash, it'll be excluded in the search.
560 static struct untracked_cache_dir
*lookup_untracked(struct untracked_cache
*uc
,
561 struct untracked_cache_dir
*dir
,
562 const char *name
, int len
)
565 struct untracked_cache_dir
*d
;
568 if (len
&& name
[len
- 1] == '/')
572 while (last
> first
) {
573 int cmp
, next
= (last
+ first
) >> 1;
575 cmp
= strncmp(name
, d
->name
, len
);
576 if (!cmp
&& strlen(d
->name
) > len
)
588 d
= xmalloc(sizeof(*d
) + len
+ 1);
589 memset(d
, 0, sizeof(*d
));
590 memcpy(d
->name
, name
, len
);
593 ALLOC_GROW(dir
->dirs
, dir
->dirs_nr
+ 1, dir
->dirs_alloc
);
594 memmove(dir
->dirs
+ first
+ 1, dir
->dirs
+ first
,
595 (dir
->dirs_nr
- first
) * sizeof(*dir
->dirs
));
597 dir
->dirs
[first
] = d
;
601 static void do_invalidate_gitignore(struct untracked_cache_dir
*dir
)
605 dir
->untracked_nr
= 0;
606 for (i
= 0; i
< dir
->dirs_nr
; i
++)
607 do_invalidate_gitignore(dir
->dirs
[i
]);
610 static void invalidate_gitignore(struct untracked_cache
*uc
,
611 struct untracked_cache_dir
*dir
)
613 uc
->gitignore_invalidated
++;
614 do_invalidate_gitignore(dir
);
617 static void invalidate_directory(struct untracked_cache
*uc
,
618 struct untracked_cache_dir
*dir
)
621 uc
->dir_invalidated
++;
623 dir
->untracked_nr
= 0;
624 for (i
= 0; i
< dir
->dirs_nr
; i
++)
625 dir
->dirs
[i
]->recurse
= 0;
629 * Given a file with name "fname", read it (either from disk, or from
630 * the index if "check_index" is non-zero), parse it and store the
631 * exclude rules in "el".
633 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
634 * stat data from disk (only valid if add_excludes returns zero). If
635 * ss_valid is non-zero, "ss" must contain good value as input.
637 static int add_excludes(const char *fname
, const char *base
, int baselen
,
638 struct exclude_list
*el
, int check_index
,
639 struct sha1_stat
*sha1_stat
)
642 int fd
, i
, lineno
= 1;
646 fd
= open(fname
, O_RDONLY
);
647 if (fd
< 0 || fstat(fd
, &st
) < 0) {
649 warn_on_inaccessible(fname
);
653 (buf
= read_skip_worktree_file_from_index(fname
, &size
, sha1_stat
)) == NULL
)
659 if (buf
[size
-1] != '\n') {
660 buf
= xrealloc(buf
, size
+1);
664 size
= xsize_t(st
.st_size
);
667 fill_stat_data(&sha1_stat
->stat
, &st
);
668 hashcpy(sha1_stat
->sha1
, EMPTY_BLOB_SHA1_BIN
);
669 sha1_stat
->valid
= 1;
674 buf
= xmalloc(size
+1);
675 if (read_in_full(fd
, buf
, size
) != size
) {
684 if (sha1_stat
->valid
&&
685 !match_stat_data_racy(&the_index
, &sha1_stat
->stat
, &st
))
686 ; /* no content change, ss->sha1 still good */
687 else if (check_index
&&
688 (pos
= cache_name_pos(fname
, strlen(fname
))) >= 0 &&
689 !ce_stage(active_cache
[pos
]) &&
690 ce_uptodate(active_cache
[pos
]) &&
691 !would_convert_to_git(fname
))
692 hashcpy(sha1_stat
->sha1
, active_cache
[pos
]->sha1
);
694 hash_sha1_file(buf
, size
, "blob", sha1_stat
->sha1
);
695 fill_stat_data(&sha1_stat
->stat
, &st
);
696 sha1_stat
->valid
= 1;
702 for (i
= 0; i
< size
; i
++) {
703 if (buf
[i
] == '\n') {
704 if (entry
!= buf
+ i
&& entry
[0] != '#') {
705 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
706 trim_trailing_spaces(entry
);
707 add_exclude(entry
, base
, baselen
, el
, lineno
);
716 int add_excludes_from_file_to_list(const char *fname
, const char *base
,
717 int baselen
, struct exclude_list
*el
,
720 return add_excludes(fname
, base
, baselen
, el
, check_index
, NULL
);
723 struct exclude_list
*add_exclude_list(struct dir_struct
*dir
,
724 int group_type
, const char *src
)
726 struct exclude_list
*el
;
727 struct exclude_list_group
*group
;
729 group
= &dir
->exclude_list_group
[group_type
];
730 ALLOC_GROW(group
->el
, group
->nr
+ 1, group
->alloc
);
731 el
= &group
->el
[group
->nr
++];
732 memset(el
, 0, sizeof(*el
));
738 * Used to set up core.excludesfile and .git/info/exclude lists.
740 static void add_excludes_from_file_1(struct dir_struct
*dir
, const char *fname
,
741 struct sha1_stat
*sha1_stat
)
743 struct exclude_list
*el
;
745 * catch setup_standard_excludes() that's called before
746 * dir->untracked is assigned. That function behaves
747 * differently when dir->untracked is non-NULL.
750 dir
->unmanaged_exclude_files
++;
751 el
= add_exclude_list(dir
, EXC_FILE
, fname
);
752 if (add_excludes(fname
, "", 0, el
, 0, sha1_stat
) < 0)
753 die("cannot use %s as an exclude file", fname
);
756 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
758 dir
->unmanaged_exclude_files
++; /* see validate_untracked_cache() */
759 add_excludes_from_file_1(dir
, fname
, NULL
);
762 int match_basename(const char *basename
, int basenamelen
,
763 const char *pattern
, int prefix
, int patternlen
,
766 if (prefix
== patternlen
) {
767 if (patternlen
== basenamelen
&&
768 !strncmp_icase(pattern
, basename
, basenamelen
))
770 } else if (flags
& EXC_FLAG_ENDSWITH
) {
771 /* "*literal" matching against "fooliteral" */
772 if (patternlen
- 1 <= basenamelen
&&
773 !strncmp_icase(pattern
+ 1,
774 basename
+ basenamelen
- (patternlen
- 1),
778 if (fnmatch_icase_mem(pattern
, patternlen
,
779 basename
, basenamelen
,
786 int match_pathname(const char *pathname
, int pathlen
,
787 const char *base
, int baselen
,
788 const char *pattern
, int prefix
, int patternlen
,
795 * match with FNM_PATHNAME; the pattern has base implicitly
798 if (*pattern
== '/') {
805 * baselen does not count the trailing slash. base[] may or
806 * may not end with a trailing slash though.
808 if (pathlen
< baselen
+ 1 ||
809 (baselen
&& pathname
[baselen
] != '/') ||
810 strncmp_icase(pathname
, base
, baselen
))
813 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
814 name
= pathname
+ pathlen
- namelen
;
818 * if the non-wildcard part is longer than the
819 * remaining pathname, surely it cannot match.
821 if (prefix
> namelen
)
824 if (strncmp_icase(pattern
, name
, prefix
))
827 patternlen
-= prefix
;
832 * If the whole pattern did not have a wildcard,
833 * then our prefix match is all we need; we
834 * do not need to call fnmatch at all.
836 if (!patternlen
&& !namelen
)
840 return fnmatch_icase_mem(pattern
, patternlen
,
846 * Scan the given exclude list in reverse to see whether pathname
847 * should be ignored. The first match (i.e. the last on the list), if
848 * any, determines the fate. Returns the exclude_list element which
849 * matched, or NULL for undecided.
851 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
853 const char *basename
,
855 struct exclude_list
*el
)
860 return NULL
; /* undefined */
862 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
863 struct exclude
*x
= el
->excludes
[i
];
864 const char *exclude
= x
->pattern
;
865 int prefix
= x
->nowildcardlen
;
867 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
868 if (*dtype
== DT_UNKNOWN
)
869 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
870 if (*dtype
!= DT_DIR
)
874 if (x
->flags
& EXC_FLAG_NODIR
) {
875 if (match_basename(basename
,
876 pathlen
- (basename
- pathname
),
877 exclude
, prefix
, x
->patternlen
,
883 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
884 if (match_pathname(pathname
, pathlen
,
885 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
886 exclude
, prefix
, x
->patternlen
, x
->flags
))
889 return NULL
; /* undecided */
893 * Scan the list and let the last match determine the fate.
894 * Return 1 for exclude, 0 for include and -1 for undecided.
896 int is_excluded_from_list(const char *pathname
,
897 int pathlen
, const char *basename
, int *dtype
,
898 struct exclude_list
*el
)
900 struct exclude
*exclude
;
901 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
903 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
904 return -1; /* undecided */
907 static struct exclude
*last_exclude_matching_from_lists(struct dir_struct
*dir
,
908 const char *pathname
, int pathlen
, const char *basename
,
912 struct exclude_list_group
*group
;
913 struct exclude
*exclude
;
914 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
915 group
= &dir
->exclude_list_group
[i
];
916 for (j
= group
->nr
- 1; j
>= 0; j
--) {
917 exclude
= last_exclude_matching_from_list(
918 pathname
, pathlen
, basename
, dtype_p
,
928 * Loads the per-directory exclude list for the substring of base
929 * which has a char length of baselen.
931 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
933 struct exclude_list_group
*group
;
934 struct exclude_list
*el
;
935 struct exclude_stack
*stk
= NULL
;
936 struct untracked_cache_dir
*untracked
;
939 group
= &dir
->exclude_list_group
[EXC_DIRS
];
942 * Pop the exclude lists from the EXCL_DIRS exclude_list_group
943 * which originate from directories not in the prefix of the
944 * path being checked.
946 while ((stk
= dir
->exclude_stack
) != NULL
) {
947 if (stk
->baselen
<= baselen
&&
948 !strncmp(dir
->basebuf
.buf
, base
, stk
->baselen
))
950 el
= &group
->el
[dir
->exclude_stack
->exclude_ix
];
951 dir
->exclude_stack
= stk
->prev
;
953 free((char *)el
->src
); /* see strbuf_detach() below */
954 clear_exclude_list(el
);
959 /* Skip traversing into sub directories if the parent is excluded */
964 * Lazy initialization. All call sites currently just
965 * memset(dir, 0, sizeof(*dir)) before use. Changing all of
966 * them seems lots of work for little benefit.
968 if (!dir
->basebuf
.buf
)
969 strbuf_init(&dir
->basebuf
, PATH_MAX
);
971 /* Read from the parent directories and push them down. */
972 current
= stk
? stk
->baselen
: -1;
973 strbuf_setlen(&dir
->basebuf
, current
< 0 ? 0 : current
);
975 untracked
= stk
? stk
->ucd
: dir
->untracked
->root
;
979 while (current
< baselen
) {
981 struct sha1_stat sha1_stat
;
983 stk
= xcalloc(1, sizeof(*stk
));
988 cp
= strchr(base
+ current
+ 1, '/');
990 die("oops in prep_exclude");
993 lookup_untracked(dir
->untracked
, untracked
,
995 cp
- base
- current
);
997 stk
->prev
= dir
->exclude_stack
;
998 stk
->baselen
= cp
- base
;
999 stk
->exclude_ix
= group
->nr
;
1000 stk
->ucd
= untracked
;
1001 el
= add_exclude_list(dir
, EXC_DIRS
, NULL
);
1002 strbuf_add(&dir
->basebuf
, base
+ current
, stk
->baselen
- current
);
1003 assert(stk
->baselen
== dir
->basebuf
.len
);
1005 /* Abort if the directory is excluded */
1008 dir
->basebuf
.buf
[stk
->baselen
- 1] = 0;
1009 dir
->exclude
= last_exclude_matching_from_lists(dir
,
1010 dir
->basebuf
.buf
, stk
->baselen
- 1,
1011 dir
->basebuf
.buf
+ current
, &dt
);
1012 dir
->basebuf
.buf
[stk
->baselen
- 1] = '/';
1014 dir
->exclude
->flags
& EXC_FLAG_NEGATIVE
)
1015 dir
->exclude
= NULL
;
1017 dir
->exclude_stack
= stk
;
1022 /* Try to read per-directory file */
1023 hashclr(sha1_stat
.sha1
);
1024 sha1_stat
.valid
= 0;
1025 if (dir
->exclude_per_dir
&&
1027 * If we know that no files have been added in
1028 * this directory (i.e. valid_cached_dir() has
1029 * been executed and set untracked->valid) ..
1031 (!untracked
|| !untracked
->valid
||
1033 * .. and .gitignore does not exist before
1034 * (i.e. null exclude_sha1 and skip_worktree is
1035 * not set). Then we can skip loading .gitignore,
1036 * which would result in ENOENT anyway.
1037 * skip_worktree is taken care in read_directory()
1039 !is_null_sha1(untracked
->exclude_sha1
))) {
1041 * dir->basebuf gets reused by the traversal, but we
1042 * need fname to remain unchanged to ensure the src
1043 * member of each struct exclude correctly
1044 * back-references its source file. Other invocations
1045 * of add_exclude_list provide stable strings, so we
1046 * strbuf_detach() and free() here in the caller.
1048 struct strbuf sb
= STRBUF_INIT
;
1049 strbuf_addbuf(&sb
, &dir
->basebuf
);
1050 strbuf_addstr(&sb
, dir
->exclude_per_dir
);
1051 el
->src
= strbuf_detach(&sb
, NULL
);
1052 add_excludes(el
->src
, el
->src
, stk
->baselen
, el
, 1,
1053 untracked
? &sha1_stat
: NULL
);
1056 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1057 * will first be called in valid_cached_dir() then maybe many
1058 * times more in last_exclude_matching(). When the cache is
1059 * used, last_exclude_matching() will not be called and
1060 * reading .gitignore content will be a waste.
1062 * So when it's called by valid_cached_dir() and we can get
1063 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1064 * modified on work tree), we could delay reading the
1065 * .gitignore content until we absolutely need it in
1066 * last_exclude_matching(). Be careful about ignore rule
1067 * order, though, if you do that.
1070 hashcmp(sha1_stat
.sha1
, untracked
->exclude_sha1
)) {
1071 invalidate_gitignore(dir
->untracked
, untracked
);
1072 hashcpy(untracked
->exclude_sha1
, sha1_stat
.sha1
);
1074 dir
->exclude_stack
= stk
;
1075 current
= stk
->baselen
;
1077 strbuf_setlen(&dir
->basebuf
, baselen
);
1081 * Loads the exclude lists for the directory containing pathname, then
1082 * scans all exclude lists to determine whether pathname is excluded.
1083 * Returns the exclude_list element which matched, or NULL for
1086 struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
1087 const char *pathname
,
1090 int pathlen
= strlen(pathname
);
1091 const char *basename
= strrchr(pathname
, '/');
1092 basename
= (basename
) ? basename
+1 : pathname
;
1094 prep_exclude(dir
, pathname
, basename
-pathname
);
1097 return dir
->exclude
;
1099 return last_exclude_matching_from_lists(dir
, pathname
, pathlen
,
1104 * Loads the exclude lists for the directory containing pathname, then
1105 * scans all exclude lists to determine whether pathname is excluded.
1106 * Returns 1 if true, otherwise 0.
1108 int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
1110 struct exclude
*exclude
=
1111 last_exclude_matching(dir
, pathname
, dtype_p
);
1113 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
1117 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
1119 struct dir_entry
*ent
;
1121 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
1123 memcpy(ent
->name
, pathname
, len
);
1128 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
1130 if (cache_file_exists(pathname
, len
, ignore_case
))
1133 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
1134 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
1137 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
1139 if (!cache_name_is_other(pathname
, len
))
1142 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
1143 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
1147 index_nonexistent
= 0,
1153 * Do not use the alphabetically sorted index to look up
1154 * the directory name; instead, use the case insensitive
1157 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
1159 const struct cache_entry
*ce
= cache_dir_exists(dirname
, len
);
1160 unsigned char endchar
;
1163 return index_nonexistent
;
1164 endchar
= ce
->name
[len
];
1167 * The cache_entry structure returned will contain this dirname
1168 * and possibly additional path components.
1171 return index_directory
;
1174 * If there are no additional path components, then this cache_entry
1175 * represents a submodule. Submodules, despite being directories,
1176 * are stored in the cache without a closing slash.
1178 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
1179 return index_gitdir
;
1181 /* This should never be hit, but it exists just in case. */
1182 return index_nonexistent
;
1186 * The index sorts alphabetically by entry name, which
1187 * means that a gitlink sorts as '\0' at the end, while
1188 * a directory (which is defined not as an entry, but as
1189 * the files it contains) will sort with the '/' at the
1192 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
1197 return directory_exists_in_index_icase(dirname
, len
);
1199 pos
= cache_name_pos(dirname
, len
);
1202 while (pos
< active_nr
) {
1203 const struct cache_entry
*ce
= active_cache
[pos
++];
1204 unsigned char endchar
;
1206 if (strncmp(ce
->name
, dirname
, len
))
1208 endchar
= ce
->name
[len
];
1212 return index_directory
;
1213 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
1214 return index_gitdir
;
1216 return index_nonexistent
;
1220 * When we find a directory when traversing the filesystem, we
1221 * have three distinct cases:
1224 * - see it as a directory
1227 * and which one we choose depends on a combination of existing
1228 * git index contents and the flags passed into the directory
1229 * traversal routine.
1231 * Case 1: If we *already* have entries in the index under that
1232 * directory name, we always recurse into the directory to see
1235 * Case 2: If we *already* have that directory name as a gitlink,
1236 * we always continue to see it as a gitlink, regardless of whether
1237 * there is an actual git directory there or not (it might not
1238 * be checked out as a subproject!)
1240 * Case 3: if we didn't have it in the index previously, we
1241 * have a few sub-cases:
1243 * (a) if "show_other_directories" is true, we show it as
1244 * just a directory, unless "hide_empty_directories" is
1245 * also true, in which case we need to check if it contains any
1246 * untracked and / or ignored files.
1247 * (b) if it looks like a git directory, and we don't have
1248 * 'no_gitlinks' set we treat it as a gitlink, and show it
1250 * (c) otherwise, we recurse into it.
1252 static enum path_treatment
treat_directory(struct dir_struct
*dir
,
1253 struct untracked_cache_dir
*untracked
,
1254 const char *dirname
, int len
, int exclude
,
1255 const struct path_simplify
*simplify
)
1257 /* The "len-1" is to strip the final '/' */
1258 switch (directory_exists_in_index(dirname
, len
-1)) {
1259 case index_directory
:
1260 return path_recurse
;
1265 case index_nonexistent
:
1266 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1268 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
1269 unsigned char sha1
[20];
1270 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
1271 return path_untracked
;
1273 return path_recurse
;
1276 /* This is the "show_other_directories" case */
1278 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1279 return exclude
? path_excluded
: path_untracked
;
1281 untracked
= lookup_untracked(dir
->untracked
, untracked
, dirname
, len
);
1282 return read_directory_recursive(dir
, dirname
, len
,
1283 untracked
, 1, simplify
);
1287 * This is an inexact early pruning of any recursive directory
1288 * reading - if the path cannot possibly be in the pathspec,
1289 * return true, and we'll skip it early.
1291 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1295 const char *match
= simplify
->path
;
1296 int len
= simplify
->len
;
1302 if (!memcmp(path
, match
, len
))
1312 * This function tells us whether an excluded path matches a
1313 * list of "interesting" pathspecs. That is, whether a path matched
1314 * by any of the pathspecs could possibly be ignored by excluding
1315 * the specified path. This can happen if:
1317 * 1. the path is mentioned explicitly in the pathspec
1319 * 2. the path is a directory prefix of some element in the
1322 static int exclude_matches_pathspec(const char *path
, int len
,
1323 const struct path_simplify
*simplify
)
1326 for (; simplify
->path
; simplify
++) {
1327 if (len
== simplify
->len
1328 && !memcmp(path
, simplify
->path
, len
))
1330 if (len
< simplify
->len
1331 && simplify
->path
[len
] == '/'
1332 && !memcmp(path
, simplify
->path
, len
))
1339 static int get_index_dtype(const char *path
, int len
)
1342 const struct cache_entry
*ce
;
1344 ce
= cache_file_exists(path
, len
, 0);
1346 if (!ce_uptodate(ce
))
1348 if (S_ISGITLINK(ce
->ce_mode
))
1351 * Nobody actually cares about the
1352 * difference between DT_LNK and DT_REG
1357 /* Try to look it up as a directory */
1358 pos
= cache_name_pos(path
, len
);
1362 while (pos
< active_nr
) {
1363 ce
= active_cache
[pos
++];
1364 if (strncmp(ce
->name
, path
, len
))
1366 if (ce
->name
[len
] > '/')
1368 if (ce
->name
[len
] < '/')
1370 if (!ce_uptodate(ce
))
1371 break; /* continue? */
1377 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1379 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1382 if (dtype
!= DT_UNKNOWN
)
1384 dtype
= get_index_dtype(path
, len
);
1385 if (dtype
!= DT_UNKNOWN
)
1387 if (lstat(path
, &st
))
1389 if (S_ISREG(st
.st_mode
))
1391 if (S_ISDIR(st
.st_mode
))
1393 if (S_ISLNK(st
.st_mode
))
1398 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1399 struct untracked_cache_dir
*untracked
,
1400 struct strbuf
*path
,
1401 const struct path_simplify
*simplify
,
1402 int dtype
, struct dirent
*de
)
1405 int has_path_in_index
= !!cache_file_exists(path
->buf
, path
->len
, ignore_case
);
1407 if (dtype
== DT_UNKNOWN
)
1408 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1410 /* Always exclude indexed files */
1411 if (dtype
!= DT_DIR
&& has_path_in_index
)
1415 * When we are looking at a directory P in the working tree,
1416 * there are three cases:
1418 * (1) P exists in the index. Everything inside the directory P in
1419 * the working tree needs to go when P is checked out from the
1422 * (2) P does not exist in the index, but there is P/Q in the index.
1423 * We know P will stay a directory when we check out the contents
1424 * of the index, but we do not know yet if there is a directory
1425 * P/Q in the working tree to be killed, so we need to recurse.
1427 * (3) P does not exist in the index, and there is no P/Q in the index
1428 * to require P to be a directory, either. Only in this case, we
1429 * know that everything inside P will not be killed without
1432 if ((dir
->flags
& DIR_COLLECT_KILLED_ONLY
) &&
1433 (dtype
== DT_DIR
) &&
1434 !has_path_in_index
&&
1435 (directory_exists_in_index(path
->buf
, path
->len
) == index_nonexistent
))
1438 exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1441 * Excluded? If we don't explicitly want to show
1442 * ignored files, ignore it
1444 if (exclude
&& !(dir
->flags
& (DIR_SHOW_IGNORED
|DIR_SHOW_IGNORED_TOO
)))
1445 return path_excluded
;
1451 strbuf_addch(path
, '/');
1452 return treat_directory(dir
, untracked
, path
->buf
, path
->len
, exclude
,
1456 return exclude
? path_excluded
: path_untracked
;
1460 static enum path_treatment
treat_path_fast(struct dir_struct
*dir
,
1461 struct untracked_cache_dir
*untracked
,
1462 struct cached_dir
*cdir
,
1463 struct strbuf
*path
,
1465 const struct path_simplify
*simplify
)
1467 strbuf_setlen(path
, baselen
);
1469 strbuf_addstr(path
, cdir
->file
);
1470 return path_untracked
;
1472 strbuf_addstr(path
, cdir
->ucd
->name
);
1473 /* treat_one_path() does this before it calls treat_directory() */
1474 if (path
->buf
[path
->len
- 1] != '/')
1475 strbuf_addch(path
, '/');
1476 if (cdir
->ucd
->check_only
)
1478 * check_only is set as a result of treat_directory() getting
1479 * to its bottom. Verify again the same set of directories
1480 * with check_only set.
1482 return read_directory_recursive(dir
, path
->buf
, path
->len
,
1483 cdir
->ucd
, 1, simplify
);
1485 * We get path_recurse in the first run when
1486 * directory_exists_in_index() returns index_nonexistent. We
1487 * are sure that new changes in the index does not impact the
1488 * outcome. Return now.
1490 return path_recurse
;
1493 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1494 struct untracked_cache_dir
*untracked
,
1495 struct cached_dir
*cdir
,
1496 struct strbuf
*path
,
1498 const struct path_simplify
*simplify
)
1501 struct dirent
*de
= cdir
->de
;
1504 return treat_path_fast(dir
, untracked
, cdir
, path
,
1506 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1508 strbuf_setlen(path
, baselen
);
1509 strbuf_addstr(path
, de
->d_name
);
1510 if (simplify_away(path
->buf
, path
->len
, simplify
))
1514 return treat_one_path(dir
, untracked
, path
, simplify
, dtype
, de
);
1517 static void add_untracked(struct untracked_cache_dir
*dir
, const char *name
)
1521 ALLOC_GROW(dir
->untracked
, dir
->untracked_nr
+ 1,
1522 dir
->untracked_alloc
);
1523 dir
->untracked
[dir
->untracked_nr
++] = xstrdup(name
);
1526 static int valid_cached_dir(struct dir_struct
*dir
,
1527 struct untracked_cache_dir
*untracked
,
1528 struct strbuf
*path
,
1536 if (stat(path
->len
? path
->buf
: ".", &st
)) {
1537 invalidate_directory(dir
->untracked
, untracked
);
1538 memset(&untracked
->stat_data
, 0, sizeof(untracked
->stat_data
));
1541 if (!untracked
->valid
||
1542 match_stat_data_racy(&the_index
, &untracked
->stat_data
, &st
)) {
1543 if (untracked
->valid
)
1544 invalidate_directory(dir
->untracked
, untracked
);
1545 fill_stat_data(&untracked
->stat_data
, &st
);
1549 if (untracked
->check_only
!= !!check_only
) {
1550 invalidate_directory(dir
->untracked
, untracked
);
1555 * prep_exclude will be called eventually on this directory,
1556 * but it's called much later in last_exclude_matching(). We
1557 * need it now to determine the validity of the cache for this
1558 * path. The next calls will be nearly no-op, the way
1559 * prep_exclude() is designed.
1561 if (path
->len
&& path
->buf
[path
->len
- 1] != '/') {
1562 strbuf_addch(path
, '/');
1563 prep_exclude(dir
, path
->buf
, path
->len
);
1564 strbuf_setlen(path
, path
->len
- 1);
1566 prep_exclude(dir
, path
->buf
, path
->len
);
1568 /* hopefully prep_exclude() haven't invalidated this entry... */
1569 return untracked
->valid
;
1572 static int open_cached_dir(struct cached_dir
*cdir
,
1573 struct dir_struct
*dir
,
1574 struct untracked_cache_dir
*untracked
,
1575 struct strbuf
*path
,
1578 memset(cdir
, 0, sizeof(*cdir
));
1579 cdir
->untracked
= untracked
;
1580 if (valid_cached_dir(dir
, untracked
, path
, check_only
))
1582 cdir
->fdir
= opendir(path
->len
? path
->buf
: ".");
1584 dir
->untracked
->dir_opened
++;
1590 static int read_cached_dir(struct cached_dir
*cdir
)
1593 cdir
->de
= readdir(cdir
->fdir
);
1598 while (cdir
->nr_dirs
< cdir
->untracked
->dirs_nr
) {
1599 struct untracked_cache_dir
*d
= cdir
->untracked
->dirs
[cdir
->nr_dirs
];
1609 if (cdir
->nr_files
< cdir
->untracked
->untracked_nr
) {
1610 struct untracked_cache_dir
*d
= cdir
->untracked
;
1611 cdir
->file
= d
->untracked
[cdir
->nr_files
++];
1617 static void close_cached_dir(struct cached_dir
*cdir
)
1620 closedir(cdir
->fdir
);
1622 * We have gone through this directory and found no untracked
1623 * entries. Mark it valid.
1625 if (cdir
->untracked
) {
1626 cdir
->untracked
->valid
= 1;
1627 cdir
->untracked
->recurse
= 1;
1632 * Read a directory tree. We currently ignore anything but
1633 * directories, regular files and symlinks. That's because git
1634 * doesn't handle them at all yet. Maybe that will change some
1637 * Also, we ignore the name ".git" (even if it is not a directory).
1638 * That likely will not change.
1640 * Returns the most significant path_treatment value encountered in the scan.
1642 static enum path_treatment
read_directory_recursive(struct dir_struct
*dir
,
1643 const char *base
, int baselen
,
1644 struct untracked_cache_dir
*untracked
, int check_only
,
1645 const struct path_simplify
*simplify
)
1647 struct cached_dir cdir
;
1648 enum path_treatment state
, subdir_state
, dir_state
= path_none
;
1649 struct strbuf path
= STRBUF_INIT
;
1651 strbuf_add(&path
, base
, baselen
);
1653 if (open_cached_dir(&cdir
, dir
, untracked
, &path
, check_only
))
1657 untracked
->check_only
= !!check_only
;
1659 while (!read_cached_dir(&cdir
)) {
1660 /* check how the file or directory should be treated */
1661 state
= treat_path(dir
, untracked
, &cdir
, &path
, baselen
, simplify
);
1663 if (state
> dir_state
)
1666 /* recurse into subdir if instructed by treat_path */
1667 if (state
== path_recurse
) {
1668 struct untracked_cache_dir
*ud
;
1669 ud
= lookup_untracked(dir
->untracked
, untracked
,
1671 path
.len
- baselen
);
1673 read_directory_recursive(dir
, path
.buf
, path
.len
,
1674 ud
, check_only
, simplify
);
1675 if (subdir_state
> dir_state
)
1676 dir_state
= subdir_state
;
1680 /* abort early if maximum state has been reached */
1681 if (dir_state
== path_untracked
) {
1683 add_untracked(untracked
, path
.buf
+ baselen
);
1686 /* skip the dir_add_* part */
1690 /* add the path to the appropriate result list */
1693 if (dir
->flags
& DIR_SHOW_IGNORED
)
1694 dir_add_name(dir
, path
.buf
, path
.len
);
1695 else if ((dir
->flags
& DIR_SHOW_IGNORED_TOO
) ||
1696 ((dir
->flags
& DIR_COLLECT_IGNORED
) &&
1697 exclude_matches_pathspec(path
.buf
, path
.len
,
1699 dir_add_ignored(dir
, path
.buf
, path
.len
);
1702 case path_untracked
:
1703 if (dir
->flags
& DIR_SHOW_IGNORED
)
1705 dir_add_name(dir
, path
.buf
, path
.len
);
1707 add_untracked(untracked
, path
.buf
+ baselen
);
1714 close_cached_dir(&cdir
);
1716 strbuf_release(&path
);
1721 static int cmp_name(const void *p1
, const void *p2
)
1723 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1724 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1726 return name_compare(e1
->name
, e1
->len
, e2
->name
, e2
->len
);
1729 static struct path_simplify
*create_simplify(const char **pathspec
)
1732 struct path_simplify
*simplify
= NULL
;
1737 for (nr
= 0 ; ; nr
++) {
1739 ALLOC_GROW(simplify
, nr
+ 1, alloc
);
1740 match
= *pathspec
++;
1743 simplify
[nr
].path
= match
;
1744 simplify
[nr
].len
= simple_length(match
);
1746 simplify
[nr
].path
= NULL
;
1747 simplify
[nr
].len
= 0;
1751 static void free_simplify(struct path_simplify
*simplify
)
1756 static int treat_leading_path(struct dir_struct
*dir
,
1757 const char *path
, int len
,
1758 const struct path_simplify
*simplify
)
1760 struct strbuf sb
= STRBUF_INIT
;
1761 int baselen
, rc
= 0;
1763 int old_flags
= dir
->flags
;
1765 while (len
&& path
[len
- 1] == '/')
1770 dir
->flags
&= ~DIR_SHOW_OTHER_DIRECTORIES
;
1772 cp
= path
+ baselen
+ !!baselen
;
1773 cp
= memchr(cp
, '/', path
+ len
- cp
);
1777 baselen
= cp
- path
;
1778 strbuf_setlen(&sb
, 0);
1779 strbuf_add(&sb
, path
, baselen
);
1780 if (!is_directory(sb
.buf
))
1782 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1784 if (treat_one_path(dir
, NULL
, &sb
, simplify
,
1785 DT_DIR
, NULL
) == path_none
)
1786 break; /* do not recurse into it */
1787 if (len
<= baselen
) {
1789 break; /* finished checking */
1792 strbuf_release(&sb
);
1793 dir
->flags
= old_flags
;
1797 static const char *get_ident_string(void)
1799 static struct strbuf sb
= STRBUF_INIT
;
1805 die_errno(_("failed to get kernel name and information"));
1806 strbuf_addf(&sb
, "Location %s, system %s %s %s", get_git_work_tree(),
1807 uts
.sysname
, uts
.release
, uts
.version
);
1811 static int ident_in_untracked(const struct untracked_cache
*uc
)
1813 const char *end
= uc
->ident
.buf
+ uc
->ident
.len
;
1814 const char *p
= uc
->ident
.buf
;
1816 for (p
= uc
->ident
.buf
; p
< end
; p
+= strlen(p
) + 1)
1817 if (!strcmp(p
, get_ident_string()))
1822 void add_untracked_ident(struct untracked_cache
*uc
)
1824 if (ident_in_untracked(uc
))
1826 strbuf_addstr(&uc
->ident
, get_ident_string());
1827 /* this strbuf contains a list of strings, save NUL too */
1828 strbuf_addch(&uc
->ident
, 0);
1831 static struct untracked_cache_dir
*validate_untracked_cache(struct dir_struct
*dir
,
1833 const struct pathspec
*pathspec
)
1835 struct untracked_cache_dir
*root
;
1838 if (!dir
->untracked
|| getenv("GIT_DISABLE_UNTRACKED_CACHE"))
1842 * We only support $GIT_DIR/info/exclude and core.excludesfile
1843 * as the global ignore rule files. Any other additions
1844 * (e.g. from command line) invalidate the cache. This
1845 * condition also catches running setup_standard_excludes()
1846 * before setting dir->untracked!
1848 if (dir
->unmanaged_exclude_files
)
1852 * Optimize for the main use case only: whole-tree git
1853 * status. More work involved in treat_leading_path() if we
1854 * use cache on just a subset of the worktree. pathspec
1855 * support could make the matter even worse.
1857 if (base_len
|| (pathspec
&& pathspec
->nr
))
1860 /* Different set of flags may produce different results */
1861 if (dir
->flags
!= dir
->untracked
->dir_flags
||
1863 * See treat_directory(), case index_nonexistent. Without
1864 * this flag, we may need to also cache .git file content
1865 * for the resolve_gitlink_ref() call, which we don't.
1867 !(dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
) ||
1868 /* We don't support collecting ignore files */
1869 (dir
->flags
& (DIR_SHOW_IGNORED
| DIR_SHOW_IGNORED_TOO
|
1870 DIR_COLLECT_IGNORED
)))
1874 * If we use .gitignore in the cache and now you change it to
1875 * .gitexclude, everything will go wrong.
1877 if (dir
->exclude_per_dir
!= dir
->untracked
->exclude_per_dir
&&
1878 strcmp(dir
->exclude_per_dir
, dir
->untracked
->exclude_per_dir
))
1882 * EXC_CMDL is not considered in the cache. If people set it,
1885 if (dir
->exclude_list_group
[EXC_CMDL
].nr
)
1889 * An optimization in prep_exclude() does not play well with
1890 * CE_SKIP_WORKTREE. It's a rare case anyway, if a single
1891 * entry has that bit set, disable the whole untracked cache.
1893 for (i
= 0; i
< active_nr
; i
++)
1894 if (ce_skip_worktree(active_cache
[i
]))
1897 if (!ident_in_untracked(dir
->untracked
)) {
1898 warning(_("Untracked cache is disabled on this system."));
1902 if (!dir
->untracked
->root
) {
1903 const int len
= sizeof(*dir
->untracked
->root
);
1904 dir
->untracked
->root
= xmalloc(len
);
1905 memset(dir
->untracked
->root
, 0, len
);
1908 /* Validate $GIT_DIR/info/exclude and core.excludesfile */
1909 root
= dir
->untracked
->root
;
1910 if (hashcmp(dir
->ss_info_exclude
.sha1
,
1911 dir
->untracked
->ss_info_exclude
.sha1
)) {
1912 invalidate_gitignore(dir
->untracked
, root
);
1913 dir
->untracked
->ss_info_exclude
= dir
->ss_info_exclude
;
1915 if (hashcmp(dir
->ss_excludes_file
.sha1
,
1916 dir
->untracked
->ss_excludes_file
.sha1
)) {
1917 invalidate_gitignore(dir
->untracked
, root
);
1918 dir
->untracked
->ss_excludes_file
= dir
->ss_excludes_file
;
1921 /* Make sure this directory is not dropped out at saving phase */
1926 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const struct pathspec
*pathspec
)
1928 struct path_simplify
*simplify
;
1929 struct untracked_cache_dir
*untracked
;
1932 * Check out create_simplify()
1935 GUARD_PATHSPEC(pathspec
,
1943 if (has_symlink_leading_path(path
, len
))
1947 * exclude patterns are treated like positive ones in
1948 * create_simplify. Usually exclude patterns should be a
1949 * subset of positive ones, which has no impacts on
1950 * create_simplify().
1952 simplify
= create_simplify(pathspec
? pathspec
->_raw
: NULL
);
1953 untracked
= validate_untracked_cache(dir
, len
, pathspec
);
1956 * make sure untracked cache code path is disabled,
1957 * e.g. prep_exclude()
1959 dir
->untracked
= NULL
;
1960 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1961 read_directory_recursive(dir
, path
, len
, untracked
, 0, simplify
);
1962 free_simplify(simplify
);
1963 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1964 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1965 if (dir
->untracked
) {
1966 static struct trace_key trace_untracked_stats
= TRACE_KEY_INIT(UNTRACKED_STATS
);
1967 trace_printf_key(&trace_untracked_stats
,
1968 "node creation: %u\n"
1969 "gitignore invalidation: %u\n"
1970 "directory invalidation: %u\n"
1972 dir
->untracked
->dir_created
,
1973 dir
->untracked
->gitignore_invalidated
,
1974 dir
->untracked
->dir_invalidated
,
1975 dir
->untracked
->dir_opened
);
1976 if (dir
->untracked
== the_index
.untracked
&&
1977 (dir
->untracked
->dir_opened
||
1978 dir
->untracked
->gitignore_invalidated
||
1979 dir
->untracked
->dir_invalidated
))
1980 the_index
.cache_changed
|= UNTRACKED_CHANGED
;
1981 if (dir
->untracked
!= the_index
.untracked
) {
1982 free(dir
->untracked
);
1983 dir
->untracked
= NULL
;
1989 int file_exists(const char *f
)
1992 return lstat(f
, &sb
) == 0;
1996 * Given two normalized paths (a trailing slash is ok), if subdir is
1997 * outside dir, return -1. Otherwise return the offset in subdir that
1998 * can be used as relative path to dir.
2000 int dir_inside_of(const char *subdir
, const char *dir
)
2004 assert(dir
&& subdir
&& *dir
&& *subdir
);
2006 while (*dir
&& *subdir
&& *dir
== *subdir
) {
2012 /* hel[p]/me vs hel[l]/yeah */
2013 if (*dir
&& *subdir
)
2017 return !*dir
? offset
: -1; /* same dir */
2019 /* foo/[b]ar vs foo/[] */
2020 if (is_dir_sep(dir
[-1]))
2021 return is_dir_sep(subdir
[-1]) ? offset
: -1;
2023 /* foo[/]bar vs foo[] */
2024 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
2027 int is_inside_dir(const char *dir
)
2036 rc
= (dir_inside_of(cwd
, dir
) >= 0);
2041 int is_empty_dir(const char *path
)
2043 DIR *dir
= opendir(path
);
2050 while ((e
= readdir(dir
)) != NULL
)
2051 if (!is_dot_or_dotdot(e
->d_name
)) {
2060 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
2064 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
2065 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
2066 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
2067 unsigned char submodule_head
[20];
2069 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
2070 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
2071 /* Do not descend and nuke a nested git work tree. */
2077 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
2078 dir
= opendir(path
->buf
);
2080 if (errno
== ENOENT
)
2081 return keep_toplevel
? -1 : 0;
2082 else if (errno
== EACCES
&& !keep_toplevel
)
2084 * An empty dir could be removable even if it
2087 return rmdir(path
->buf
);
2091 if (path
->buf
[original_len
- 1] != '/')
2092 strbuf_addch(path
, '/');
2095 while ((e
= readdir(dir
)) != NULL
) {
2097 if (is_dot_or_dotdot(e
->d_name
))
2100 strbuf_setlen(path
, len
);
2101 strbuf_addstr(path
, e
->d_name
);
2102 if (lstat(path
->buf
, &st
)) {
2103 if (errno
== ENOENT
)
2105 * file disappeared, which is what we
2110 } else if (S_ISDIR(st
.st_mode
)) {
2111 if (!remove_dir_recurse(path
, flag
, &kept_down
))
2112 continue; /* happy */
2113 } else if (!only_empty
&&
2114 (!unlink(path
->buf
) || errno
== ENOENT
)) {
2115 continue; /* happy, too */
2118 /* path too long, stat fails, or non-directory still exists */
2124 strbuf_setlen(path
, original_len
);
2125 if (!ret
&& !keep_toplevel
&& !kept_down
)
2126 ret
= (!rmdir(path
->buf
) || errno
== ENOENT
) ? 0 : -1;
2129 * report the uplevel that it is not an error that we
2130 * did not rmdir() our directory.
2136 int remove_dir_recursively(struct strbuf
*path
, int flag
)
2138 return remove_dir_recurse(path
, flag
, NULL
);
2141 void setup_standard_excludes(struct dir_struct
*dir
)
2146 dir
->exclude_per_dir
= ".gitignore";
2147 path
= git_path("info/exclude");
2148 if (!excludes_file
) {
2149 home_config_paths(NULL
, &xdg_path
, "ignore");
2150 excludes_file
= xdg_path
;
2152 if (!access_or_warn(path
, R_OK
, 0))
2153 add_excludes_from_file_1(dir
, path
,
2154 dir
->untracked
? &dir
->ss_info_exclude
: NULL
);
2155 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
, 0))
2156 add_excludes_from_file_1(dir
, excludes_file
,
2157 dir
->untracked
? &dir
->ss_excludes_file
: NULL
);
2160 int remove_path(const char *name
)
2164 if (unlink(name
) && errno
!= ENOENT
&& errno
!= ENOTDIR
)
2167 slash
= strrchr(name
, '/');
2169 char *dirs
= xstrdup(name
);
2170 slash
= dirs
+ (slash
- name
);
2173 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
2180 * Frees memory within dir which was allocated for exclude lists and
2181 * the exclude_stack. Does not free dir itself.
2183 void clear_directory(struct dir_struct
*dir
)
2186 struct exclude_list_group
*group
;
2187 struct exclude_list
*el
;
2188 struct exclude_stack
*stk
;
2190 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
2191 group
= &dir
->exclude_list_group
[i
];
2192 for (j
= 0; j
< group
->nr
; j
++) {
2195 free((char *)el
->src
);
2196 clear_exclude_list(el
);
2201 stk
= dir
->exclude_stack
;
2203 struct exclude_stack
*prev
= stk
->prev
;
2207 strbuf_release(&dir
->basebuf
);
2210 struct ondisk_untracked_cache
{
2211 struct stat_data info_exclude_stat
;
2212 struct stat_data excludes_file_stat
;
2214 unsigned char info_exclude_sha1
[20];
2215 unsigned char excludes_file_sha1
[20];
2216 char exclude_per_dir
[FLEX_ARRAY
];
2219 #define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)
2222 int index
; /* number of written untracked_cache_dir */
2223 struct ewah_bitmap
*check_only
; /* from untracked_cache_dir */
2224 struct ewah_bitmap
*valid
; /* from untracked_cache_dir */
2225 struct ewah_bitmap
*sha1_valid
; /* set if exclude_sha1 is not null */
2227 struct strbuf sb_stat
;
2228 struct strbuf sb_sha1
;
2231 static void stat_data_to_disk(struct stat_data
*to
, const struct stat_data
*from
)
2233 to
->sd_ctime
.sec
= htonl(from
->sd_ctime
.sec
);
2234 to
->sd_ctime
.nsec
= htonl(from
->sd_ctime
.nsec
);
2235 to
->sd_mtime
.sec
= htonl(from
->sd_mtime
.sec
);
2236 to
->sd_mtime
.nsec
= htonl(from
->sd_mtime
.nsec
);
2237 to
->sd_dev
= htonl(from
->sd_dev
);
2238 to
->sd_ino
= htonl(from
->sd_ino
);
2239 to
->sd_uid
= htonl(from
->sd_uid
);
2240 to
->sd_gid
= htonl(from
->sd_gid
);
2241 to
->sd_size
= htonl(from
->sd_size
);
2244 static void write_one_dir(struct untracked_cache_dir
*untracked
,
2245 struct write_data
*wd
)
2247 struct stat_data stat_data
;
2248 struct strbuf
*out
= &wd
->out
;
2249 unsigned char intbuf
[16];
2250 unsigned int intlen
, value
;
2251 int i
= wd
->index
++;
2254 * untracked_nr should be reset whenever valid is clear, but
2257 if (!untracked
->valid
) {
2258 untracked
->untracked_nr
= 0;
2259 untracked
->check_only
= 0;
2262 if (untracked
->check_only
)
2263 ewah_set(wd
->check_only
, i
);
2264 if (untracked
->valid
) {
2265 ewah_set(wd
->valid
, i
);
2266 stat_data_to_disk(&stat_data
, &untracked
->stat_data
);
2267 strbuf_add(&wd
->sb_stat
, &stat_data
, sizeof(stat_data
));
2269 if (!is_null_sha1(untracked
->exclude_sha1
)) {
2270 ewah_set(wd
->sha1_valid
, i
);
2271 strbuf_add(&wd
->sb_sha1
, untracked
->exclude_sha1
, 20);
2274 intlen
= encode_varint(untracked
->untracked_nr
, intbuf
);
2275 strbuf_add(out
, intbuf
, intlen
);
2277 /* skip non-recurse directories */
2278 for (i
= 0, value
= 0; i
< untracked
->dirs_nr
; i
++)
2279 if (untracked
->dirs
[i
]->recurse
)
2281 intlen
= encode_varint(value
, intbuf
);
2282 strbuf_add(out
, intbuf
, intlen
);
2284 strbuf_add(out
, untracked
->name
, strlen(untracked
->name
) + 1);
2286 for (i
= 0; i
< untracked
->untracked_nr
; i
++)
2287 strbuf_add(out
, untracked
->untracked
[i
],
2288 strlen(untracked
->untracked
[i
]) + 1);
2290 for (i
= 0; i
< untracked
->dirs_nr
; i
++)
2291 if (untracked
->dirs
[i
]->recurse
)
2292 write_one_dir(untracked
->dirs
[i
], wd
);
2295 void write_untracked_extension(struct strbuf
*out
, struct untracked_cache
*untracked
)
2297 struct ondisk_untracked_cache
*ouc
;
2298 struct write_data wd
;
2299 unsigned char varbuf
[16];
2300 int len
= 0, varint_len
;
2301 if (untracked
->exclude_per_dir
)
2302 len
= strlen(untracked
->exclude_per_dir
);
2303 ouc
= xmalloc(sizeof(*ouc
) + len
+ 1);
2304 stat_data_to_disk(&ouc
->info_exclude_stat
, &untracked
->ss_info_exclude
.stat
);
2305 stat_data_to_disk(&ouc
->excludes_file_stat
, &untracked
->ss_excludes_file
.stat
);
2306 hashcpy(ouc
->info_exclude_sha1
, untracked
->ss_info_exclude
.sha1
);
2307 hashcpy(ouc
->excludes_file_sha1
, untracked
->ss_excludes_file
.sha1
);
2308 ouc
->dir_flags
= htonl(untracked
->dir_flags
);
2309 memcpy(ouc
->exclude_per_dir
, untracked
->exclude_per_dir
, len
+ 1);
2311 varint_len
= encode_varint(untracked
->ident
.len
, varbuf
);
2312 strbuf_add(out
, varbuf
, varint_len
);
2313 strbuf_add(out
, untracked
->ident
.buf
, untracked
->ident
.len
);
2315 strbuf_add(out
, ouc
, ouc_size(len
));
2319 if (!untracked
->root
) {
2320 varint_len
= encode_varint(0, varbuf
);
2321 strbuf_add(out
, varbuf
, varint_len
);
2326 wd
.check_only
= ewah_new();
2327 wd
.valid
= ewah_new();
2328 wd
.sha1_valid
= ewah_new();
2329 strbuf_init(&wd
.out
, 1024);
2330 strbuf_init(&wd
.sb_stat
, 1024);
2331 strbuf_init(&wd
.sb_sha1
, 1024);
2332 write_one_dir(untracked
->root
, &wd
);
2334 varint_len
= encode_varint(wd
.index
, varbuf
);
2335 strbuf_add(out
, varbuf
, varint_len
);
2336 strbuf_addbuf(out
, &wd
.out
);
2337 ewah_serialize_strbuf(wd
.valid
, out
);
2338 ewah_serialize_strbuf(wd
.check_only
, out
);
2339 ewah_serialize_strbuf(wd
.sha1_valid
, out
);
2340 strbuf_addbuf(out
, &wd
.sb_stat
);
2341 strbuf_addbuf(out
, &wd
.sb_sha1
);
2342 strbuf_addch(out
, '\0'); /* safe guard for string lists */
2344 ewah_free(wd
.valid
);
2345 ewah_free(wd
.check_only
);
2346 ewah_free(wd
.sha1_valid
);
2347 strbuf_release(&wd
.out
);
2348 strbuf_release(&wd
.sb_stat
);
2349 strbuf_release(&wd
.sb_sha1
);
2352 static void free_untracked(struct untracked_cache_dir
*ucd
)
2357 for (i
= 0; i
< ucd
->dirs_nr
; i
++)
2358 free_untracked(ucd
->dirs
[i
]);
2359 for (i
= 0; i
< ucd
->untracked_nr
; i
++)
2360 free(ucd
->untracked
[i
]);
2361 free(ucd
->untracked
);
2366 void free_untracked_cache(struct untracked_cache
*uc
)
2369 free_untracked(uc
->root
);
2375 struct untracked_cache_dir
**ucd
;
2376 struct ewah_bitmap
*check_only
;
2377 struct ewah_bitmap
*valid
;
2378 struct ewah_bitmap
*sha1_valid
;
2379 const unsigned char *data
;
2380 const unsigned char *end
;
2383 static void stat_data_from_disk(struct stat_data
*to
, const struct stat_data
*from
)
2385 to
->sd_ctime
.sec
= get_be32(&from
->sd_ctime
.sec
);
2386 to
->sd_ctime
.nsec
= get_be32(&from
->sd_ctime
.nsec
);
2387 to
->sd_mtime
.sec
= get_be32(&from
->sd_mtime
.sec
);
2388 to
->sd_mtime
.nsec
= get_be32(&from
->sd_mtime
.nsec
);
2389 to
->sd_dev
= get_be32(&from
->sd_dev
);
2390 to
->sd_ino
= get_be32(&from
->sd_ino
);
2391 to
->sd_uid
= get_be32(&from
->sd_uid
);
2392 to
->sd_gid
= get_be32(&from
->sd_gid
);
2393 to
->sd_size
= get_be32(&from
->sd_size
);
2396 static int read_one_dir(struct untracked_cache_dir
**untracked_
,
2397 struct read_data
*rd
)
2399 struct untracked_cache_dir ud
, *untracked
;
2400 const unsigned char *next
, *data
= rd
->data
, *end
= rd
->end
;
2404 memset(&ud
, 0, sizeof(ud
));
2407 value
= decode_varint(&next
);
2411 ud
.untracked_alloc
= value
;
2412 ud
.untracked_nr
= value
;
2413 if (ud
.untracked_nr
)
2414 ud
.untracked
= xmalloc(sizeof(*ud
.untracked
) * ud
.untracked_nr
);
2418 ud
.dirs_alloc
= ud
.dirs_nr
= decode_varint(&next
);
2421 ud
.dirs
= xmalloc(sizeof(*ud
.dirs
) * ud
.dirs_nr
);
2424 len
= strlen((const char *)data
);
2425 next
= data
+ len
+ 1;
2428 *untracked_
= untracked
= xmalloc(sizeof(*untracked
) + len
);
2429 memcpy(untracked
, &ud
, sizeof(ud
));
2430 memcpy(untracked
->name
, data
, len
+ 1);
2433 for (i
= 0; i
< untracked
->untracked_nr
; i
++) {
2434 len
= strlen((const char *)data
);
2435 next
= data
+ len
+ 1;
2438 untracked
->untracked
[i
] = xstrdup((const char*)data
);
2442 rd
->ucd
[rd
->index
++] = untracked
;
2445 for (i
= 0; i
< untracked
->dirs_nr
; i
++) {
2446 len
= read_one_dir(untracked
->dirs
+ i
, rd
);
2453 static void set_check_only(size_t pos
, void *cb
)
2455 struct read_data
*rd
= cb
;
2456 struct untracked_cache_dir
*ud
= rd
->ucd
[pos
];
2460 static void read_stat(size_t pos
, void *cb
)
2462 struct read_data
*rd
= cb
;
2463 struct untracked_cache_dir
*ud
= rd
->ucd
[pos
];
2464 if (rd
->data
+ sizeof(struct stat_data
) > rd
->end
) {
2465 rd
->data
= rd
->end
+ 1;
2468 stat_data_from_disk(&ud
->stat_data
, (struct stat_data
*)rd
->data
);
2469 rd
->data
+= sizeof(struct stat_data
);
2473 static void read_sha1(size_t pos
, void *cb
)
2475 struct read_data
*rd
= cb
;
2476 struct untracked_cache_dir
*ud
= rd
->ucd
[pos
];
2477 if (rd
->data
+ 20 > rd
->end
) {
2478 rd
->data
= rd
->end
+ 1;
2481 hashcpy(ud
->exclude_sha1
, rd
->data
);
2485 static void load_sha1_stat(struct sha1_stat
*sha1_stat
,
2486 const struct stat_data
*stat
,
2487 const unsigned char *sha1
)
2489 stat_data_from_disk(&sha1_stat
->stat
, stat
);
2490 hashcpy(sha1_stat
->sha1
, sha1
);
2491 sha1_stat
->valid
= 1;
2494 struct untracked_cache
*read_untracked_extension(const void *data
, unsigned long sz
)
2496 const struct ondisk_untracked_cache
*ouc
;
2497 struct untracked_cache
*uc
;
2498 struct read_data rd
;
2499 const unsigned char *next
= data
, *end
= (const unsigned char *)data
+ sz
;
2503 if (sz
<= 1 || end
[-1] != '\0')
2507 ident_len
= decode_varint(&next
);
2508 if (next
+ ident_len
> end
)
2510 ident
= (const char *)next
;
2513 ouc
= (const struct ondisk_untracked_cache
*)next
;
2514 if (next
+ ouc_size(0) > end
)
2517 uc
= xcalloc(1, sizeof(*uc
));
2518 strbuf_init(&uc
->ident
, ident_len
);
2519 strbuf_add(&uc
->ident
, ident
, ident_len
);
2520 load_sha1_stat(&uc
->ss_info_exclude
, &ouc
->info_exclude_stat
,
2521 ouc
->info_exclude_sha1
);
2522 load_sha1_stat(&uc
->ss_excludes_file
, &ouc
->excludes_file_stat
,
2523 ouc
->excludes_file_sha1
);
2524 uc
->dir_flags
= get_be32(&ouc
->dir_flags
);
2525 uc
->exclude_per_dir
= xstrdup(ouc
->exclude_per_dir
);
2526 /* NUL after exclude_per_dir is covered by sizeof(*ouc) */
2527 next
+= ouc_size(strlen(ouc
->exclude_per_dir
));
2531 len
= decode_varint(&next
);
2532 if (next
> end
|| len
== 0)
2535 rd
.valid
= ewah_new();
2536 rd
.check_only
= ewah_new();
2537 rd
.sha1_valid
= ewah_new();
2541 rd
.ucd
= xmalloc(sizeof(*rd
.ucd
) * len
);
2543 if (read_one_dir(&uc
->root
, &rd
) || rd
.index
!= len
)
2547 len
= ewah_read_mmap(rd
.valid
, next
, end
- next
);
2552 len
= ewah_read_mmap(rd
.check_only
, next
, end
- next
);
2557 len
= ewah_read_mmap(rd
.sha1_valid
, next
, end
- next
);
2561 ewah_each_bit(rd
.check_only
, set_check_only
, &rd
);
2562 rd
.data
= next
+ len
;
2563 ewah_each_bit(rd
.valid
, read_stat
, &rd
);
2564 ewah_each_bit(rd
.sha1_valid
, read_sha1
, &rd
);
2569 ewah_free(rd
.valid
);
2570 ewah_free(rd
.check_only
);
2571 ewah_free(rd
.sha1_valid
);
2574 free_untracked_cache(uc
);
2580 void untracked_cache_invalidate_path(struct index_state
*istate
,
2584 struct untracked_cache_dir
*d
;
2585 if (!istate
->untracked
|| !istate
->untracked
->root
)
2587 sep
= strrchr(path
, '/');
2589 d
= lookup_untracked(istate
->untracked
,
2590 istate
->untracked
->root
,
2593 d
= istate
->untracked
->root
;
2594 istate
->untracked
->dir_invalidated
++;
2596 d
->untracked_nr
= 0;
2599 void untracked_cache_remove_from_index(struct index_state
*istate
,
2602 untracked_cache_invalidate_path(istate
, path
);
2605 void untracked_cache_add_to_index(struct index_state
*istate
,
2608 untracked_cache_invalidate_path(istate
, path
);