]>
Commit | Line | Data |
---|---|---|
eae0bfdc PP |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
8 | #include "ignore.h" | |
9 | ||
f004c4a8 | 10 | #include "git2/ignore.h" |
157cef10 | 11 | #include "common.h" |
823c0e9c | 12 | #include "attrcache.h" |
df743c7d | 13 | #include "path.h" |
ec40b7f9 | 14 | #include "config.h" |
e05b2ff1 | 15 | #include "fnmatch.h" |
df743c7d RB |
16 | |
17 | #define GIT_IGNORE_INTERNAL "[internal]exclude" | |
df743c7d | 18 | |
02df42dd RB |
19 | #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n" |
20 | ||
4f358603 | 21 | /** |
fcb2c1c8 PS |
22 | * A negative ignore pattern can negate a positive one without |
23 | * wildcards if it is a basename only and equals the basename of | |
24 | * the positive pattern. Thus | |
4f358603 PS |
25 | * |
26 | * foo/bar | |
27 | * !bar | |
28 | * | |
fcb2c1c8 PS |
29 | * would result in foo/bar being unignored again while |
30 | * | |
31 | * moo/foo/bar | |
32 | * !foo/bar | |
33 | * | |
34 | * would do nothing. The reverse also holds true: a positive | |
35 | * basename pattern can be negated by unignoring the basename in | |
36 | * subdirectories. Thus | |
37 | * | |
38 | * bar | |
39 | * !foo/bar | |
40 | * | |
41 | * would result in foo/bar being unignored again. As with the | |
42 | * first case, | |
43 | * | |
44 | * foo/bar | |
45 | * !moo/foo/bar | |
46 | * | |
47 | * would do nothing, again. | |
4f358603 PS |
48 | */ |
49 | static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg) | |
50 | { | |
eae0bfdc | 51 | int (*cmp)(const char *, const char *, size_t); |
fcb2c1c8 | 52 | git_attr_fnmatch *longer, *shorter; |
4f358603 PS |
53 | char *p; |
54 | ||
eae0bfdc PP |
55 | if ((rule->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 |
56 | || (neg->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) | |
57 | return false; | |
58 | ||
59 | if (neg->flags & GIT_ATTR_FNMATCH_ICASE) | |
60 | cmp = git__strncasecmp; | |
61 | else | |
62 | cmp = git__strncmp; | |
63 | ||
64 | /* If lengths match we need to have an exact match */ | |
65 | if (rule->length == neg->length) { | |
66 | return cmp(rule->pattern, neg->pattern, rule->length) == 0; | |
67 | } else if (rule->length < neg->length) { | |
68 | shorter = rule; | |
69 | longer = neg; | |
70 | } else { | |
71 | shorter = neg; | |
72 | longer = rule; | |
73 | } | |
fcb2c1c8 | 74 | |
eae0bfdc PP |
75 | /* Otherwise, we need to check if the shorter |
76 | * rule is a basename only (that is, it contains | |
77 | * no path separator) and, if so, if it | |
78 | * matches the tail of the longer rule */ | |
79 | p = longer->pattern + longer->length - shorter->length; | |
4f358603 | 80 | |
eae0bfdc PP |
81 | if (p[-1] != '/') |
82 | return false; | |
83 | if (memchr(shorter->pattern, '/', shorter->length) != NULL) | |
84 | return false; | |
4f358603 | 85 | |
eae0bfdc | 86 | return cmp(p, shorter->pattern, shorter->length) == 0; |
4f358603 PS |
87 | } |
88 | ||
e05b2ff1 CMN |
89 | /** |
90 | * A negative ignore can only unignore a file which is given explicitly before, thus | |
91 | * | |
92 | * foo | |
93 | * !foo/bar | |
94 | * | |
95 | * does not unignore 'foo/bar' as it's not in the list. However | |
96 | * | |
97 | * foo/<star> | |
98 | * !foo/bar | |
99 | * | |
100 | * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule. | |
101 | */ | |
102 | static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match) | |
103 | { | |
eae0bfdc | 104 | int error = 0, fnflags; |
e05b2ff1 CMN |
105 | size_t i; |
106 | git_attr_fnmatch *rule; | |
107 | char *path; | |
108 | git_buf buf = GIT_BUF_INIT; | |
109 | ||
4f358603 PS |
110 | *out = 0; |
111 | ||
eae0bfdc PP |
112 | fnflags = FNM_PATHNAME; |
113 | if (match->flags & GIT_ATTR_FNMATCH_ICASE) | |
114 | fnflags |= FNM_IGNORECASE; | |
115 | ||
e05b2ff1 CMN |
116 | /* path of the file relative to the workdir, so we match the rules in subdirs */ |
117 | if (match->containing_dir) { | |
118 | git_buf_puts(&buf, match->containing_dir); | |
119 | } | |
120 | if (git_buf_puts(&buf, match->pattern) < 0) | |
121 | return -1; | |
122 | ||
123 | path = git_buf_detach(&buf); | |
124 | ||
125 | git_vector_foreach(rules, i, rule) { | |
4f358603 PS |
126 | if (!(rule->flags & GIT_ATTR_FNMATCH_HASWILD)) { |
127 | if (does_negate_pattern(rule, match)) { | |
2c57114f | 128 | error = 0; |
4f358603 PS |
129 | *out = 1; |
130 | goto out; | |
131 | } | |
132 | else | |
133 | continue; | |
134 | } | |
e05b2ff1 | 135 | |
eae0bfdc PP |
136 | /* |
137 | * When dealing with a directory, we add '/<star>' so | |
138 | * p_fnmatch() honours FNM_PATHNAME. Checking for LEADINGDIR | |
139 | * alone isn't enough as that's also set for nagations, so we | |
140 | * need to check that NEGATIVE is off. | |
141 | */ | |
e05b2ff1 CMN |
142 | git_buf_clear(&buf); |
143 | if (rule->containing_dir) { | |
144 | git_buf_puts(&buf, rule->containing_dir); | |
145 | } | |
657afd35 CMN |
146 | |
147 | error = git_buf_puts(&buf, rule->pattern); | |
148 | ||
149 | if ((rule->flags & (GIT_ATTR_FNMATCH_LEADINGDIR | GIT_ATTR_FNMATCH_NEGATIVE)) == GIT_ATTR_FNMATCH_LEADINGDIR) | |
150 | error = git_buf_PUTS(&buf, "/*"); | |
e05b2ff1 CMN |
151 | |
152 | if (error < 0) | |
153 | goto out; | |
154 | ||
eae0bfdc | 155 | if ((error = p_fnmatch(git_buf_cstr(&buf), path, fnflags)) < 0) { |
e05b2ff1 CMN |
156 | giterr_set(GITERR_INVALID, "error matching pattern"); |
157 | goto out; | |
158 | } | |
159 | ||
160 | /* if we found a match, we want to keep this rule */ | |
161 | if (error != FNM_NOMATCH) { | |
162 | *out = 1; | |
163 | error = 0; | |
164 | goto out; | |
165 | } | |
166 | } | |
167 | ||
e05b2ff1 CMN |
168 | error = 0; |
169 | ||
170 | out: | |
171 | git__free(path); | |
172 | git_buf_free(&buf); | |
173 | return error; | |
174 | } | |
175 | ||
f917481e | 176 | static int parse_ignore_file( |
823c0e9c | 177 | git_repository *repo, git_attr_file *attrs, const char *data) |
df743c7d | 178 | { |
b709e951 | 179 | int error = 0; |
eac76c23 | 180 | int ignore_case = false; |
7d490872 RB |
181 | const char *scan = data, *context = NULL; |
182 | git_attr_fnmatch *match = NULL; | |
ec40b7f9 | 183 | |
823c0e9c | 184 | if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0) |
7d490872 | 185 | giterr_clear(); |
df743c7d | 186 | |
7d490872 | 187 | /* if subdir file path, convert context for file paths */ |
823c0e9c RB |
188 | if (attrs->entry && |
189 | git_path_root(attrs->entry->path) < 0 && | |
190 | !git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE)) | |
191 | context = attrs->entry->path; | |
df743c7d | 192 | |
e6e8530a | 193 | if (git_mutex_lock(&attrs->lock) < 0) { |
909d5494 | 194 | giterr_set(GITERR_OS, "failed to lock ignore file"); |
e6e8530a RB |
195 | return -1; |
196 | } | |
197 | ||
0d0fa7c3 | 198 | while (!error && *scan) { |
e05b2ff1 CMN |
199 | int valid_rule = 1; |
200 | ||
17ef678c RB |
201 | if (!match && !(match = git__calloc(1, sizeof(*match)))) { |
202 | error = -1; | |
203 | break; | |
df743c7d RB |
204 | } |
205 | ||
4ba64794 | 206 | match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG; |
9939e602 | 207 | |
4ba64794 | 208 | if (!(error = git_attr_fnmatch__parse( |
7d490872 | 209 | match, &attrs->pool, context, &scan))) |
19fa2bc1 | 210 | { |
edb456c3 PK |
211 | match->flags |= GIT_ATTR_FNMATCH_IGNORE; |
212 | ||
213 | if (ignore_case) | |
214 | match->flags |= GIT_ATTR_FNMATCH_ICASE; | |
215 | ||
df743c7d | 216 | scan = git__next_line(scan); |
e05b2ff1 | 217 | |
eae0bfdc PP |
218 | /* |
219 | * If a negative match doesn't actually do anything, | |
220 | * throw it away. As we cannot always verify whether a | |
221 | * rule containing wildcards negates another rule, we | |
222 | * do not optimize away these rules, though. | |
223 | * */ | |
224 | if (match->flags & GIT_ATTR_FNMATCH_NEGATIVE | |
225 | && !(match->flags & GIT_ATTR_FNMATCH_HASWILD)) | |
e05b2ff1 CMN |
226 | error = does_negate_rule(&valid_rule, &attrs->rules, match); |
227 | ||
228 | if (!error && valid_rule) | |
229 | error = git_vector_insert(&attrs->rules, match); | |
df743c7d RB |
230 | } |
231 | ||
e05b2ff1 | 232 | if (error != 0 || !valid_rule) { |
df743c7d RB |
233 | match->pattern = NULL; |
234 | ||
235 | if (error == GIT_ENOTFOUND) | |
0d0fa7c3 | 236 | error = 0; |
df743c7d RB |
237 | } else { |
238 | match = NULL; /* vector now "owns" the match */ | |
239 | } | |
240 | } | |
241 | ||
e6e8530a | 242 | git_mutex_unlock(&attrs->lock); |
1dbcc9fc | 243 | git__free(match); |
df743c7d | 244 | |
df743c7d RB |
245 | return error; |
246 | } | |
247 | ||
7d490872 RB |
248 | static int push_ignore_file( |
249 | git_ignores *ignores, | |
250 | git_vector *which_list, | |
251 | const char *base, | |
252 | const char *filename) | |
253 | { | |
254 | int error = 0; | |
255 | git_attr_file *file = NULL; | |
256 | ||
2e9d813b | 257 | error = git_attr_cache__get( |
9f779aac | 258 | &file, ignores->repo, NULL, GIT_ATTR_FILE__FROM_FILE, |
823c0e9c | 259 | base, filename, parse_ignore_file); |
2e9d813b RB |
260 | if (error < 0) |
261 | return error; | |
262 | ||
263 | if (file != NULL) { | |
264 | if ((error = git_vector_insert(which_list, file)) < 0) | |
265 | git_attr_file__free(file); | |
266 | } | |
7d490872 RB |
267 | |
268 | return error; | |
269 | } | |
df743c7d | 270 | |
bbb988a5 | 271 | static int push_one_ignore(void *payload, const char *path) |
0cfcff5d | 272 | { |
25e0b157 | 273 | git_ignores *ign = payload; |
8f7bc646 | 274 | ign->depth++; |
bbb988a5 | 275 | return push_ignore_file(ign, &ign->ign_path, path, GIT_IGNORE_FILE); |
0cfcff5d RB |
276 | } |
277 | ||
7d490872 | 278 | static int get_internal_ignores(git_attr_file **out, git_repository *repo) |
02df42dd RB |
279 | { |
280 | int error; | |
281 | ||
7d490872 RB |
282 | if ((error = git_attr_cache__init(repo)) < 0) |
283 | return error; | |
284 | ||
7d490872 | 285 | error = git_attr_cache__get( |
9f779aac | 286 | out, repo, NULL, GIT_ATTR_FILE__IN_MEMORY, NULL, GIT_IGNORE_INTERNAL, NULL); |
02df42dd | 287 | |
7d490872 RB |
288 | /* if internal rules list is empty, insert default rules */ |
289 | if (!error && !(*out)->rules.length) | |
823c0e9c | 290 | error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES); |
02df42dd RB |
291 | |
292 | return error; | |
293 | } | |
294 | ||
f917481e RB |
295 | int git_ignore__for_path( |
296 | git_repository *repo, | |
297 | const char *path, | |
298 | git_ignores *ignores) | |
df743c7d | 299 | { |
0d0fa7c3 | 300 | int error = 0; |
df743c7d | 301 | const char *workdir = git_repository_workdir(repo); |
c5f3da96 | 302 | git_buf infopath = GIT_BUF_INIT; |
adc9bdb3 | 303 | |
8a349bf2 | 304 | assert(repo && ignores && path); |
df743c7d | 305 | |
2e9d813b | 306 | memset(ignores, 0, sizeof(*ignores)); |
b6c93aef | 307 | ignores->repo = repo; |
b6c93aef | 308 | |
eac76c23 RB |
309 | /* Read the ignore_case flag */ |
310 | if ((error = git_repository__cvar( | |
311 | &ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0) | |
ec40b7f9 PK |
312 | goto cleanup; |
313 | ||
2e9d813b | 314 | if ((error = git_attr_cache__init(repo)) < 0) |
df743c7d RB |
315 | goto cleanup; |
316 | ||
f917481e | 317 | /* given a unrooted path in a non-bare repo, resolve it */ |
d364dc8b CMN |
318 | if (workdir && git_path_root(path) < 0) { |
319 | git_buf local = GIT_BUF_INIT; | |
320 | ||
321 | if ((error = git_path_dirname_r(&local, path)) < 0 || | |
322 | (error = git_path_resolve_relative(&local, 0)) < 0 || | |
323 | (error = git_path_to_dir(&local)) < 0 || | |
324 | (error = git_buf_joinpath(&ignores->dir, workdir, local.ptr)) < 0) | |
325 | {;} /* Nothing, we just want to stop on the first error */ | |
326 | git_buf_free(&local); | |
327 | } else { | |
6a0956e5 | 328 | error = git_buf_joinpath(&ignores->dir, path, ""); |
d364dc8b | 329 | } |
f917481e | 330 | if (error < 0) |
df743c7d RB |
331 | goto cleanup; |
332 | ||
6a0956e5 RB |
333 | if (workdir && !git__prefixcmp(ignores->dir.ptr, workdir)) |
334 | ignores->dir_root = strlen(workdir); | |
335 | ||
b6c93aef | 336 | /* set up internals */ |
7d490872 | 337 | if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0) |
df743c7d RB |
338 | goto cleanup; |
339 | ||
340 | /* load .gitignore up the path */ | |
f917481e RB |
341 | if (workdir != NULL) { |
342 | error = git_path_walk_up( | |
25e0b157 | 343 | &ignores->dir, workdir, push_one_ignore, ignores); |
f917481e RB |
344 | if (error < 0) |
345 | goto cleanup; | |
346 | } | |
df743c7d | 347 | |
c5f3da96 PS |
348 | if ((error = git_repository_item_path(&infopath, |
349 | repo, GIT_REPOSITORY_ITEM_INFO)) < 0) | |
350 | goto cleanup; | |
351 | ||
df743c7d | 352 | /* load .git/info/exclude */ |
7d490872 RB |
353 | error = push_ignore_file( |
354 | ignores, &ignores->ign_global, | |
c5f3da96 | 355 | infopath.ptr, GIT_IGNORE_FILE_INREPO); |
0d0fa7c3 | 356 | if (error < 0) |
df743c7d RB |
357 | goto cleanup; |
358 | ||
359 | /* load core.excludesfile */ | |
95dfb031 | 360 | if (git_repository_attr_cache(repo)->cfg_excl_file != NULL) |
7d490872 RB |
361 | error = push_ignore_file( |
362 | ignores, &ignores->ign_global, NULL, | |
95dfb031 | 363 | git_repository_attr_cache(repo)->cfg_excl_file); |
df743c7d RB |
364 | |
365 | cleanup: | |
c5f3da96 | 366 | git_buf_free(&infopath); |
0d0fa7c3 | 367 | if (error < 0) |
b6c93aef | 368 | git_ignore__free(ignores); |
95dfb031 | 369 | |
b6c93aef RB |
370 | return error; |
371 | } | |
372 | ||
373 | int git_ignore__push_dir(git_ignores *ign, const char *dir) | |
374 | { | |
0d0fa7c3 RB |
375 | if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0) |
376 | return -1; | |
ba8b8c04 | 377 | |
8f7bc646 RB |
378 | ign->depth++; |
379 | ||
ba8b8c04 | 380 | return push_ignore_file( |
7d490872 | 381 | ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE); |
df743c7d RB |
382 | } |
383 | ||
b6c93aef RB |
384 | int git_ignore__pop_dir(git_ignores *ign) |
385 | { | |
386 | if (ign->ign_path.length > 0) { | |
387 | git_attr_file *file = git_vector_last(&ign->ign_path); | |
823c0e9c | 388 | const char *start = file->entry->path, *end; |
3bc3ed80 | 389 | |
7d490872 RB |
390 | /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/") |
391 | * - file->path looks something like "a/b/.gitignore | |
3bc3ed80 | 392 | * |
7d490872 RB |
393 | * We are popping the last directory off ign->dir. We also want |
394 | * to remove the file from the vector if the popped directory | |
395 | * matches the ignore path. We need to test if the "a/b" part of | |
3bc3ed80 RB |
396 | * the file key matches the path we are about to pop. |
397 | */ | |
398 | ||
7d490872 RB |
399 | if ((end = strrchr(start, '/')) != NULL) { |
400 | size_t dirlen = (end - start) + 1; | |
6a0956e5 RB |
401 | const char *relpath = ign->dir.ptr + ign->dir_root; |
402 | size_t pathlen = ign->dir.size - ign->dir_root; | |
3bc3ed80 | 403 | |
6a0956e5 | 404 | if (pathlen == dirlen && !memcmp(relpath, start, dirlen)) { |
7d490872 RB |
405 | git_vector_pop(&ign->ign_path); |
406 | git_attr_file__free(file); | |
407 | } | |
cef170ab | 408 | } |
8f7bc646 | 409 | } |
ba8b8c04 | 410 | |
8f7bc646 | 411 | if (--ign->depth > 0) { |
b6c93aef | 412 | git_buf_rtruncate_at_char(&ign->dir, '/'); |
8f7bc646 | 413 | git_path_to_dir(&ign->dir); |
b6c93aef | 414 | } |
8f7bc646 | 415 | |
0d0fa7c3 | 416 | return 0; |
b6c93aef RB |
417 | } |
418 | ||
adc9bdb3 | 419 | void git_ignore__free(git_ignores *ignores) |
df743c7d | 420 | { |
b8777615 RB |
421 | unsigned int i; |
422 | git_attr_file *file; | |
423 | ||
7d490872 | 424 | git_attr_file__free(ignores->ign_internal); |
b8777615 RB |
425 | |
426 | git_vector_foreach(&ignores->ign_path, i, file) { | |
427 | git_attr_file__free(file); | |
428 | ignores->ign_path.contents[i] = NULL; | |
429 | } | |
b6c93aef | 430 | git_vector_free(&ignores->ign_path); |
b8777615 RB |
431 | |
432 | git_vector_foreach(&ignores->ign_global, i, file) { | |
433 | git_attr_file__free(file); | |
434 | ignores->ign_global.contents[i] = NULL; | |
435 | } | |
b6c93aef | 436 | git_vector_free(&ignores->ign_global); |
b8777615 | 437 | |
b6c93aef RB |
438 | git_buf_free(&ignores->dir); |
439 | } | |
440 | ||
0d0fa7c3 | 441 | static bool ignore_lookup_in_rules( |
f554611a | 442 | int *ignored, git_attr_file *file, git_attr_path *path) |
b6c93aef | 443 | { |
b8457baa | 444 | size_t j; |
b6c93aef RB |
445 | git_attr_fnmatch *match; |
446 | ||
e6e8530a | 447 | git_vector_rforeach(&file->rules, j, match) { |
ab43ad2f | 448 | if (git_attr_fnmatch__match(match, path)) { |
f554611a RB |
449 | *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) ? |
450 | GIT_IGNORE_TRUE : GIT_IGNORE_FALSE; | |
0d0fa7c3 | 451 | return true; |
b6c93aef RB |
452 | } |
453 | } | |
454 | ||
0d0fa7c3 | 455 | return false; |
df743c7d RB |
456 | } |
457 | ||
f917481e | 458 | int git_ignore__lookup( |
4c09e19a | 459 | int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag) |
df743c7d | 460 | { |
b6c93aef | 461 | unsigned int i; |
df743c7d RB |
462 | git_attr_file *file; |
463 | git_attr_path path; | |
df743c7d | 464 | |
f554611a RB |
465 | *out = GIT_IGNORE_NOTFOUND; |
466 | ||
0d0fa7c3 | 467 | if (git_attr_path__init( |
4c09e19a | 468 | &path, pathname, git_repository_workdir(ignores->repo), dir_flag) < 0) |
0d0fa7c3 | 469 | return -1; |
df743c7d | 470 | |
0d0fa7c3 | 471 | /* first process builtins - success means path was found */ |
f554611a | 472 | if (ignore_lookup_in_rules(out, ignores->ign_internal, &path)) |
d58336dd | 473 | goto cleanup; |
df743c7d | 474 | |
b6c93aef RB |
475 | /* next process files in the path */ |
476 | git_vector_foreach(&ignores->ign_path, i, file) { | |
f554611a | 477 | if (ignore_lookup_in_rules(out, file, &path)) |
d58336dd | 478 | goto cleanup; |
df743c7d | 479 | } |
df743c7d | 480 | |
b6c93aef RB |
481 | /* last process global ignores */ |
482 | git_vector_foreach(&ignores->ign_global, i, file) { | |
f554611a | 483 | if (ignore_lookup_in_rules(out, file, &path)) |
d58336dd | 484 | goto cleanup; |
b6c93aef RB |
485 | } |
486 | ||
d58336dd RB |
487 | cleanup: |
488 | git_attr_path__free(&path); | |
0d0fa7c3 | 489 | return 0; |
df743c7d | 490 | } |
f004c4a8 | 491 | |
823c0e9c | 492 | int git_ignore_add_rule(git_repository *repo, const char *rules) |
f004c4a8 RB |
493 | { |
494 | int error; | |
7d490872 | 495 | git_attr_file *ign_internal = NULL; |
f004c4a8 | 496 | |
823c0e9c RB |
497 | if ((error = get_internal_ignores(&ign_internal, repo)) < 0) |
498 | return error; | |
499 | ||
500 | error = parse_ignore_file(repo, ign_internal, rules); | |
501 | git_attr_file__free(ign_internal); | |
f004c4a8 RB |
502 | |
503 | return error; | |
504 | } | |
505 | ||
823c0e9c | 506 | int git_ignore_clear_internal_rules(git_repository *repo) |
f004c4a8 RB |
507 | { |
508 | int error; | |
509 | git_attr_file *ign_internal; | |
510 | ||
823c0e9c RB |
511 | if ((error = get_internal_ignores(&ign_internal, repo)) < 0) |
512 | return error; | |
7d490872 | 513 | |
823c0e9c RB |
514 | if (!(error = git_attr_file__clear_rules(ign_internal, true))) |
515 | error = parse_ignore_file( | |
516 | repo, ign_internal, GIT_IGNORE_DEFAULT_RULES); | |
02df42dd | 517 | |
823c0e9c | 518 | git_attr_file__free(ign_internal); |
f004c4a8 RB |
519 | return error; |
520 | } | |
2fb4e9b3 RB |
521 | |
522 | int git_ignore_path_is_ignored( | |
523 | int *ignored, | |
524 | git_repository *repo, | |
52032ae5 | 525 | const char *pathname) |
2fb4e9b3 RB |
526 | { |
527 | int error; | |
52032ae5 RB |
528 | const char *workdir; |
529 | git_attr_path path; | |
2fb4e9b3 | 530 | git_ignores ignores; |
52032ae5 RB |
531 | unsigned int i; |
532 | git_attr_file *file; | |
eae0bfdc | 533 | git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; |
2fb4e9b3 | 534 | |
8a349bf2 | 535 | assert(repo && ignored && pathname); |
52032ae5 | 536 | |
8a349bf2 | 537 | workdir = git_repository_workdir(repo); |
52032ae5 | 538 | |
f554611a RB |
539 | memset(&path, 0, sizeof(path)); |
540 | memset(&ignores, 0, sizeof(ignores)); | |
52032ae5 | 541 | |
eae0bfdc PP |
542 | if (git_repository_is_bare(repo)) |
543 | dir_flag = GIT_DIR_FLAG_FALSE; | |
544 | ||
545 | if ((error = git_attr_path__init(&path, pathname, workdir, dir_flag)) < 0 || | |
f554611a RB |
546 | (error = git_ignore__for_path(repo, path.path, &ignores)) < 0) |
547 | goto cleanup; | |
2fb4e9b3 | 548 | |
52032ae5 | 549 | while (1) { |
52032ae5 | 550 | /* first process builtins - success means path was found */ |
f554611a | 551 | if (ignore_lookup_in_rules(ignored, ignores.ign_internal, &path)) |
52032ae5 RB |
552 | goto cleanup; |
553 | ||
554 | /* next process files in the path */ | |
555 | git_vector_foreach(&ignores.ign_path, i, file) { | |
f554611a | 556 | if (ignore_lookup_in_rules(ignored, file, &path)) |
52032ae5 RB |
557 | goto cleanup; |
558 | } | |
559 | ||
560 | /* last process global ignores */ | |
561 | git_vector_foreach(&ignores.ign_global, i, file) { | |
f554611a | 562 | if (ignore_lookup_in_rules(ignored, file, &path)) |
52032ae5 RB |
563 | goto cleanup; |
564 | } | |
565 | ||
f554611a RB |
566 | /* move up one directory */ |
567 | if (path.basename == path.path) | |
52032ae5 | 568 | break; |
f554611a RB |
569 | path.basename[-1] = '\0'; |
570 | while (path.basename > path.path && *path.basename != '/') | |
571 | path.basename--; | |
572 | if (path.basename > path.path) | |
573 | path.basename++; | |
574 | path.is_dir = 1; | |
575 | ||
576 | if ((error = git_ignore__pop_dir(&ignores)) < 0) | |
ba8b8c04 | 577 | break; |
52032ae5 RB |
578 | } |
579 | ||
580 | *ignored = 0; | |
581 | ||
582 | cleanup: | |
583 | git_attr_path__free(&path); | |
2fb4e9b3 RB |
584 | git_ignore__free(&ignores); |
585 | return error; | |
586 | } | |
587 | ||
85b8b18b RB |
588 | int git_ignore__check_pathspec_for_exact_ignores( |
589 | git_repository *repo, | |
590 | git_vector *vspec, | |
591 | bool no_fnmatch) | |
592 | { | |
593 | int error = 0; | |
594 | size_t i; | |
595 | git_attr_fnmatch *match; | |
596 | int ignored; | |
597 | git_buf path = GIT_BUF_INIT; | |
598 | const char *wd, *filename; | |
599 | git_index *idx; | |
600 | ||
601 | if ((error = git_repository__ensure_not_bare( | |
602 | repo, "validate pathspec")) < 0 || | |
603 | (error = git_repository_index(&idx, repo)) < 0) | |
604 | return error; | |
605 | ||
606 | wd = git_repository_workdir(repo); | |
607 | ||
608 | git_vector_foreach(vspec, i, match) { | |
609 | /* skip wildcard matches (if they are being used) */ | |
610 | if ((match->flags & GIT_ATTR_FNMATCH_HASWILD) != 0 && | |
611 | !no_fnmatch) | |
612 | continue; | |
613 | ||
614 | filename = match->pattern; | |
615 | ||
616 | /* if file is already in the index, it's fine */ | |
617 | if (git_index_get_bypath(idx, filename, 0) != NULL) | |
618 | continue; | |
619 | ||
620 | if ((error = git_buf_joinpath(&path, wd, filename)) < 0) | |
621 | break; | |
622 | ||
623 | /* is there a file on disk that matches this exactly? */ | |
624 | if (!git_path_isfile(path.ptr)) | |
625 | continue; | |
626 | ||
627 | /* is that file ignored? */ | |
628 | if ((error = git_ignore_path_is_ignored(&ignored, repo, filename)) < 0) | |
629 | break; | |
630 | ||
631 | if (ignored) { | |
632 | giterr_set(GITERR_INVALID, "pathspec contains ignored file '%s'", | |
633 | filename); | |
634 | error = GIT_EINVALIDSPEC; | |
635 | break; | |
636 | } | |
637 | } | |
638 | ||
639 | git_index_free(idx); | |
640 | git_buf_free(&path); | |
641 | ||
642 | return error; | |
643 | } | |
644 |