]> git.proxmox.com Git - libgit2.git/blob - src/ignore.c
Merge pull request #3086 from yongthecoder/master
[libgit2.git] / src / ignore.c
1 #include "git2/ignore.h"
2 #include "common.h"
3 #include "ignore.h"
4 #include "attrcache.h"
5 #include "path.h"
6 #include "config.h"
7 #include "fnmatch.h"
8
9 #define GIT_IGNORE_INTERNAL "[internal]exclude"
10
11 #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
12
13 /**
14 * A negative ignore pattern can match a positive one without
15 * wildcards if its pattern equals the tail of the positive
16 * pattern. Thus
17 *
18 * foo/bar
19 * !bar
20 *
21 * would result in foo/bar being unignored again.
22 */
23 static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg)
24 {
25 char *p;
26
27 if ((rule->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0
28 && (neg->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0) {
29 /*
30 * no chance of matching if rule is shorter than
31 * the negated one
32 */
33 if (rule->length < neg->length)
34 return false;
35
36 /*
37 * shift pattern so its tail aligns with the
38 * negated pattern
39 */
40 p = rule->pattern + rule->length - neg->length;
41 if (strcmp(p, neg->pattern) == 0)
42 return true;
43 }
44
45 return false;
46 }
47
48 /**
49 * A negative ignore can only unignore a file which is given explicitly before, thus
50 *
51 * foo
52 * !foo/bar
53 *
54 * does not unignore 'foo/bar' as it's not in the list. However
55 *
56 * foo/<star>
57 * !foo/bar
58 *
59 * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule.
60 */
61 static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match)
62 {
63 int error = 0;
64 size_t i;
65 git_attr_fnmatch *rule;
66 char *path;
67 git_buf buf = GIT_BUF_INIT;
68
69 *out = 0;
70
71 /* path of the file relative to the workdir, so we match the rules in subdirs */
72 if (match->containing_dir) {
73 git_buf_puts(&buf, match->containing_dir);
74 }
75 if (git_buf_puts(&buf, match->pattern) < 0)
76 return -1;
77
78 path = git_buf_detach(&buf);
79
80 git_vector_foreach(rules, i, rule) {
81 if (!(rule->flags & GIT_ATTR_FNMATCH_HASWILD)) {
82 if (does_negate_pattern(rule, match)) {
83 *out = 1;
84 goto out;
85 }
86 else
87 continue;
88 }
89
90 /*
91 * If we're dealing with a directory (which we know via the
92 * strchr() check) we want to use 'dirname/<star>' as the
93 * pattern so p_fnmatch() honours FNM_PATHNAME
94 */
95 git_buf_clear(&buf);
96 if (rule->containing_dir) {
97 git_buf_puts(&buf, rule->containing_dir);
98 }
99 if (!strchr(rule->pattern, '*'))
100 error = git_buf_printf(&buf, "%s/*", rule->pattern);
101 else
102 error = git_buf_puts(&buf, rule->pattern);
103
104 if (error < 0)
105 goto out;
106
107 if ((error = p_fnmatch(git_buf_cstr(&buf), path, FNM_PATHNAME)) < 0) {
108 giterr_set(GITERR_INVALID, "error matching pattern");
109 goto out;
110 }
111
112 /* if we found a match, we want to keep this rule */
113 if (error != FNM_NOMATCH) {
114 *out = 1;
115 error = 0;
116 goto out;
117 }
118 }
119
120 error = 0;
121
122 out:
123 git__free(path);
124 git_buf_free(&buf);
125 return error;
126 }
127
128 static int parse_ignore_file(
129 git_repository *repo, git_attr_file *attrs, const char *data)
130 {
131 int error = 0;
132 int ignore_case = false;
133 const char *scan = data, *context = NULL;
134 git_attr_fnmatch *match = NULL;
135
136 if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
137 giterr_clear();
138
139 /* if subdir file path, convert context for file paths */
140 if (attrs->entry &&
141 git_path_root(attrs->entry->path) < 0 &&
142 !git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE))
143 context = attrs->entry->path;
144
145 if (git_mutex_lock(&attrs->lock) < 0) {
146 giterr_set(GITERR_OS, "Failed to lock ignore file");
147 return -1;
148 }
149
150 while (!error && *scan) {
151 int valid_rule = 1;
152
153 if (!match && !(match = git__calloc(1, sizeof(*match)))) {
154 error = -1;
155 break;
156 }
157
158 match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG;
159
160 if (!(error = git_attr_fnmatch__parse(
161 match, &attrs->pool, context, &scan)))
162 {
163 match->flags |= GIT_ATTR_FNMATCH_IGNORE;
164
165 if (ignore_case)
166 match->flags |= GIT_ATTR_FNMATCH_ICASE;
167
168 scan = git__next_line(scan);
169
170 /* if a negative match doesn't actually do anything, throw it away */
171 if (match->flags & GIT_ATTR_FNMATCH_NEGATIVE)
172 error = does_negate_rule(&valid_rule, &attrs->rules, match);
173
174 if (!error && valid_rule)
175 error = git_vector_insert(&attrs->rules, match);
176 }
177
178 if (error != 0 || !valid_rule) {
179 match->pattern = NULL;
180
181 if (error == GIT_ENOTFOUND)
182 error = 0;
183 } else {
184 match = NULL; /* vector now "owns" the match */
185 }
186 }
187
188 git_mutex_unlock(&attrs->lock);
189 git__free(match);
190
191 return error;
192 }
193
194 static int push_ignore_file(
195 git_ignores *ignores,
196 git_vector *which_list,
197 const char *base,
198 const char *filename)
199 {
200 int error = 0;
201 git_attr_file *file = NULL;
202
203 error = git_attr_cache__get(
204 &file, ignores->repo, NULL, GIT_ATTR_FILE__FROM_FILE,
205 base, filename, parse_ignore_file);
206 if (error < 0)
207 return error;
208
209 if (file != NULL) {
210 if ((error = git_vector_insert(which_list, file)) < 0)
211 git_attr_file__free(file);
212 }
213
214 return error;
215 }
216
217 static int push_one_ignore(void *payload, const char *path)
218 {
219 git_ignores *ign = payload;
220 ign->depth++;
221 return push_ignore_file(ign, &ign->ign_path, path, GIT_IGNORE_FILE);
222 }
223
224 static int get_internal_ignores(git_attr_file **out, git_repository *repo)
225 {
226 int error;
227
228 if ((error = git_attr_cache__init(repo)) < 0)
229 return error;
230
231 error = git_attr_cache__get(
232 out, repo, NULL, GIT_ATTR_FILE__IN_MEMORY, NULL, GIT_IGNORE_INTERNAL, NULL);
233
234 /* if internal rules list is empty, insert default rules */
235 if (!error && !(*out)->rules.length)
236 error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES);
237
238 return error;
239 }
240
241 int git_ignore__for_path(
242 git_repository *repo,
243 const char *path,
244 git_ignores *ignores)
245 {
246 int error = 0;
247 const char *workdir = git_repository_workdir(repo);
248
249 assert(ignores && path);
250
251 memset(ignores, 0, sizeof(*ignores));
252 ignores->repo = repo;
253
254 /* Read the ignore_case flag */
255 if ((error = git_repository__cvar(
256 &ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0)
257 goto cleanup;
258
259 if ((error = git_attr_cache__init(repo)) < 0)
260 goto cleanup;
261
262 /* given a unrooted path in a non-bare repo, resolve it */
263 if (workdir && git_path_root(path) < 0)
264 error = git_path_find_dir(&ignores->dir, path, workdir);
265 else
266 error = git_buf_joinpath(&ignores->dir, path, "");
267 if (error < 0)
268 goto cleanup;
269
270 if (workdir && !git__prefixcmp(ignores->dir.ptr, workdir))
271 ignores->dir_root = strlen(workdir);
272
273 /* set up internals */
274 if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0)
275 goto cleanup;
276
277 /* load .gitignore up the path */
278 if (workdir != NULL) {
279 error = git_path_walk_up(
280 &ignores->dir, workdir, push_one_ignore, ignores);
281 if (error < 0)
282 goto cleanup;
283 }
284
285 /* load .git/info/exclude */
286 error = push_ignore_file(
287 ignores, &ignores->ign_global,
288 git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
289 if (error < 0)
290 goto cleanup;
291
292 /* load core.excludesfile */
293 if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
294 error = push_ignore_file(
295 ignores, &ignores->ign_global, NULL,
296 git_repository_attr_cache(repo)->cfg_excl_file);
297
298 cleanup:
299 if (error < 0)
300 git_ignore__free(ignores);
301
302 return error;
303 }
304
305 int git_ignore__push_dir(git_ignores *ign, const char *dir)
306 {
307 if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0)
308 return -1;
309
310 ign->depth++;
311
312 return push_ignore_file(
313 ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
314 }
315
316 int git_ignore__pop_dir(git_ignores *ign)
317 {
318 if (ign->ign_path.length > 0) {
319 git_attr_file *file = git_vector_last(&ign->ign_path);
320 const char *start = file->entry->path, *end;
321
322 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
323 * - file->path looks something like "a/b/.gitignore
324 *
325 * We are popping the last directory off ign->dir. We also want
326 * to remove the file from the vector if the popped directory
327 * matches the ignore path. We need to test if the "a/b" part of
328 * the file key matches the path we are about to pop.
329 */
330
331 if ((end = strrchr(start, '/')) != NULL) {
332 size_t dirlen = (end - start) + 1;
333 const char *relpath = ign->dir.ptr + ign->dir_root;
334 size_t pathlen = ign->dir.size - ign->dir_root;
335
336 if (pathlen == dirlen && !memcmp(relpath, start, dirlen)) {
337 git_vector_pop(&ign->ign_path);
338 git_attr_file__free(file);
339 }
340 }
341 }
342
343 if (--ign->depth > 0) {
344 git_buf_rtruncate_at_char(&ign->dir, '/');
345 git_path_to_dir(&ign->dir);
346 }
347
348 return 0;
349 }
350
351 void git_ignore__free(git_ignores *ignores)
352 {
353 unsigned int i;
354 git_attr_file *file;
355
356 git_attr_file__free(ignores->ign_internal);
357
358 git_vector_foreach(&ignores->ign_path, i, file) {
359 git_attr_file__free(file);
360 ignores->ign_path.contents[i] = NULL;
361 }
362 git_vector_free(&ignores->ign_path);
363
364 git_vector_foreach(&ignores->ign_global, i, file) {
365 git_attr_file__free(file);
366 ignores->ign_global.contents[i] = NULL;
367 }
368 git_vector_free(&ignores->ign_global);
369
370 git_buf_free(&ignores->dir);
371 }
372
373 static bool ignore_lookup_in_rules(
374 int *ignored, git_attr_file *file, git_attr_path *path)
375 {
376 size_t j;
377 git_attr_fnmatch *match;
378
379 git_vector_rforeach(&file->rules, j, match) {
380 if (git_attr_fnmatch__match(match, path)) {
381 *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) ?
382 GIT_IGNORE_TRUE : GIT_IGNORE_FALSE;
383 return true;
384 }
385 }
386
387 return false;
388 }
389
390 int git_ignore__lookup(
391 int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag)
392 {
393 unsigned int i;
394 git_attr_file *file;
395 git_attr_path path;
396
397 *out = GIT_IGNORE_NOTFOUND;
398
399 if (git_attr_path__init(
400 &path, pathname, git_repository_workdir(ignores->repo), dir_flag) < 0)
401 return -1;
402
403 /* first process builtins - success means path was found */
404 if (ignore_lookup_in_rules(out, ignores->ign_internal, &path))
405 goto cleanup;
406
407 /* next process files in the path */
408 git_vector_foreach(&ignores->ign_path, i, file) {
409 if (ignore_lookup_in_rules(out, file, &path))
410 goto cleanup;
411 }
412
413 /* last process global ignores */
414 git_vector_foreach(&ignores->ign_global, i, file) {
415 if (ignore_lookup_in_rules(out, file, &path))
416 goto cleanup;
417 }
418
419 cleanup:
420 git_attr_path__free(&path);
421 return 0;
422 }
423
424 int git_ignore_add_rule(git_repository *repo, const char *rules)
425 {
426 int error;
427 git_attr_file *ign_internal = NULL;
428
429 if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
430 return error;
431
432 error = parse_ignore_file(repo, ign_internal, rules);
433 git_attr_file__free(ign_internal);
434
435 return error;
436 }
437
438 int git_ignore_clear_internal_rules(git_repository *repo)
439 {
440 int error;
441 git_attr_file *ign_internal;
442
443 if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
444 return error;
445
446 if (!(error = git_attr_file__clear_rules(ign_internal, true)))
447 error = parse_ignore_file(
448 repo, ign_internal, GIT_IGNORE_DEFAULT_RULES);
449
450 git_attr_file__free(ign_internal);
451 return error;
452 }
453
454 int git_ignore_path_is_ignored(
455 int *ignored,
456 git_repository *repo,
457 const char *pathname)
458 {
459 int error;
460 const char *workdir;
461 git_attr_path path;
462 git_ignores ignores;
463 unsigned int i;
464 git_attr_file *file;
465
466 assert(ignored && pathname);
467
468 workdir = repo ? git_repository_workdir(repo) : NULL;
469
470 memset(&path, 0, sizeof(path));
471 memset(&ignores, 0, sizeof(ignores));
472
473 if ((error = git_attr_path__init(&path, pathname, workdir, GIT_DIR_FLAG_UNKNOWN)) < 0 ||
474 (error = git_ignore__for_path(repo, path.path, &ignores)) < 0)
475 goto cleanup;
476
477 while (1) {
478 /* first process builtins - success means path was found */
479 if (ignore_lookup_in_rules(ignored, ignores.ign_internal, &path))
480 goto cleanup;
481
482 /* next process files in the path */
483 git_vector_foreach(&ignores.ign_path, i, file) {
484 if (ignore_lookup_in_rules(ignored, file, &path))
485 goto cleanup;
486 }
487
488 /* last process global ignores */
489 git_vector_foreach(&ignores.ign_global, i, file) {
490 if (ignore_lookup_in_rules(ignored, file, &path))
491 goto cleanup;
492 }
493
494 /* move up one directory */
495 if (path.basename == path.path)
496 break;
497 path.basename[-1] = '\0';
498 while (path.basename > path.path && *path.basename != '/')
499 path.basename--;
500 if (path.basename > path.path)
501 path.basename++;
502 path.is_dir = 1;
503
504 if ((error = git_ignore__pop_dir(&ignores)) < 0)
505 break;
506 }
507
508 *ignored = 0;
509
510 cleanup:
511 git_attr_path__free(&path);
512 git_ignore__free(&ignores);
513 return error;
514 }
515
516 int git_ignore__check_pathspec_for_exact_ignores(
517 git_repository *repo,
518 git_vector *vspec,
519 bool no_fnmatch)
520 {
521 int error = 0;
522 size_t i;
523 git_attr_fnmatch *match;
524 int ignored;
525 git_buf path = GIT_BUF_INIT;
526 const char *wd, *filename;
527 git_index *idx;
528
529 if ((error = git_repository__ensure_not_bare(
530 repo, "validate pathspec")) < 0 ||
531 (error = git_repository_index(&idx, repo)) < 0)
532 return error;
533
534 wd = git_repository_workdir(repo);
535
536 git_vector_foreach(vspec, i, match) {
537 /* skip wildcard matches (if they are being used) */
538 if ((match->flags & GIT_ATTR_FNMATCH_HASWILD) != 0 &&
539 !no_fnmatch)
540 continue;
541
542 filename = match->pattern;
543
544 /* if file is already in the index, it's fine */
545 if (git_index_get_bypath(idx, filename, 0) != NULL)
546 continue;
547
548 if ((error = git_buf_joinpath(&path, wd, filename)) < 0)
549 break;
550
551 /* is there a file on disk that matches this exactly? */
552 if (!git_path_isfile(path.ptr))
553 continue;
554
555 /* is that file ignored? */
556 if ((error = git_ignore_path_is_ignored(&ignored, repo, filename)) < 0)
557 break;
558
559 if (ignored) {
560 giterr_set(GITERR_INVALID, "pathspec contains ignored file '%s'",
561 filename);
562 error = GIT_EINVALIDSPEC;
563 break;
564 }
565 }
566
567 git_index_free(idx);
568 git_buf_free(&path);
569
570 return error;
571 }
572