mirror of https://github.com/git/git.git
Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
https://git-scm.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4007 lines
107 KiB
4007 lines
107 KiB
/* |
|
* This handles recursive filename detection with exclude |
|
* files, index knowledge etc.. |
|
* |
|
* Copyright (C) Linus Torvalds, 2005-2006 |
|
* Junio Hamano, 2005-2006 |
|
*/ |
|
#include "cache.h" |
|
#include "config.h" |
|
#include "dir.h" |
|
#include "object-store.h" |
|
#include "attr.h" |
|
#include "refs.h" |
|
#include "wildmatch.h" |
|
#include "pathspec.h" |
|
#include "utf8.h" |
|
#include "varint.h" |
|
#include "ewah/ewok.h" |
|
#include "fsmonitor.h" |
|
#include "submodule-config.h" |
|
|
|
/* |
|
* Tells read_directory_recursive how a file or directory should be treated. |
|
* Values are ordered by significance, e.g. if a directory contains both |
|
* excluded and untracked files, it is listed as untracked because |
|
* path_untracked > path_excluded. |
|
*/ |
|
enum path_treatment { |
|
path_none = 0, |
|
path_recurse, |
|
path_excluded, |
|
path_untracked |
|
}; |
|
|
|
/* |
|
* Support data structure for our opendir/readdir/closedir wrappers |
|
*/ |
|
struct cached_dir { |
|
DIR *fdir; |
|
struct untracked_cache_dir *untracked; |
|
int nr_files; |
|
int nr_dirs; |
|
|
|
const char *d_name; |
|
int d_type; |
|
const char *file; |
|
struct untracked_cache_dir *ucd; |
|
}; |
|
|
|
static enum path_treatment read_directory_recursive(struct dir_struct *dir, |
|
struct index_state *istate, const char *path, int len, |
|
struct untracked_cache_dir *untracked, |
|
int check_only, int stop_at_first_file, const struct pathspec *pathspec); |
|
static int resolve_dtype(int dtype, struct index_state *istate, |
|
const char *path, int len); |
|
struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp) |
|
{ |
|
struct dirent *e; |
|
|
|
while ((e = readdir(dirp)) != NULL) { |
|
if (!is_dot_or_dotdot(e->d_name)) |
|
break; |
|
} |
|
return e; |
|
} |
|
|
|
int count_slashes(const char *s) |
|
{ |
|
int cnt = 0; |
|
while (*s) |
|
if (*s++ == '/') |
|
cnt++; |
|
return cnt; |
|
} |
|
|
|
int fspathcmp(const char *a, const char *b) |
|
{ |
|
return ignore_case ? strcasecmp(a, b) : strcmp(a, b); |
|
} |
|
|
|
int fspatheq(const char *a, const char *b) |
|
{ |
|
return !fspathcmp(a, b); |
|
} |
|
|
|
int fspathncmp(const char *a, const char *b, size_t count) |
|
{ |
|
return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); |
|
} |
|
|
|
unsigned int fspathhash(const char *str) |
|
{ |
|
return ignore_case ? strihash(str) : strhash(str); |
|
} |
|
|
|
int git_fnmatch(const struct pathspec_item *item, |
|
const char *pattern, const char *string, |
|
int prefix) |
|
{ |
|
if (prefix > 0) { |
|
if (ps_strncmp(item, pattern, string, prefix)) |
|
return WM_NOMATCH; |
|
pattern += prefix; |
|
string += prefix; |
|
} |
|
if (item->flags & PATHSPEC_ONESTAR) { |
|
int pattern_len = strlen(++pattern); |
|
int string_len = strlen(string); |
|
return string_len < pattern_len || |
|
ps_strcmp(item, pattern, |
|
string + string_len - pattern_len); |
|
} |
|
if (item->magic & PATHSPEC_GLOB) |
|
return wildmatch(pattern, string, |
|
WM_PATHNAME | |
|
(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0)); |
|
else |
|
/* wildmatch has not learned no FNM_PATHNAME mode yet */ |
|
return wildmatch(pattern, string, |
|
item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0); |
|
} |
|
|
|
static int fnmatch_icase_mem(const char *pattern, int patternlen, |
|
const char *string, int stringlen, |
|
int flags) |
|
{ |
|
int match_status; |
|
struct strbuf pat_buf = STRBUF_INIT; |
|
struct strbuf str_buf = STRBUF_INIT; |
|
const char *use_pat = pattern; |
|
const char *use_str = string; |
|
|
|
if (pattern[patternlen]) { |
|
strbuf_add(&pat_buf, pattern, patternlen); |
|
use_pat = pat_buf.buf; |
|
} |
|
if (string[stringlen]) { |
|
strbuf_add(&str_buf, string, stringlen); |
|
use_str = str_buf.buf; |
|
} |
|
|
|
if (ignore_case) |
|
flags |= WM_CASEFOLD; |
|
match_status = wildmatch(use_pat, use_str, flags); |
|
|
|
strbuf_release(&pat_buf); |
|
strbuf_release(&str_buf); |
|
|
|
return match_status; |
|
} |
|
|
|
static size_t common_prefix_len(const struct pathspec *pathspec) |
|
{ |
|
int n; |
|
size_t max = 0; |
|
|
|
/* |
|
* ":(icase)path" is treated as a pathspec full of |
|
* wildcard. In other words, only prefix is considered common |
|
* prefix. If the pathspec is abc/foo abc/bar, running in |
|
* subdir xyz, the common prefix is still xyz, not xyz/abc as |
|
* in non-:(icase). |
|
*/ |
|
GUARD_PATHSPEC(pathspec, |
|
PATHSPEC_FROMTOP | |
|
PATHSPEC_MAXDEPTH | |
|
PATHSPEC_LITERAL | |
|
PATHSPEC_GLOB | |
|
PATHSPEC_ICASE | |
|
PATHSPEC_EXCLUDE | |
|
PATHSPEC_ATTR); |
|
|
|
for (n = 0; n < pathspec->nr; n++) { |
|
size_t i = 0, len = 0, item_len; |
|
if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) |
|
continue; |
|
if (pathspec->items[n].magic & PATHSPEC_ICASE) |
|
item_len = pathspec->items[n].prefix; |
|
else |
|
item_len = pathspec->items[n].nowildcard_len; |
|
while (i < item_len && (n == 0 || i < max)) { |
|
char c = pathspec->items[n].match[i]; |
|
if (c != pathspec->items[0].match[i]) |
|
break; |
|
if (c == '/') |
|
len = i + 1; |
|
i++; |
|
} |
|
if (n == 0 || len < max) { |
|
max = len; |
|
if (!max) |
|
break; |
|
} |
|
} |
|
return max; |
|
} |
|
|
|
/* |
|
* Returns a copy of the longest leading path common among all |
|
* pathspecs. |
|
*/ |
|
char *common_prefix(const struct pathspec *pathspec) |
|
{ |
|
unsigned long len = common_prefix_len(pathspec); |
|
|
|
return len ? xmemdupz(pathspec->items[0].match, len) : NULL; |
|
} |
|
|
|
int fill_directory(struct dir_struct *dir, |
|
struct index_state *istate, |
|
const struct pathspec *pathspec) |
|
{ |
|
const char *prefix; |
|
size_t prefix_len; |
|
|
|
unsigned exclusive_flags = DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO; |
|
if ((dir->flags & exclusive_flags) == exclusive_flags) |
|
BUG("DIR_SHOW_IGNORED and DIR_SHOW_IGNORED_TOO are exclusive"); |
|
|
|
/* |
|
* Calculate common prefix for the pathspec, and |
|
* use that to optimize the directory walk |
|
*/ |
|
prefix_len = common_prefix_len(pathspec); |
|
prefix = prefix_len ? pathspec->items[0].match : ""; |
|
|
|
/* Read the directory and prune it */ |
|
read_directory(dir, istate, prefix, prefix_len, pathspec); |
|
|
|
return prefix_len; |
|
} |
|
|
|
int within_depth(const char *name, int namelen, |
|
int depth, int max_depth) |
|
{ |
|
const char *cp = name, *cpe = name + namelen; |
|
|
|
while (cp < cpe) { |
|
if (*cp++ != '/') |
|
continue; |
|
depth++; |
|
if (depth > max_depth) |
|
return 0; |
|
} |
|
return 1; |
|
} |
|
|
|
/* |
|
* Read the contents of the blob with the given OID into a buffer. |
|
* Append a trailing LF to the end if the last line doesn't have one. |
|
* |
|
* Returns: |
|
* -1 when the OID is invalid or unknown or does not refer to a blob. |
|
* 0 when the blob is empty. |
|
* 1 along with { data, size } of the (possibly augmented) buffer |
|
* when successful. |
|
* |
|
* Optionally updates the given oid_stat with the given OID (when valid). |
|
*/ |
|
static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat, |
|
size_t *size_out, char **data_out) |
|
{ |
|
enum object_type type; |
|
unsigned long sz; |
|
char *data; |
|
|
|
*size_out = 0; |
|
*data_out = NULL; |
|
|
|
data = read_object_file(oid, &type, &sz); |
|
if (!data || type != OBJ_BLOB) { |
|
free(data); |
|
return -1; |
|
} |
|
|
|
if (oid_stat) { |
|
memset(&oid_stat->stat, 0, sizeof(oid_stat->stat)); |
|
oidcpy(&oid_stat->oid, oid); |
|
} |
|
|
|
if (sz == 0) { |
|
free(data); |
|
return 0; |
|
} |
|
|
|
if (data[sz - 1] != '\n') { |
|
data = xrealloc(data, st_add(sz, 1)); |
|
data[sz++] = '\n'; |
|
} |
|
|
|
*size_out = xsize_t(sz); |
|
*data_out = data; |
|
|
|
return 1; |
|
} |
|
|
|
#define DO_MATCH_EXCLUDE (1<<0) |
|
#define DO_MATCH_DIRECTORY (1<<1) |
|
#define DO_MATCH_LEADING_PATHSPEC (1<<2) |
|
|
|
/* |
|
* Does the given pathspec match the given name? A match is found if |
|
* |
|
* (1) the pathspec string is leading directory of 'name' ("RECURSIVELY"), or |
|
* (2) the pathspec string has a leading part matching 'name' ("LEADING"), or |
|
* (3) the pathspec string is a wildcard and matches 'name' ("WILDCARD"), or |
|
* (4) the pathspec string is exactly the same as 'name' ("EXACT"). |
|
* |
|
* Return value tells which case it was (1-4), or 0 when there is no match. |
|
* |
|
* It may be instructive to look at a small table of concrete examples |
|
* to understand the differences between 1, 2, and 4: |
|
* |
|
* Pathspecs |
|
* | a/b | a/b/ | a/b/c |
|
* ------+-----------+-----------+------------ |
|
* a/b | EXACT | EXACT[1] | LEADING[2] |
|
* Names a/b/ | RECURSIVE | EXACT | LEADING[2] |
|
* a/b/c | RECURSIVE | RECURSIVE | EXACT |
|
* |
|
* [1] Only if DO_MATCH_DIRECTORY is passed; otherwise, this is NOT a match. |
|
* [2] Only if DO_MATCH_LEADING_PATHSPEC is passed; otherwise, not a match. |
|
*/ |
|
static int match_pathspec_item(struct index_state *istate, |
|
const struct pathspec_item *item, int prefix, |
|
const char *name, int namelen, unsigned flags) |
|
{ |
|
/* name/namelen has prefix cut off by caller */ |
|
const char *match = item->match + prefix; |
|
int matchlen = item->len - prefix; |
|
|
|
/* |
|
* The normal call pattern is: |
|
* 1. prefix = common_prefix_len(ps); |
|
* 2. prune something, or fill_directory |
|
* 3. match_pathspec() |
|
* |
|
* 'prefix' at #1 may be shorter than the command's prefix and |
|
* it's ok for #2 to match extra files. Those extras will be |
|
* trimmed at #3. |
|
* |
|
* Suppose the pathspec is 'foo' and '../bar' running from |
|
* subdir 'xyz'. The common prefix at #1 will be empty, thanks |
|
* to "../". We may have xyz/foo _and_ XYZ/foo after #2. The |
|
* user does not want XYZ/foo, only the "foo" part should be |
|
* case-insensitive. We need to filter out XYZ/foo here. In |
|
* other words, we do not trust the caller on comparing the |
|
* prefix part when :(icase) is involved. We do exact |
|
* comparison ourselves. |
|
* |
|
* Normally the caller (common_prefix_len() in fact) does |
|
* _exact_ matching on name[-prefix+1..-1] and we do not need |
|
* to check that part. Be defensive and check it anyway, in |
|
* case common_prefix_len is changed, or a new caller is |
|
* introduced that does not use common_prefix_len. |
|
* |
|
* If the penalty turns out too high when prefix is really |
|
* long, maybe change it to |
|
* strncmp(match, name, item->prefix - prefix) |
|
*/ |
|
if (item->prefix && (item->magic & PATHSPEC_ICASE) && |
|
strncmp(item->match, name - prefix, item->prefix)) |
|
return 0; |
|
|
|
if (item->attr_match_nr && |
|
!match_pathspec_attrs(istate, name, namelen, item)) |
|
return 0; |
|
|
|
/* If the match was just the prefix, we matched */ |
|
if (!*match) |
|
return MATCHED_RECURSIVELY; |
|
|
|
if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { |
|
if (matchlen == namelen) |
|
return MATCHED_EXACTLY; |
|
|
|
if (match[matchlen-1] == '/' || name[matchlen] == '/') |
|
return MATCHED_RECURSIVELY; |
|
} else if ((flags & DO_MATCH_DIRECTORY) && |
|
match[matchlen - 1] == '/' && |
|
namelen == matchlen - 1 && |
|
!ps_strncmp(item, match, name, namelen)) |
|
return MATCHED_EXACTLY; |
|
|
|
if (item->nowildcard_len < item->len && |
|
!git_fnmatch(item, match, name, |
|
item->nowildcard_len - prefix)) |
|
return MATCHED_FNMATCH; |
|
|
|
/* Perform checks to see if "name" is a leading string of the pathspec */ |
|
if ( (flags & DO_MATCH_LEADING_PATHSPEC) && |
|
!(flags & DO_MATCH_EXCLUDE)) { |
|
/* name is a literal prefix of the pathspec */ |
|
int offset = name[namelen-1] == '/' ? 1 : 0; |
|
if ((namelen < matchlen) && |
|
(match[namelen-offset] == '/') && |
|
!ps_strncmp(item, match, name, namelen)) |
|
return MATCHED_RECURSIVELY_LEADING_PATHSPEC; |
|
|
|
/* name doesn't match up to the first wild character */ |
|
if (item->nowildcard_len < item->len && |
|
ps_strncmp(item, match, name, |
|
item->nowildcard_len - prefix)) |
|
return 0; |
|
|
|
/* |
|
* name has no wildcard, and it didn't match as a leading |
|
* pathspec so return. |
|
*/ |
|
if (item->nowildcard_len == item->len) |
|
return 0; |
|
|
|
/* |
|
* Here is where we would perform a wildmatch to check if |
|
* "name" can be matched as a directory (or a prefix) against |
|
* the pathspec. Since wildmatch doesn't have this capability |
|
* at the present we have to punt and say that it is a match, |
|
* potentially returning a false positive |
|
* The submodules themselves will be able to perform more |
|
* accurate matching to determine if the pathspec matches. |
|
*/ |
|
return MATCHED_RECURSIVELY_LEADING_PATHSPEC; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
/* |
|
* do_match_pathspec() is meant to ONLY be called by |
|
* match_pathspec_with_flags(); calling it directly risks pathspecs |
|
* like ':!unwanted_path' being ignored. |
|
* |
|
* Given a name and a list of pathspecs, returns the nature of the |
|
* closest (i.e. most specific) match of the name to any of the |
|
* pathspecs. |
|
* |
|
* The caller typically calls this multiple times with the same |
|
* pathspec and seen[] array but with different name/namelen |
|
* (e.g. entries from the index) and is interested in seeing if and |
|
* how each pathspec matches all the names it calls this function |
|
* with. A mark is left in the seen[] array for each pathspec element |
|
* indicating the closest type of match that element achieved, so if |
|
* seen[n] remains zero after multiple invocations, that means the nth |
|
* pathspec did not match any names, which could indicate that the |
|
* user mistyped the nth pathspec. |
|
*/ |
|
static int do_match_pathspec(struct index_state *istate, |
|
const struct pathspec *ps, |
|
const char *name, int namelen, |
|
int prefix, char *seen, |
|
unsigned flags) |
|
{ |
|
int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE; |
|
|
|
GUARD_PATHSPEC(ps, |
|
PATHSPEC_FROMTOP | |
|
PATHSPEC_MAXDEPTH | |
|
PATHSPEC_LITERAL | |
|
PATHSPEC_GLOB | |
|
PATHSPEC_ICASE | |
|
PATHSPEC_EXCLUDE | |
|
PATHSPEC_ATTR); |
|
|
|
if (!ps->nr) { |
|
if (!ps->recursive || |
|
!(ps->magic & PATHSPEC_MAXDEPTH) || |
|
ps->max_depth == -1) |
|
return MATCHED_RECURSIVELY; |
|
|
|
if (within_depth(name, namelen, 0, ps->max_depth)) |
|
return MATCHED_EXACTLY; |
|
else |
|
return 0; |
|
} |
|
|
|
name += prefix; |
|
namelen -= prefix; |
|
|
|
for (i = ps->nr - 1; i >= 0; i--) { |
|
int how; |
|
|
|
if ((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || |
|
( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) |
|
continue; |
|
|
|
if (seen && seen[i] == MATCHED_EXACTLY) |
|
continue; |
|
/* |
|
* Make exclude patterns optional and never report |
|
* "pathspec ':(exclude)foo' matches no files" |
|
*/ |
|
if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE) |
|
seen[i] = MATCHED_FNMATCH; |
|
how = match_pathspec_item(istate, ps->items+i, prefix, name, |
|
namelen, flags); |
|
if (ps->recursive && |
|
(ps->magic & PATHSPEC_MAXDEPTH) && |
|
ps->max_depth != -1 && |
|
how && how != MATCHED_FNMATCH) { |
|
int len = ps->items[i].len; |
|
if (name[len] == '/') |
|
len++; |
|
if (within_depth(name+len, namelen-len, 0, ps->max_depth)) |
|
how = MATCHED_EXACTLY; |
|
else |
|
how = 0; |
|
} |
|
if (how) { |
|
if (retval < how) |
|
retval = how; |
|
if (seen && seen[i] < how) |
|
seen[i] = how; |
|
} |
|
} |
|
return retval; |
|
} |
|
|
|
static int match_pathspec_with_flags(struct index_state *istate, |
|
const struct pathspec *ps, |
|
const char *name, int namelen, |
|
int prefix, char *seen, unsigned flags) |
|
{ |
|
int positive, negative; |
|
positive = do_match_pathspec(istate, ps, name, namelen, |
|
prefix, seen, flags); |
|
if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) |
|
return positive; |
|
negative = do_match_pathspec(istate, ps, name, namelen, |
|
prefix, seen, |
|
flags | DO_MATCH_EXCLUDE); |
|
return negative ? 0 : positive; |
|
} |
|
|
|
int match_pathspec(struct index_state *istate, |
|
const struct pathspec *ps, |
|
const char *name, int namelen, |
|
int prefix, char *seen, int is_dir) |
|
{ |
|
unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0; |
|
return match_pathspec_with_flags(istate, ps, name, namelen, |
|
prefix, seen, flags); |
|
} |
|
|
|
/** |
|
* Check if a submodule is a superset of the pathspec |
|
*/ |
|
int submodule_path_match(struct index_state *istate, |
|
const struct pathspec *ps, |
|
const char *submodule_name, |
|
char *seen) |
|
{ |
|
int matched = match_pathspec_with_flags(istate, ps, submodule_name, |
|
strlen(submodule_name), |
|
0, seen, |
|
DO_MATCH_DIRECTORY | |
|
DO_MATCH_LEADING_PATHSPEC); |
|
return matched; |
|
} |
|
|
|
int report_path_error(const char *ps_matched, |
|
const struct pathspec *pathspec) |
|
{ |
|
/* |
|
* Make sure all pathspec matched; otherwise it is an error. |
|
*/ |
|
int num, errors = 0; |
|
for (num = 0; num < pathspec->nr; num++) { |
|
int other, found_dup; |
|
|
|
if (ps_matched[num]) |
|
continue; |
|
/* |
|
* The caller might have fed identical pathspec |
|
* twice. Do not barf on such a mistake. |
|
* FIXME: parse_pathspec should have eliminated |
|
* duplicate pathspec. |
|
*/ |
|
for (found_dup = other = 0; |
|
!found_dup && other < pathspec->nr; |
|
other++) { |
|
if (other == num || !ps_matched[other]) |
|
continue; |
|
if (!strcmp(pathspec->items[other].original, |
|
pathspec->items[num].original)) |
|
/* |
|
* Ok, we have a match already. |
|
*/ |
|
found_dup = 1; |
|
} |
|
if (found_dup) |
|
continue; |
|
|
|
error(_("pathspec '%s' did not match any file(s) known to git"), |
|
pathspec->items[num].original); |
|
errors++; |
|
} |
|
return errors; |
|
} |
|
|
|
/* |
|
* Return the length of the "simple" part of a path match limiter. |
|
*/ |
|
int simple_length(const char *match) |
|
{ |
|
int len = -1; |
|
|
|
for (;;) { |
|
unsigned char c = *match++; |
|
len++; |
|
if (c == '\0' || is_glob_special(c)) |
|
return len; |
|
} |
|
} |
|
|
|
int no_wildcard(const char *string) |
|
{ |
|
return string[simple_length(string)] == '\0'; |
|
} |
|
|
|
void parse_path_pattern(const char **pattern, |
|
int *patternlen, |
|
unsigned *flags, |
|
int *nowildcardlen) |
|
{ |
|
const char *p = *pattern; |
|
size_t i, len; |
|
|
|
*flags = 0; |
|
if (*p == '!') { |
|
*flags |= PATTERN_FLAG_NEGATIVE; |
|
p++; |
|
} |
|
len = strlen(p); |
|
if (len && p[len - 1] == '/') { |
|
len--; |
|
*flags |= PATTERN_FLAG_MUSTBEDIR; |
|
} |
|
for (i = 0; i < len; i++) { |
|
if (p[i] == '/') |
|
break; |
|
} |
|
if (i == len) |
|
*flags |= PATTERN_FLAG_NODIR; |
|
*nowildcardlen = simple_length(p); |
|
/* |
|
* we should have excluded the trailing slash from 'p' too, |
|
* but that's one more allocation. Instead just make sure |
|
* nowildcardlen does not exceed real patternlen |
|
*/ |
|
if (*nowildcardlen > len) |
|
*nowildcardlen = len; |
|
if (*p == '*' && no_wildcard(p + 1)) |
|
*flags |= PATTERN_FLAG_ENDSWITH; |
|
*pattern = p; |
|
*patternlen = len; |
|
} |
|
|
|
int pl_hashmap_cmp(const void *unused_cmp_data, |
|
const struct hashmap_entry *a, |
|
const struct hashmap_entry *b, |
|
const void *key) |
|
{ |
|
const struct pattern_entry *ee1 = |
|
container_of(a, struct pattern_entry, ent); |
|
const struct pattern_entry *ee2 = |
|
container_of(b, struct pattern_entry, ent); |
|
|
|
size_t min_len = ee1->patternlen <= ee2->patternlen |
|
? ee1->patternlen |
|
: ee2->patternlen; |
|
|
|
if (ignore_case) |
|
return strncasecmp(ee1->pattern, ee2->pattern, min_len); |
|
return strncmp(ee1->pattern, ee2->pattern, min_len); |
|
} |
|
|
|
static char *dup_and_filter_pattern(const char *pattern) |
|
{ |
|
char *set, *read; |
|
size_t count = 0; |
|
char *result = xstrdup(pattern); |
|
|
|
set = result; |
|
read = result; |
|
|
|
while (*read) { |
|
/* skip escape characters (once) */ |
|
if (*read == '\\') |
|
read++; |
|
|
|
*set = *read; |
|
|
|
set++; |
|
read++; |
|
count++; |
|
} |
|
*set = 0; |
|
|
|
if (count > 2 && |
|
*(set - 1) == '*' && |
|
*(set - 2) == '/') |
|
*(set - 2) = 0; |
|
|
|
return result; |
|
} |
|
|
|
static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given) |
|
{ |
|
struct pattern_entry *translated; |
|
char *truncated; |
|
char *data = NULL; |
|
const char *prev, *cur, *next; |
|
|
|
if (!pl->use_cone_patterns) |
|
return; |
|
|
|
if (given->flags & PATTERN_FLAG_NEGATIVE && |
|
given->flags & PATTERN_FLAG_MUSTBEDIR && |
|
!strcmp(given->pattern, "/*")) { |
|
pl->full_cone = 0; |
|
return; |
|
} |
|
|
|
if (!given->flags && !strcmp(given->pattern, "/*")) { |
|
pl->full_cone = 1; |
|
return; |
|
} |
|
|
|
if (given->patternlen < 2 || |
|
*given->pattern != '/' || |
|
strstr(given->pattern, "**")) { |
|
/* Not a cone pattern. */ |
|
warning(_("unrecognized pattern: '%s'"), given->pattern); |
|
goto clear_hashmaps; |
|
} |
|
|
|
prev = given->pattern; |
|
cur = given->pattern + 1; |
|
next = given->pattern + 2; |
|
|
|
while (*cur) { |
|
/* Watch for glob characters '*', '\', '[', '?' */ |
|
if (!is_glob_special(*cur)) |
|
goto increment; |
|
|
|
/* But only if *prev != '\\' */ |
|
if (*prev == '\\') |
|
goto increment; |
|
|
|
/* But allow the initial '\' */ |
|
if (*cur == '\\' && |
|
is_glob_special(*next)) |
|
goto increment; |
|
|
|
/* But a trailing '/' then '*' is fine */ |
|
if (*prev == '/' && |
|
*cur == '*' && |
|
*next == 0) |
|
goto increment; |
|
|
|
/* Not a cone pattern. */ |
|
warning(_("unrecognized pattern: '%s'"), given->pattern); |
|
goto clear_hashmaps; |
|
|
|
increment: |
|
prev++; |
|
cur++; |
|
next++; |
|
} |
|
|
|
if (given->patternlen > 2 && |
|
!strcmp(given->pattern + given->patternlen - 2, "/*")) { |
|
if (!(given->flags & PATTERN_FLAG_NEGATIVE)) { |
|
/* Not a cone pattern. */ |
|
warning(_("unrecognized pattern: '%s'"), given->pattern); |
|
goto clear_hashmaps; |
|
} |
|
|
|
truncated = dup_and_filter_pattern(given->pattern); |
|
|
|
translated = xmalloc(sizeof(struct pattern_entry)); |
|
translated->pattern = truncated; |
|
translated->patternlen = given->patternlen - 2; |
|
hashmap_entry_init(&translated->ent, |
|
fspathhash(translated->pattern)); |
|
|
|
if (!hashmap_get_entry(&pl->recursive_hashmap, |
|
translated, ent, NULL)) { |
|
/* We did not see the "parent" included */ |
|
warning(_("unrecognized negative pattern: '%s'"), |
|
given->pattern); |
|
free(truncated); |
|
free(translated); |
|
goto clear_hashmaps; |
|
} |
|
|
|
hashmap_add(&pl->parent_hashmap, &translated->ent); |
|
hashmap_remove(&pl->recursive_hashmap, &translated->ent, &data); |
|
free(data); |
|
return; |
|
} |
|
|
|
if (given->flags & PATTERN_FLAG_NEGATIVE) { |
|
warning(_("unrecognized negative pattern: '%s'"), |
|
given->pattern); |
|
goto clear_hashmaps; |
|
} |
|
|
|
translated = xmalloc(sizeof(struct pattern_entry)); |
|
|
|
translated->pattern = dup_and_filter_pattern(given->pattern); |
|
translated->patternlen = given->patternlen; |
|
hashmap_entry_init(&translated->ent, |
|
fspathhash(translated->pattern)); |
|
|
|
hashmap_add(&pl->recursive_hashmap, &translated->ent); |
|
|
|
if (hashmap_get_entry(&pl->parent_hashmap, translated, ent, NULL)) { |
|
/* we already included this at the parent level */ |
|
warning(_("your sparse-checkout file may have issues: pattern '%s' is repeated"), |
|
given->pattern); |
|
goto clear_hashmaps; |
|
} |
|
|
|
return; |
|
|
|
clear_hashmaps: |
|
warning(_("disabling cone pattern matching")); |
|
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent); |
|
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent); |
|
pl->use_cone_patterns = 0; |
|
} |
|
|
|
static int hashmap_contains_path(struct hashmap *map, |
|
struct strbuf *pattern) |
|
{ |
|
struct pattern_entry p; |
|
|
|
/* Check straight mapping */ |
|
p.pattern = pattern->buf; |
|
p.patternlen = pattern->len; |
|
hashmap_entry_init(&p.ent, fspathhash(p.pattern)); |
|
return !!hashmap_get_entry(map, &p, ent, NULL); |
|
} |
|
|
|
int hashmap_contains_parent(struct hashmap *map, |
|
const char *path, |
|
struct strbuf *buffer) |
|
{ |
|
char *slash_pos; |
|
|
|
strbuf_setlen(buffer, 0); |
|
|
|
if (path[0] != '/') |
|
strbuf_addch(buffer, '/'); |
|
|
|
strbuf_addstr(buffer, path); |
|
|
|
slash_pos = strrchr(buffer->buf, '/'); |
|
|
|
while (slash_pos > buffer->buf) { |
|
strbuf_setlen(buffer, slash_pos - buffer->buf); |
|
|
|
if (hashmap_contains_path(map, buffer)) |
|
return 1; |
|
|
|
slash_pos = strrchr(buffer->buf, '/'); |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
void add_pattern(const char *string, const char *base, |
|
int baselen, struct pattern_list *pl, int srcpos) |
|
{ |
|
struct path_pattern *pattern; |
|
int patternlen; |
|
unsigned flags; |
|
int nowildcardlen; |
|
|
|
parse_path_pattern(&string, &patternlen, &flags, &nowildcardlen); |
|
if (flags & PATTERN_FLAG_MUSTBEDIR) { |
|
FLEXPTR_ALLOC_MEM(pattern, pattern, string, patternlen); |
|
} else { |
|
pattern = xmalloc(sizeof(*pattern)); |
|
pattern->pattern = string; |
|
} |
|
pattern->patternlen = patternlen; |
|
pattern->nowildcardlen = nowildcardlen; |
|
pattern->base = base; |
|
pattern->baselen = baselen; |
|
pattern->flags = flags; |
|
pattern->srcpos = srcpos; |
|
ALLOC_GROW(pl->patterns, pl->nr + 1, pl->alloc); |
|
pl->patterns[pl->nr++] = pattern; |
|
pattern->pl = pl; |
|
|
|
add_pattern_to_hashsets(pl, pattern); |
|
} |
|
|
|
static int read_skip_worktree_file_from_index(struct index_state *istate, |
|
const char *path, |
|
size_t *size_out, char **data_out, |
|
struct oid_stat *oid_stat) |
|
{ |
|
int pos, len; |
|
|
|
len = strlen(path); |
|
pos = index_name_pos(istate, path, len); |
|
if (pos < 0) |
|
return -1; |
|
if (!ce_skip_worktree(istate->cache[pos])) |
|
return -1; |
|
|
|
return do_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out); |
|
} |
|
|
|
/* |
|
* Frees memory within pl which was allocated for exclude patterns and |
|
* the file buffer. Does not free pl itself. |
|
*/ |
|
void clear_pattern_list(struct pattern_list *pl) |
|
{ |
|
int i; |
|
|
|
for (i = 0; i < pl->nr; i++) |
|
free(pl->patterns[i]); |
|
free(pl->patterns); |
|
free(pl->filebuf); |
|
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent); |
|
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent); |
|
|
|
memset(pl, 0, sizeof(*pl)); |
|
} |
|
|
|
static void trim_trailing_spaces(char *buf) |
|
{ |
|
char *p, *last_space = NULL; |
|
|
|
for (p = buf; *p; p++) |
|
switch (*p) { |
|
case ' ': |
|
if (!last_space) |
|
last_space = p; |
|
break; |
|
case '\\': |
|
p++; |
|
if (!*p) |
|
return; |
|
/* fallthrough */ |
|
default: |
|
last_space = NULL; |
|
} |
|
|
|
if (last_space) |
|
*last_space = '\0'; |
|
} |
|
|
|
/* |
|
* Given a subdirectory name and "dir" of the current directory, |
|
* search the subdir in "dir" and return it, or create a new one if it |
|
* does not exist in "dir". |
|
* |
|
* If "name" has the trailing slash, it'll be excluded in the search. |
|
*/ |
|
static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, |
|
struct untracked_cache_dir *dir, |
|
const char *name, int len) |
|
{ |
|
int first, last; |
|
struct untracked_cache_dir *d; |
|
if (!dir) |
|
return NULL; |
|
if (len && name[len - 1] == '/') |
|
len--; |
|
first = 0; |
|
last = dir->dirs_nr; |
|
while (last > first) { |
|
int cmp, next = first + ((last - first) >> 1); |
|
d = dir->dirs[next]; |
|
cmp = strncmp(name, d->name, len); |
|
if (!cmp && strlen(d->name) > len) |
|
cmp = -1; |
|
if (!cmp) |
|
return d; |
|
if (cmp < 0) { |
|
last = next; |
|
continue; |
|
} |
|
first = next+1; |
|
} |
|
|
|
uc->dir_created++; |
|
FLEX_ALLOC_MEM(d, name, name, len); |
|
|
|
ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc); |
|
MOVE_ARRAY(dir->dirs + first + 1, dir->dirs + first, |
|
dir->dirs_nr - first); |
|
dir->dirs_nr++; |
|
dir->dirs[first] = d; |
|
return d; |
|
} |
|
|
|
static void do_invalidate_gitignore(struct untracked_cache_dir *dir) |
|
{ |
|
int i; |
|
dir->valid = 0; |
|
dir->untracked_nr = 0; |
|
for (i = 0; i < dir->dirs_nr; i++) |
|
do_invalidate_gitignore(dir->dirs[i]); |
|
} |
|
|
|
static void invalidate_gitignore(struct untracked_cache *uc, |
|
struct untracked_cache_dir *dir) |
|
{ |
|
uc->gitignore_invalidated++; |
|
do_invalidate_gitignore(dir); |
|
} |
|
|
|
static void invalidate_directory(struct untracked_cache *uc, |
|
struct untracked_cache_dir *dir) |
|
{ |
|
int i; |
|
|
|
/* |
|
* Invalidation increment here is just roughly correct. If |
|
* untracked_nr or any of dirs[].recurse is non-zero, we |
|
* should increment dir_invalidated too. But that's more |
|
* expensive to do. |
|
*/ |
|
if (dir->valid) |
|
uc->dir_invalidated++; |
|
|
|
dir->valid = 0; |
|
dir->untracked_nr = 0; |
|
for (i = 0; i < dir->dirs_nr; i++) |
|
dir->dirs[i]->recurse = 0; |
|
} |
|
|
|
static int add_patterns_from_buffer(char *buf, size_t size, |
|
const char *base, int baselen, |
|
struct pattern_list *pl); |
|
|
|
/* Flags for add_patterns() */ |
|
#define PATTERN_NOFOLLOW (1<<0) |
|
|
|
/* |
|
* Given a file with name "fname", read it (either from disk, or from |
|
* an index if 'istate' is non-null), parse it and store the |
|
* exclude rules in "pl". |
|
* |
|
* If "oid_stat" is not NULL, compute oid of the exclude file and fill |
|
* stat data from disk (only valid if add_patterns returns zero). If |
|
* oid_stat.valid is non-zero, "oid_stat" must contain good value as input. |
|
*/ |
|
static int add_patterns(const char *fname, const char *base, int baselen, |
|
struct pattern_list *pl, struct index_state *istate, |
|
unsigned flags, struct oid_stat *oid_stat) |
|
{ |
|
struct stat st; |
|
int r; |
|
int fd; |
|
size_t size = 0; |
|
char *buf; |
|
|
|
if (flags & PATTERN_NOFOLLOW) |
|
fd = open_nofollow(fname, O_RDONLY); |
|
else |
|
fd = open(fname, O_RDONLY); |
|
|
|
if (fd < 0 || fstat(fd, &st) < 0) { |
|
if (fd < 0) |
|
warn_on_fopen_errors(fname); |
|
else |
|
close(fd); |
|
if (!istate) |
|
return -1; |
|
r = read_skip_worktree_file_from_index(istate, fname, |
|
&size, &buf, |
|
oid_stat); |
|
if (r != 1) |
|
return r; |
|
} else { |
|
size = xsize_t(st.st_size); |
|
if (size == 0) { |
|
if (oid_stat) { |
|
fill_stat_data(&oid_stat->stat, &st); |
|
oidcpy(&oid_stat->oid, the_hash_algo->empty_blob); |
|
oid_stat->valid = 1; |
|
} |
|
close(fd); |
|
return 0; |
|
} |
|
buf = xmallocz(size); |
|
if (read_in_full(fd, buf, size) != size) { |
|
free(buf); |
|
close(fd); |
|
return -1; |
|
} |
|
buf[size++] = '\n'; |
|
close(fd); |
|
if (oid_stat) { |
|
int pos; |
|
if (oid_stat->valid && |
|
!match_stat_data_racy(istate, &oid_stat->stat, &st)) |
|
; /* no content change, oid_stat->oid still good */ |
|
else if (istate && |
|
(pos = index_name_pos(istate, fname, strlen(fname))) >= 0 && |
|
!ce_stage(istate->cache[pos]) && |
|
ce_uptodate(istate->cache[pos]) && |
|
!would_convert_to_git(istate, fname)) |
|
oidcpy(&oid_stat->oid, |
|
&istate->cache[pos]->oid); |
|
else |
|
hash_object_file(the_hash_algo, buf, size, |
|
OBJ_BLOB, &oid_stat->oid); |
|
fill_stat_data(&oid_stat->stat, &st); |
|
oid_stat->valid = 1; |
|
} |
|
} |
|
|
|
add_patterns_from_buffer(buf, size, base, baselen, pl); |
|
return 0; |
|
} |
|
|
|
static int add_patterns_from_buffer(char *buf, size_t size, |
|
const char *base, int baselen, |
|
struct pattern_list *pl) |
|
{ |
|
int i, lineno = 1; |
|
char *entry; |
|
|
|
hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0); |
|
hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0); |
|
|
|
pl->filebuf = buf; |
|
|
|
if (skip_utf8_bom(&buf, size)) |
|
size -= buf - pl->filebuf; |
|
|
|
entry = buf; |
|
|
|
for (i = 0; i < size; i++) { |
|
if (buf[i] == '\n') { |
|
if (entry != buf + i && entry[0] != '#') { |
|
buf[i - (i && buf[i-1] == '\r')] = 0; |
|
trim_trailing_spaces(entry); |
|
add_pattern(entry, base, baselen, pl, lineno); |
|
} |
|
lineno++; |
|
entry = buf + i + 1; |
|
} |
|
} |
|
return 0; |
|
} |
|
|
|
int add_patterns_from_file_to_list(const char *fname, const char *base, |
|
int baselen, struct pattern_list *pl, |
|
struct index_state *istate, |
|
unsigned flags) |
|
{ |
|
return add_patterns(fname, base, baselen, pl, istate, flags, NULL); |
|
} |
|
|
|
int add_patterns_from_blob_to_list( |
|
struct object_id *oid, |
|
const char *base, int baselen, |
|
struct pattern_list *pl) |
|
{ |
|
char *buf; |
|
size_t size; |
|
int r; |
|
|
|
r = do_read_blob(oid, NULL, &size, &buf); |
|
if (r != 1) |
|
return r; |
|
|
|
add_patterns_from_buffer(buf, size, base, baselen, pl); |
|
return 0; |
|
} |
|
|
|
struct pattern_list *add_pattern_list(struct dir_struct *dir, |
|
int group_type, const char *src) |
|
{ |
|
struct pattern_list *pl; |
|
struct exclude_list_group *group; |
|
|
|
group = &dir->exclude_list_group[group_type]; |
|
ALLOC_GROW(group->pl, group->nr + 1, group->alloc); |
|
pl = &group->pl[group->nr++]; |
|
memset(pl, 0, sizeof(*pl)); |
|
pl->src = src; |
|
return pl; |
|
} |
|
|
|
/* |
|
* Used to set up core.excludesfile and .git/info/exclude lists. |
|
*/ |
|
static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname, |
|
struct oid_stat *oid_stat) |
|
{ |
|
struct pattern_list *pl; |
|
/* |
|
* catch setup_standard_excludes() that's called before |
|
* dir->untracked is assigned. That function behaves |
|
* differently when dir->untracked is non-NULL. |
|
*/ |
|
if (!dir->untracked) |
|
dir->unmanaged_exclude_files++; |
|
pl = add_pattern_list(dir, EXC_FILE, fname); |
|
if (add_patterns(fname, "", 0, pl, NULL, 0, oid_stat) < 0) |
|
die(_("cannot use %s as an exclude file"), fname); |
|
} |
|
|
|
void add_patterns_from_file(struct dir_struct *dir, const char *fname) |
|
{ |
|
dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */ |
|
add_patterns_from_file_1(dir, fname, NULL); |
|
} |
|
|
|
int match_basename(const char *basename, int basenamelen, |
|
const char *pattern, int prefix, int patternlen, |
|
unsigned flags) |
|
{ |
|
if (prefix == patternlen) { |
|
if (patternlen == basenamelen && |
|
!fspathncmp(pattern, basename, basenamelen)) |
|
return 1; |
|
} else if (flags & PATTERN_FLAG_ENDSWITH) { |
|
/* "*literal" matching against "fooliteral" */ |
|
if (patternlen - 1 <= basenamelen && |
|
!fspathncmp(pattern + 1, |
|
basename + basenamelen - (patternlen - 1), |
|
patternlen - 1)) |
|
return 1; |
|
} else { |
|
if (fnmatch_icase_mem(pattern, patternlen, |
|
basename, basenamelen, |
|
0) == 0) |
|
return 1; |
|
} |
|
return 0; |
|
} |
|
|
|
int match_pathname(const char *pathname, int pathlen, |
|
const char *base, int baselen, |
|
const char *pattern, int prefix, int patternlen, |
|
unsigned flags) |
|
{ |
|
const char *name; |
|
int namelen; |
|
|
|
/* |
|
* match with FNM_PATHNAME; the pattern has base implicitly |
|
* in front of it. |
|
*/ |
|
if (*pattern == '/') { |
|
pattern++; |
|
patternlen--; |
|
prefix--; |
|
} |
|
|
|
/* |
|
* baselen does not count the trailing slash. base[] may or |
|
* may not end with a trailing slash though. |
|
*/ |
|
if (pathlen < baselen + 1 || |
|
(baselen && pathname[baselen] != '/') || |
|
fspathncmp(pathname, base, baselen)) |
|
return 0; |
|
|
|
namelen = baselen ? pathlen - baselen - 1 : pathlen; |
|
name = pathname + pathlen - namelen; |
|
|
|
if (prefix) { |
|
/* |
|
* if the non-wildcard part is longer than the |
|
* remaining pathname, surely it cannot match. |
|
*/ |
|
if (prefix > namelen) |
|
return 0; |
|
|
|
if (fspathncmp(pattern, name, prefix)) |
|
return 0; |
|
pattern += prefix; |
|
patternlen -= prefix; |
|
name += prefix; |
|
namelen -= prefix; |
|
|
|
/* |
|
* If the whole pattern did not have a wildcard, |
|
* then our prefix match is all we need; we |
|
* do not need to call fnmatch at all. |
|
*/ |
|
if (!patternlen && !namelen) |
|
return 1; |
|
} |
|
|
|
return fnmatch_icase_mem(pattern, patternlen, |
|
name, namelen, |
|
WM_PATHNAME) == 0; |
|
} |
|
|
|
/* |
|
* Scan the given exclude list in reverse to see whether pathname |
|
* should be ignored. The first match (i.e. the last on the list), if |
|
* any, determines the fate. Returns the exclude_list element which |
|
* matched, or NULL for undecided. |
|
*/ |
|
static struct path_pattern *last_matching_pattern_from_list(const char *pathname, |
|
int pathlen, |
|
const char *basename, |
|
int *dtype, |
|
struct pattern_list *pl, |
|
struct index_state *istate) |
|
{ |
|
struct path_pattern *res = NULL; /* undecided */ |
|
int i; |
|
|
|
if (!pl->nr) |
|
return NULL; /* undefined */ |
|
|
|
for (i = pl->nr - 1; 0 <= i; i--) { |
|
struct path_pattern *pattern = pl->patterns[i]; |
|
const char *exclude = pattern->pattern; |
|
int prefix = pattern->nowildcardlen; |
|
|
|
if (pattern->flags & PATTERN_FLAG_MUSTBEDIR) { |
|
*dtype = resolve_dtype(*dtype, istate, pathname, pathlen); |
|
if (*dtype != DT_DIR) |
|
continue; |
|
} |
|
|
|
if (pattern->flags & PATTERN_FLAG_NODIR) { |
|
if (match_basename(basename, |
|
pathlen - (basename - pathname), |
|
exclude, prefix, pattern->patternlen, |
|
pattern->flags)) { |
|
res = pattern; |
|
break; |
|
} |
|
continue; |
|
} |
|
|
|
assert(pattern->baselen == 0 || |
|
pattern->base[pattern->baselen - 1] == '/'); |
|
if (match_pathname(pathname, pathlen, |
|
pattern->base, |
|
pattern->baselen ? pattern->baselen - 1 : 0, |
|
exclude, prefix, pattern->patternlen, |
|
pattern->flags)) { |
|
res = pattern; |
|
break; |
|
} |
|
} |
|
return res; |
|
} |
|
|
|
/* |
|
* Scan the list of patterns to determine if the ordered list |
|
* of patterns matches on 'pathname'. |
|
* |
|
* Return 1 for a match, 0 for not matched and -1 for undecided. |
|
*/ |
|
enum pattern_match_result path_matches_pattern_list( |
|
const char *pathname, int pathlen, |
|
const char *basename, int *dtype, |
|
struct pattern_list *pl, |
|
struct index_state *istate) |
|
{ |
|
struct path_pattern *pattern; |
|
struct strbuf parent_pathname = STRBUF_INIT; |
|
int result = NOT_MATCHED; |
|
size_t slash_pos; |
|
|
|
if (!pl->use_cone_patterns) { |
|
pattern = last_matching_pattern_from_list(pathname, pathlen, basename, |
|
dtype, pl, istate); |
|
if (pattern) { |
|
if (pattern->flags & PATTERN_FLAG_NEGATIVE) |
|
return NOT_MATCHED; |
|
else |
|
return MATCHED; |
|
} |
|
|
|
return UNDECIDED; |
|
} |
|
|
|
if (pl->full_cone) |
|
return MATCHED; |
|
|
|
strbuf_addch(&parent_pathname, '/'); |
|
strbuf_add(&parent_pathname, pathname, pathlen); |
|
|
|
/* |
|
* Directory entries are matched if and only if a file |
|
* contained immediately within them is matched. For the |
|
* case of a directory entry, modify the path to create |
|
* a fake filename within this directory, allowing us to |
|
* use the file-base matching logic in an equivalent way. |
|
*/ |
|
if (parent_pathname.len > 0 && |
|
parent_pathname.buf[parent_pathname.len - 1] == '/') { |
|
slash_pos = parent_pathname.len - 1; |
|
strbuf_add(&parent_pathname, "-", 1); |
|
} else { |
|
const char *slash_ptr = strrchr(parent_pathname.buf, '/'); |
|
slash_pos = slash_ptr ? slash_ptr - parent_pathname.buf : 0; |
|
} |
|
|
|
if (hashmap_contains_path(&pl->recursive_hashmap, |
|
&parent_pathname)) { |
|
result = MATCHED_RECURSIVE; |
|
goto done; |
|
} |
|
|
|
if (!slash_pos) { |
|
/* include every file in root */ |
|
result = MATCHED; |
|
goto done; |
|
} |
|
|
|
strbuf_setlen(&parent_pathname, slash_pos); |
|
|
|
if (hashmap_contains_path(&pl->parent_hashmap, &parent_pathname)) { |
|
result = MATCHED; |
|
goto done; |
|
} |
|
|
|
if (hashmap_contains_parent(&pl->recursive_hashmap, |
|
pathname, |
|
&parent_pathname)) |
|
result = MATCHED_RECURSIVE; |
|
|
|
done: |
|
strbuf_release(&parent_pathname); |
|
return result; |
|
} |
|
|
|
int init_sparse_checkout_patterns(struct index_state *istate) |
|
{ |
|
if (!core_apply_sparse_checkout) |
|
return 1; |
|
if (istate->sparse_checkout_patterns) |
|
return 0; |
|
|
|
CALLOC_ARRAY(istate->sparse_checkout_patterns, 1); |
|
|
|
if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0) { |
|
FREE_AND_NULL(istate->sparse_checkout_patterns); |
|
return -1; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
static int path_in_sparse_checkout_1(const char *path, |
|
struct index_state *istate, |
|
int require_cone_mode) |
|
{ |
|
int dtype = DT_REG; |
|
enum pattern_match_result match = UNDECIDED; |
|
const char *end, *slash; |
|
|
|
/* |
|
* We default to accepting a path if the path is empty, there are no |
|
* patterns, or the patterns are of the wrong type. |
|
*/ |
|
if (!*path || |
|
init_sparse_checkout_patterns(istate) || |
|
(require_cone_mode && |
|
!istate->sparse_checkout_patterns->use_cone_patterns)) |
|
return 1; |
|
|
|
/* |
|
* If UNDECIDED, use the match from the parent dir (recursively), or |
|
* fall back to NOT_MATCHED at the topmost level. Note that cone mode |
|
* never returns UNDECIDED, so we will execute only one iteration in |
|
* this case. |
|
*/ |
|
for (end = path + strlen(path); |
|
end > path && match == UNDECIDED; |
|
end = slash) { |
|
|
|
for (slash = end - 1; slash > path && *slash != '/'; slash--) |
|
; /* do nothing */ |
|
|
|
match = path_matches_pattern_list(path, end - path, |
|
slash > path ? slash + 1 : path, &dtype, |
|
istate->sparse_checkout_patterns, istate); |
|
|
|
/* We are going to match the parent dir now */ |
|
dtype = DT_DIR; |
|
} |
|
return match > 0; |
|
} |
|
|
|
int path_in_sparse_checkout(const char *path, |
|
struct index_state *istate) |
|
{ |
|
return path_in_sparse_checkout_1(path, istate, 0); |
|
} |
|
|
|
int path_in_cone_mode_sparse_checkout(const char *path, |
|
struct index_state *istate) |
|
{ |
|
return path_in_sparse_checkout_1(path, istate, 1); |
|
} |
|
|
|
static struct path_pattern *last_matching_pattern_from_lists( |
|
struct dir_struct *dir, struct index_state *istate, |
|
const char *pathname, int pathlen, |
|
const char *basename, int *dtype_p) |
|
{ |
|
int i, j; |
|
struct exclude_list_group *group; |
|
struct path_pattern *pattern; |
|
for (i = EXC_CMDL; i <= EXC_FILE; i++) { |
|
group = &dir->exclude_list_group[i]; |
|
for (j = group->nr - 1; j >= 0; j--) { |
|
pattern = last_matching_pattern_from_list( |
|
pathname, pathlen, basename, dtype_p, |
|
&group->pl[j], istate); |
|
if (pattern) |
|
return pattern; |
|
} |
|
} |
|
return NULL; |
|
} |
|
|
|
/* |
|
* Loads the per-directory exclude list for the substring of base |
|
* which has a char length of baselen. |
|
*/ |
|
static void prep_exclude(struct dir_struct *dir, |
|
struct index_state *istate, |
|
const char *base, int baselen) |
|
{ |
|
struct exclude_list_group *group; |
|
struct pattern_list *pl; |
|
struct exclude_stack *stk = NULL; |
|
struct untracked_cache_dir *untracked; |
|
int current; |
|
|
|
group = &dir->exclude_list_group[EXC_DIRS]; |
|
|
|
/* |
|
* Pop the exclude lists from the EXCL_DIRS exclude_list_group |
|
* which originate from directories not in the prefix of the |
|
* path being checked. |
|
*/ |
|
while ((stk = dir->exclude_stack) != NULL) { |
|
if (stk->baselen <= baselen && |
|
!strncmp(dir->basebuf.buf, base, stk->baselen)) |
|
break; |
|
pl = &group->pl[dir->exclude_stack->exclude_ix]; |
|
dir->exclude_stack = stk->prev; |
|
dir->pattern = NULL; |
|
free((char *)pl->src); /* see strbuf_detach() below */ |
|
clear_pattern_list(pl); |
|
free(stk); |
|
group->nr--; |
|
} |
|
|
|
/* Skip traversing into sub directories if the parent is excluded */ |
|
if (dir->pattern) |
|
return; |
|
|
|
/* |
|
* Lazy initialization. All call sites currently just |
|
* memset(dir, 0, sizeof(*dir)) before use. Changing all of |
|
* them seems lots of work for little benefit. |
|
*/ |
|
if (!dir->basebuf.buf) |
|
strbuf_init(&dir->basebuf, PATH_MAX); |
|
|
|
/* Read from the parent directories and push them down. */ |
|
current = stk ? stk->baselen : -1; |
|
strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current); |
|
if (dir->untracked) |
|
untracked = stk ? stk->ucd : dir->untracked->root; |
|
else |
|
untracked = NULL; |
|
|
|
while (current < baselen) { |
|
const char *cp; |
|
struct oid_stat oid_stat; |
|
|
|
CALLOC_ARRAY(stk, 1); |
|
if (current < 0) { |
|
cp = base; |
|
current = 0; |
|
} else { |
|
cp = strchr(base + current + 1, '/'); |
|
if (!cp) |
|
die("oops in prep_exclude"); |
|
cp++; |
|
untracked = |
|
lookup_untracked(dir->untracked, untracked, |
|
base + current, |
|
cp - base - current); |
|
} |
|
stk->prev = dir->exclude_stack; |
|
stk->baselen = cp - base; |
|
stk->exclude_ix = group->nr; |
|
stk->ucd = untracked; |
|
pl = add_pattern_list(dir, EXC_DIRS, NULL); |
|
strbuf_add(&dir->basebuf, base + current, stk->baselen - current); |
|
assert(stk->baselen == dir->basebuf.len); |
|
|
|
/* Abort if the directory is excluded */ |
|
if (stk->baselen) { |
|
int dt = DT_DIR; |
|
dir->basebuf.buf[stk->baselen - 1] = 0; |
|
dir->pattern = last_matching_pattern_from_lists(dir, |
|
istate, |
|
dir->basebuf.buf, stk->baselen - 1, |
|
dir->basebuf.buf + current, &dt); |
|
dir->basebuf.buf[stk->baselen - 1] = '/'; |
|
if (dir->pattern && |
|
dir->pattern->flags & PATTERN_FLAG_NEGATIVE) |
|
dir->pattern = NULL; |
|
if (dir->pattern) { |
|
dir->exclude_stack = stk; |
|
return; |
|
} |
|
} |
|
|
|
/* Try to read per-directory file */ |
|
oidclr(&oid_stat.oid); |
|
oid_stat.valid = 0; |
|
if (dir->exclude_per_dir && |
|
/* |
|
* If we know that no files have been added in |
|
* this directory (i.e. valid_cached_dir() has |
|
* been executed and set untracked->valid) .. |
|
*/ |
|
(!untracked || !untracked->valid || |
|
/* |
|
* .. and .gitignore does not exist before |
|
* (i.e. null exclude_oid). Then we can skip |
|
* loading .gitignore, which would result in |
|
* ENOENT anyway. |
|
*/ |
|
!is_null_oid(&untracked->exclude_oid))) { |
|
/* |
|
* dir->basebuf gets reused by the traversal, but we |
|
* need fname to remain unchanged to ensure the src |
|
* member of each struct path_pattern correctly |
|
* back-references its source file. Other invocations |
|
* of add_pattern_list provide stable strings, so we |
|
* strbuf_detach() and free() here in the caller. |
|
*/ |
|
struct strbuf sb = STRBUF_INIT; |
|
strbuf_addbuf(&sb, &dir->basebuf); |
|
strbuf_addstr(&sb, dir->exclude_per_dir); |
|
pl->src = strbuf_detach(&sb, NULL); |
|
add_patterns(pl->src, pl->src, stk->baselen, pl, istate, |
|
PATTERN_NOFOLLOW, |
|
untracked ? &oid_stat : NULL); |
|
} |
|
/* |
|
* NEEDSWORK: when untracked cache is enabled, prep_exclude() |
|
* will first be called in valid_cached_dir() then maybe many |
|
* times more in last_matching_pattern(). When the cache is |
|
* used, last_matching_pattern() will not be called and |
|
* reading .gitignore content will be a waste. |
|
* |
|
* So when it's called by valid_cached_dir() and we can get |
|
* .gitignore SHA-1 from the index (i.e. .gitignore is not |
|
* modified on work tree), we could delay reading the |
|
* .gitignore content until we absolutely need it in |
|
* last_matching_pattern(). Be careful about ignore rule |
|
* order, though, if you do that. |
|
*/ |
|
if (untracked && |
|
!oideq(&oid_stat.oid, &untracked->exclude_oid)) { |
|
invalidate_gitignore(dir->untracked, untracked); |
|
oidcpy(&untracked->exclude_oid, &oid_stat.oid); |
|
} |
|
dir->exclude_stack = stk; |
|
current = stk->baselen; |
|
} |
|
strbuf_setlen(&dir->basebuf, baselen); |
|
} |
|
|
|
/* |
|
* Loads the exclude lists for the directory containing pathname, then |
|
* scans all exclude lists to determine whether pathname is excluded. |
|
* Returns the exclude_list element which matched, or NULL for |
|
* undecided. |
|
*/ |
|
struct path_pattern *last_matching_pattern(struct dir_struct *dir, |
|
struct index_state *istate, |
|
const char *pathname, |
|
int *dtype_p) |
|
{ |
|
int pathlen = strlen(pathname); |
|
const char *basename = strrchr(pathname, '/'); |
|
basename = (basename) ? basename+1 : pathname; |
|
|
|
prep_exclude(dir, istate, pathname, basename-pathname); |
|
|
|
if (dir->pattern) |
|
return dir->pattern; |
|
|
|
return last_matching_pattern_from_lists(dir, istate, pathname, pathlen, |
|
basename, dtype_p); |
|
} |
|
|
|
/* |
|
* Loads the exclude lists for the directory containing pathname, then |
|
* scans all exclude lists to determine whether pathname is excluded. |
|
* Returns 1 if true, otherwise 0. |
|
*/ |
|
int is_excluded(struct dir_struct *dir, struct index_state *istate, |
|
const char *pathname, int *dtype_p) |
|
{ |
|
struct path_pattern *pattern = |
|
last_matching_pattern(dir, istate, pathname, dtype_p); |
|
if (pattern) |
|
return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1; |
|
return 0; |
|
} |
|
|
|
static struct dir_entry *dir_entry_new(const char *pathname, int len) |
|
{ |
|
struct dir_entry *ent; |
|
|
|
FLEX_ALLOC_MEM(ent, name, pathname, len); |
|
ent->len = len; |
|
return ent; |
|
} |
|
|
|
static struct dir_entry *dir_add_name(struct dir_struct *dir, |
|
struct index_state *istate, |
|
const char *pathname, int len) |
|
{ |
|
if (index_file_exists(istate, pathname, len, ignore_case)) |
|
return NULL; |
|
|
|
ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); |
|
return dir->entries[dir->nr++] = dir_entry_new(pathname, len); |
|
} |
|
|
|
struct dir_entry *dir_add_ignored(struct dir_struct *dir, |
|
struct index_state *istate, |
|
const char *pathname, int len) |
|
{ |
|
if (!index_name_is_other(istate, pathname, len)) |
|
return NULL; |
|
|
|
ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); |
|
return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len); |
|
} |
|
|
|
enum exist_status { |
|
index_nonexistent = 0, |
|
index_directory, |
|
index_gitdir |
|
}; |
|
|
|
/* |
|
* Do not use the alphabetically sorted index to look up |
|
* the directory name; instead, use the case insensitive |
|
* directory hash. |
|
*/ |
|
static enum exist_status directory_exists_in_index_icase(struct index_state *istate, |
|
const char *dirname, int len) |
|
{ |
|
struct cache_entry *ce; |
|
|
|
if (index_dir_exists(istate, dirname, len)) |
|
return index_directory; |
|
|
|
ce = index_file_exists(istate, dirname, len, ignore_case); |
|
if (ce && S_ISGITLINK(ce->ce_mode)) |
|
return index_gitdir; |
|
|
|
return index_nonexistent; |
|
} |
|
|
|
/* |
|
* The index sorts alphabetically by entry name, which |
|
* means that a gitlink sorts as '\0' at the end, while |
|
* a directory (which is defined not as an entry, but as |
|
* the files it contains) will sort with the '/' at the |
|
* end. |
|
*/ |
|
static enum exist_status directory_exists_in_index(struct index_state *istate, |
|
const char *dirname, int len) |
|
{ |
|
int pos; |
|
|
|
if (ignore_case) |
|
return directory_exists_in_index_icase(istate, dirname, len); |
|
|
|
pos = index_name_pos(istate, dirname, len); |
|
if (pos < 0) |
|
pos = -pos-1; |
|
while (pos < istate->cache_nr) { |
|
const struct cache_entry *ce = istate->cache[pos++]; |
|
unsigned char endchar; |
|
|
|
if (strncmp(ce->name, dirname, len)) |
|
break; |
|
endchar = ce->name[len]; |
|
if (endchar > '/') |
|
break; |
|
if (endchar == '/') |
|
return index_directory; |
|
if (!endchar && S_ISGITLINK(ce->ce_mode)) |
|
return index_gitdir; |
|
} |
|
return index_nonexistent; |
|
} |
|
|
|
/* |
|
* When we find a directory when traversing the filesystem, we |
|
* have three distinct cases: |
|
* |
|
* - ignore it |
|
* - see it as a directory |
|
* - recurse into it |
|
* |
|
* and which one we choose depends on a combination of existing |
|
* git index contents and the flags passed into the directory |
|
* traversal routine. |
|
* |
|
* Case 1: If we *already* have entries in the index under that |
|
* directory name, we always recurse into the directory to see |
|
* all the files. |
|
* |
|
* Case 2: If we *already* have that directory name as a gitlink, |
|
* we always continue to see it as a gitlink, regardless of whether |
|
* there is an actual git directory there or not (it might not |
|
* be checked out as a subproject!) |
|
* |
|
* Case 3: if we didn't have it in the index previously, we |
|
* have a few sub-cases: |
|
* |
|
* (a) if DIR_SHOW_OTHER_DIRECTORIES flag is set, we show it as |
|
* just a directory, unless DIR_HIDE_EMPTY_DIRECTORIES is |
|
* also true, in which case we need to check if it contains any |
|
* untracked and / or ignored files. |
|
* (b) if it looks like a git directory and we don't have the |
|
* DIR_NO_GITLINKS flag, then we treat it as a gitlink, and |
|
* show it as a directory. |
|
* (c) otherwise, we recurse into it. |
|
*/ |
|
static enum path_treatment treat_directory(struct dir_struct *dir, |
|
struct index_state *istate, |
|
struct untracked_cache_dir *untracked, |
|
const char *dirname, int len, int baselen, int excluded, |
|
const struct pathspec *pathspec) |
|
{ |
|
/* |
|
* WARNING: From this function, you can return path_recurse or you |
|
* can call read_directory_recursive() (or neither), but |
|
* you CAN'T DO BOTH. |
|
*/ |
|
enum path_treatment state; |
|
int matches_how = 0; |
|
int check_only, stop_early; |
|
int old_ignored_nr, old_untracked_nr; |
|
/* The "len-1" is to strip the final '/' */ |
|
enum exist_status status = directory_exists_in_index(istate, dirname, len-1); |
|
|
|
if (status == index_directory) |
|
return path_recurse; |
|
if (status == index_gitdir) |
|
return path_none; |
|
if (status != index_nonexistent) |
|
BUG("Unhandled value for directory_exists_in_index: %d\n", status); |
|
|
|
/* |
|
* We don't want to descend into paths that don't match the necessary |
|
* patterns. Clearly, if we don't have a pathspec, then we can't check |
|
* for matching patterns. Also, if (excluded) then we know we matched |
|
* the exclusion patterns so as an optimization we can skip checking |
|
* for matching patterns. |
|
*/ |
|
if (pathspec && !excluded) { |
|
matches_how = match_pathspec_with_flags(istate, pathspec, |
|
dirname, len, |
|
0 /* prefix */, |
|
NULL /* seen */, |
|
DO_MATCH_LEADING_PATHSPEC); |
|
if (!matches_how) |
|
return path_none; |
|
} |
|
|
|
|
|
if ((dir->flags & DIR_SKIP_NESTED_GIT) || |
|
!(dir->flags & DIR_NO_GITLINKS)) { |
|
/* |
|
* Determine if `dirname` is a nested repo by confirming that: |
|
* 1) we are in a nonbare repository, and |
|
* 2) `dirname` is not an immediate parent of `the_repository->gitdir`, |
|
* which could occur if the git_dir or worktree location was |
|
* manually configured by the user; see t2205 testcases 1-3 for |
|
* examples where this matters |
|
*/ |
|
int nested_repo; |
|
struct strbuf sb = STRBUF_INIT; |
|
strbuf_addstr(&sb, dirname); |
|
nested_repo = is_nonbare_repository_dir(&sb); |
|
|
|
if (nested_repo) { |
|
char *real_dirname, *real_gitdir; |
|
strbuf_addstr(&sb, ".git"); |
|
real_dirname = real_pathdup(sb.buf, 1); |
|
real_gitdir = real_pathdup(the_repository->gitdir, 1); |
|
|
|
nested_repo = !!strcmp(real_dirname, real_gitdir); |
|
free(real_gitdir); |
|
free(real_dirname); |
|
} |
|
strbuf_release(&sb); |
|
|
|
if (nested_repo) { |
|
if ((dir->flags & DIR_SKIP_NESTED_GIT) || |
|
(matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC)) |
|
return path_none; |
|
return excluded ? path_excluded : path_untracked; |
|
} |
|
} |
|
|
|
if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) { |
|
if (excluded && |
|
(dir->flags & DIR_SHOW_IGNORED_TOO) && |
|
(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) { |
|
|
|
/* |
|
* This is an excluded directory and we are |
|
* showing ignored paths that match an exclude |
|
* pattern. (e.g. show directory as ignored |
|
* only if it matches an exclude pattern). |
|
* This path will either be 'path_excluded` |
|
* (if we are showing empty directories or if |
|
* the directory is not empty), or will be |
|
* 'path_none' (empty directory, and we are |
|
* not showing empty directories). |
|
*/ |
|
if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
|
return path_excluded; |
|
|
|
if (read_directory_recursive(dir, istate, dirname, len, |
|
untracked, 1, 1, pathspec) == path_excluded) |
|
return path_excluded; |
|
|
|
return path_none; |
|
} |
|
return path_recurse; |
|
} |
|
|
|
assert(dir->flags & DIR_SHOW_OTHER_DIRECTORIES); |
|
|
|
/* |
|
* If we have a pathspec which could match something _below_ this |
|
* directory (e.g. when checking 'subdir/' having a pathspec like |
|
* 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we |
|
* need to recurse. |
|
*/ |
|
if (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC) |
|
return path_recurse; |
|
|
|
/* Special cases for where this directory is excluded/ignored */ |
|
if (excluded) { |
|
/* |
|
* If DIR_SHOW_OTHER_DIRECTORIES is set and we're not |
|
* hiding empty directories, there is no need to |
|
* recurse into an ignored directory. |
|
*/ |
|
if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
|
return path_excluded; |
|
|
|
/* |
|
* Even if we are hiding empty directories, we can still avoid |
|
* recursing into ignored directories for DIR_SHOW_IGNORED_TOO |
|
* if DIR_SHOW_IGNORED_TOO_MODE_MATCHING is also set. |
|
*/ |
|
if ((dir->flags & DIR_SHOW_IGNORED_TOO) && |
|
(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)) |
|
return path_excluded; |
|
} |
|
|
|
/* |
|
* Other than the path_recurse case above, we only need to |
|
* recurse into untracked directories if any of the following |
|
* bits is set: |
|
* - DIR_SHOW_IGNORED (because then we need to determine if |
|
* there are ignored entries below) |
|
* - DIR_SHOW_IGNORED_TOO (same as above) |
|
* - DIR_HIDE_EMPTY_DIRECTORIES (because we have to determine if |
|
* the directory is empty) |
|
*/ |
|
if (!excluded && |
|
!(dir->flags & (DIR_SHOW_IGNORED | |
|
DIR_SHOW_IGNORED_TOO | |
|
DIR_HIDE_EMPTY_DIRECTORIES))) { |
|
return path_untracked; |
|
} |
|
|
|
/* |
|
* Even if we don't want to know all the paths under an untracked or |
|
* ignored directory, we may still need to go into the directory to |
|
* determine if it is empty (because with DIR_HIDE_EMPTY_DIRECTORIES, |
|
* an empty directory should be path_none instead of path_excluded or |
|
* path_untracked). |
|
*/ |
|
check_only = ((dir->flags & DIR_HIDE_EMPTY_DIRECTORIES) && |
|
!(dir->flags & DIR_SHOW_IGNORED_TOO)); |
|
|
|
/* |
|
* However, there's another optimization possible as a subset of |
|
* check_only, based on the cases we have to consider: |
|
* A) Directory matches no exclude patterns: |
|
* * Directory is empty => path_none |
|
* * Directory has an untracked file under it => path_untracked |
|
* * Directory has only ignored files under it => path_excluded |
|
* B) Directory matches an exclude pattern: |
|
* * Directory is empty => path_none |
|
* * Directory has an untracked file under it => path_excluded |
|
* * Directory has only ignored files under it => path_excluded |
|
* In case A, we can exit as soon as we've found an untracked |
|
* file but otherwise have to walk all files. In case B, though, |
|
* we can stop at the first file we find under the directory. |
|
*/ |
|
stop_early = check_only && excluded; |
|
|
|
/* |
|
* If /every/ file within an untracked directory is ignored, then |
|
* we want to treat the directory as ignored (for e.g. status |
|
* --porcelain), without listing the individual ignored files |
|
* underneath. To do so, we'll save the current ignored_nr, and |
|
* pop all the ones added after it if it turns out the entire |
|
* directory is ignored. Also, when DIR_SHOW_IGNORED_TOO and |
|
* !DIR_KEEP_UNTRACKED_CONTENTS then we don't want to show |
|
* untracked paths so will need to pop all those off the last |
|
* after we traverse. |
|
*/ |
|
old_ignored_nr = dir->ignored_nr; |
|
old_untracked_nr = dir->nr; |
|
|
|
/* Actually recurse into dirname now, we'll fixup the state later. */ |
|
untracked = lookup_untracked(dir->untracked, untracked, |
|
dirname + baselen, len - baselen); |
|
state = read_directory_recursive(dir, istate, dirname, len, untracked, |
|
check_only, stop_early, pathspec); |
|
|
|
/* There are a variety of reasons we may need to fixup the state... */ |
|
if (state == path_excluded) { |
|
/* state == path_excluded implies all paths under |
|
* dirname were ignored... |
|
* |
|
* if running e.g. `git status --porcelain --ignored=matching`, |
|
* then we want to see the subpaths that are ignored. |
|
* |
|
* if running e.g. just `git status --porcelain`, then |
|
* we just want the directory itself to be listed as ignored |
|
* and not the individual paths underneath. |
|
*/ |
|
int want_ignored_subpaths = |
|
((dir->flags & DIR_SHOW_IGNORED_TOO) && |
|
(dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING)); |
|
|
|
if (want_ignored_subpaths) { |
|
/* |
|
* with --ignored=matching, we want the subpaths |
|
* INSTEAD of the directory itself. |
|
*/ |
|
state = path_none; |
|
} else { |
|
int i; |
|
for (i = old_ignored_nr + 1; i<dir->ignored_nr; ++i) |
|
FREE_AND_NULL(dir->ignored[i]); |
|
dir->ignored_nr = old_ignored_nr; |
|
} |
|
} |
|
|
|
/* |
|
* We may need to ignore some of the untracked paths we found while |
|
* traversing subdirectories. |
|
*/ |
|
if ((dir->flags & DIR_SHOW_IGNORED_TOO) && |
|
!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) { |
|
int i; |
|
for (i = old_untracked_nr + 1; i<dir->nr; ++i) |
|
FREE_AND_NULL(dir->entries[i]); |
|
dir->nr = old_untracked_nr; |
|
} |
|
|
|
/* |
|
* If there is nothing under the current directory and we are not |
|
* hiding empty directories, then we need to report on the |
|
* untracked or ignored status of the directory itself. |
|
*/ |
|
if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES)) |
|
state = excluded ? path_excluded : path_untracked; |
|
|
|
return state; |
|
} |
|
|
|
/* |
|
* This is an inexact early pruning of any recursive directory |
|
* reading - if the path cannot possibly be in the pathspec, |
|
* return true, and we'll skip it early. |
|
*/ |
|
static int simplify_away(const char *path, int pathlen, |
|
const struct pathspec *pathspec) |
|
{ |
|
int i; |
|
|
|
if (!pathspec || !pathspec->nr) |
|
return 0; |
|
|
|
GUARD_PATHSPEC(pathspec, |
|
PATHSPEC_FROMTOP | |
|
PATHSPEC_MAXDEPTH | |
|
PATHSPEC_LITERAL | |
|
PATHSPEC_GLOB | |
|
PATHSPEC_ICASE | |
|
PATHSPEC_EXCLUDE | |
|
PATHSPEC_ATTR); |
|
|
|
for (i = 0; i < pathspec->nr; i++) { |
|
const struct pathspec_item *item = &pathspec->items[i]; |
|
int len = item->nowildcard_len; |
|
|
|
if (len > pathlen) |
|
len = pathlen; |
|
if (!ps_strncmp(item, item->match, path, len)) |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
/* |
|
* This function tells us whether an excluded path matches a |
|
* list of "interesting" pathspecs. That is, whether a path matched |
|
* by any of the pathspecs could possibly be ignored by excluding |
|
* the specified path. This can happen if: |
|
* |
|
* 1. the path is mentioned explicitly in the pathspec |
|
* |
|
* 2. the path is a directory prefix of some element in the |
|
* pathspec |
|
*/ |
|
static int exclude_matches_pathspec(const char *path, int pathlen, |
|
const struct pathspec *pathspec) |
|
{ |
|
int i; |
|
|
|
if (!pathspec || !pathspec->nr) |
|
return 0; |
|
|
|
GUARD_PATHSPEC(pathspec, |
|
PATHSPEC_FROMTOP | |
|
PATHSPEC_MAXDEPTH | |
|
PATHSPEC_LITERAL | |
|
PATHSPEC_GLOB | |
|
PATHSPEC_ICASE | |
|
PATHSPEC_EXCLUDE); |
|
|
|
for (i = 0; i < pathspec->nr; i++) { |
|
const struct pathspec_item *item = &pathspec->items[i]; |
|
int len = item->nowildcard_len; |
|
|
|
if (len == pathlen && |
|
!ps_strncmp(item, item->match, path, pathlen)) |
|
return 1; |
|
if (len > pathlen && |
|
item->match[pathlen] == '/' && |
|
!ps_strncmp(item, item->match, path, pathlen)) |
|
return 1; |
|
} |
|
return 0; |
|
} |
|
|
|
static int get_index_dtype(struct index_state *istate, |
|
const char *path, int len) |
|
{ |
|
int pos; |
|
const struct cache_entry *ce; |
|
|
|
ce = index_file_exists(istate, path, len, 0); |
|
if (ce) { |
|
if (!ce_uptodate(ce)) |
|
return DT_UNKNOWN; |
|
if (S_ISGITLINK(ce->ce_mode)) |
|
return DT_DIR; |
|
/* |
|
* Nobody actually cares about the |
|
* difference between DT_LNK and DT_REG |
|
*/ |
|
return DT_REG; |
|
} |
|
|
|
/* Try to look it up as a directory */ |
|
pos = index_name_pos(istate, path, len); |
|
if (pos >= 0) |
|
return DT_UNKNOWN; |
|
pos = -pos-1; |
|
while (pos < istate->cache_nr) { |
|
ce = istate->cache[pos++]; |
|
if (strncmp(ce->name, path, len)) |
|
break; |
|
if (ce->name[len] > '/') |
|
break; |
|
if (ce->name[len] < '/') |
|
continue; |
|
if (!ce_uptodate(ce)) |
|
break; /* continue? */ |
|
return DT_DIR; |
|
} |
|
return DT_UNKNOWN; |
|
} |
|
|
|
static int resolve_dtype(int dtype, struct index_state *istate, |
|
const char *path, int len) |
|
{ |
|
struct stat st; |
|
|
|
if (dtype != DT_UNKNOWN) |
|
return dtype; |
|
dtype = get_index_dtype(istate, path, len); |
|
if (dtype != DT_UNKNOWN) |
|
return dtype; |
|
if (lstat(path, &st)) |
|
return dtype; |
|
if (S_ISREG(st.st_mode)) |
|
return DT_REG; |
|
if (S_ISDIR(st.st_mode)) |
|
return DT_DIR; |
|
if (S_ISLNK(st.st_mode)) |
|
return DT_LNK; |
|
return dtype; |
|
} |
|
|
|
static enum path_treatment treat_path_fast(struct dir_struct *dir, |
|
struct cached_dir *cdir, |
|
struct index_state *istate, |
|
struct strbuf *path, |
|
int baselen, |
|
const struct pathspec *pathspec) |
|
{ |
|
/* |
|
* WARNING: From this function, you can return path_recurse or you |
|
* can call read_directory_recursive() (or neither), but |
|
* you CAN'T DO BOTH. |
|
*/ |
|
strbuf_setlen(path, baselen); |
|
if (!cdir->ucd) { |
|
strbuf_addstr(path, cdir->file); |
|
return path_untracked; |
|
} |
|
strbuf_addstr(path, cdir->ucd->name); |
|
/* treat_one_path() does this before it calls treat_directory() */ |
|
strbuf_complete(path, '/'); |
|
if (cdir->ucd->check_only) |
|
/* |
|
* check_only is set as a result of treat_directory() getting |
|
* to its bottom. Verify again the same set of directories |
|
* with check_only set. |
|
*/ |
|
return read_directory_recursive(dir, istate, path->buf, path->len, |
|
cdir->ucd, 1, 0, pathspec); |
|
/* |
|
* We get path_recurse in the first run when |
|
* directory_exists_in_index() returns index_nonexistent. We |
|
* are sure that new changes in the index does not impact the |
|
* outcome. Return now. |
|
*/ |
|
return path_recurse; |
|
} |
|
|
|
static enum path_treatment treat_path(struct dir_struct *dir, |
|
struct untracked_cache_dir *untracked, |
|
struct cached_dir *cdir, |
|
struct index_state *istate, |
|
struct strbuf *path, |
|
int baselen, |
|
const struct pathspec *pathspec) |
|
{ |
|
int has_path_in_index, dtype, excluded; |
|
|
|
if (!cdir->d_name) |
|
return treat_path_fast(dir, cdir, istate, path, |
|
baselen, pathspec); |
|
if (is_dot_or_dotdot(cdir->d_name) || !fspathcmp(cdir->d_name, ".git")) |
|
return path_none; |
|
strbuf_setlen(path, baselen); |
|
strbuf_addstr(path, cdir->d_name); |
|
if (simplify_away(path->buf, path->len, pathspec)) |
|
return path_none; |
|
|
|
dtype = resolve_dtype(cdir->d_type, istate, path->buf, path->len); |
|
|
|
/* Always exclude indexed files */ |
|
has_path_in_index = !!index_file_exists(istate, path->buf, path->len, |
|
ignore_case); |
|
if (dtype != DT_DIR && has_path_in_index) |
|
return path_none |