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
;
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
))
144 /* if we found a match, we want to keep this rule */
145 if ((wildmatch(git_buf_cstr(&buf
), path
, wildmatch_flags
)) == WM_MATCH
) {
156 git_buf_dispose(&buf
);
160 static int parse_ignore_file(
161 git_repository
*repo
, git_attr_file
*attrs
, const char *data
, bool allow_macros
)
164 int ignore_case
= false;
165 const char *scan
= data
, *context
= NULL
;
166 git_attr_fnmatch
*match
= NULL
;
168 GIT_UNUSED(allow_macros
);
170 if (git_repository__configmap_lookup(&ignore_case
, repo
, GIT_CONFIGMAP_IGNORECASE
) < 0)
173 /* if subdir file path, convert context for file paths */
175 git_path_root(attrs
->entry
->path
) < 0 &&
176 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_IGNORE_FILE
))
177 context
= attrs
->entry
->path
;
179 if (git_mutex_lock(&attrs
->lock
) < 0) {
180 git_error_set(GIT_ERROR_OS
, "failed to lock ignore file");
184 while (!error
&& *scan
) {
187 if (!match
&& !(match
= git__calloc(1, sizeof(*match
)))) {
193 GIT_ATTR_FNMATCH_ALLOWSPACE
| GIT_ATTR_FNMATCH_ALLOWNEG
;
195 if (!(error
= git_attr_fnmatch__parse(
196 match
, &attrs
->pool
, context
, &scan
)))
198 match
->flags
|= GIT_ATTR_FNMATCH_IGNORE
;
201 match
->flags
|= GIT_ATTR_FNMATCH_ICASE
;
203 scan
= git__next_line(scan
);
206 * If a negative match doesn't actually do anything,
207 * throw it away. As we cannot always verify whether a
208 * rule containing wildcards negates another rule, we
209 * do not optimize away these rules, though.
211 if (match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
212 && !(match
->flags
& GIT_ATTR_FNMATCH_HASWILD
))
213 error
= does_negate_rule(&valid_rule
, &attrs
->rules
, match
);
215 if (!error
&& valid_rule
)
216 error
= git_vector_insert(&attrs
->rules
, match
);
219 if (error
!= 0 || !valid_rule
) {
220 match
->pattern
= NULL
;
222 if (error
== GIT_ENOTFOUND
)
225 match
= NULL
; /* vector now "owns" the match */
229 git_mutex_unlock(&attrs
->lock
);
235 static int push_ignore_file(
236 git_ignores
*ignores
,
237 git_vector
*which_list
,
239 const char *filename
)
242 git_attr_file
*file
= NULL
;
244 error
= git_attr_cache__get(&file
, ignores
->repo
, NULL
, GIT_ATTR_FILE__FROM_FILE
,
245 base
, filename
, parse_ignore_file
, false);
250 if ((error
= git_vector_insert(which_list
, file
)) < 0)
251 git_attr_file__free(file
);
257 static int push_one_ignore(void *payload
, const char *path
)
259 git_ignores
*ign
= payload
;
261 return push_ignore_file(ign
, &ign
->ign_path
, path
, GIT_IGNORE_FILE
);
264 static int get_internal_ignores(git_attr_file
**out
, git_repository
*repo
)
268 if ((error
= git_attr_cache__init(repo
)) < 0)
271 error
= git_attr_cache__get(out
, repo
, NULL
, GIT_ATTR_FILE__IN_MEMORY
, NULL
,
272 GIT_IGNORE_INTERNAL
, NULL
, false);
274 /* if internal rules list is empty, insert default rules */
275 if (!error
&& !(*out
)->rules
.length
)
276 error
= parse_ignore_file(repo
, *out
, GIT_IGNORE_DEFAULT_RULES
, false);
281 int git_ignore__for_path(
282 git_repository
*repo
,
284 git_ignores
*ignores
)
287 const char *workdir
= git_repository_workdir(repo
);
288 git_buf infopath
= GIT_BUF_INIT
;
290 assert(repo
&& ignores
&& path
);
292 memset(ignores
, 0, sizeof(*ignores
));
293 ignores
->repo
= repo
;
295 /* Read the ignore_case flag */
296 if ((error
= git_repository__configmap_lookup(
297 &ignores
->ignore_case
, repo
, GIT_CONFIGMAP_IGNORECASE
)) < 0)
300 if ((error
= git_attr_cache__init(repo
)) < 0)
303 /* given a unrooted path in a non-bare repo, resolve it */
304 if (workdir
&& git_path_root(path
) < 0) {
305 git_buf local
= GIT_BUF_INIT
;
307 if ((error
= git_path_dirname_r(&local
, path
)) < 0 ||
308 (error
= git_path_resolve_relative(&local
, 0)) < 0 ||
309 (error
= git_path_to_dir(&local
)) < 0 ||
310 (error
= git_buf_joinpath(&ignores
->dir
, workdir
, local
.ptr
)) < 0)
311 {;} /* Nothing, we just want to stop on the first error */
312 git_buf_dispose(&local
);
314 error
= git_buf_joinpath(&ignores
->dir
, path
, "");
319 if (workdir
&& !git__prefixcmp(ignores
->dir
.ptr
, workdir
))
320 ignores
->dir_root
= strlen(workdir
);
322 /* set up internals */
323 if ((error
= get_internal_ignores(&ignores
->ign_internal
, repo
)) < 0)
326 /* load .gitignore up the path */
327 if (workdir
!= NULL
) {
328 error
= git_path_walk_up(
329 &ignores
->dir
, workdir
, push_one_ignore
, ignores
);
334 /* load .git/info/exclude if possible */
335 if ((error
= git_repository_item_path(&infopath
, repo
, GIT_REPOSITORY_ITEM_INFO
)) < 0 ||
336 (error
= push_ignore_file(ignores
, &ignores
->ign_global
, infopath
.ptr
, GIT_IGNORE_FILE_INREPO
)) < 0) {
337 if (error
!= GIT_ENOTFOUND
)
342 /* load core.excludesfile */
343 if (git_repository_attr_cache(repo
)->cfg_excl_file
!= NULL
)
344 error
= push_ignore_file(
345 ignores
, &ignores
->ign_global
, NULL
,
346 git_repository_attr_cache(repo
)->cfg_excl_file
);
349 git_buf_dispose(&infopath
);
351 git_ignore__free(ignores
);
356 int git_ignore__push_dir(git_ignores
*ign
, const char *dir
)
358 if (git_buf_joinpath(&ign
->dir
, ign
->dir
.ptr
, dir
) < 0)
363 return push_ignore_file(
364 ign
, &ign
->ign_path
, ign
->dir
.ptr
, GIT_IGNORE_FILE
);
367 int git_ignore__pop_dir(git_ignores
*ign
)
369 if (ign
->ign_path
.length
> 0) {
370 git_attr_file
*file
= git_vector_last(&ign
->ign_path
);
371 const char *start
= file
->entry
->path
, *end
;
373 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
374 * - file->path looks something like "a/b/.gitignore
376 * We are popping the last directory off ign->dir. We also want
377 * to remove the file from the vector if the popped directory
378 * matches the ignore path. We need to test if the "a/b" part of
379 * the file key matches the path we are about to pop.
382 if ((end
= strrchr(start
, '/')) != NULL
) {
383 size_t dirlen
= (end
- start
) + 1;
384 const char *relpath
= ign
->dir
.ptr
+ ign
->dir_root
;
385 size_t pathlen
= ign
->dir
.size
- ign
->dir_root
;
387 if (pathlen
== dirlen
&& !memcmp(relpath
, start
, dirlen
)) {
388 git_vector_pop(&ign
->ign_path
);
389 git_attr_file__free(file
);
394 if (--ign
->depth
> 0) {
395 git_buf_rtruncate_at_char(&ign
->dir
, '/');
396 git_path_to_dir(&ign
->dir
);
402 void git_ignore__free(git_ignores
*ignores
)
407 git_attr_file__free(ignores
->ign_internal
);
409 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
410 git_attr_file__free(file
);
411 ignores
->ign_path
.contents
[i
] = NULL
;
413 git_vector_free(&ignores
->ign_path
);
415 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
416 git_attr_file__free(file
);
417 ignores
->ign_global
.contents
[i
] = NULL
;
419 git_vector_free(&ignores
->ign_global
);
421 git_buf_dispose(&ignores
->dir
);
424 static bool ignore_lookup_in_rules(
425 int *ignored
, git_attr_file
*file
, git_attr_path
*path
)
428 git_attr_fnmatch
*match
;
430 git_vector_rforeach(&file
->rules
, j
, match
) {
431 if (match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
&&
432 path
->is_dir
== GIT_DIR_FLAG_FALSE
)
434 if (git_attr_fnmatch__match(match
, path
)) {
435 *ignored
= ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0) ?
436 GIT_IGNORE_TRUE
: GIT_IGNORE_FALSE
;
444 int git_ignore__lookup(
445 int *out
, git_ignores
*ignores
, const char *pathname
, git_dir_flag dir_flag
)
451 *out
= GIT_IGNORE_NOTFOUND
;
453 if (git_attr_path__init(
454 &path
, pathname
, git_repository_workdir(ignores
->repo
), dir_flag
) < 0)
457 /* first process builtins - success means path was found */
458 if (ignore_lookup_in_rules(out
, ignores
->ign_internal
, &path
))
461 /* next process files in the path.
462 * this process has to process ignores in reverse order
463 * to ensure correct prioritization of rules
465 git_vector_rforeach(&ignores
->ign_path
, i
, file
) {
466 if (ignore_lookup_in_rules(out
, file
, &path
))
470 /* last process global ignores */
471 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
472 if (ignore_lookup_in_rules(out
, file
, &path
))
477 git_attr_path__free(&path
);
481 int git_ignore_add_rule(git_repository
*repo
, const char *rules
)
484 git_attr_file
*ign_internal
= NULL
;
486 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
489 error
= parse_ignore_file(repo
, ign_internal
, rules
, false);
490 git_attr_file__free(ign_internal
);
495 int git_ignore_clear_internal_rules(git_repository
*repo
)
498 git_attr_file
*ign_internal
;
500 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
503 if (!(error
= git_attr_file__clear_rules(ign_internal
, true)))
504 error
= parse_ignore_file(
505 repo
, ign_internal
, GIT_IGNORE_DEFAULT_RULES
, false);
507 git_attr_file__free(ign_internal
);
511 int git_ignore_path_is_ignored(
513 git_repository
*repo
,
514 const char *pathname
)
522 git_dir_flag dir_flag
= GIT_DIR_FLAG_UNKNOWN
;
524 assert(repo
&& ignored
&& pathname
);
526 workdir
= git_repository_workdir(repo
);
528 memset(&path
, 0, sizeof(path
));
529 memset(&ignores
, 0, sizeof(ignores
));
531 if (!git__suffixcmp(pathname
, "/"))
532 dir_flag
= GIT_DIR_FLAG_TRUE
;
533 else if (git_repository_is_bare(repo
))
534 dir_flag
= GIT_DIR_FLAG_FALSE
;
536 if ((error
= git_attr_path__init(&path
, pathname
, workdir
, dir_flag
)) < 0 ||
537 (error
= git_ignore__for_path(repo
, path
.path
, &ignores
)) < 0)
541 /* first process builtins - success means path was found */
542 if (ignore_lookup_in_rules(ignored
, ignores
.ign_internal
, &path
))
545 /* next process files in the path */
546 git_vector_foreach(&ignores
.ign_path
, i
, file
) {
547 if (ignore_lookup_in_rules(ignored
, file
, &path
))
551 /* last process global ignores */
552 git_vector_foreach(&ignores
.ign_global
, i
, file
) {
553 if (ignore_lookup_in_rules(ignored
, file
, &path
))
557 /* move up one directory */
558 if (path
.basename
== path
.path
)
560 path
.basename
[-1] = '\0';
561 while (path
.basename
> path
.path
&& *path
.basename
!= '/')
563 if (path
.basename
> path
.path
)
567 if ((error
= git_ignore__pop_dir(&ignores
)) < 0)
574 git_attr_path__free(&path
);
575 git_ignore__free(&ignores
);
579 int git_ignore__check_pathspec_for_exact_ignores(
580 git_repository
*repo
,
586 git_attr_fnmatch
*match
;
588 git_buf path
= GIT_BUF_INIT
;
589 const char *wd
, *filename
;
592 if ((error
= git_repository__ensure_not_bare(
593 repo
, "validate pathspec")) < 0 ||
594 (error
= git_repository_index(&idx
, repo
)) < 0)
597 wd
= git_repository_workdir(repo
);
599 git_vector_foreach(vspec
, i
, match
) {
600 /* skip wildcard matches (if they are being used) */
601 if ((match
->flags
& GIT_ATTR_FNMATCH_HASWILD
) != 0 &&
605 filename
= match
->pattern
;
607 /* if file is already in the index, it's fine */
608 if (git_index_get_bypath(idx
, filename
, 0) != NULL
)
611 if ((error
= git_buf_joinpath(&path
, wd
, filename
)) < 0)
614 /* is there a file on disk that matches this exactly? */
615 if (!git_path_isfile(path
.ptr
))
618 /* is that file ignored? */
619 if ((error
= git_ignore_path_is_ignored(&ignored
, repo
, filename
)) < 0)
623 git_error_set(GIT_ERROR_INVALID
, "pathspec contains ignored file '%s'",
625 error
= GIT_EINVALIDSPEC
;
631 git_buf_dispose(&path
);