treewide: rename 'exclude' methods to 'pattern'

The first consumer of pattern-matching filenames was the .gitignore feature. In that context, storing a list of patterns as a 'struct exclude_list' makes sense. However, the sparse-checkout feature then adopted these structures and methods, but with the opposite meaning: these patterns match the files that should be included! It would be clearer to rename this entire library as a "pattern matching" library, and the callers apply exclusion/inclusion logic accordingly based on their needs. This commit renames several methods defined in dir.h to make more sense with the renamed 'struct exclude_list' to 'struct pattern_list' and 'struct exclude' to 'struct path_pattern': * last_exclude_matching() -> last_matching_pattern() * parse_exclude() -> parse_path_pattern() In addition, the word 'exclude' was replaced with 'pattern' in the methods below: * add_exclude_list() * add_excludes_from_file_to_list() * add_excludes_from_file() * add_excludes_from_blob_to_list() * add_exclude() * clear_exclude_list() A few methods with the word "exclude" remain. These will be handled seperately. In particular, the method "is_excluded()" is concretely about the .gitignore file relative to a specific directory. This is the important boundary between library and consumer: is_excluded() cares about .gitignore, but is_excluded() calls last_matching_pattern() to make that decision. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 3, 2019 at 11:04 UTC 65edd96aecdee2cd4d16a7c17ae9f723c3fe61a4
11 files changed +67 -67
Documentation/RelNotes/2.7.1.txt
+1 -1
@@ -10,7 +10,7 @@ Fixes since v2.7
10 setting GIT_WORK_TREE environment themselves.
11
12 * The "exclude_list" structure has the usual "alloc, nr" pair of
13 - fields to be used by ALLOC_GROW(), but clear_exclude_list() forgot
13 + fields to be used by ALLOC_GROW(), but clear_pattern_list() forgot
14 to reset 'alloc' to 0 when it cleared 'nr' to discard the managed
15 array.
16
Documentation/RelNotes/2.8.0.txt
+1 -1
@@ -270,7 +270,7 @@ notes for details).
270 setting GIT_WORK_TREE environment themselves.
271
272 * The "exclude_list" structure has the usual "alloc, nr" pair of
273 - fields to be used by ALLOC_GROW(), but clear_exclude_list() forgot
273 + fields to be used by ALLOC_GROW(), but clear_pattern_list() forgot
274 to reset 'alloc' to 0 when it cleared 'nr' to discard the managed
275 array.
276
Documentation/technical/api-directory-listing.txt
+3 -3
@@ -111,11 +111,11 @@ marked. If you to exclude files, make sure you have loaded index first.
111 * Prepare `struct dir_struct dir` and clear it with `memset(&dir, 0,
112 sizeof(dir))`.
113
114 -* To add single exclude pattern, call `add_exclude_list()` and then
115 - `add_exclude()`.
114 +* To add single exclude pattern, call `add_pattern_list()` and then
115 + `add_pattern()`.
116
117 * To add patterns from a file (e.g. `.git/info/exclude`), call
118 - `add_excludes_from_file()` , and/or set `dir.exclude_per_dir`. A
118 + `add_patterns_from_file()` , and/or set `dir.exclude_per_dir`. A
119 short-hand function `setup_standard_excludes()` can be used to set
120 up the standard set of exclude settings.
121
attr.c
+1 -1
@@ -400,7 +400,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
400 char *p = (char *)&(res->state[num_attr]);
401 memcpy(p, name, namelen);
402 res->u.pat.pattern = p;
403 - parse_exclude_pattern(&res->u.pat.pattern,
403 + parse_path_pattern(&res->u.pat.pattern,
404 &res->u.pat.patternlen,
405 &res->u.pat.flags,
406 &res->u.pat.nowildcardlen);
builtin/check-ignore.c
+1 -1
@@ -106,7 +106,7 @@ static int check_ignore(struct dir_struct *dir,
106 pattern = NULL;
107 if (!seen[i]) {
108 int dtype = DT_UNKNOWN;
109 - pattern = last_exclude_matching(dir, &the_index,
109 + pattern = last_matching_pattern(dir, &the_index,
110 full_path, &dtype);
111 }
112 if (!quiet && (pattern || show_non_matching))
builtin/clean.c
+4 -4
@@ -670,7 +670,7 @@ static int filter_by_patterns_cmd(void)
670 break;
671
672 memset(&dir, 0, sizeof(dir));
673 - pl = add_exclude_list(&dir, EXC_CMDL, "manual exclude");
673 + pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
674 ignore_list = strbuf_split_max(&confirm, ' ', 0);
675
676 for (i = 0; ignore_list[i]; i++) {
@@ -678,7 +678,7 @@ static int filter_by_patterns_cmd(void)
678 if (!ignore_list[i]->len)
679 continue;
680
681 - add_exclude(ignore_list[i]->buf, "", 0, pl, -(i+1));
681 + add_pattern(ignore_list[i]->buf, "", 0, pl, -(i+1));
682 }
683
684 changed = 0;
@@ -957,9 +957,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
957 if (!ignored)
958 setup_standard_excludes(&dir);
959
960 - pl = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
960 + pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
961 for (i = 0; i < exclude_list.nr; i++)
962 - add_exclude(exclude_list.items[i].string, "", 0, pl, -(i+1));
962 + add_pattern(exclude_list.items[i].string, "", 0, pl, -(i+1));
963
964 parse_pathspec(&pathspec, 0,
965 PATHSPEC_PREFER_CWD,
builtin/ls-files.c
+3 -3
@@ -492,7 +492,7 @@ static int option_parse_exclude_from(const struct option *opt,
492 BUG_ON_OPT_NEG(unset);
493
494 exc_given = 1;
495 - add_excludes_from_file(dir, arg);
495 + add_patterns_from_file(dir, arg);
496
497 return 0;
498 }
@@ -594,9 +594,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
594
595 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
596 ls_files_usage, 0);
597 - pl = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
597 + pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
598 for (i = 0; i < exclude_list.nr; i++) {
599 - add_exclude(exclude_list.items[i].string, "", 0, pl, --exclude_args);
599 + add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
600 }
601 if (show_tag || show_valid_bit || show_fsmonitor_bit) {
602 tag_cached = "H ";
dir.c
+39 -39
@@ -561,7 +561,7 @@ int no_wildcard(const char *string)
561 return string[simple_length(string)] == '\0';
562 }
563
564 -void parse_exclude_pattern(const char **pattern,
564 +void parse_path_pattern(const char **pattern,
565 int *patternlen,
566 unsigned *flags,
567 int *nowildcardlen)
@@ -599,7 +599,7 @@ void parse_exclude_pattern(const char **pattern,
599 *patternlen = len;
600 }
601
602 -void add_exclude(const char *string, const char *base,
602 +void add_pattern(const char *string, const char *base,
603 int baselen, struct pattern_list *pl, int srcpos)
604 {
605 struct path_pattern *pattern;
@@ -607,7 +607,7 @@ void add_exclude(const char *string, const char *base,
607 unsigned flags;
608 int nowildcardlen;
609
610 - parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
610 + parse_path_pattern(&string, &patternlen, &flags, &nowildcardlen);
611 if (flags & PATTERN_FLAG_MUSTBEDIR) {
612 FLEXPTR_ALLOC_MEM(pattern, pattern, string, patternlen);
613 } else {
@@ -646,7 +646,7 @@ static int read_skip_worktree_file_from_index(const struct index_state *istate,
646 * Frees memory within pl which was allocated for exclude patterns and
647 * the file buffer. Does not free pl itself.
648 */
649 -void clear_exclude_list(struct pattern_list *pl)
649 +void clear_pattern_list(struct pattern_list *pl)
650 {
651 int i;
652
@@ -762,7 +762,7 @@ static void invalidate_directory(struct untracked_cache *uc,
762 dir->dirs[i]->recurse = 0;
763 }
764
765 -static int add_excludes_from_buffer(char *buf, size_t size,
765 +static int add_patterns_from_buffer(char *buf, size_t size,
766 const char *base, int baselen,
767 struct pattern_list *pl);
768
@@ -772,10 +772,10 @@ static int add_excludes_from_buffer(char *buf, size_t size,
772 * exclude rules in "pl".
773 *
774 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
775 - * stat data from disk (only valid if add_excludes returns zero). If
775 + * stat data from disk (only valid if add_patterns returns zero). If
776 * ss_valid is non-zero, "ss" must contain good value as input.
777 */
778 -static int add_excludes(const char *fname, const char *base, int baselen,
778 +static int add_patterns(const char *fname, const char *base, int baselen,
779 struct pattern_list *pl, struct index_state *istate,
780 struct oid_stat *oid_stat)
781 {
@@ -837,11 +837,11 @@ static int add_excludes(const char *fname, const char *base, int baselen,
837 }
838 }
839
840 - add_excludes_from_buffer(buf, size, base, baselen, pl);
840 + add_patterns_from_buffer(buf, size, base, baselen, pl);
841 return 0;
842 }
843
844 -static int add_excludes_from_buffer(char *buf, size_t size,
844 +static int add_patterns_from_buffer(char *buf, size_t size,
845 const char *base, int baselen,
846 struct pattern_list *pl)
847 {
@@ -860,7 +860,7 @@ static int add_excludes_from_buffer(char *buf, size_t size,
860 if (entry != buf + i && entry[0] != '#') {
861 buf[i - (i && buf[i-1] == '\r')] = 0;
862 trim_trailing_spaces(entry);
863 - add_exclude(entry, base, baselen, pl, lineno);
863 + add_pattern(entry, base, baselen, pl, lineno);
864 }
865 lineno++;
866 entry = buf + i + 1;
@@ -869,14 +869,14 @@ static int add_excludes_from_buffer(char *buf, size_t size,
869 return 0;
870 }
871
872 -int add_excludes_from_file_to_list(const char *fname, const char *base,
872 +int add_patterns_from_file_to_list(const char *fname, const char *base,
873 int baselen, struct pattern_list *pl,
874 struct index_state *istate)
875 {
876 - return add_excludes(fname, base, baselen, pl, istate, NULL);
876 + return add_patterns(fname, base, baselen, pl, istate, NULL);
877 }
878
879 -int add_excludes_from_blob_to_list(
879 +int add_patterns_from_blob_to_list(
880 struct object_id *oid,
881 const char *base, int baselen,
882 struct pattern_list *pl)
@@ -889,11 +889,11 @@ int add_excludes_from_blob_to_list(
889 if (r != 1)
890 return r;
891
892 - add_excludes_from_buffer(buf, size, base, baselen, pl);
892 + add_patterns_from_buffer(buf, size, base, baselen, pl);
893 return 0;
894 }
895
896 -struct pattern_list *add_exclude_list(struct dir_struct *dir,
896 +struct pattern_list *add_pattern_list(struct dir_struct *dir,
897 int group_type, const char *src)
898 {
899 struct pattern_list *pl;
@@ -910,7 +910,7 @@ struct pattern_list *add_exclude_list(struct dir_struct *dir,
910 /*
911 * Used to set up core.excludesfile and .git/info/exclude lists.
912 */
913 -static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
913 +static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname,
914 struct oid_stat *oid_stat)
915 {
916 struct pattern_list *pl;
@@ -921,15 +921,15 @@ static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
921 */
922 if (!dir->untracked)
923 dir->unmanaged_exclude_files++;
924 - pl = add_exclude_list(dir, EXC_FILE, fname);
925 - if (add_excludes(fname, "", 0, pl, NULL, oid_stat) < 0)
924 + pl = add_pattern_list(dir, EXC_FILE, fname);
925 + if (add_patterns(fname, "", 0, pl, NULL, oid_stat) < 0)
926 die(_("cannot use %s as an exclude file"), fname);
927 }
928
929 -void add_excludes_from_file(struct dir_struct *dir, const char *fname)
929 +void add_patterns_from_file(struct dir_struct *dir, const char *fname)
930 {
931 dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
932 - add_excludes_from_file_1(dir, fname, NULL);
932 + add_patterns_from_file_1(dir, fname, NULL);
933 }
934
935 int match_basename(const char *basename, int basenamelen,
@@ -1021,7 +1021,7 @@ int match_pathname(const char *pathname, int pathlen,
1021 * any, determines the fate. Returns the exclude_list element which
1022 * matched, or NULL for undecided.
1023 */
1024 -static struct path_pattern *last_exclude_matching_from_list(const char *pathname,
1024 +static struct path_pattern *last_matching_pattern_from_list(const char *pathname,
1025 int pathlen,
1026 const char *basename,
1027 int *dtype,
@@ -1080,14 +1080,14 @@ int is_excluded_from_list(const char *pathname,
1080 struct pattern_list *pl, struct index_state *istate)
1081 {
1082 struct path_pattern *pattern;
1083 - pattern = last_exclude_matching_from_list(pathname, pathlen, basename,
1083 + pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
1084 dtype, pl, istate);
1085 if (pattern)
1086 return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
1087 return -1; /* undecided */
1088 }
1089
1090 -static struct path_pattern *last_exclude_matching_from_lists(
1090 +static struct path_pattern *last_matching_pattern_from_lists(
1091 struct dir_struct *dir, struct index_state *istate,
1092 const char *pathname, int pathlen,
1093 const char *basename, int *dtype_p)
@@ -1098,7 +1098,7 @@ static struct path_pattern *last_exclude_matching_from_lists(
1098 for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1099 group = &dir->exclude_list_group[i];
1100 for (j = group->nr - 1; j >= 0; j--) {
1101 - pattern = last_exclude_matching_from_list(
1101 + pattern = last_matching_pattern_from_list(
1102 pathname, pathlen, basename, dtype_p,
1103 &group->pl[j], istate);
1104 if (pattern)
@@ -1137,7 +1137,7 @@ static void prep_exclude(struct dir_struct *dir,
1137 dir->exclude_stack = stk->prev;
1138 dir->pattern = NULL;
1139 free((char *)pl->src); /* see strbuf_detach() below */
1140 - clear_exclude_list(pl);
1140 + clear_pattern_list(pl);
1141 free(stk);
1142 group->nr--;
1143 }
@@ -1184,7 +1184,7 @@ static void prep_exclude(struct dir_struct *dir,
1184 stk->baselen = cp - base;
1185 stk->exclude_ix = group->nr;
1186 stk->ucd = untracked;
1187 - pl = add_exclude_list(dir, EXC_DIRS, NULL);
1187 + pl = add_pattern_list(dir, EXC_DIRS, NULL);
1188 strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
1189 assert(stk->baselen == dir->basebuf.len);
1190
@@ -1192,7 +1192,7 @@ static void prep_exclude(struct dir_struct *dir,
1192 if (stk->baselen) {
1193 int dt = DT_DIR;
1194 dir->basebuf.buf[stk->baselen - 1] = 0;
1195 - dir->pattern = last_exclude_matching_from_lists(dir,
1195 + dir->pattern = last_matching_pattern_from_lists(dir,
1196 istate,
1197 dir->basebuf.buf, stk->baselen - 1,
1198 dir->basebuf.buf + current, &dt);
@@ -1228,28 +1228,28 @@ static void prep_exclude(struct dir_struct *dir,
1228 * need fname to remain unchanged to ensure the src
1229 * member of each struct path_pattern correctly
1230 * back-references its source file. Other invocations
1231 - * of add_exclude_list provide stable strings, so we
1231 + * of add_pattern_list provide stable strings, so we
1232 * strbuf_detach() and free() here in the caller.
1233 */
1234 struct strbuf sb = STRBUF_INIT;
1235 strbuf_addbuf(&sb, &dir->basebuf);
1236 strbuf_addstr(&sb, dir->exclude_per_dir);
1237 pl->src = strbuf_detach(&sb, NULL);
1238 - add_excludes(pl->src, pl->src, stk->baselen, pl, istate,
1238 + add_patterns(pl->src, pl->src, stk->baselen, pl, istate,
1239 untracked ? &oid_stat : NULL);
1240 }
1241 /*
1242 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1243 * will first be called in valid_cached_dir() then maybe many
1244 - * times more in last_exclude_matching(). When the cache is
1245 - * used, last_exclude_matching() will not be called and
1244 + * times more in last_matching_pattern(). When the cache is
1245 + * used, last_matching_pattern() will not be called and
1246 * reading .gitignore content will be a waste.
1247 *
1248 * So when it's called by valid_cached_dir() and we can get
1249 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1250 * modified on work tree), we could delay reading the
1251 * .gitignore content until we absolutely need it in
1252 - * last_exclude_matching(). Be careful about ignore rule
1252 + * last_matching_pattern(). Be careful about ignore rule
1253 * order, though, if you do that.
1254 */
1255 if (untracked &&
@@ -1269,7 +1269,7 @@ static void prep_exclude(struct dir_struct *dir,
1269 * Returns the exclude_list element which matched, or NULL for
1270 * undecided.
1271 */
1272 -struct path_pattern *last_exclude_matching(struct dir_struct *dir,
1272 +struct path_pattern *last_matching_pattern(struct dir_struct *dir,
1273 struct index_state *istate,
1274 const char *pathname,
1275 int *dtype_p)
@@ -1283,7 +1283,7 @@ struct path_pattern *last_exclude_matching(struct dir_struct *dir,
1283 if (dir->pattern)
1284 return dir->pattern;
1285
1286 - return last_exclude_matching_from_lists(dir, istate, pathname, pathlen,
1286 + return last_matching_pattern_from_lists(dir, istate, pathname, pathlen,
1287 basename, dtype_p);
1288 }
1289
@@ -1296,7 +1296,7 @@ int is_excluded(struct dir_struct *dir, struct index_state *istate,
1296 const char *pathname, int *dtype_p)
1297 {
1298 struct path_pattern *pattern =
1299 - last_exclude_matching(dir, istate, pathname, dtype_p);
1299 + last_matching_pattern(dir, istate, pathname, dtype_p);
1300 if (pattern)
1301 return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
1302 return 0;
@@ -1811,7 +1811,7 @@ static int valid_cached_dir(struct dir_struct *dir,
1811
1812 /*
1813 * prep_exclude will be called eventually on this directory,
1814 - * but it's called much later in last_exclude_matching(). We
1814 + * but it's called much later in last_matching_pattern(). We
1815 * need it now to determine the validity of the cache for this
1816 * path. The next calls will be nearly no-op, the way
1817 * prep_exclude() is designed.
@@ -2491,14 +2491,14 @@ void setup_standard_excludes(struct dir_struct *dir)
2491 if (!excludes_file)
2492 excludes_file = xdg_config_home("ignore");
2493 if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
2494 - add_excludes_from_file_1(dir, excludes_file,
2494 + add_patterns_from_file_1(dir, excludes_file,
2495 dir->untracked ? &dir->ss_excludes_file : NULL);
2496
2497 /* per repository user preference */
2498 if (startup_info->have_repository) {
2499 const char *path = git_path_info_exclude();
2500 if (!access_or_warn(path, R_OK, 0))
2501 - add_excludes_from_file_1(dir, path,
2501 + add_patterns_from_file_1(dir, path,
2502 dir->untracked ? &dir->ss_info_exclude : NULL);
2503 }
2504 }
@@ -2539,7 +2539,7 @@ void clear_directory(struct dir_struct *dir)
2539 pl = &group->pl[j];
2540 if (i == EXC_DIRS)
2541 free((char *)pl->src);
2542 - clear_exclude_list(pl);
2542 + clear_pattern_list(pl);
2543 }
2544 free(group->pl);
2545 }
dir.h
+11 -11
@@ -18,7 +18,7 @@ struct dir_entry {
18
19 struct path_pattern {
20 /*
21 - * This allows callers of last_exclude_matching() etc.
21 + * This allows callers of last_matching_pattern() etc.
22 * to determine the origin of the matching pattern.
23 */
24 struct pattern_list *pl;
@@ -248,26 +248,26 @@ int match_pathname(const char *, int,
248 const char *, int,
249 const char *, int, int, unsigned);
250
251 -struct path_pattern *last_exclude_matching(struct dir_struct *dir,
252 - struct index_state *istate,
253 - const char *name, int *dtype);
251 +struct path_pattern *last_matching_pattern(struct dir_struct *dir,
252 + struct index_state *istate,
253 + const char *name, int *dtype);
254
255 int is_excluded(struct dir_struct *dir,
256 struct index_state *istate,
257 const char *name, int *dtype);
258
259 -struct pattern_list *add_exclude_list(struct dir_struct *dir,
259 +struct pattern_list *add_pattern_list(struct dir_struct *dir,
260 int group_type, const char *src);
261 -int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
261 +int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
262 struct pattern_list *pl, struct index_state *istate);
263 -void add_excludes_from_file(struct dir_struct *, const char *fname);
264 -int add_excludes_from_blob_to_list(struct object_id *oid,
263 +void add_patterns_from_file(struct dir_struct *, const char *fname);
264 +int add_patterns_from_blob_to_list(struct object_id *oid,
265 const char *base, int baselen,
266 struct pattern_list *pl);
267 -void parse_exclude_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
268 -void add_exclude(const char *string, const char *base,
267 +void parse_path_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
268 +void add_pattern(const char *string, const char *base,
269 int baselen, struct pattern_list *pl, int srcpos);
270 -void clear_exclude_list(struct pattern_list *pl);
270 +void clear_pattern_list(struct pattern_list *pl);
271 void clear_directory(struct dir_struct *dir);
272
273 int repo_file_exists(struct repository *repo, const char *path);
list-objects-filter.c
+1 -1
@@ -482,7 +482,7 @@ static void filter_sparse_oid__init(
482 struct filter *filter)
483 {
484 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
485 - if (add_excludes_from_blob_to_list(filter_options->sparse_oid_value,
485 + if (add_patterns_from_blob_to_list(filter_options->sparse_oid_value,
486 NULL, 0, &d->pl) < 0)
487 die("could not load filter specification");
488
unpack-trees.c
+2 -2
@@ -1464,7 +1464,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1464 o->skip_sparse_checkout = 1;
1465 if (!o->skip_sparse_checkout) {
1466 char *sparse = git_pathdup("info/sparse-checkout");
1467 - if (add_excludes_from_file_to_list(sparse, "", 0, &pl, NULL) < 0)
1467 + if (add_patterns_from_file_to_list(sparse, "", 0, &pl, NULL) < 0)
1468 o->skip_sparse_checkout = 1;
1469 else
1470 o->pl = &pl;
@@ -1631,7 +1631,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1631
1632 done:
1633 trace_performance_leave("unpack_trees");
1634 - clear_exclude_list(&pl);
1634 + clear_pattern_list(&pl);
1635 return ret;
1636
1637 return_failed: