summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c215
1 files changed, 153 insertions, 62 deletions
diff --git a/dir.c b/dir.c
index 9efcf1eab6..3c54366a17 100644
--- a/dir.c
+++ b/dir.c
@@ -9,6 +9,7 @@
*/
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
+#include "config.h"
#include "dir.h"
#include "attr.h"
#include "refs.h"
@@ -17,6 +18,7 @@
#include "utf8.h"
#include "varint.h"
#include "ewah/ewok.h"
+#include "fsmonitor.h"
/*
* Tells read_directory_recursive how a file or directory should be treated.
@@ -48,10 +50,19 @@ struct cached_dir {
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, const struct pathspec *pathspec);
+ int check_only, int stop_at_first_file, const struct pathspec *pathspec);
static int get_dtype(struct dirent *de, struct index_state *istate,
const char *path, int len);
+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);
@@ -82,13 +93,11 @@ int git_fnmatch(const struct pathspec_item *item,
if (item->magic & PATHSPEC_GLOB)
return wildmatch(pattern, string,
WM_PATHNAME |
- (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
- NULL);
+ (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,
- NULL);
+ item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0);
}
static int fnmatch_icase_mem(const char *pattern, int patternlen,
@@ -112,7 +121,7 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,
if (ignore_case)
flags |= WM_CASEFOLD;
- match_status = wildmatch(use_pat, use_str, flags, NULL);
+ match_status = wildmatch(use_pat, use_str, flags);
strbuf_release(&pat_buf);
strbuf_release(&str_buf);
@@ -752,9 +761,9 @@ static int add_excludes(const char *fname, const char *base, int baselen,
fd = open(fname, O_RDONLY);
if (fd < 0 || fstat(fd, &st) < 0) {
- if (errno != ENOENT)
- warn_on_inaccessible(fname);
- if (0 <= fd)
+ if (fd < 0)
+ warn_on_fopen_errors(fname);
+ else
close(fd);
if (!istate ||
(buf = read_skip_worktree_file_from_index(istate, fname, &size, sha1_stat)) == NULL)
@@ -795,7 +804,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,
(pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
!ce_stage(istate->cache[pos]) &&
ce_uptodate(istate->cache[pos]) &&
- !would_convert_to_git(fname))
+ !would_convert_to_git(istate, fname))
hashcpy(sha1_stat->sha1,
istate->cache[pos]->oid.hash);
else
@@ -1381,10 +1390,34 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
case index_nonexistent:
if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
break;
+ if (exclude &&
+ (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;
+ }
if (!(dir->flags & DIR_NO_GITLINKS)) {
- unsigned char sha1[20];
- if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
- return path_untracked;
+ struct object_id oid;
+ if (resolve_gitlink_ref(dirname, "HEAD", &oid) == 0)
+ return exclude ? path_excluded : path_untracked;
}
return path_recurse;
}
@@ -1396,8 +1429,13 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
untracked = lookup_untracked(dir->untracked, untracked,
dirname + baselen, len - baselen);
+
+ /*
+ * If this is an excluded directory, then we only need to check if
+ * the directory contains any files.
+ */
return read_directory_recursive(dir, istate, dirname, len,
- untracked, 1, pathspec);
+ untracked, 1, exclude, pathspec);
}
/*
@@ -1548,6 +1586,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
{
int exclude;
int has_path_in_index = !!index_file_exists(istate, path->buf, path->len, ignore_case);
+ enum path_treatment path_treatment;
if (dtype == DT_UNKNOWN)
dtype = get_dtype(de, istate, path->buf, path->len);
@@ -1594,8 +1633,23 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
return path_none;
case DT_DIR:
strbuf_addch(path, '/');
- return treat_directory(dir, istate, untracked, path->buf, path->len,
- baselen, exclude, pathspec);
+ path_treatment = treat_directory(dir, istate, untracked,
+ path->buf, path->len,
+ baselen, exclude, pathspec);
+ /*
+ * If 1) we only want to return directories that
+ * match an exclude pattern and 2) this directory does
+ * not match an exclude pattern but all of its
+ * contents are excluded, then indicate that we should
+ * recurse into this directory (instead of marking the
+ * directory itself as an ignored path).
+ */
+ if (!exclude &&
+ path_treatment == path_excluded &&
+ (dir->flags & DIR_SHOW_IGNORED_TOO) &&
+ (dir->flags & DIR_SHOW_IGNORED_TOO_MODE_MATCHING))
+ return path_recurse;
+ return path_treatment;
case DT_REG:
case DT_LNK:
return exclude ? path_excluded : path_untracked;
@@ -1625,7 +1679,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,
* with check_only set.
*/
return read_directory_recursive(dir, istate, path->buf, path->len,
- cdir->ucd, 1, pathspec);
+ cdir->ucd, 1, 0, pathspec);
/*
* We get path_recurse in the first run when
* directory_exists_in_index() returns index_nonexistent. We
@@ -1680,17 +1734,23 @@ static int valid_cached_dir(struct dir_struct *dir,
if (!untracked)
return 0;
- if (stat(path->len ? path->buf : ".", &st)) {
- invalidate_directory(dir->untracked, untracked);
- memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
- return 0;
- }
- if (!untracked->valid ||
- match_stat_data_racy(istate, &untracked->stat_data, &st)) {
- if (untracked->valid)
+ /*
+ * With fsmonitor, we can trust the untracked cache's valid field.
+ */
+ refresh_fsmonitor(istate);
+ if (!(dir->untracked->use_fsmonitor && untracked->valid)) {
+ if (stat(path->len ? path->buf : ".", &st)) {
invalidate_directory(dir->untracked, untracked);
- fill_stat_data(&untracked->stat_data, &st);
- return 0;
+ memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
+ return 0;
+ }
+ if (!untracked->valid ||
+ match_stat_data_racy(istate, &untracked->stat_data, &st)) {
+ if (untracked->valid)
+ invalidate_directory(dir->untracked, untracked);
+ fill_stat_data(&untracked->stat_data, &st);
+ return 0;
+ }
}
if (untracked->check_only != !!check_only) {
@@ -1785,12 +1845,20 @@ static void close_cached_dir(struct cached_dir *cdir)
* Also, we ignore the name ".git" (even if it is not a directory).
* That likely will not change.
*
+ * If 'stop_at_first_file' is specified, 'path_excluded' is returned
+ * to signal that a file was found. This is the least significant value that
+ * indicates that a file was encountered that does not depend on the order of
+ * whether an untracked or exluded path was encountered first.
+ *
* Returns the most significant path_treatment value encountered in the scan.
+ * If 'stop_at_first_file' is specified, `path_excluded` is the most
+ * significant path_treatment value that will be returned.
*/
+
static enum path_treatment read_directory_recursive(struct dir_struct *dir,
struct index_state *istate, const char *base, int baselen,
struct untracked_cache_dir *untracked, int check_only,
- const struct pathspec *pathspec)
+ int stop_at_first_file, const struct pathspec *pathspec)
{
struct cached_dir cdir;
enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1824,12 +1892,34 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
subdir_state =
read_directory_recursive(dir, istate, path.buf,
path.len, ud,
- check_only, pathspec);
+ check_only, stop_at_first_file, pathspec);
if (subdir_state > dir_state)
dir_state = subdir_state;
}
if (check_only) {
+ if (stop_at_first_file) {
+ /*
+ * If stopping at first file, then
+ * signal that a file was found by
+ * returning `path_excluded`. This is
+ * to return a consistent value
+ * regardless of whether an ignored or
+ * excluded file happened to be
+ * encountered 1st.
+ *
+ * In current usage, the
+ * `stop_at_first_file` is passed when
+ * an ancestor directory has matched
+ * an exclude pattern, so any found
+ * files will be excluded.
+ */
+ if (dir_state >= path_excluded) {
+ dir_state = path_excluded;
+ break;
+ }
+ }
+
/* abort early if maximum state has been reached */
if (dir_state == path_untracked) {
if (cdir.fdir)
@@ -2100,7 +2190,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
*/
dir->untracked = NULL;
if (!len || treat_leading_path(dir, istate, path, len, pathspec))
- read_directory_recursive(dir, istate, path, len, untracked, 0, pathspec);
+ read_directory_recursive(dir, istate, path, len, untracked, 0, 0, pathspec);
QSORT(dir->entries, dir->nr, cmp_dir_entry);
QSORT(dir->ignored, dir->ignored_nr, cmp_dir_entry);
@@ -2117,8 +2207,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
for (i = j = 0; j < dir->nr; j++) {
if (i &&
check_dir_entry_contains(dir->entries[i - 1], dir->entries[j])) {
- free(dir->entries[j]);
- dir->entries[j] = NULL;
+ FREE_AND_NULL(dir->entries[j]);
} else {
dir->entries[i++] = dir->entries[j];
}
@@ -2144,8 +2233,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
dir->untracked->dir_invalidated))
istate->cache_changed |= UNTRACKED_CHANGED;
if (dir->untracked != istate->untracked) {
- free(dir->untracked);
- dir->untracked = NULL;
+ FREE_AND_NULL(dir->untracked);
}
}
return dir->nr;
@@ -2238,10 +2326,10 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
int ret = 0, original_len = path->len, len, kept_down = 0;
int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
- unsigned char submodule_head[20];
+ struct object_id submodule_head;
if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
- !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
+ !resolve_gitlink_ref(path->buf, "HEAD", &submodule_head)) {
/* Do not descend and nuke a nested git work tree. */
if (kept_up)
*kept_up = 1;
@@ -2337,7 +2425,7 @@ int remove_path(const char *name)
{
char *slash;
- if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
+ if (unlink(name) && !is_missing_file_error(errno))
return -1;
slash = strrchr(name, '/');
@@ -2392,7 +2480,8 @@ struct ondisk_untracked_cache {
char exclude_per_dir[FLEX_ARRAY];
};
-#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)
+#define ouc_offset(x) offsetof(struct ondisk_untracked_cache, x)
+#define ouc_size(len) (ouc_offset(exclude_per_dir) + len + 1)
struct write_data {
int index; /* number of written untracked_cache_dir */
@@ -2488,8 +2577,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
strbuf_addbuf(out, &untracked->ident);
strbuf_add(out, ouc, ouc_size(len));
- free(ouc);
- ouc = NULL;
+ FREE_AND_NULL(ouc);
if (!untracked->root) {
varint_len = encode_varint(0, varbuf);
@@ -2555,17 +2643,18 @@ struct read_data {
const unsigned char *end;
};
-static void stat_data_from_disk(struct stat_data *to, const struct stat_data *from)
+static void stat_data_from_disk(struct stat_data *to, const unsigned char *data)
{
- to->sd_ctime.sec = get_be32(&from->sd_ctime.sec);
- to->sd_ctime.nsec = get_be32(&from->sd_ctime.nsec);
- to->sd_mtime.sec = get_be32(&from->sd_mtime.sec);
- to->sd_mtime.nsec = get_be32(&from->sd_mtime.nsec);
- to->sd_dev = get_be32(&from->sd_dev);
- to->sd_ino = get_be32(&from->sd_ino);
- to->sd_uid = get_be32(&from->sd_uid);
- to->sd_gid = get_be32(&from->sd_gid);
- to->sd_size = get_be32(&from->sd_size);
+ memcpy(to, data, sizeof(*to));
+ to->sd_ctime.sec = ntohl(to->sd_ctime.sec);
+ to->sd_ctime.nsec = ntohl(to->sd_ctime.nsec);
+ to->sd_mtime.sec = ntohl(to->sd_mtime.sec);
+ to->sd_mtime.nsec = ntohl(to->sd_mtime.nsec);
+ to->sd_dev = ntohl(to->sd_dev);
+ to->sd_ino = ntohl(to->sd_ino);
+ to->sd_uid = ntohl(to->sd_uid);
+ to->sd_gid = ntohl(to->sd_gid);
+ to->sd_size = ntohl(to->sd_size);
}
static int read_one_dir(struct untracked_cache_dir **untracked_,
@@ -2640,7 +2729,7 @@ static void read_stat(size_t pos, void *cb)
rd->data = rd->end + 1;
return;
}
- stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);
+ stat_data_from_disk(&ud->stat_data, rd->data);
rd->data += sizeof(struct stat_data);
ud->valid = 1;
}
@@ -2658,22 +2747,22 @@ static void read_sha1(size_t pos, void *cb)
}
static void load_sha1_stat(struct sha1_stat *sha1_stat,
- const struct stat_data *stat,
+ const unsigned char *data,
const unsigned char *sha1)
{
- stat_data_from_disk(&sha1_stat->stat, stat);
+ stat_data_from_disk(&sha1_stat->stat, data);
hashcpy(sha1_stat->sha1, sha1);
sha1_stat->valid = 1;
}
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
{
- const struct ondisk_untracked_cache *ouc;
struct untracked_cache *uc;
struct read_data rd;
const unsigned char *next = data, *end = (const unsigned char *)data + sz;
const char *ident;
int ident_len, len;
+ const char *exclude_per_dir;
if (sz <= 1 || end[-1] != '\0')
return NULL;
@@ -2685,21 +2774,23 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
ident = (const char *)next;
next += ident_len;
- ouc = (const struct ondisk_untracked_cache *)next;
if (next + ouc_size(0) > end)
return NULL;
uc = xcalloc(1, sizeof(*uc));
strbuf_init(&uc->ident, ident_len);
strbuf_add(&uc->ident, ident, ident_len);
- load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,
- ouc->info_exclude_sha1);
- load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,
- ouc->excludes_file_sha1);
- uc->dir_flags = get_be32(&ouc->dir_flags);
- uc->exclude_per_dir = xstrdup(ouc->exclude_per_dir);
+ load_sha1_stat(&uc->ss_info_exclude,
+ next + ouc_offset(info_exclude_stat),
+ next + ouc_offset(info_exclude_sha1));
+ load_sha1_stat(&uc->ss_excludes_file,
+ next + ouc_offset(excludes_file_stat),
+ next + ouc_offset(excludes_file_sha1));
+ uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
+ exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
+ uc->exclude_per_dir = xstrdup(exclude_per_dir);
/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
- next += ouc_size(strlen(ouc->exclude_per_dir));
+ next += ouc_size(strlen(exclude_per_dir));
if (next >= end)
goto done2;