treewide: rename 'struct exclude' to 'struct path_pattern'

The first consumer of pattern-matching filenames was the .gitignore feature. In that context, storing a list of patterns as a list of 'struct exclude' items 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 'struct exclude' to 'struct path_pattern' and renames several variable names to match. 'struct pattern' was already taken by attr.c, and this more completely describes that the patterns are specific to file paths. 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 ab8db61390afd803ddd9778f84ba4c28dfa975c6
3 files changed +80 -77
builtin/check-ignore.c
+17 -17
@@ -32,19 +32,19 @@ static const struct option check_ignore_options[] = {
32 OPT_END()
33 };
34
35 -static void output_exclude(const char *path, struct exclude *exclude)
35 +static void output_pattern(const char *path, struct path_pattern *pattern)
36 {
37 - char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
38 - char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
37 + char *bang = (pattern && pattern->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
38 + char *slash = (pattern && pattern->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
39 if (!nul_term_line) {
40 if (!verbose) {
41 write_name_quoted(path, stdout, '\n');
42 } else {
43 - if (exclude) {
44 - quote_c_style(exclude->el->src, NULL, stdout, 0);
43 + if (pattern) {
44 + quote_c_style(pattern->el->src, NULL, stdout, 0);
45 printf(":%d:%s%s%s\t",
46 - exclude->srcpos,
47 - bang, exclude->pattern, slash);
46 + pattern->srcpos,
47 + bang, pattern->pattern, slash);
48 }
49 else {
50 printf("::\t");
@@ -56,11 +56,11 @@ static void output_exclude(const char *path, struct exclude *exclude)
56 if (!verbose) {
57 printf("%s%c", path, '\0');
58 } else {
59 - if (exclude)
59 + if (pattern)
60 printf("%s%c%d%c%s%s%s%c%s%c",
61 - exclude->el->src, '\0',
62 - exclude->srcpos, '\0',
63 - bang, exclude->pattern, slash, '\0',
61 + pattern->el->src, '\0',
62 + pattern->srcpos, '\0',
63 + bang, pattern->pattern, slash, '\0',
64 path, '\0');
65 else
66 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
@@ -74,7 +74,7 @@ static int check_ignore(struct dir_struct *dir,
74 const char *full_path;
75 char *seen;
76 int num_ignored = 0, i;
77 - struct exclude *exclude;
77 + struct path_pattern *pattern;
78 struct pathspec pathspec;
79
80 if (!argc) {
@@ -103,15 +103,15 @@ static int check_ignore(struct dir_struct *dir,
103 seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
104 for (i = 0; i < pathspec.nr; i++) {
105 full_path = pathspec.items[i].match;
106 - exclude = NULL;
106 + pattern = NULL;
107 if (!seen[i]) {
108 int dtype = DT_UNKNOWN;
109 - exclude = last_exclude_matching(dir, &the_index,
109 + pattern = last_exclude_matching(dir, &the_index,
110 full_path, &dtype);
111 }
112 - if (!quiet && (exclude || show_non_matching))
113 - output_exclude(pathspec.items[i].original, exclude);
114 - if (exclude)
112 + if (!quiet && (pattern || show_non_matching))
113 + output_pattern(pathspec.items[i].original, pattern);
114 + if (pattern)
115 num_ignored++;
116 }
117 free(seen);
dir.c
+59 -56
@@ -602,27 +602,27 @@ void parse_exclude_pattern(const char **pattern,
602 void add_exclude(const char *string, const char *base,
603 int baselen, struct exclude_list *el, int srcpos)
604 {
605 - struct exclude *x;
605 + struct path_pattern *pattern;
606 int patternlen;
607 unsigned flags;
608 int nowildcardlen;
609
610 parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
611 if (flags & EXC_FLAG_MUSTBEDIR) {
612 - FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
612 + FLEXPTR_ALLOC_MEM(pattern, pattern, string, patternlen);
613 } else {
614 - x = xmalloc(sizeof(*x));
615 - x->pattern = string;
614 + pattern = xmalloc(sizeof(*pattern));
615 + pattern->pattern = string;
616 }
617 - x->patternlen = patternlen;
618 - x->nowildcardlen = nowildcardlen;
619 - x->base = base;
620 - x->baselen = baselen;
621 - x->flags = flags;
622 - x->srcpos = srcpos;
623 - ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
624 - el->excludes[el->nr++] = x;
625 - x->el = el;
617 + pattern->patternlen = patternlen;
618 + pattern->nowildcardlen = nowildcardlen;
619 + pattern->base = base;
620 + pattern->baselen = baselen;
621 + pattern->flags = flags;
622 + pattern->srcpos = srcpos;
623 + ALLOC_GROW(el->patterns, el->nr + 1, el->alloc);
624 + el->patterns[el->nr++] = pattern;
625 + pattern->el = el;
626 }
627
628 static int read_skip_worktree_file_from_index(const struct index_state *istate,
@@ -651,8 +651,8 @@ void clear_exclude_list(struct exclude_list *el)
651 int i;
652
653 for (i = 0; i < el->nr; i++)
654 - free(el->excludes[i]);
655 - free(el->excludes);
654 + free(el->patterns[i]);
655 + free(el->patterns);
656 free(el->filebuf);
657
658 memset(el, 0, sizeof(*el));
@@ -1021,51 +1021,54 @@ 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 exclude *last_exclude_matching_from_list(const char *pathname,
1024 +static struct path_pattern *last_exclude_matching_from_list(const char *pathname,
1025 int pathlen,
1026 const char *basename,
1027 int *dtype,
1028 struct exclude_list *el,
1029 struct index_state *istate)
1030 {
1031 - struct exclude *exc = NULL; /* undecided */
1031 + struct path_pattern *res = NULL; /* undecided */
1032 int i;
1033
1034 if (!el->nr)
1035 return NULL; /* undefined */
1036
1037 for (i = el->nr - 1; 0 <= i; i--) {
1038 - struct exclude *x = el->excludes[i];
1039 - const char *exclude = x->pattern;
1040 - int prefix = x->nowildcardlen;
1038 + struct path_pattern *pattern = el->patterns[i];
1039 + const char *exclude = pattern->pattern;
1040 + int prefix = pattern->nowildcardlen;
1041
1042 - if (x->flags & EXC_FLAG_MUSTBEDIR) {
1042 + if (pattern->flags & EXC_FLAG_MUSTBEDIR) {
1043 if (*dtype == DT_UNKNOWN)
1044 *dtype = get_dtype(NULL, istate, pathname, pathlen);
1045 if (*dtype != DT_DIR)
1046 continue;
1047 }
1048
1049 - if (x->flags & EXC_FLAG_NODIR) {
1049 + if (pattern->flags & EXC_FLAG_NODIR) {
1050 if (match_basename(basename,
1051 pathlen - (basename - pathname),
1052 - exclude, prefix, x->patternlen,
1053 - x->flags)) {
1054 - exc = x;
1052 + exclude, prefix, pattern->patternlen,
1053 + pattern->flags)) {
1054 + res = pattern;
1055 break;
1056 }
1057 continue;
1058 }
1059
1060 - assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
1060 + assert(pattern->baselen == 0 ||
1061 + pattern->base[pattern->baselen - 1] == '/');
1062 if (match_pathname(pathname, pathlen,
1062 - x->base, x->baselen ? x->baselen - 1 : 0,
1063 - exclude, prefix, x->patternlen, x->flags)) {
1064 - exc = x;
1063 + pattern->base,
1064 + pattern->baselen ? pattern->baselen - 1 : 0,
1065 + exclude, prefix, pattern->patternlen,
1066 + pattern->flags)) {
1067 + res = pattern;
1068 break;
1069 }
1070 }
1068 - return exc;
1071 + return res;
1072 }
1073
1074 /*
@@ -1076,30 +1079,30 @@ int is_excluded_from_list(const char *pathname,
1079 int pathlen, const char *basename, int *dtype,
1080 struct exclude_list *el, struct index_state *istate)
1081 {
1079 - struct exclude *exclude;
1080 - exclude = last_exclude_matching_from_list(pathname, pathlen, basename,
1082 + struct path_pattern *pattern;
1083 + pattern = last_exclude_matching_from_list(pathname, pathlen, basename,
1084 dtype, el, istate);
1082 - if (exclude)
1083 - return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1085 + if (pattern)
1086 + return pattern->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1087 return -1; /* undecided */
1088 }
1089
1087 -static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
1088 - struct index_state *istate,
1089 - const char *pathname, int pathlen, const char *basename,
1090 - int *dtype_p)
1090 +static struct path_pattern *last_exclude_matching_from_lists(
1091 + struct dir_struct *dir, struct index_state *istate,
1092 + const char *pathname, int pathlen,
1093 + const char *basename, int *dtype_p)
1094 {
1095 int i, j;
1096 struct exclude_list_group *group;
1094 - struct exclude *exclude;
1097 + struct path_pattern *pattern;
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--) {
1098 - exclude = last_exclude_matching_from_list(
1101 + pattern = last_exclude_matching_from_list(
1102 pathname, pathlen, basename, dtype_p,
1103 &group->el[j], istate);
1101 - if (exclude)
1102 - return exclude;
1104 + if (pattern)
1105 + return pattern;
1106 }
1107 }
1108 return NULL;
@@ -1132,7 +1135,7 @@ static void prep_exclude(struct dir_struct *dir,
1135 break;
1136 el = &group->el[dir->exclude_stack->exclude_ix];
1137 dir->exclude_stack = stk->prev;
1135 - dir->exclude = NULL;
1138 + dir->pattern = NULL;
1139 free((char *)el->src); /* see strbuf_detach() below */
1140 clear_exclude_list(el);
1141 free(stk);
@@ -1140,7 +1143,7 @@ static void prep_exclude(struct dir_struct *dir,
1143 }
1144
1145 /* Skip traversing into sub directories if the parent is excluded */
1143 - if (dir->exclude)
1146 + if (dir->pattern)
1147 return;
1148
1149 /*
@@ -1189,15 +1192,15 @@ 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;
1192 - dir->exclude = last_exclude_matching_from_lists(dir,
1195 + dir->pattern = last_exclude_matching_from_lists(dir,
1196 istate,
1197 dir->basebuf.buf, stk->baselen - 1,
1198 dir->basebuf.buf + current, &dt);
1199 dir->basebuf.buf[stk->baselen - 1] = '/';
1197 - if (dir->exclude &&
1198 - dir->exclude->flags & EXC_FLAG_NEGATIVE)
1199 - dir->exclude = NULL;
1200 - if (dir->exclude) {
1200 + if (dir->pattern &&
1201 + dir->pattern->flags & EXC_FLAG_NEGATIVE)
1202 + dir->pattern = NULL;
1203 + if (dir->pattern) {
1204 dir->exclude_stack = stk;
1205 return;
1206 }
@@ -1223,7 +1226,7 @@ static void prep_exclude(struct dir_struct *dir,
1226 /*
1227 * dir->basebuf gets reused by the traversal, but we
1228 * need fname to remain unchanged to ensure the src
1226 - * member of each struct exclude correctly
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
1232 * strbuf_detach() and free() here in the caller.
@@ -1266,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 */
1269 -struct exclude *last_exclude_matching(struct dir_struct *dir,
1272 +struct path_pattern *last_exclude_matching(struct dir_struct *dir,
1273 struct index_state *istate,
1274 const char *pathname,
1275 int *dtype_p)
@@ -1277,8 +1280,8 @@ struct exclude *last_exclude_matching(struct dir_struct *dir,
1280
1281 prep_exclude(dir, istate, pathname, basename-pathname);
1282
1280 - if (dir->exclude)
1281 - return dir->exclude;
1283 + if (dir->pattern)
1284 + return dir->pattern;
1285
1286 return last_exclude_matching_from_lists(dir, istate, pathname, pathlen,
1287 basename, dtype_p);
@@ -1292,10 +1295,10 @@ struct exclude *last_exclude_matching(struct dir_struct *dir,
1295 int is_excluded(struct dir_struct *dir, struct index_state *istate,
1296 const char *pathname, int *dtype_p)
1297 {
1295 - struct exclude *exclude =
1298 + struct path_pattern *pattern =
1299 last_exclude_matching(dir, istate, pathname, dtype_p);
1297 - if (exclude)
1298 - return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1300 + if (pattern)
1301 + return pattern->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1302 return 0;
1303 }
1304
dir.h
+4 -4
@@ -16,7 +16,7 @@ struct dir_entry {
16 #define EXC_FLAG_MUSTBEDIR 8
17 #define EXC_FLAG_NEGATIVE 16
18
19 -struct exclude {
19 +struct path_pattern {
20 /*
21 * This allows callers of last_exclude_matching() etc.
22 * to determine the origin of the matching pattern.
@@ -54,7 +54,7 @@ struct exclude_list {
54 /* origin of list, e.g. path to filename, or descriptive string */
55 const char *src;
56
57 - struct exclude **excludes;
57 + struct path_pattern **patterns;
58 };
59
60 /*
@@ -191,7 +191,7 @@ struct dir_struct {
191 * matching exclude struct if the directory is excluded.
192 */
193 struct exclude_stack *exclude_stack;
194 - struct exclude *exclude;
194 + struct path_pattern *pattern;
195 struct strbuf basebuf;
196
197 /* Enable untracked file cache if set */
@@ -248,7 +248,7 @@ int match_pathname(const char *, int,
248 const char *, int,
249 const char *, int, int, unsigned);
250
251 -struct exclude *last_exclude_matching(struct dir_struct *dir,
251 +struct path_pattern *last_exclude_matching(struct dir_struct *dir,
252 struct index_state *istate,
253 const char *name, int *dtype);
254