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 negate a positive one without
15 * wildcards if it is a basename only and equals the basename of
16 * the positive pattern. Thus
21 * would result in foo/bar being unignored again while
26 * would do nothing. The reverse also holds true: a positive
27 * basename pattern can be negated by unignoring the basename in
28 * subdirectories. Thus
33 * would result in foo/bar being unignored again. As with the
39 * would do nothing, again.
41 static int does_negate_pattern(git_attr_fnmatch
*rule
, git_attr_fnmatch
*neg
)
43 git_attr_fnmatch
*longer
, *shorter
;
46 if ((rule
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0
47 && (neg
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) != 0) {
49 /* If lengths match we need to have an exact match */
50 if (rule
->length
== neg
->length
) {
51 return strcmp(rule
->pattern
, neg
->pattern
) == 0;
52 } else if (rule
->length
< neg
->length
) {
60 /* Otherwise, we need to check if the shorter
61 * rule is a basename only (that is, it contains
62 * no path separator) and, if so, if it
63 * matches the tail of the longer rule */
64 p
= longer
->pattern
+ longer
->length
- shorter
->length
;
68 if (memchr(shorter
->pattern
, '/', shorter
->length
) != NULL
)
71 return memcmp(p
, shorter
->pattern
, shorter
->length
) == 0;
78 * A negative ignore can only unignore a file which is given explicitly before, thus
83 * does not unignore 'foo/bar' as it's not in the list. However
88 * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule.
90 static int does_negate_rule(int *out
, git_vector
*rules
, git_attr_fnmatch
*match
)
94 git_attr_fnmatch
*rule
;
96 git_buf buf
= GIT_BUF_INIT
;
100 /* path of the file relative to the workdir, so we match the rules in subdirs */
101 if (match
->containing_dir
) {
102 git_buf_puts(&buf
, match
->containing_dir
);
104 if (git_buf_puts(&buf
, match
->pattern
) < 0)
107 path
= git_buf_detach(&buf
);
109 git_vector_foreach(rules
, i
, rule
) {
110 if (!(rule
->flags
& GIT_ATTR_FNMATCH_HASWILD
)) {
111 if (does_negate_pattern(rule
, match
)) {
121 * When dealing with a directory, we add '/<star>' so
122 * p_fnmatch() honours FNM_PATHNAME. Checking for LEADINGDIR
123 * alone isn't enough as that's also set for nagations, so we
124 * need to check that NEGATIVE is off.
127 if (rule
->containing_dir
) {
128 git_buf_puts(&buf
, rule
->containing_dir
);
131 error
= git_buf_puts(&buf
, rule
->pattern
);
133 if ((rule
->flags
& (GIT_ATTR_FNMATCH_LEADINGDIR
| GIT_ATTR_FNMATCH_NEGATIVE
)) == GIT_ATTR_FNMATCH_LEADINGDIR
)
134 error
= git_buf_PUTS(&buf
, "/*");
139 if ((error
= p_fnmatch(git_buf_cstr(&buf
), path
, FNM_PATHNAME
)) < 0) {
140 giterr_set(GITERR_INVALID
, "error matching pattern");
144 /* if we found a match, we want to keep this rule */
145 if (error
!= FNM_NOMATCH
) {
160 static int parse_ignore_file(
161 git_repository
*repo
, git_attr_file
*attrs
, const char *data
)
164 int ignore_case
= false;
165 const char *scan
= data
, *context
= NULL
;
166 git_attr_fnmatch
*match
= NULL
;
168 if (git_repository__cvar(&ignore_case
, repo
, GIT_CVAR_IGNORECASE
) < 0)
171 /* if subdir file path, convert context for file paths */
173 git_path_root(attrs
->entry
->path
) < 0 &&
174 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_IGNORE_FILE
))
175 context
= attrs
->entry
->path
;
177 if (git_mutex_lock(&attrs
->lock
) < 0) {
178 giterr_set(GITERR_OS
, "failed to lock ignore file");
182 while (!error
&& *scan
) {
185 if (!match
&& !(match
= git__calloc(1, sizeof(*match
)))) {
190 match
->flags
= GIT_ATTR_FNMATCH_ALLOWSPACE
| GIT_ATTR_FNMATCH_ALLOWNEG
;
192 if (!(error
= git_attr_fnmatch__parse(
193 match
, &attrs
->pool
, context
, &scan
)))
195 match
->flags
|= GIT_ATTR_FNMATCH_IGNORE
;
198 match
->flags
|= GIT_ATTR_FNMATCH_ICASE
;
200 scan
= git__next_line(scan
);
202 /* if a negative match doesn't actually do anything, throw it away */
203 if (match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
204 error
= does_negate_rule(&valid_rule
, &attrs
->rules
, match
);
206 if (!error
&& valid_rule
)
207 error
= git_vector_insert(&attrs
->rules
, match
);
210 if (error
!= 0 || !valid_rule
) {
211 match
->pattern
= NULL
;
213 if (error
== GIT_ENOTFOUND
)
216 match
= NULL
; /* vector now "owns" the match */
220 git_mutex_unlock(&attrs
->lock
);
226 static int push_ignore_file(
227 git_ignores
*ignores
,
228 git_vector
*which_list
,
230 const char *filename
)
233 git_attr_file
*file
= NULL
;
235 error
= git_attr_cache__get(
236 &file
, ignores
->repo
, NULL
, GIT_ATTR_FILE__FROM_FILE
,
237 base
, filename
, parse_ignore_file
);
242 if ((error
= git_vector_insert(which_list
, file
)) < 0)
243 git_attr_file__free(file
);
249 static int push_one_ignore(void *payload
, const char *path
)
251 git_ignores
*ign
= payload
;
253 return push_ignore_file(ign
, &ign
->ign_path
, path
, GIT_IGNORE_FILE
);
256 static int get_internal_ignores(git_attr_file
**out
, git_repository
*repo
)
260 if ((error
= git_attr_cache__init(repo
)) < 0)
263 error
= git_attr_cache__get(
264 out
, repo
, NULL
, GIT_ATTR_FILE__IN_MEMORY
, NULL
, GIT_IGNORE_INTERNAL
, NULL
);
266 /* if internal rules list is empty, insert default rules */
267 if (!error
&& !(*out
)->rules
.length
)
268 error
= parse_ignore_file(repo
, *out
, GIT_IGNORE_DEFAULT_RULES
);
273 int git_ignore__for_path(
274 git_repository
*repo
,
276 git_ignores
*ignores
)
279 const char *workdir
= git_repository_workdir(repo
);
281 assert(ignores
&& path
);
283 memset(ignores
, 0, sizeof(*ignores
));
284 ignores
->repo
= repo
;
286 /* Read the ignore_case flag */
287 if ((error
= git_repository__cvar(
288 &ignores
->ignore_case
, repo
, GIT_CVAR_IGNORECASE
)) < 0)
291 if ((error
= git_attr_cache__init(repo
)) < 0)
294 /* given a unrooted path in a non-bare repo, resolve it */
295 if (workdir
&& git_path_root(path
) < 0) {
296 git_buf local
= GIT_BUF_INIT
;
298 if ((error
= git_path_dirname_r(&local
, path
)) < 0 ||
299 (error
= git_path_resolve_relative(&local
, 0)) < 0 ||
300 (error
= git_path_to_dir(&local
)) < 0 ||
301 (error
= git_buf_joinpath(&ignores
->dir
, workdir
, local
.ptr
)) < 0)
302 {;} /* Nothing, we just want to stop on the first error */
303 git_buf_free(&local
);
305 error
= git_buf_joinpath(&ignores
->dir
, path
, "");
310 if (workdir
&& !git__prefixcmp(ignores
->dir
.ptr
, workdir
))
311 ignores
->dir_root
= strlen(workdir
);
313 /* set up internals */
314 if ((error
= get_internal_ignores(&ignores
->ign_internal
, repo
)) < 0)
317 /* load .gitignore up the path */
318 if (workdir
!= NULL
) {
319 error
= git_path_walk_up(
320 &ignores
->dir
, workdir
, push_one_ignore
, ignores
);
325 /* load .git/info/exclude */
326 error
= push_ignore_file(
327 ignores
, &ignores
->ign_global
,
328 git_repository_path(repo
), GIT_IGNORE_FILE_INREPO
);
332 /* load core.excludesfile */
333 if (git_repository_attr_cache(repo
)->cfg_excl_file
!= NULL
)
334 error
= push_ignore_file(
335 ignores
, &ignores
->ign_global
, NULL
,
336 git_repository_attr_cache(repo
)->cfg_excl_file
);
340 git_ignore__free(ignores
);
345 int git_ignore__push_dir(git_ignores
*ign
, const char *dir
)
347 if (git_buf_joinpath(&ign
->dir
, ign
->dir
.ptr
, dir
) < 0)
352 return push_ignore_file(
353 ign
, &ign
->ign_path
, ign
->dir
.ptr
, GIT_IGNORE_FILE
);
356 int git_ignore__pop_dir(git_ignores
*ign
)
358 if (ign
->ign_path
.length
> 0) {
359 git_attr_file
*file
= git_vector_last(&ign
->ign_path
);
360 const char *start
= file
->entry
->path
, *end
;
362 /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
363 * - file->path looks something like "a/b/.gitignore
365 * We are popping the last directory off ign->dir. We also want
366 * to remove the file from the vector if the popped directory
367 * matches the ignore path. We need to test if the "a/b" part of
368 * the file key matches the path we are about to pop.
371 if ((end
= strrchr(start
, '/')) != NULL
) {
372 size_t dirlen
= (end
- start
) + 1;
373 const char *relpath
= ign
->dir
.ptr
+ ign
->dir_root
;
374 size_t pathlen
= ign
->dir
.size
- ign
->dir_root
;
376 if (pathlen
== dirlen
&& !memcmp(relpath
, start
, dirlen
)) {
377 git_vector_pop(&ign
->ign_path
);
378 git_attr_file__free(file
);
383 if (--ign
->depth
> 0) {
384 git_buf_rtruncate_at_char(&ign
->dir
, '/');
385 git_path_to_dir(&ign
->dir
);
391 void git_ignore__free(git_ignores
*ignores
)
396 git_attr_file__free(ignores
->ign_internal
);
398 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
399 git_attr_file__free(file
);
400 ignores
->ign_path
.contents
[i
] = NULL
;
402 git_vector_free(&ignores
->ign_path
);
404 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
405 git_attr_file__free(file
);
406 ignores
->ign_global
.contents
[i
] = NULL
;
408 git_vector_free(&ignores
->ign_global
);
410 git_buf_free(&ignores
->dir
);
413 static bool ignore_lookup_in_rules(
414 int *ignored
, git_attr_file
*file
, git_attr_path
*path
)
417 git_attr_fnmatch
*match
;
419 git_vector_rforeach(&file
->rules
, j
, match
) {
420 if (git_attr_fnmatch__match(match
, path
)) {
421 *ignored
= ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) == 0) ?
422 GIT_IGNORE_TRUE
: GIT_IGNORE_FALSE
;
430 int git_ignore__lookup(
431 int *out
, git_ignores
*ignores
, const char *pathname
, git_dir_flag dir_flag
)
437 *out
= GIT_IGNORE_NOTFOUND
;
439 if (git_attr_path__init(
440 &path
, pathname
, git_repository_workdir(ignores
->repo
), dir_flag
) < 0)
443 /* first process builtins - success means path was found */
444 if (ignore_lookup_in_rules(out
, ignores
->ign_internal
, &path
))
447 /* next process files in the path */
448 git_vector_foreach(&ignores
->ign_path
, i
, file
) {
449 if (ignore_lookup_in_rules(out
, file
, &path
))
453 /* last process global ignores */
454 git_vector_foreach(&ignores
->ign_global
, i
, file
) {
455 if (ignore_lookup_in_rules(out
, file
, &path
))
460 git_attr_path__free(&path
);
464 int git_ignore_add_rule(git_repository
*repo
, const char *rules
)
467 git_attr_file
*ign_internal
= NULL
;
469 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
472 error
= parse_ignore_file(repo
, ign_internal
, rules
);
473 git_attr_file__free(ign_internal
);
478 int git_ignore_clear_internal_rules(git_repository
*repo
)
481 git_attr_file
*ign_internal
;
483 if ((error
= get_internal_ignores(&ign_internal
, repo
)) < 0)
486 if (!(error
= git_attr_file__clear_rules(ign_internal
, true)))
487 error
= parse_ignore_file(
488 repo
, ign_internal
, GIT_IGNORE_DEFAULT_RULES
);
490 git_attr_file__free(ign_internal
);
494 int git_ignore_path_is_ignored(
496 git_repository
*repo
,
497 const char *pathname
)
506 assert(ignored
&& pathname
);
508 workdir
= repo
? git_repository_workdir(repo
) : NULL
;
510 memset(&path
, 0, sizeof(path
));
511 memset(&ignores
, 0, sizeof(ignores
));
513 if ((error
= git_attr_path__init(&path
, pathname
, workdir
, GIT_DIR_FLAG_UNKNOWN
)) < 0 ||
514 (error
= git_ignore__for_path(repo
, path
.path
, &ignores
)) < 0)
518 /* first process builtins - success means path was found */
519 if (ignore_lookup_in_rules(ignored
, ignores
.ign_internal
, &path
))
522 /* next process files in the path */
523 git_vector_foreach(&ignores
.ign_path
, i
, file
) {
524 if (ignore_lookup_in_rules(ignored
, file
, &path
))
528 /* last process global ignores */
529 git_vector_foreach(&ignores
.ign_global
, i
, file
) {
530 if (ignore_lookup_in_rules(ignored
, file
, &path
))
534 /* move up one directory */
535 if (path
.basename
== path
.path
)
537 path
.basename
[-1] = '\0';
538 while (path
.basename
> path
.path
&& *path
.basename
!= '/')
540 if (path
.basename
> path
.path
)
544 if ((error
= git_ignore__pop_dir(&ignores
)) < 0)
551 git_attr_path__free(&path
);
552 git_ignore__free(&ignores
);
556 int git_ignore__check_pathspec_for_exact_ignores(
557 git_repository
*repo
,
563 git_attr_fnmatch
*match
;
565 git_buf path
= GIT_BUF_INIT
;
566 const char *wd
, *filename
;
569 if ((error
= git_repository__ensure_not_bare(
570 repo
, "validate pathspec")) < 0 ||
571 (error
= git_repository_index(&idx
, repo
)) < 0)
574 wd
= git_repository_workdir(repo
);
576 git_vector_foreach(vspec
, i
, match
) {
577 /* skip wildcard matches (if they are being used) */
578 if ((match
->flags
& GIT_ATTR_FNMATCH_HASWILD
) != 0 &&
582 filename
= match
->pattern
;
584 /* if file is already in the index, it's fine */
585 if (git_index_get_bypath(idx
, filename
, 0) != NULL
)
588 if ((error
= git_buf_joinpath(&path
, wd
, filename
)) < 0)
591 /* is there a file on disk that matches this exactly? */
592 if (!git_path_isfile(path
.ptr
))
595 /* is that file ignored? */
596 if ((error
= git_ignore_path_is_ignored(&ignored
, repo
, filename
)) < 0)
600 giterr_set(GITERR_INVALID
, "pathspec contains ignored file '%s'",
602 error
= GIT_EINVALIDSPEC
;