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