1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "ignore.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
struct ignore_rule {
char base[IGNORE_PATH_MAX];
char pattern[1024];
bool negated;
bool dir_only;
bool anchored;
bool has_slash;
};
static void
path_copy(char *dst, size_t dstsz, const char *src)
{
snprintf(dst, dstsz, "%s", src ? src : "");
for (char *p = dst; *p; p++)
if (*p == '\\')
*p = '/';
}
static void
trim_trailing_slashes(char *s)
{
size_t n = strlen(s);
while (n > 1 && s[n - 1] == '/')
s[--n] = '\0';
}
void
ignore_init(ignore_context_t *ctx, const char *root)
{
path_copy(ctx->root, sizeof(ctx->root), root && *root ? root : ".");
trim_trailing_slashes(ctx->root);
}
bool
ignore_is_ignore_file(const char *path)
{
char norm[IGNORE_PATH_MAX];
path_copy(norm, sizeof(norm), path);
const char *base = strrchr(norm, '/');
base = base ? base + 1 : norm;
return strcmp(base, ".gitignore") == 0 || strcmp(base, ".livesideignore") == 0;
}
static bool
is_builtin_ignored(const char *rel)
{
if (strcmp(rel, ".") == 0 || strcmp(rel, "..") == 0)
return true;
const char *p = rel;
while (*p) {
const char *slash = strchr(p, '/');
size_t n = slash ? (size_t)(slash - p) : strlen(p);
if ((n == 1 && p[0] == '.') || (n == 2 && p[0] == '.' && p[1] == '.') ||
(n == 4 && strncmp(p, ".git", 4) == 0))
return true;
if (!slash)
break;
p = slash + 1;
}
return false;
}
static void
relative_to_root(const ignore_context_t *ctx, const char *path,
char *rel, size_t relsz)
{
char norm[IGNORE_PATH_MAX];
path_copy(norm, sizeof(norm), path);
trim_trailing_slashes(norm);
size_t root_len = strlen(ctx->root);
if (strncmp(norm, ctx->root, root_len) == 0 &&
(norm[root_len] == '/' || norm[root_len] == '\0')) {
const char *r = norm + root_len;
if (*r == '/')
r++;
snprintf(rel, relsz, "%s", *r ? r : ".");
} else {
snprintf(rel, relsz, "%s", norm);
}
}
static bool
unescape_line(char *s)
{
char *r = s, *w = s;
while (*r) {
if (*r == '\\' && r[1])
r++;
*w++ = *r++;
}
*w = '\0';
return *s != '\0';
}
static bool
parse_rule(const char *base, char *line, struct ignore_rule *rule)
{
char *p = line;
while (*p == ' ' || *p == '\t')
p++;
if (*p == '\0')
return false;
if (*p == '#' || (*p == '\\' && p[1] == '#')) {
if (*p == '#')
return false;
p++;
}
char *end = p + strlen(p);
while (end > p && (end[-1] == '\n' || end[-1] == '\r'))
*--end = '\0';
while (end > p && (end[-1] == ' ' || end[-1] == '\t') &&
!(end - 2 >= p && end[-2] == '\\'))
*--end = '\0';
if (*p == '\0')
return false;
memset(rule, 0, sizeof(*rule));
snprintf(rule->base, sizeof(rule->base), "%s", base);
if (*p == '!') {
rule->negated = true;
p++;
}
if (*p == '/') {
rule->anchored = true;
p++;
}
size_t len = strlen(p);
if (len > 0 && p[len - 1] == '/') {
rule->dir_only = true;
p[len - 1] = '\0';
}
if (!unescape_line(p))
return false;
rule->has_slash = strchr(p, '/') != NULL;
snprintf(rule->pattern, sizeof(rule->pattern), "%s", p);
return rule->pattern[0] != '\0';
}
static bool
char_match(char pc, char c)
{
return pc == '?' ? c != '/' && c != '\0' : pc == c;
}
static bool
wildmatch(const char *pat, const char *text)
{
if (*pat == '\0')
return *text == '\0';
if (pat[0] == '*' && pat[1] == '*') {
pat += 2;
while (*pat == '*')
pat++;
if (*pat == '/') {
pat++;
if (wildmatch(pat, text))
return true;
for (const char *t = text; *t; t++)
if (*t == '/' && wildmatch(pat, t + 1))
return true;
return false;
}
for (const char *t = text;; t++) {
if (wildmatch(pat, t))
return true;
if (*t == '\0')
return false;
}
}
if (*pat == '*') {
pat++;
for (const char *t = text;; t++) {
if (wildmatch(pat, t))
return true;
if (*t == '\0' || *t == '/')
return false;
}
}
return char_match(*pat, *text) && wildmatch(pat + 1, text + 1);
}
static bool
match_basename(const char *pattern, const char *rel)
{
const char *p = rel;
while (*p) {
const char *slash = strchr(p, '/');
size_t n = slash ? (size_t)(slash - p) : strlen(p);
char comp[1024];
if (n >= sizeof(comp))
n = sizeof(comp) - 1;
memcpy(comp, p, n);
comp[n] = '\0';
if (wildmatch(pattern, comp))
return true;
if (!slash)
break;
p = slash + 1;
}
return false;
}
static bool
rule_matches(const struct ignore_rule *rule, const char *rel, bool is_dir)
{
char scoped[IGNORE_PATH_MAX];
if (rule->base[0]) {
size_t n = strlen(rule->base);
if (strncmp(rel, rule->base, n) != 0 || (rel[n] != '/' && rel[n] != '\0'))
return false;
const char *r = rel + n;
if (*r == '/')
r++;
snprintf(scoped, sizeof(scoped), "%s", *r ? r : ".");
} else {
snprintf(scoped, sizeof(scoped), "%s", rel);
}
if (rule->dir_only) {
if (is_dir && (wildmatch(rule->pattern, scoped) || match_basename(rule->pattern, scoped)))
return true;
char prefix[1024];
snprintf(prefix, sizeof(prefix), "%s/", rule->pattern);
return strncmp(scoped, prefix, strlen(prefix)) == 0 ||
(!rule->anchored && strstr(scoped, prefix) != NULL);
}
if (rule->anchored || rule->has_slash)
return wildmatch(rule->pattern, scoped);
return match_basename(rule->pattern, scoped);
}
static void
load_file_rules(const char *root, const char *base, const char *name,
const char *rel, bool is_dir, bool *ignored)
{
char file[IGNORE_PATH_MAX];
if (base[0])
snprintf(file, sizeof(file), "%s/%s/%s", root, base, name);
else
snprintf(file, sizeof(file), "%s/%s", root, name);
FILE *fp = fopen(file, "r");
if (!fp)
return;
char line[2048];
while (fgets(line, sizeof(line), fp)) {
struct ignore_rule rule;
if (parse_rule(base, line, &rule) && rule_matches(&rule, rel, is_dir))
*ignored = !rule.negated;
}
fclose(fp);
}
bool
ignore_path_ignored(const ignore_context_t *ctx, const char *path, bool is_dir)
{
char rel[IGNORE_PATH_MAX];
relative_to_root(ctx, path, rel, sizeof(rel));
if (is_builtin_ignored(rel))
return true;
bool ignored = false;
char base[IGNORE_PATH_MAX] = "";
load_file_rules(ctx->root, base, ".gitignore", rel, is_dir, &ignored);
load_file_rules(ctx->root, base, ".livesideignore", rel, is_dir, &ignored);
char walk[IGNORE_PATH_MAX];
snprintf(walk, sizeof(walk), "%s", rel);
char *slash = walk;
while ((slash = strchr(slash, '/')) != NULL) {
*slash = '\0';
snprintf(base, sizeof(base), "%s", walk);
load_file_rules(ctx->root, base, ".gitignore", rel, is_dir, &ignored);
load_file_rules(ctx->root, base, ".livesideignore", rel, is_dir, &ignored);
*slash++ = '/';
}
return ignored;
}