/*
* 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 "dir.h"
#include "refs.h"
struct path_simplify {
int len;
const char *path;
};
static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
int check_only, const struct path_simplify *simplify);
static int get_dtype(struct dirent *de, const char *path, int len);
static int common_prefix(const char **pathspec)
{
const char *path, *slash, *next;
int prefix;
if (!pathspec)
return 0;
path = *pathspec;
slash = strrchr(path, '/');
if (!slash)
return 0;
/*
* The first 'prefix' characters of 'path' are common leading
* path components among the pathspecs we have seen so far,
* including the trailing slash.
*/
prefix = slash - path + 1;
while ((next = *++pathspec) != NULL) {
int len, last_matching_slash = -1;
for (len = 0; len < prefix && next[len] == path[len]; len++)
if (next[len] == '/')
last_matching_slash = len;
if (len == prefix)
continue;
if (last_matching_slash < 0)
return 0;
prefix = last_matching_slash + 1;
}
return prefix;
}
int fill_directory(struct dir_struct *dir, const char **pathspec)
{
const char *path;
int len;
/*
* Calculate common prefix for the pathspec, and
* use that to optimize the directory walk
*/
len = common_prefix(pathspec);
path = "";
if (len)
path = xmemdupz(*pathspec, len);
/* Read the directory and prune it */
read_directory(dir, path, len, pathspec);
return len;
}
/*
* Does 'match' match the given name?
* A match is found if
*
* (1) the 'match' string is leading directory of 'name', or
* (2) the 'match' string is a wildcard and matches 'name', or
* (3) the 'match' string is exactly the same as 'name'.
*
* and the return value tells which case it was.
*
* It returns 0 when there is no match.
*/
static int match_one(const char *match, const char *name, int namelen)
{
int matchlen;
/* If the match was just the prefix, we matched */
if (!*match)
return MATCHED_RECURSIVELY;
for (;;) {
unsigned char c1 = *match;
unsigned char c2 = *name;
if (c1 == '\0' || is_glob_special(c1))
break;
if (c1 != c2)
return 0;
match++;
name++;
namelen--;
}
/*
* If we don't match the matchstring exactly,
* we need to match by fnmatch
*/
matchlen = strlen(match);
if (strncmp(match, name, matchlen))
return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
if (namelen == matchlen)
return MATCHED_EXACTLY;
if (match[matchlen-1] == '/' || name[matchlen] == '/')
return MATCHED_RECURSIVELY;
return 0;
}
/*
* Given a name and a list of pathspecs, see if the name matches
* any of the pathspecs. The caller is also interested in seeing
* all pathspec matches some names it calls this function with
* (otherwise the user could have mistyped the unmatched pathspec),
* and a mark is left in seen[] array for pathspec element that
* actually matched anything.
*/
int match_pathspec(const char **pathspec, const char *name, int namelen,
int prefix, char *seen)
{
int i, retval = 0;
if (!pathspec)
return 1;
name += prefix;
namelen -= prefix;
for (i = 0; pathspec[i] != NULL; i++) {
int how;
const char *match = pathspec[i] + prefix;
if (seen && seen[i] == MATCHED_EXACTLY)
continue;
how = match_one(match, name, namelen);
if (how) {
if (retval < how)
retval = how;
if (seen && seen[i] < how)
seen[i] = how;
}
}
return retval;
}
static int no_wildcard(const char *string)
{
return string[strcspn(string, "*?[{\\")] == '\0';
}
void add_exclude(const char *string, const char *base,
int baselen, struct exclude_list *which)
{
struct exclude *x;
size_t len;
int to_exclude = 1;
int flags = 0;
if (*string == '!') {
to_exclude = 0;
string++;
}
len = strlen(string);
if (len && string[len - 1] == '/') {
char *s;
x = xmalloc(sizeof(*x) + len);
s = (char *)(x+1);
memcpy(s, string, len - 1);
s[len - 1] = '\0';
string = s;
x->pattern = s;
flags = EXC_FLAG_MUSTBEDIR;
} else {
x = xmalloc(sizeof(*x));
x->pattern = string;
}
x->to_exclude = to_exclude;
x->patternlen = strlen(string);
x->base = base;
x->baselen = baselen;
x->flags = flags;
if (!strchr(string, '/'))
x->flags |= EXC_FLAG_NODIR;
if (no_wildcard(string))
x->flags |= EXC_FLAG_NOWILDCARD;
if (*string == '*' && no_wildcard(string+1))
x->flags |= EXC_FLAG_ENDSWITH;
ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
which->excludes[which->nr++] = x;
}
static void *read_skip_worktree_file_from_index(
|