From c082df24539329c2e75395cf378f0a3fe187c028 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sun, 6 Jan 2013 16:58:03 +0000 Subject: dir.c: use a single struct exclude_list per source of excludes Previously each exclude_list could potentially contain patterns from multiple sources. For example dir->exclude_list[EXC_FILE] would typically contain patterns from .git/info/exclude and core.excludesfile, and dir->exclude_list[EXC_DIRS] could contain patterns from multiple per-directory .gitignore files during directory traversal (i.e. when dir->exclude_stack was more than one item deep). We split these composite exclude_lists up into three groups of exclude_lists (EXC_CMDL / EXC_DIRS / EXC_FILE as before), so that each exclude_list now contains patterns from a single source. This will allow us to cleanly track the origin of each pattern simply by adding a src field to struct exclude_list, rather than to struct exclude, which would make memory management of the source string tricky in the EXC_DIRS case where its contents are dynamically generated. Similarly, by moving the filebuf member from struct exclude_stack to struct exclude_list, it allows us to track and subsequently free memory buffers allocated during the parsing of all exclude files, rather than only tracking buffers allocated for files in the EXC_DIRS group. Signed-off-by: Adam Spiers Signed-off-by: Junio C Hamano --- dir.c | 64 +++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 19 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index 41f141c8d6..3a15cb9bc9 100644 --- a/dir.c +++ b/dir.c @@ -411,15 +411,16 @@ void clear_exclude_list(struct exclude_list *el) for (i = 0; i < el->nr; i++) free(el->excludes[i]); free(el->excludes); + free(el->filebuf); el->nr = 0; el->excludes = NULL; + el->filebuf = NULL; } int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen, - char **buf_p, struct exclude_list *el, int check_index) { @@ -460,8 +461,7 @@ int add_excludes_from_file_to_list(const char *fname, close(fd); } - if (buf_p) - *buf_p = buf; + el->filebuf = buf; entry = buf; for (i = 0; i < size; i++) { if (buf[i] == '\n') { @@ -475,10 +475,26 @@ int add_excludes_from_file_to_list(const char *fname, return 0; } +struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type) +{ + struct exclude_list *el; + struct exclude_list_group *group; + + group = &dir->exclude_list_group[group_type]; + ALLOC_GROW(group->el, group->nr + 1, group->alloc); + el = &group->el[group->nr++]; + memset(el, 0, sizeof(*el)); + return el; +} + +/* + * Used to set up core.excludesfile and .git/info/exclude lists. + */ void add_excludes_from_file(struct dir_struct *dir, const char *fname) { - if (add_excludes_from_file_to_list(fname, "", 0, NULL, - &dir->exclude_list[EXC_FILE], 0) < 0) + struct exclude_list *el; + el = add_exclude_list(dir, EXC_FILE); + if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0) die("cannot use %s as an exclude file", fname); } @@ -488,6 +504,7 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname) */ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) { + struct exclude_list_group *group; struct exclude_list *el; struct exclude_stack *stk = NULL; int current; @@ -496,17 +513,20 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX)) return; /* too long a path -- ignore */ - /* Pop the directories that are not the prefix of the path being checked. */ - el = &dir->exclude_list[EXC_DIRS]; + 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, base, stk->baselen)) break; + el = &group->el[dir->exclude_stack->exclude_ix]; dir->exclude_stack = stk->prev; - while (stk->exclude_ix < el->nr) - free(el->excludes[--el->nr]); - free(stk->filebuf); + clear_exclude_list(el); free(stk); + group->nr--; } /* Read from the parent directories and push them down. */ @@ -527,13 +547,14 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) } stk->prev = dir->exclude_stack; stk->baselen = cp - base; - stk->exclude_ix = el->nr; memcpy(dir->basebuf + current, base + current, stk->baselen - current); strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); + el = add_exclude_list(dir, EXC_DIRS); + stk->exclude_ix = group->nr - 1; add_excludes_from_file_to_list(dir->basebuf, dir->basebuf, stk->baselen, - &stk->filebuf, el, 1); + el, 1); dir->exclude_stack = stk; current = stk->baselen; } @@ -679,18 +700,23 @@ static struct exclude *last_exclude_matching(struct dir_struct *dir, int *dtype_p) { int pathlen = strlen(pathname); - int st; + int i, j; + struct exclude_list_group *group; struct exclude *exclude; const char *basename = strrchr(pathname, '/'); basename = (basename) ? basename+1 : pathname; prep_exclude(dir, pathname, basename-pathname); - for (st = EXC_CMDL; st <= EXC_FILE; st++) { - exclude = last_exclude_matching_from_list( - pathname, pathlen, basename, dtype_p, - &dir->exclude_list[st]); - if (exclude) - return exclude; + + for (i = EXC_CMDL; i <= EXC_FILE; i++) { + group = &dir->exclude_list_group[i]; + for (j = group->nr - 1; j >= 0; j--) { + exclude = last_exclude_matching_from_list( + pathname, pathlen, basename, dtype_p, + &group->el[j]); + if (exclude) + return exclude; + } } return NULL; } -- cgit v1.2.3 From c04318e46aae79b8b8df059e2118519d83dfee12 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sun, 6 Jan 2013 16:58:04 +0000 Subject: dir.c: keep track of where patterns came from For exclude patterns read in from files, the filename is stored in the exclude list, and the originating line number is stored in the individual exclude (counting starting at 1). For exclude patterns provided on the command line, a string describing the source of the patterns is stored in the exclude list, and the sequence number assigned to each exclude pattern is negative, with counting starting at -1. So for example the 2nd pattern provided via --exclude would be numbered -2. This allows any future consumers of that data to easily distinguish between exclude patterns from files vs. from the CLI. Signed-off-by: Adam Spiers Signed-off-by: Junio C Hamano --- dir.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index 3a15cb9bc9..d3f462bd15 100644 --- a/dir.c +++ b/dir.c @@ -349,7 +349,7 @@ void parse_exclude_pattern(const char **pattern, } void add_exclude(const char *string, const char *base, - int baselen, struct exclude_list *el) + int baselen, struct exclude_list *el, int srcpos) { struct exclude *x; int patternlen; @@ -373,8 +373,10 @@ void add_exclude(const char *string, const char *base, x->base = base; x->baselen = baselen; x->flags = flags; + x->srcpos = srcpos; ALLOC_GROW(el->excludes, el->nr + 1, el->alloc); el->excludes[el->nr++] = x; + x->el = el; } static void *read_skip_worktree_file_from_index(const char *path, size_t *size) @@ -425,7 +427,7 @@ int add_excludes_from_file_to_list(const char *fname, int check_index) { struct stat st; - int fd, i; + int fd, i, lineno = 1; size_t size = 0; char *buf, *entry; @@ -467,15 +469,17 @@ int add_excludes_from_file_to_list(const char *fname, if (buf[i] == '\n') { if (entry != buf + i && entry[0] != '#') { buf[i - (i && buf[i-1] == '\r')] = 0; - add_exclude(entry, base, baselen, el); + add_exclude(entry, base, baselen, el, lineno); } + lineno++; entry = buf + i + 1; } } return 0; } -struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type) +struct exclude_list *add_exclude_list(struct dir_struct *dir, + int group_type, const char *src) { struct exclude_list *el; struct exclude_list_group *group; @@ -484,6 +488,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type) ALLOC_GROW(group->el, group->nr + 1, group->alloc); el = &group->el[group->nr++]; memset(el, 0, sizeof(*el)); + el->src = src; return el; } @@ -493,7 +498,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type) void add_excludes_from_file(struct dir_struct *dir, const char *fname) { struct exclude_list *el; - el = add_exclude_list(dir, EXC_FILE); + el = add_exclude_list(dir, EXC_FILE, fname); if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0) die("cannot use %s as an exclude file", fname); } @@ -524,6 +529,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) break; el = &group->el[dir->exclude_stack->exclude_ix]; dir->exclude_stack = stk->prev; + free((char *)el->src); /* see strdup() below */ clear_exclude_list(el); free(stk); group->nr--; @@ -550,7 +556,15 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen) memcpy(dir->basebuf + current, base + current, stk->baselen - current); strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); - el = add_exclude_list(dir, EXC_DIRS); + /* + * dir->basebuf gets reused by the traversal, but we + * need fname to remain unchanged to ensure the src + * member of each struct exclude correctly + * back-references its source file. Other invocations + * of add_exclude_list provide stable strings, so we + * strdup() and free() here in the caller. + */ + el = add_exclude_list(dir, EXC_DIRS, strdup(dir->basebuf)); stk->exclude_ix = group->nr - 1; add_excludes_from_file_to_list(dir->basebuf, dir->basebuf, stk->baselen, -- cgit v1.2.3 From 270be8160493eb25ee43eb7db8dda2504343dc65 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sun, 6 Jan 2013 16:58:05 +0000 Subject: dir.c: provide clear_directory() for reclaiming dir_struct memory By the end of a directory traversal, a dir_struct instance will typically contains pointers to various data structures on the heap. clear_directory() provides a convenient way to reclaim that memory. Signed-off-by: Adam Spiers Signed-off-by: Junio C Hamano --- dir.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'dir.c') diff --git a/dir.c b/dir.c index d3f462bd15..46f362ed69 100644 --- a/dir.c +++ b/dir.c @@ -1557,3 +1557,33 @@ void free_pathspec(struct pathspec *pathspec) free(pathspec->items); pathspec->items = NULL; } + +/* + * Frees memory within dir which was allocated for exclude lists and + * the exclude_stack. Does not free dir itself. + */ +void clear_directory(struct dir_struct *dir) +{ + int i, j; + struct exclude_list_group *group; + struct exclude_list *el; + struct exclude_stack *stk; + + for (i = EXC_CMDL; i <= EXC_FILE; i++) { + group = &dir->exclude_list_group[i]; + for (j = 0; j < group->nr; j++) { + el = &group->el[j]; + if (i == EXC_DIRS) + free((char *)el->src); + clear_exclude_list(el); + } + free(group->el); + } + + stk = dir->exclude_stack; + while (stk) { + struct exclude_stack *prev = stk->prev; + free(stk); + stk = prev; + } +} -- cgit v1.2.3 From 52ed1894b04e7664feaee85c0f14360b415a755c Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sun, 6 Jan 2013 16:58:06 +0000 Subject: dir.c: improve docs for match_pathspec() and match_pathspec_depth() Fix a grammatical issue in the description of these functions, and make it more obvious how and why seen[] can be reused across multiple invocations. Signed-off-by: Adam Spiers Signed-off-by: Junio C Hamano --- dir.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index 46f362ed69..547b83f210 100644 --- a/dir.c +++ b/dir.c @@ -167,12 +167,19 @@ static int match_one(const char *match, const char *name, int namelen) } /* - * 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. + * 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. */ int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen) @@ -239,12 +246,19 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix, } /* - * 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. + * 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. */ int match_pathspec_depth(const struct pathspec *ps, const char *name, int namelen, -- cgit v1.2.3