1 #include "git2/ignore.h"
9 #define GIT_IGNORE_INTERNAL "[internal]exclude"
11 #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
14 * A negative ignore pattern can match a positive one without
15 * wildcards if its pattern equals the tail of the positive
21 * would result in foo/bar being unignored again.
23 static int does_negate_pattern(git_attr_fnmatch
*rule
, git_attr_fnmatch
*neg
)
27 if ((rule
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0
28 && (neg
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) != 0) {
30 * no chance of matching if rule is shorter than
33 if (rule
->length
< neg
->length
)
37 * shift pattern so its tail aligns with the
40 p
= rule
->pattern
+ rule
->length
- neg
->length
;
41 if (strcmp(p
, neg
->pattern
) == 0)
49 * A negative ignore can only unignore a file which is given explicitly before, thus
54 * does not unignore 'foo/bar' as it's not in the list. However
59 * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule.
61 static int does_negate_rule(int *out
, git_vector
*rules
, git_attr_fnmatch
*match
)
65 git_attr_fnmatch
*rule
;
67 git_buf buf
= GIT_BUF_INIT
;
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
);
75 if (git_buf_puts(&buf
, match
->pattern
) < 0)
78 path
= git_buf_detach(&buf
);
80 git_vector_foreach(rules
, i
, rule
) {
81 if (!(rule
->flags
& GIT_ATTR_FNMATCH_HASWILD
)) {
82 if (does_negate_pattern(rule
, match
)) {
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
96 if (rule
->containing_dir
) {
97 git_buf_puts(&buf
, rule
->containing_dir
);
99 if (!strchr(rule
->pattern
, '*'))
100 error
= git_buf_printf(&buf
, "%s/*", rule
->pattern
);
102 error
= git_buf_puts(&buf
, rule
->pattern
);
107 if ((error
= p_fnmatch(git_buf_cstr(&buf
), path
, FNM_PATHNAME
)) < 0) {
108 giterr_set(GITERR_INVALID
, "error matching pattern");
112 /* if we found a match, we want to keep this rule */
113 if (error
!= FNM_NOMATCH
) {
128 static int parse_ignore_file(
129 git_repository
*repo
, git_attr_file
*attrs
, const char *data
)
132 int ignore_case
= false;
133 const char *scan
= data
, *context
= NULL
;
134 git_attr_fnmatch
*match
= NULL
;
136 if (git_repository__cvar(&ignore_case
, repo
, GIT_CVAR_IGNORECASE
) < 0)
139 /* if subdir file path, convert context for file paths */
141 git_path_root(attrs
->entry
->path
) < 0 &&
142 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_IGNORE_FILE
))
143 context
= attrs
->entry
->path
;
145 if (git_mutex_lock(&attrs
->lock
) < 0) {
146 giterr_set(GITERR_OS
, "Failed to lock ignore file");
150 while (!error
&& *scan
) {
153 if (!match
&& !(match
= git__calloc(1, sizeof(*match
)))) {
158 match
->flags
= GIT_ATTR_FNMATCH_ALLOWSPACE
| GIT_ATTR_FNMATCH_ALLOWNEG
;
160 if (!(error
= git_attr_fnmatch__parse(
161 match
, &attrs
->pool
, context
, &scan
)))
163 match
->flags
|= GIT_ATTR_FNMATCH_IGNORE
;
166 match
->flags
|= GIT_ATTR_FNMATCH_ICASE
;
168 scan
= git__next_line(scan
);
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
);
174 if (!error
&& valid_rule
)
175 error
= git_vector_insert(&attrs
->rules
, match
);
178 if (error
!= 0 || !valid_rule
) {
179 match
->pattern
= NULL
;
181 if (error
== GIT_ENOTFOUND
)
184 match
= NULL
; /* vector now "owns" the match */
188 git_mutex_unlock(&attrs
->lock
);
194 static int push_ignore_file(
195 git_ignores
*ignores
,
196 git_vector
*which_list
,
198 const char *filename
)
201 git_attr_file
*file
= NULL
;
203 error
= git_attr_cache__get(
204 &file
, ignores
->repo
, NULL
, GIT_ATTR_FILE__FROM_FILE
,
205 base
, filename
, parse_ignore_file
);
210 if ((error
= git_vector_insert(which_list
, file
)) < 0)
211 git_attr_file__free(file
);
217 static int push_one_ignore(void *payload
, const char *path
)
219 git_ignores
*ign
= payload
;
221 return push_ignore_file(ign
, &ign
->ign_path
, path
, GIT_IGNORE_FILE
);
224 static int get_internal_ignores(git_attr_file
**out
, git_repository
*repo
)
228 if ((error
= git_attr_cache__init(repo
)) < 0)
231 error
= git_attr_cache__get(
232 out
, repo
, NULL
, GIT_ATTR_FILE__IN_MEMORY
, NULL
, GIT_IGNORE_INTERNAL
, NULL
);
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
);
241 int git_ignore__for_path(
242 git_repository
*repo
,
244 git_ignores
*ignores
)
247 const char *workdir
= git_repository_workdir(repo
);
249 assert(ignores
&& path
);
251 memset(ignores
, 0, sizeof(*ignores
));
252 ignores
->repo
= repo
;
254 /* Read the ignore_case flag */
255 if ((error
= git_repository__cvar(
256 &ignores
->ignore_case
, repo
, GIT_CVAR_IGNORECASE
)) < 0)
259 if ((error
= git_attr_cache__init(repo
)) < 0)
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
);
266 error
= git_buf_joinpath(&ignores
->dir
, path
, "");
270 if (workdir
&& !git__prefixcmp(ignores
->dir
.ptr
, workdir
))
271 ignores
->dir_root
= strlen(workdir
);
273 /* set up internals */
274 if ((error
= get_internal_ignores(&ignores
->ign_internal
, repo
)) < 0)
277 /* load .gitignore up the path */
278 if (workdir
!= NULL
) {
279 error
= git_path_walk_up(
280 &ignores
->dir
, workdir
, push_one_ignore
, ignores
);
285 /* load .git/info/exclude */
286 error
= push_ignore_file(
287 ignores
, &ignores
->ign_global
,
288 git_repository_path(repo
), GIT_IGNORE_FILE_INREPO
);
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
);
300 git_ignore__free(ignores
);
305 int git_ignore__push_dir(git_ignores
*ign
, const char *dir
)
307 if (git_buf_joinpath(&ign
->dir
, ign
->dir
.ptr
, dir
) < 0)
312 return push_ignore_file(
313 ign
, &ign
->ign_path
, ign
->dir
.ptr
, GIT_IGNORE_FILE
);
316 int git_ignore__pop_dir(git_ignores
*ign
)
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
;
322 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
323 * - file->path looks something like "a/b/.gitignore
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.
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
;
336 if (pathlen
== dirlen
&& !memcmp(relpath
, start
, dirlen
)) {
337 git_vector_pop(&ign
->ign_path
);
338 git_attr_file__free(file
);
343 if (--ign
->depth
> 0) {
344 git_buf_rtruncate_at_char(&ign
->dir
, '/');
345 git_path_to_dir(&ign
->dir
);
351 void git_ignore__free(git_ignores
*ignores
)
356 git_attr_file__free(ignores
->ign_internal
);
358 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
359 git_attr_file__free(file
);
360 ignores
->ign_path
.contents
[i
] = NULL
;
362 git_vector_free(&ignores
->ign_path
);
364 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
365 git_attr_file__free(file
);
366 ignores
->ign_global
.contents
[i
] = NULL
;
368 git_vector_free(&ignores
->ign_global
);
370 git_buf_free(&ignores
->dir
);
373 static bool ignore_lookup_in_rules(
374 int *ignored
, git_attr_file
*file
, git_attr_path
*path
)
377 git_attr_fnmatch
*match
;
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
;
390 int git_ignore__lookup(
391 int *out
, git_ignores
*ignores
, const char *pathname
, git_dir_flag dir_flag
)
397 *out
= GIT_IGNORE_NOTFOUND
;
399 if (git_attr_path__init(
400 &path
, pathname
, git_repository_workdir(ignores
->repo
), dir_flag
) < 0)
403 /* first process builtins - success means path was found */
404 if (ignore_lookup_in_rules(out
, ignores
->ign_internal
, &path
))
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
))
413 /* last process global ignores */
414 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
415 if (ignore_lookup_in_rules(out
, file
, &path
))
420 git_attr_path__free(&path
);
424 int git_ignore_add_rule(git_repository
*repo
, const char *rules
)
427 git_attr_file
*ign_internal
= NULL
;
429 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
432 error
= parse_ignore_file(repo
, ign_internal
, rules
);
433 git_attr_file__free(ign_internal
);
438 int git_ignore_clear_internal_rules(git_repository
*repo
)
441 git_attr_file
*ign_internal
;
443 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
446 if (!(error
= git_attr_file__clear_rules(ign_internal
, true)))
447 error
= parse_ignore_file(
448 repo
, ign_internal
, GIT_IGNORE_DEFAULT_RULES
);
450 git_attr_file__free(ign_internal
);
454 int git_ignore_path_is_ignored(
456 git_repository
*repo
,
457 const char *pathname
)
466 assert(ignored
&& pathname
);
468 workdir
= repo
? git_repository_workdir(repo
) : NULL
;
470 memset(&path
, 0, sizeof(path
));
471 memset(&ignores
, 0, sizeof(ignores
));
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)
478 /* first process builtins - success means path was found */
479 if (ignore_lookup_in_rules(ignored
, ignores
.ign_internal
, &path
))
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
))
488 /* last process global ignores */
489 git_vector_foreach(&ignores
.ign_global
, i
, file
) {
490 if (ignore_lookup_in_rules(ignored
, file
, &path
))
494 /* move up one directory */
495 if (path
.basename
== path
.path
)
497 path
.basename
[-1] = '\0';
498 while (path
.basename
> path
.path
&& *path
.basename
!= '/')
500 if (path
.basename
> path
.path
)
504 if ((error
= git_ignore__pop_dir(&ignores
)) < 0)
511 git_attr_path__free(&path
);
512 git_ignore__free(&ignores
);
516 int git_ignore__check_pathspec_for_exact_ignores(
517 git_repository
*repo
,
523 git_attr_fnmatch
*match
;
525 git_buf path
= GIT_BUF_INIT
;
526 const char *wd
, *filename
;
529 if ((error
= git_repository__ensure_not_bare(
530 repo
, "validate pathspec")) < 0 ||
531 (error
= git_repository_index(&idx
, repo
)) < 0)
534 wd
= git_repository_workdir(repo
);
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 &&
542 filename
= match
->pattern
;
544 /* if file is already in the index, it's fine */
545 if (git_index_get_bypath(idx
, filename
, 0) != NULL
)
548 if ((error
= git_buf_joinpath(&path
, wd
, filename
)) < 0)
551 /* is there a file on disk that matches this exactly? */
552 if (!git_path_isfile(path
.ptr
))
555 /* is that file ignored? */
556 if ((error
= git_ignore_path_is_ignored(&ignored
, repo
, filename
)) < 0)
560 giterr_set(GITERR_INVALID
, "pathspec contains ignored file '%s'",
562 error
= GIT_EINVALIDSPEC
;