2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "git2/ignore.h"
12 #include "attrcache.h"
15 #include "wildmatch.h"
17 #define GIT_IGNORE_INTERNAL "[internal]exclude"
19 #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
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
29 * would result in foo/bar being unignored again while
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
41 * would result in foo/bar being unignored again. As with the
47 * would do nothing, again.
49 static int does_negate_pattern(git_attr_fnmatch
*rule
, git_attr_fnmatch
*neg
)
51 int (*cmp
)(const char *, const char *, size_t);
52 git_attr_fnmatch
*longer
, *shorter
;
55 if ((rule
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) != 0
56 || (neg
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0)
59 if (neg
->flags
& GIT_ATTR_FNMATCH_ICASE
)
60 cmp
= git__strncasecmp
;
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
) {
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
;
83 if (memchr(shorter
->pattern
, '/', shorter
->length
) != NULL
)
86 return cmp(p
, shorter
->pattern
, shorter
->length
) == 0;
90 * A negative ignore can only unignore a file which is given explicitly before, thus
95 * does not unignore 'foo/bar' as it's not in the list. However
100 * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule.
102 static int does_negate_rule(int *out
, git_vector
*rules
, git_attr_fnmatch
*match
)
104 int error
= 0, wildmatch_flags
, effective_flags
;
106 git_attr_fnmatch
*rule
;
108 git_buf buf
= GIT_BUF_INIT
;
112 wildmatch_flags
= WM_PATHNAME
;
113 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
)
114 wildmatch_flags
|= WM_CASEFOLD
;
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
);
120 if (git_buf_puts(&buf
, match
->pattern
) < 0)
123 path
= git_buf_detach(&buf
);
125 git_vector_foreach(rules
, i
, rule
) {
126 if (!(rule
->flags
& GIT_ATTR_FNMATCH_HASWILD
)) {
127 if (does_negate_pattern(rule
, match
)) {
137 if (rule
->containing_dir
)
138 git_buf_puts(&buf
, rule
->containing_dir
);
139 git_buf_puts(&buf
, rule
->pattern
);
141 if (git_buf_oom(&buf
))
145 * if rule isn't for full path we match without PATHNAME flag
146 * as lines like *.txt should match something like dir/test.txt
147 * requiring * to also match /
149 effective_flags
= wildmatch_flags
;
150 if (!(rule
->flags
& GIT_ATTR_FNMATCH_FULLPATH
))
151 effective_flags
&= ~WM_PATHNAME
;
153 /* if we found a match, we want to keep this rule */
154 if ((wildmatch(git_buf_cstr(&buf
), path
, effective_flags
)) == WM_MATCH
) {
165 git_buf_dispose(&buf
);
169 static int parse_ignore_file(
170 git_repository
*repo
, git_attr_file
*attrs
, const char *data
, bool allow_macros
)
173 int ignore_case
= false;
174 const char *scan
= data
, *context
= NULL
;
175 git_attr_fnmatch
*match
= NULL
;
177 GIT_UNUSED(allow_macros
);
179 if (git_repository__configmap_lookup(&ignore_case
, repo
, GIT_CONFIGMAP_IGNORECASE
) < 0)
182 /* if subdir file path, convert context for file paths */
184 git_path_root(attrs
->entry
->path
) < 0 &&
185 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_IGNORE_FILE
))
186 context
= attrs
->entry
->path
;
188 if (git_mutex_lock(&attrs
->lock
) < 0) {
189 git_error_set(GIT_ERROR_OS
, "failed to lock ignore file");
193 while (!error
&& *scan
) {
196 if (!match
&& !(match
= git__calloc(1, sizeof(*match
)))) {
202 GIT_ATTR_FNMATCH_ALLOWSPACE
| GIT_ATTR_FNMATCH_ALLOWNEG
;
204 if (!(error
= git_attr_fnmatch__parse(
205 match
, &attrs
->pool
, context
, &scan
)))
207 match
->flags
|= GIT_ATTR_FNMATCH_IGNORE
;
210 match
->flags
|= GIT_ATTR_FNMATCH_ICASE
;
212 scan
= git__next_line(scan
);
215 * If a negative match doesn't actually do anything,
216 * throw it away. As we cannot always verify whether a
217 * rule containing wildcards negates another rule, we
218 * do not optimize away these rules, though.
220 if (match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
221 && !(match
->flags
& GIT_ATTR_FNMATCH_HASWILD
))
222 error
= does_negate_rule(&valid_rule
, &attrs
->rules
, match
);
224 if (!error
&& valid_rule
)
225 error
= git_vector_insert(&attrs
->rules
, match
);
228 if (error
!= 0 || !valid_rule
) {
229 match
->pattern
= NULL
;
231 if (error
== GIT_ENOTFOUND
)
234 match
= NULL
; /* vector now "owns" the match */
238 git_mutex_unlock(&attrs
->lock
);
244 static int push_ignore_file(
245 git_ignores
*ignores
,
246 git_vector
*which_list
,
248 const char *filename
)
250 git_attr_file_source source
= { GIT_ATTR_FILE_SOURCE_FILE
, base
, filename
};
251 git_attr_file
*file
= NULL
;
254 error
= git_attr_cache__get(&file
, ignores
->repo
, NULL
, &source
, parse_ignore_file
, false);
260 if ((error
= git_vector_insert(which_list
, file
)) < 0)
261 git_attr_file__free(file
);
267 static int push_one_ignore(void *payload
, const char *path
)
269 git_ignores
*ign
= payload
;
271 return push_ignore_file(ign
, &ign
->ign_path
, path
, GIT_IGNORE_FILE
);
274 static int get_internal_ignores(git_attr_file
**out
, git_repository
*repo
)
276 git_attr_file_source source
= { GIT_ATTR_FILE_SOURCE_MEMORY
, NULL
, GIT_IGNORE_INTERNAL
};
279 if ((error
= git_attr_cache__init(repo
)) < 0)
282 error
= git_attr_cache__get(out
, repo
, NULL
, &source
, NULL
, false);
284 /* if internal rules list is empty, insert default rules */
285 if (!error
&& !(*out
)->rules
.length
)
286 error
= parse_ignore_file(repo
, *out
, GIT_IGNORE_DEFAULT_RULES
, false);
291 int git_ignore__for_path(
292 git_repository
*repo
,
294 git_ignores
*ignores
)
297 const char *workdir
= git_repository_workdir(repo
);
298 git_buf infopath
= GIT_BUF_INIT
;
300 GIT_ASSERT_ARG(repo
);
301 GIT_ASSERT_ARG(ignores
);
302 GIT_ASSERT_ARG(path
);
304 memset(ignores
, 0, sizeof(*ignores
));
305 ignores
->repo
= repo
;
307 /* Read the ignore_case flag */
308 if ((error
= git_repository__configmap_lookup(
309 &ignores
->ignore_case
, repo
, GIT_CONFIGMAP_IGNORECASE
)) < 0)
312 if ((error
= git_attr_cache__init(repo
)) < 0)
315 /* given a unrooted path in a non-bare repo, resolve it */
316 if (workdir
&& git_path_root(path
) < 0) {
317 git_buf local
= GIT_BUF_INIT
;
319 if ((error
= git_path_dirname_r(&local
, path
)) < 0 ||
320 (error
= git_path_resolve_relative(&local
, 0)) < 0 ||
321 (error
= git_path_to_dir(&local
)) < 0 ||
322 (error
= git_buf_joinpath(&ignores
->dir
, workdir
, local
.ptr
)) < 0 ||
323 (error
= git_path_validate_workdir_buf(repo
, &ignores
->dir
)) < 0) {
324 /* Nothing, we just want to stop on the first error */
327 git_buf_dispose(&local
);
329 if (!(error
= git_buf_joinpath(&ignores
->dir
, path
, "")))
330 error
= git_path_validate_filesystem(ignores
->dir
.ptr
, ignores
->dir
.size
);
336 if (workdir
&& !git__prefixcmp(ignores
->dir
.ptr
, workdir
))
337 ignores
->dir_root
= strlen(workdir
);
339 /* set up internals */
340 if ((error
= get_internal_ignores(&ignores
->ign_internal
, repo
)) < 0)
343 /* load .gitignore up the path */
344 if (workdir
!= NULL
) {
345 error
= git_path_walk_up(
346 &ignores
->dir
, workdir
, push_one_ignore
, ignores
);
351 /* load .git/info/exclude if possible */
352 if ((error
= git_repository_item_path(&infopath
, repo
, GIT_REPOSITORY_ITEM_INFO
)) < 0 ||
353 (error
= push_ignore_file(ignores
, &ignores
->ign_global
, infopath
.ptr
, GIT_IGNORE_FILE_INREPO
)) < 0) {
354 if (error
!= GIT_ENOTFOUND
)
359 /* load core.excludesfile */
360 if (git_repository_attr_cache(repo
)->cfg_excl_file
!= NULL
)
361 error
= push_ignore_file(
362 ignores
, &ignores
->ign_global
, NULL
,
363 git_repository_attr_cache(repo
)->cfg_excl_file
);
366 git_buf_dispose(&infopath
);
368 git_ignore__free(ignores
);
373 int git_ignore__push_dir(git_ignores
*ign
, const char *dir
)
375 if (git_buf_joinpath(&ign
->dir
, ign
->dir
.ptr
, dir
) < 0)
380 return push_ignore_file(
381 ign
, &ign
->ign_path
, ign
->dir
.ptr
, GIT_IGNORE_FILE
);
384 int git_ignore__pop_dir(git_ignores
*ign
)
386 if (ign
->ign_path
.length
> 0) {
387 git_attr_file
*file
= git_vector_last(&ign
->ign_path
);
388 const char *start
= file
->entry
->path
, *end
;
390 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
391 * - file->path looks something like "a/b/.gitignore
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
396 * the file key matches the path we are about to pop.
399 if ((end
= strrchr(start
, '/')) != NULL
) {
400 size_t dirlen
= (end
- start
) + 1;
401 const char *relpath
= ign
->dir
.ptr
+ ign
->dir_root
;
402 size_t pathlen
= ign
->dir
.size
- ign
->dir_root
;
404 if (pathlen
== dirlen
&& !memcmp(relpath
, start
, dirlen
)) {
405 git_vector_pop(&ign
->ign_path
);
406 git_attr_file__free(file
);
411 if (--ign
->depth
> 0) {
412 git_buf_rtruncate_at_char(&ign
->dir
, '/');
413 git_path_to_dir(&ign
->dir
);
419 void git_ignore__free(git_ignores
*ignores
)
424 git_attr_file__free(ignores
->ign_internal
);
426 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
427 git_attr_file__free(file
);
428 ignores
->ign_path
.contents
[i
] = NULL
;
430 git_vector_free(&ignores
->ign_path
);
432 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
433 git_attr_file__free(file
);
434 ignores
->ign_global
.contents
[i
] = NULL
;
436 git_vector_free(&ignores
->ign_global
);
438 git_buf_dispose(&ignores
->dir
);
441 static bool ignore_lookup_in_rules(
442 int *ignored
, git_attr_file
*file
, git_attr_path
*path
)
445 git_attr_fnmatch
*match
;
447 git_vector_rforeach(&file
->rules
, j
, match
) {
448 if (match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
&&
449 path
->is_dir
== GIT_DIR_FLAG_FALSE
)
451 if (git_attr_fnmatch__match(match
, path
)) {
452 *ignored
= ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0) ?
453 GIT_IGNORE_TRUE
: GIT_IGNORE_FALSE
;
461 int git_ignore__lookup(
462 int *out
, git_ignores
*ignores
, const char *pathname
, git_dir_flag dir_flag
)
468 *out
= GIT_IGNORE_NOTFOUND
;
470 if (git_attr_path__init(
471 &path
, pathname
, git_repository_workdir(ignores
->repo
), dir_flag
) < 0)
474 /* first process builtins - success means path was found */
475 if (ignore_lookup_in_rules(out
, ignores
->ign_internal
, &path
))
478 /* next process files in the path.
479 * this process has to process ignores in reverse order
480 * to ensure correct prioritization of rules
482 git_vector_rforeach(&ignores
->ign_path
, i
, file
) {
483 if (ignore_lookup_in_rules(out
, file
, &path
))
487 /* last process global ignores */
488 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
489 if (ignore_lookup_in_rules(out
, file
, &path
))
494 git_attr_path__free(&path
);
498 int git_ignore_add_rule(git_repository
*repo
, const char *rules
)
501 git_attr_file
*ign_internal
= NULL
;
503 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
506 error
= parse_ignore_file(repo
, ign_internal
, rules
, false);
507 git_attr_file__free(ign_internal
);
512 int git_ignore_clear_internal_rules(git_repository
*repo
)
515 git_attr_file
*ign_internal
;
517 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
520 if (!(error
= git_attr_file__clear_rules(ign_internal
, true)))
521 error
= parse_ignore_file(
522 repo
, ign_internal
, GIT_IGNORE_DEFAULT_RULES
, false);
524 git_attr_file__free(ign_internal
);
528 int git_ignore_path_is_ignored(
530 git_repository
*repo
,
531 const char *pathname
)
539 git_dir_flag dir_flag
= GIT_DIR_FLAG_UNKNOWN
;
541 GIT_ASSERT_ARG(repo
);
542 GIT_ASSERT_ARG(ignored
);
543 GIT_ASSERT_ARG(pathname
);
545 workdir
= git_repository_workdir(repo
);
547 memset(&path
, 0, sizeof(path
));
548 memset(&ignores
, 0, sizeof(ignores
));
550 if (!git__suffixcmp(pathname
, "/"))
551 dir_flag
= GIT_DIR_FLAG_TRUE
;
552 else if (git_repository_is_bare(repo
))
553 dir_flag
= GIT_DIR_FLAG_FALSE
;
555 if ((error
= git_attr_path__init(&path
, pathname
, workdir
, dir_flag
)) < 0 ||
556 (error
= git_ignore__for_path(repo
, path
.path
, &ignores
)) < 0)
560 /* first process builtins - success means path was found */
561 if (ignore_lookup_in_rules(ignored
, ignores
.ign_internal
, &path
))
564 /* next process files in the path */
565 git_vector_foreach(&ignores
.ign_path
, i
, file
) {
566 if (ignore_lookup_in_rules(ignored
, file
, &path
))
570 /* last process global ignores */
571 git_vector_foreach(&ignores
.ign_global
, i
, file
) {
572 if (ignore_lookup_in_rules(ignored
, file
, &path
))
576 /* move up one directory */
577 if (path
.basename
== path
.path
)
579 path
.basename
[-1] = '\0';
580 while (path
.basename
> path
.path
&& *path
.basename
!= '/')
582 if (path
.basename
> path
.path
)
586 if ((error
= git_ignore__pop_dir(&ignores
)) < 0)
593 git_attr_path__free(&path
);
594 git_ignore__free(&ignores
);
598 int git_ignore__check_pathspec_for_exact_ignores(
599 git_repository
*repo
,
605 git_attr_fnmatch
*match
;
607 git_buf path
= GIT_BUF_INIT
;
608 const char *filename
;
611 if ((error
= git_repository__ensure_not_bare(
612 repo
, "validate pathspec")) < 0 ||
613 (error
= git_repository_index(&idx
, repo
)) < 0)
616 git_vector_foreach(vspec
, i
, match
) {
617 /* skip wildcard matches (if they are being used) */
618 if ((match
->flags
& GIT_ATTR_FNMATCH_HASWILD
) != 0 &&
622 filename
= match
->pattern
;
624 /* if file is already in the index, it's fine */
625 if (git_index_get_bypath(idx
, filename
, 0) != NULL
)
628 if ((error
= git_repository_workdir_path(&path
, repo
, filename
)) < 0)
631 /* is there a file on disk that matches this exactly? */
632 if (!git_path_isfile(path
.ptr
))
635 /* is that file ignored? */
636 if ((error
= git_ignore_path_is_ignored(&ignored
, repo
, filename
)) < 0)
640 git_error_set(GIT_ERROR_INVALID
, "pathspec contains ignored file '%s'",
642 error
= GIT_EINVALIDSPEC
;
648 git_buf_dispose(&path
);