/*
* 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);
/* helper string functions with support for the ignore_case flag */
int strcmp_icase(const char *a, const char *b)
{
return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
}
int strncmp_icase(const char *a, const char *b, size_t count)
{
return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
}
int fnmatch_icase(const char *pattern, const char *string, int flags)
{
return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
}
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;
if (ignore_case) {
for (;;) {
unsigned char c1 = tolower(*match);
unsigned char c2 = tolower(*name);
if (c1 == '\0' || is_glob_special(c1))
break;
if (c1 != c2)
return 0;
match++;
name++;
namelen--;
}
} else {
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_icase(match, name, matchlen))
return !fnmatch_icase(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)
|