summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c215
1 files changed, 142 insertions, 73 deletions
diff --git a/dir.c b/dir.c
index 3474e67e8f..03c4d21267 100644
--- a/dir.c
+++ b/dir.c
@@ -53,10 +53,15 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
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);
-
-void dir_init(struct dir_struct *dir)
+struct dirent *readdir_skip_dot_and_dotdot(DIR *dirp)
{
- memset(dir, 0, sizeof(*dir));
+ 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)
@@ -73,11 +78,21 @@ 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)
@@ -306,7 +321,7 @@ static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat,
* [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(const struct index_state *istate,
+static int match_pathspec_item(struct index_state *istate,
const struct pathspec_item *item, int prefix,
const char *name, int namelen, unsigned flags)
{
@@ -429,7 +444,7 @@ static int match_pathspec_item(const struct index_state *istate,
* pathspec did not match any names, which could indicate that the
* user mistyped the nth pathspec.
*/
-static int do_match_pathspec(const struct index_state *istate,
+static int do_match_pathspec(struct index_state *istate,
const struct pathspec *ps,
const char *name, int namelen,
int prefix, char *seen,
@@ -500,7 +515,7 @@ static int do_match_pathspec(const struct index_state *istate,
return retval;
}
-static int match_pathspec_with_flags(const struct index_state *istate,
+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)
@@ -516,7 +531,7 @@ static int match_pathspec_with_flags(const struct index_state *istate,
return negative ? 0 : positive;
}
-int match_pathspec(const struct index_state *istate,
+int match_pathspec(struct index_state *istate,
const struct pathspec *ps,
const char *name, int namelen,
int prefix, char *seen, int is_dir)
@@ -529,7 +544,7 @@ int match_pathspec(const struct index_state *istate,
/**
* Check if a submodule is a superset of the pathspec
*/
-int submodule_path_match(const struct index_state *istate,
+int submodule_path_match(struct index_state *istate,
const struct pathspec *ps,
const char *submodule_name,
char *seen)
@@ -767,9 +782,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
translated->pattern = truncated;
translated->patternlen = given->patternlen - 2;
hashmap_entry_init(&translated->ent,
- ignore_case ?
- strihash(translated->pattern) :
- strhash(translated->pattern));
+ fspathhash(translated->pattern));
if (!hashmap_get_entry(&pl->recursive_hashmap,
translated, ent, NULL)) {
@@ -798,9 +811,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
translated->pattern = dup_and_filter_pattern(given->pattern);
translated->patternlen = given->patternlen;
hashmap_entry_init(&translated->ent,
- ignore_case ?
- strihash(translated->pattern) :
- strhash(translated->pattern));
+ fspathhash(translated->pattern));
hashmap_add(&pl->recursive_hashmap, &translated->ent);
@@ -830,10 +841,7 @@ static int hashmap_contains_path(struct hashmap *map,
/* Check straight mapping */
p.pattern = pattern->buf;
p.patternlen = pattern->len;
- hashmap_entry_init(&p.ent,
- ignore_case ?
- strihash(p.pattern) :
- strhash(p.pattern));
+ hashmap_entry_init(&p.ent, fspathhash(p.pattern));
return !!hashmap_get_entry(map, &p, ent, NULL);
}
@@ -892,7 +900,7 @@ void add_pattern(const char *string, const char *base,
add_pattern_to_hashsets(pl, pattern);
}
-static int read_skip_worktree_file_from_index(const struct index_state *istate,
+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)
@@ -1365,7 +1373,7 @@ enum pattern_match_result path_matches_pattern_list(
struct path_pattern *pattern;
struct strbuf parent_pathname = STRBUF_INIT;
int result = NOT_MATCHED;
- const char *slash_pos;
+ size_t slash_pos;
if (!pl->use_cone_patterns) {
pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
@@ -1386,21 +1394,35 @@ enum pattern_match_result path_matches_pattern_list(
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;
}
- slash_pos = strrchr(parent_pathname.buf, '/');
-
- if (slash_pos == parent_pathname.buf) {
+ if (!slash_pos) {
/* include every file in root */
result = MATCHED;
goto done;
}
- strbuf_setlen(&parent_pathname, slash_pos - parent_pathname.buf);
+ strbuf_setlen(&parent_pathname, slash_pos);
if (hashmap_contains_path(&pl->parent_hashmap, &parent_pathname)) {
result = MATCHED;
@@ -1749,13 +1771,13 @@ static enum exist_status directory_exists_in_index(struct index_state *istate,
* Case 3: if we didn't have it in the index previously, we
* have a few sub-cases:
*
- * (a) if "show_other_directories" is true, we show it as
- * just a directory, unless "hide_empty_directories" is
+ * (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
- * 'no_gitlinks' set we treat it as a gitlink, and show it
- * as a directory.
+ * (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,
@@ -1843,7 +1865,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
return path_recurse;
}
- /* This is the "show_other_directories" case */
+ assert(dir->flags & DIR_SHOW_OTHER_DIRECTORIES);
/*
* If we have a pathspec which could match something _below_ this
@@ -1854,27 +1876,42 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
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 immediately above, we only need
- * to recurse into untracked/ignored directories if either of the
- * following bits is set:
- * - DIR_SHOW_IGNORED_TOO (because then we need to determine if
- * there are ignored entries below)
+ * 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 (!(dir->flags & (DIR_SHOW_IGNORED_TOO | DIR_HIDE_EMPTY_DIRECTORIES)))
- return excluded ? path_excluded : path_untracked;
-
- /*
- * ...and even if DIR_SHOW_IGNORED_TOO is set, we can still avoid
- * recursing into ignored directories if the path is excluded and
- * DIR_SHOW_IGNORED_TOO_MODE_MATCHING is also set.
- */
- if (excluded &&
- (dir->flags & DIR_SHOW_IGNORED_TOO) &&
- (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING))
- return path_excluded;
+ 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
@@ -2326,7 +2363,7 @@ static int read_cached_dir(struct cached_dir *cdir)
struct dirent *de;
if (cdir->fdir) {
- de = readdir(cdir->fdir);
+ de = readdir_skip_dot_and_dotdot(cdir->fdir);
if (!de) {
cdir->d_name = NULL;
cdir->d_type = DT_UNKNOWN;
@@ -2440,6 +2477,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))
goto out;
+ dir->visited_directories++;
if (untracked)
untracked->check_only = !!check_only;
@@ -2448,6 +2486,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
/* check how the file or directory should be treated */
state = treat_path(dir, untracked, &cdir, istate, &path,
baselen, pathspec);
+ dir->visited_paths++;
if (state > dir_state)
dir_state = state;
@@ -2760,15 +2799,53 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
return root;
}
+static void emit_traversal_statistics(struct dir_struct *dir,
+ struct repository *repo,
+ const char *path,
+ int path_len)
+{
+ if (!trace2_is_enabled())
+ return;
+
+ if (!path_len) {
+ trace2_data_string("read_directory", repo, "path", "");
+ } else {
+ struct strbuf tmp = STRBUF_INIT;
+ strbuf_add(&tmp, path, path_len);
+ trace2_data_string("read_directory", repo, "path", tmp.buf);
+ strbuf_release(&tmp);
+ }
+
+ trace2_data_intmax("read_directory", repo,
+ "directories-visited", dir->visited_directories);
+ trace2_data_intmax("read_directory", repo,
+ "paths-visited", dir->visited_paths);
+
+ if (!dir->untracked)
+ return;
+ trace2_data_intmax("read_directory", repo,
+ "node-creation", dir->untracked->dir_created);
+ trace2_data_intmax("read_directory", repo,
+ "gitignore-invalidation",
+ dir->untracked->gitignore_invalidated);
+ trace2_data_intmax("read_directory", repo,
+ "directory-invalidation",
+ dir->untracked->dir_invalidated);
+ trace2_data_intmax("read_directory", repo,
+ "opendir", dir->untracked->dir_opened);
+}
+
int read_directory(struct dir_struct *dir, struct index_state *istate,
const char *path, int len, const struct pathspec *pathspec)
{
struct untracked_cache_dir *untracked;
- trace_performance_enter();
+ trace2_region_enter("dir", "read_directory", istate->repo);
+ dir->visited_paths = 0;
+ dir->visited_directories = 0;
if (has_symlink_leading_path(path, len)) {
- trace_performance_leave("read directory %.*s", len, path);
+ trace2_region_leave("dir", "read_directory", istate->repo);
return dir->nr;
}
@@ -2784,23 +2861,15 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
QSORT(dir->entries, dir->nr, cmp_dir_entry);
QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);
- trace_performance_leave("read directory %.*s", len, path);
+ emit_traversal_statistics(dir, istate->repo, path, len);
+
+ trace2_region_leave("dir", "read_directory", istate->repo);
if (dir->untracked) {
static int force_untracked_cache = -1;
- static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
if (force_untracked_cache < 0)
force_untracked_cache =
git_env_bool("GIT_FORCE_UNTRACKED_CACHE", 0);
- trace_printf_key(&trace_untracked_stats,
- "node creation: %u\n"
- "gitignore invalidation: %u\n"
- "directory invalidation: %u\n"
- "opendir: %u\n",
- dir->untracked->dir_created,
- dir->untracked->gitignore_invalidated,
- dir->untracked->dir_invalidated,
- dir->untracked->dir_opened);
if (force_untracked_cache &&
dir->untracked == istate->untracked &&
(dir->untracked->dir_opened ||
@@ -2811,6 +2880,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
FREE_AND_NULL(dir->untracked);
}
}
+
return dir->nr;
}
@@ -2892,11 +2962,9 @@ int is_empty_dir(const char *path)
if (!dir)
return 0;
- while ((e = readdir(dir)) != NULL)
- if (!is_dot_or_dotdot(e->d_name)) {
- ret = 0;
- break;
- }
+ e = readdir_skip_dot_and_dotdot(dir);
+ if (e)
+ ret = 0;
closedir(dir);
return ret;
@@ -2936,10 +3004,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
strbuf_complete(path, '/');
len = path->len;
- while ((e = readdir(dir)) != NULL) {
+ while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL) {
struct stat st;
- if (is_dot_or_dotdot(e->d_name))
- continue;
strbuf_setlen(path, len);
strbuf_addstr(path, e->d_name);
@@ -3050,6 +3116,7 @@ void dir_clear(struct dir_struct *dir)
struct exclude_list_group *group;
struct pattern_list *pl;
struct exclude_stack *stk;
+ struct dir_struct new = DIR_INIT;
for (i = EXC_CMDL; i <= EXC_FILE; i++) {
group = &dir->exclude_list_group[i];
@@ -3077,7 +3144,7 @@ void dir_clear(struct dir_struct *dir)
}
strbuf_release(&dir->basebuf);
- dir_init(dir);
+ memcpy(dir, &new, sizeof(*dir));
}
struct ondisk_untracked_cache {
@@ -3344,7 +3411,7 @@ static void read_oid(size_t pos, void *cb)
rd->data = rd->end + 1;
return;
}
- hashcpy(ud->exclude_oid.hash, rd->data);
+ oidread(&ud->exclude_oid, rd->data);
rd->data += the_hash_algo->rawsz;
}
@@ -3352,7 +3419,7 @@ static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data,
const unsigned char *sha1)
{
stat_data_from_disk(&oid_stat->stat, data);
- hashcpy(oid_stat->oid.hash, sha1);
+ oidread(&oid_stat->oid, sha1);
oid_stat->valid = 1;
}
@@ -3542,6 +3609,8 @@ static void connect_wt_gitdir_in_nested(const char *sub_worktree,
if (repo_read_index(&subrepo) < 0)
die(_("index file corrupt in repo %s"), subrepo.gitdir);
+ /* TODO: audit for interaction with sparse-index. */
+ ensure_full_index(subrepo.index);
for (i = 0; i < subrepo.index->cache_nr; i++) {
const struct cache_entry *ce = subrepo.index->cache[i];
@@ -3556,7 +3625,7 @@ static void connect_wt_gitdir_in_nested(const char *sub_worktree,
*/
i++;
- sub = submodule_from_path(&subrepo, &null_oid, ce->name);
+ sub = submodule_from_path(&subrepo, null_oid(), ce->name);
if (!sub || !is_submodule_active(&subrepo, ce->name))
/* .gitmodules broken or inactive sub */
continue;