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
)) {
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.
98 if (rule
->containing_dir
) {
99 git_buf_puts(&buf
, rule
->containing_dir
);
102 error
= git_buf_puts(&buf
, rule
->pattern
);
104 if ((rule
->flags
& (GIT_ATTR_FNMATCH_LEADINGDIR
| GIT_ATTR_FNMATCH_NEGATIVE
)) == GIT_ATTR_FNMATCH_LEADINGDIR
)
105 error
= git_buf_PUTS(&buf
, "/*");
110 if ((error
= p_fnmatch(git_buf_cstr(&buf
), path
, FNM_PATHNAME
)) < 0) {
111 giterr_set(GITERR_INVALID
, "error matching pattern");
115 /* if we found a match, we want to keep this rule */
116 if (error
!= FNM_NOMATCH
) {
131 static int parse_ignore_file(
132 git_repository
*repo
, git_attr_file
*attrs
, const char *data
)
135 int ignore_case
= false;
136 const char *scan
= data
, *context
= NULL
;
137 git_attr_fnmatch
*match
= NULL
;
139 if (git_repository__cvar(&ignore_case
, repo
, GIT_CVAR_IGNORECASE
) < 0)
142 /* if subdir file path, convert context for file paths */
144 git_path_root(attrs
->entry
->path
) < 0 &&
145 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_IGNORE_FILE
))
146 context
= attrs
->entry
->path
;
148 if (git_mutex_lock(&attrs
->lock
) < 0) {
149 giterr_set(GITERR_OS
, "Failed to lock ignore file");
153 while (!error
&& *scan
) {
156 if (!match
&& !(match
= git__calloc(1, sizeof(*match
)))) {
161 match
->flags
= GIT_ATTR_FNMATCH_ALLOWSPACE
| GIT_ATTR_FNMATCH_ALLOWNEG
;
163 if (!(error
= git_attr_fnmatch__parse(
164 match
, &attrs
->pool
, context
, &scan
)))
166 match
->flags
|= GIT_ATTR_FNMATCH_IGNORE
;
169 match
->flags
|= GIT_ATTR_FNMATCH_ICASE
;
171 scan
= git__next_line(scan
);
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
);
177 if (!error
&& valid_rule
)
178 error
= git_vector_insert(&attrs
->rules
, match
);
181 if (error
!= 0 || !valid_rule
) {
182 match
->pattern
= NULL
;
184 if (error
== GIT_ENOTFOUND
)
187 match
= NULL
; /* vector now "owns" the match */
191 git_mutex_unlock(&attrs
->lock
);
197 static int push_ignore_file(
198 git_ignores
*ignores
,
199 git_vector
*which_list
,
201 const char *filename
)
204 git_attr_file
*file
= NULL
;
206 error
= git_attr_cache__get(
207 &file
, ignores
->repo
, NULL
, GIT_ATTR_FILE__FROM_FILE
,
208 base
, filename
, parse_ignore_file
);
213 if ((error
= git_vector_insert(which_list
, file
)) < 0)
214 git_attr_file__free(file
);
220 static int push_one_ignore(void *payload
, const char *path
)
222 git_ignores
*ign
= payload
;
224 return push_ignore_file(ign
, &ign
->ign_path
, path
, GIT_IGNORE_FILE
);
227 static int get_internal_ignores(git_attr_file
**out
, git_repository
*repo
)
231 if ((error
= git_attr_cache__init(repo
)) < 0)
234 error
= git_attr_cache__get(
235 out
, repo
, NULL
, GIT_ATTR_FILE__IN_MEMORY
, NULL
, GIT_IGNORE_INTERNAL
, NULL
);
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
);
244 int git_ignore__for_path(
245 git_repository
*repo
,
247 git_ignores
*ignores
)
250 const char *workdir
= git_repository_workdir(repo
);
252 assert(ignores
&& path
);
254 memset(ignores
, 0, sizeof(*ignores
));
255 ignores
->repo
= repo
;
257 /* Read the ignore_case flag */
258 if ((error
= git_repository__cvar(
259 &ignores
->ignore_case
, repo
, GIT_CVAR_IGNORECASE
)) < 0)
262 if ((error
= git_attr_cache__init(repo
)) < 0)
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
;
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
);
276 error
= git_buf_joinpath(&ignores
->dir
, path
, "");
281 if (workdir
&& !git__prefixcmp(ignores
->dir
.ptr
, workdir
))
282 ignores
->dir_root
= strlen(workdir
);
284 /* set up internals */
285 if ((error
= get_internal_ignores(&ignores
->ign_internal
, repo
)) < 0)
288 /* load .gitignore up the path */
289 if (workdir
!= NULL
) {
290 error
= git_path_walk_up(
291 &ignores
->dir
, workdir
, push_one_ignore
, ignores
);
296 /* load .git/info/exclude */
297 error
= push_ignore_file(
298 ignores
, &ignores
->ign_global
,
299 git_repository_path(repo
), GIT_IGNORE_FILE_INREPO
);
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
);
311 git_ignore__free(ignores
);
316 int git_ignore__push_dir(git_ignores
*ign
, const char *dir
)
318 if (git_buf_joinpath(&ign
->dir
, ign
->dir
.ptr
, dir
) < 0)
323 return push_ignore_file(
324 ign
, &ign
->ign_path
, ign
->dir
.ptr
, GIT_IGNORE_FILE
);
327 int git_ignore__pop_dir(git_ignores
*ign
)
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
;
333 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
334 * - file->path looks something like "a/b/.gitignore
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.
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
;
347 if (pathlen
== dirlen
&& !memcmp(relpath
, start
, dirlen
)) {
348 git_vector_pop(&ign
->ign_path
);
349 git_attr_file__free(file
);
354 if (--ign
->depth
> 0) {
355 git_buf_rtruncate_at_char(&ign
->dir
, '/');
356 git_path_to_dir(&ign
->dir
);
362 void git_ignore__free(git_ignores
*ignores
)
367 git_attr_file__free(ignores
->ign_internal
);
369 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
370 git_attr_file__free(file
);
371 ignores
->ign_path
.contents
[i
] = NULL
;
373 git_vector_free(&ignores
->ign_path
);
375 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
376 git_attr_file__free(file
);
377 ignores
->ign_global
.contents
[i
] = NULL
;
379 git_vector_free(&ignores
->ign_global
);
381 git_buf_free(&ignores
->dir
);
384 static bool ignore_lookup_in_rules(
385 int *ignored
, git_attr_file
*file
, git_attr_path
*path
)
388 git_attr_fnmatch
*match
;
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
;
401 int git_ignore__lookup(
402 int *out
, git_ignores
*ignores
, const char *pathname
, git_dir_flag dir_flag
)
408 *out
= GIT_IGNORE_NOTFOUND
;
410 if (git_attr_path__init(
411 &path
, pathname
, git_repository_workdir(ignores
->repo
), dir_flag
) < 0)
414 /* first process builtins - success means path was found */
415 if (ignore_lookup_in_rules(out
, ignores
->ign_internal
, &path
))
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
))
424 /* last process global ignores */
425 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
426 if (ignore_lookup_in_rules(out
, file
, &path
))
431 git_attr_path__free(&path
);
435 int git_ignore_add_rule(git_repository
*repo
, const char *rules
)
438 git_attr_file
*ign_internal
= NULL
;
440 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
443 error
= parse_ignore_file(repo
, ign_internal
, rules
);
444 git_attr_file__free(ign_internal
);
449 int git_ignore_clear_internal_rules(git_repository
*repo
)
452 git_attr_file
*ign_internal
;
454 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
457 if (!(error
= git_attr_file__clear_rules(ign_internal
, true)))
458 error
= parse_ignore_file(
459 repo
, ign_internal
, GIT_IGNORE_DEFAULT_RULES
);
461 git_attr_file__free(ign_internal
);
465 int git_ignore_path_is_ignored(
467 git_repository
*repo
,
468 const char *pathname
)
477 assert(ignored
&& pathname
);
479 workdir
= repo
? git_repository_workdir(repo
) : NULL
;
481 memset(&path
, 0, sizeof(path
));
482 memset(&ignores
, 0, sizeof(ignores
));
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)
489 /* first process builtins - success means path was found */
490 if (ignore_lookup_in_rules(ignored
, ignores
.ign_internal
, &path
))
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
))
499 /* last process global ignores */
500 git_vector_foreach(&ignores
.ign_global
, i
, file
) {
501 if (ignore_lookup_in_rules(ignored
, file
, &path
))
505 /* move up one directory */
506 if (path
.basename
== path
.path
)
508 path
.basename
[-1] = '\0';
509 while (path
.basename
> path
.path
&& *path
.basename
!= '/')
511 if (path
.basename
> path
.path
)
515 if ((error
= git_ignore__pop_dir(&ignores
)) < 0)
522 git_attr_path__free(&path
);
523 git_ignore__free(&ignores
);
527 int git_ignore__check_pathspec_for_exact_ignores(
528 git_repository
*repo
,
534 git_attr_fnmatch
*match
;
536 git_buf path
= GIT_BUF_INIT
;
537 const char *wd
, *filename
;
540 if ((error
= git_repository__ensure_not_bare(
541 repo
, "validate pathspec")) < 0 ||
542 (error
= git_repository_index(&idx
, repo
)) < 0)
545 wd
= git_repository_workdir(repo
);
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 &&
553 filename
= match
->pattern
;
555 /* if file is already in the index, it's fine */
556 if (git_index_get_bypath(idx
, filename
, 0) != NULL
)
559 if ((error
= git_buf_joinpath(&path
, wd
, filename
)) < 0)
562 /* is there a file on disk that matches this exactly? */
563 if (!git_path_isfile(path
.ptr
))
566 /* is that file ignored? */
567 if ((error
= git_ignore_path_is_ignored(&ignored
, repo
, filename
)) < 0)
571 giterr_set(GITERR_INVALID
, "pathspec contains ignored file '%s'",
573 error
= GIT_EINVALIDSPEC
;