Skip to content

Commit ab8db61

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90d21f9 commit ab8db61

File tree

3 files changed

+80
-77
lines changed

3 files changed

+80
-77
lines changed

builtin/check-ignore.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ static const struct option check_ignore_options[] = {
3232
OPT_END()
3333
};
3434

35-
static void output_exclude(const char *path, struct exclude *exclude)
35+
static void output_pattern(const char *path, struct path_pattern *pattern)
3636
{
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) ? "/" : "";
3939
if (!nul_term_line) {
4040
if (!verbose) {
4141
write_name_quoted(path, stdout, '\n');
4242
} 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);
4545
printf(":%d:%s%s%s\t",
46-
exclude->srcpos,
47-
bang, exclude->pattern, slash);
46+
pattern->srcpos,
47+
bang, pattern->pattern, slash);
4848
}
4949
else {
5050
printf("::\t");
@@ -56,11 +56,11 @@ static void output_exclude(const char *path, struct exclude *exclude)
5656
if (!verbose) {
5757
printf("%s%c", path, '\0');
5858
} else {
59-
if (exclude)
59+
if (pattern)
6060
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',
6464
path, '\0');
6565
else
6666
printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
@@ -74,7 +74,7 @@ static int check_ignore(struct dir_struct *dir,
7474
const char *full_path;
7575
char *seen;
7676
int num_ignored = 0, i;
77-
struct exclude *exclude;
77+
struct path_pattern *pattern;
7878
struct pathspec pathspec;
7979

8080
if (!argc) {
@@ -103,15 +103,15 @@ static int check_ignore(struct dir_struct *dir,
103103
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
104104
for (i = 0; i < pathspec.nr; i++) {
105105
full_path = pathspec.items[i].match;
106-
exclude = NULL;
106+
pattern = NULL;
107107
if (!seen[i]) {
108108
int dtype = DT_UNKNOWN;
109-
exclude = last_exclude_matching(dir, &the_index,
109+
pattern = last_exclude_matching(dir, &the_index,
110110
full_path, &dtype);
111111
}
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)
115115
num_ignored++;
116116
}
117117
free(seen);

dir.c

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -602,27 +602,27 @@ void parse_exclude_pattern(const char **pattern,
602602
void add_exclude(const char *string, const char *base,
603603
int baselen, struct exclude_list *el, int srcpos)
604604
{
605-
struct exclude *x;
605+
struct path_pattern *pattern;
606606
int patternlen;
607607
unsigned flags;
608608
int nowildcardlen;
609609

610610
parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
611611
if (flags & EXC_FLAG_MUSTBEDIR) {
612-
FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
612+
FLEXPTR_ALLOC_MEM(pattern, pattern, string, patternlen);
613613
} else {
614-
x = xmalloc(sizeof(*x));
615-
x->pattern = string;
614+
pattern = xmalloc(sizeof(*pattern));
615+
pattern->pattern = string;
616616
}
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;
626626
}
627627

628628
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)
651651
int i;
652652

653653
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);
656656
free(el->filebuf);
657657

658658
memset(el, 0, sizeof(*el));
@@ -1021,51 +1021,54 @@ int match_pathname(const char *pathname, int pathlen,
10211021
* any, determines the fate. Returns the exclude_list element which
10221022
* matched, or NULL for undecided.
10231023
*/
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,
10251025
int pathlen,
10261026
const char *basename,
10271027
int *dtype,
10281028
struct exclude_list *el,
10291029
struct index_state *istate)
10301030
{
1031-
struct exclude *exc = NULL; /* undecided */
1031+
struct path_pattern *res = NULL; /* undecided */
10321032
int i;
10331033

10341034
if (!el->nr)
10351035
return NULL; /* undefined */
10361036

10371037
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;
10411041

1042-
if (x->flags & EXC_FLAG_MUSTBEDIR) {
1042+
if (pattern->flags & EXC_FLAG_MUSTBEDIR) {
10431043
if (*dtype == DT_UNKNOWN)
10441044
*dtype = get_dtype(NULL, istate, pathname, pathlen);
10451045
if (*dtype != DT_DIR)
10461046
continue;
10471047
}
10481048

1049-
if (x->flags & EXC_FLAG_NODIR) {
1049+
if (pattern->flags & EXC_FLAG_NODIR) {
10501050
if (match_basename(basename,
10511051
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;
10551055
break;
10561056
}
10571057
continue;
10581058
}
10591059

1060-
assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
1060+
assert(pattern->baselen == 0 ||
1061+
pattern->base[pattern->baselen - 1] == '/');
10611062
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;
10651068
break;
10661069
}
10671070
}
1068-
return exc;
1071+
return res;
10691072
}
10701073

10711074
/*
@@ -1076,30 +1079,30 @@ int is_excluded_from_list(const char *pathname,
10761079
int pathlen, const char *basename, int *dtype,
10771080
struct exclude_list *el, struct index_state *istate)
10781081
{
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,
10811084
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;
10841087
return -1; /* undecided */
10851088
}
10861089

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)
10911094
{
10921095
int i, j;
10931096
struct exclude_list_group *group;
1094-
struct exclude *exclude;
1097+
struct path_pattern *pattern;
10951098
for (i = EXC_CMDL; i <= EXC_FILE; i++) {
10961099
group = &dir->exclude_list_group[i];
10971100
for (j = group->nr - 1; j >= 0; j--) {
1098-
exclude = last_exclude_matching_from_list(
1101+
pattern = last_exclude_matching_from_list(
10991102
pathname, pathlen, basename, dtype_p,
11001103
&group->el[j], istate);
1101-
if (exclude)
1102-
return exclude;
1104+
if (pattern)
1105+
return pattern;
11031106
}
11041107
}
11051108
return NULL;
@@ -1132,15 +1135,15 @@ static void prep_exclude(struct dir_struct *dir,
11321135
break;
11331136
el = &group->el[dir->exclude_stack->exclude_ix];
11341137
dir->exclude_stack = stk->prev;
1135-
dir->exclude = NULL;
1138+
dir->pattern = NULL;
11361139
free((char *)el->src); /* see strbuf_detach() below */
11371140
clear_exclude_list(el);
11381141
free(stk);
11391142
group->nr--;
11401143
}
11411144

11421145
/* Skip traversing into sub directories if the parent is excluded */
1143-
if (dir->exclude)
1146+
if (dir->pattern)
11441147
return;
11451148

11461149
/*
@@ -1189,15 +1192,15 @@ static void prep_exclude(struct dir_struct *dir,
11891192
if (stk->baselen) {
11901193
int dt = DT_DIR;
11911194
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,
11931196
istate,
11941197
dir->basebuf.buf, stk->baselen - 1,
11951198
dir->basebuf.buf + current, &dt);
11961199
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) {
12011204
dir->exclude_stack = stk;
12021205
return;
12031206
}
@@ -1223,7 +1226,7 @@ static void prep_exclude(struct dir_struct *dir,
12231226
/*
12241227
* dir->basebuf gets reused by the traversal, but we
12251228
* need fname to remain unchanged to ensure the src
1226-
* member of each struct exclude correctly
1229+
* member of each struct path_pattern correctly
12271230
* back-references its source file. Other invocations
12281231
* of add_exclude_list provide stable strings, so we
12291232
* strbuf_detach() and free() here in the caller.
@@ -1266,7 +1269,7 @@ static void prep_exclude(struct dir_struct *dir,
12661269
* Returns the exclude_list element which matched, or NULL for
12671270
* undecided.
12681271
*/
1269-
struct exclude *last_exclude_matching(struct dir_struct *dir,
1272+
struct path_pattern *last_exclude_matching(struct dir_struct *dir,
12701273
struct index_state *istate,
12711274
const char *pathname,
12721275
int *dtype_p)
@@ -1277,8 +1280,8 @@ struct exclude *last_exclude_matching(struct dir_struct *dir,
12771280

12781281
prep_exclude(dir, istate, pathname, basename-pathname);
12791282

1280-
if (dir->exclude)
1281-
return dir->exclude;
1283+
if (dir->pattern)
1284+
return dir->pattern;
12821285

12831286
return last_exclude_matching_from_lists(dir, istate, pathname, pathlen,
12841287
basename, dtype_p);
@@ -1292,10 +1295,10 @@ struct exclude *last_exclude_matching(struct dir_struct *dir,
12921295
int is_excluded(struct dir_struct *dir, struct index_state *istate,
12931296
const char *pathname, int *dtype_p)
12941297
{
1295-
struct exclude *exclude =
1298+
struct path_pattern *pattern =
12961299
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;
12991302
return 0;
13001303
}
13011304

dir.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct dir_entry {
1616
#define EXC_FLAG_MUSTBEDIR 8
1717
#define EXC_FLAG_NEGATIVE 16
1818

19-
struct exclude {
19+
struct path_pattern {
2020
/*
2121
* This allows callers of last_exclude_matching() etc.
2222
* to determine the origin of the matching pattern.
@@ -54,7 +54,7 @@ struct exclude_list {
5454
/* origin of list, e.g. path to filename, or descriptive string */
5555
const char *src;
5656

57-
struct exclude **excludes;
57+
struct path_pattern **patterns;
5858
};
5959

6060
/*
@@ -191,7 +191,7 @@ struct dir_struct {
191191
* matching exclude struct if the directory is excluded.
192192
*/
193193
struct exclude_stack *exclude_stack;
194-
struct exclude *exclude;
194+
struct path_pattern *pattern;
195195
struct strbuf basebuf;
196196

197197
/* Enable untracked file cache if set */
@@ -248,7 +248,7 @@ int match_pathname(const char *, int,
248248
const char *, int,
249249
const char *, int, int, unsigned);
250250

251-
struct exclude *last_exclude_matching(struct dir_struct *dir,
251+
struct path_pattern *last_exclude_matching(struct dir_struct *dir,
252252
struct index_state *istate,
253253
const char *name, int *dtype);
254254

0 commit comments

Comments
 (0)