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