Blame src/ignore.c

Packit Service 20376f
#include "git2/ignore.h"
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "ignore.h"
Packit Service 20376f
#include "attrcache.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "config.h"
Packit Service 20376f
#include "fnmatch.h"
Packit Service 20376f
Packit Service 20376f
#define GIT_IGNORE_INTERNAL		"[internal]exclude"
Packit Service 20376f
Packit Service 20376f
#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * A negative ignore pattern can negate a positive one without
Packit Service 20376f
 * wildcards if it is a basename only and equals the basename of
Packit Service 20376f
 * the positive pattern. Thus
Packit Service 20376f
 *
Packit Service 20376f
 * foo/bar
Packit Service 20376f
 * !bar
Packit Service 20376f
 *
Packit Service 20376f
 * would result in foo/bar being unignored again while
Packit Service 20376f
 *
Packit Service 20376f
 * moo/foo/bar
Packit Service 20376f
 * !foo/bar
Packit Service 20376f
 *
Packit Service 20376f
 * would do nothing. The reverse also holds true: a positive
Packit Service 20376f
 * basename pattern can be negated by unignoring the basename in
Packit Service 20376f
 * subdirectories. Thus
Packit Service 20376f
 *
Packit Service 20376f
 * bar
Packit Service 20376f
 * !foo/bar
Packit Service 20376f
 *
Packit Service 20376f
 * would result in foo/bar being unignored again. As with the
Packit Service 20376f
 * first case,
Packit Service 20376f
 *
Packit Service 20376f
 * foo/bar
Packit Service 20376f
 * !moo/foo/bar
Packit Service 20376f
 *
Packit Service 20376f
 * would do nothing, again.
Packit Service 20376f
 */
Packit Service 20376f
static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg)
Packit Service 20376f
{
Packit Service 20376f
	int (*cmp)(const char *, const char *, size_t);
Packit Service 20376f
	git_attr_fnmatch *longer, *shorter;
Packit Service 20376f
	char *p;
Packit Service 20376f
Packit Service 20376f
	if ((rule->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0
Packit Service 20376f
	    || (neg->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (neg->flags & GIT_ATTR_FNMATCH_ICASE)
Packit Service 20376f
		cmp = git__strncasecmp;
Packit Service 20376f
	else
Packit Service 20376f
		cmp = git__strncmp;
Packit Service 20376f
Packit Service 20376f
	/* If lengths match we need to have an exact match */
Packit Service 20376f
	if (rule->length == neg->length) {
Packit Service 20376f
		return cmp(rule->pattern, neg->pattern, rule->length) == 0;
Packit Service 20376f
	} else if (rule->length < neg->length) {
Packit Service 20376f
		shorter = rule;
Packit Service 20376f
		longer = neg;
Packit Service 20376f
	} else {
Packit Service 20376f
		shorter = neg;
Packit Service 20376f
		longer = rule;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Otherwise, we need to check if the shorter
Packit Service 20376f
	 * rule is a basename only (that is, it contains
Packit Service 20376f
	 * no path separator) and, if so, if it
Packit Service 20376f
	 * matches the tail of the longer rule */
Packit Service 20376f
	p = longer->pattern + longer->length - shorter->length;
Packit Service 20376f
Packit Service 20376f
	if (p[-1] != '/')
Packit Service 20376f
		return false;
Packit Service 20376f
	if (memchr(shorter->pattern, '/', shorter->length) != NULL)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return cmp(p, shorter->pattern, shorter->length) == 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * A negative ignore can only unignore a file which is given explicitly before, thus
Packit Service 20376f
 *
Packit Service 20376f
 *    foo
Packit Service 20376f
 *    !foo/bar
Packit Service 20376f
 *
Packit Service 20376f
 * does not unignore 'foo/bar' as it's not in the list. However
Packit Service 20376f
 *
Packit Service 20376f
 *    foo/<star>
Packit Service 20376f
 *    !foo/bar
Packit Service 20376f
 *
Packit Service 20376f
 * does unignore 'foo/bar', as it is contained within the 'foo/<star>' rule.
Packit Service 20376f
 */
Packit Service 20376f
static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0, fnflags;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_attr_fnmatch *rule;
Packit Service 20376f
	char *path;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	*out = 0;
Packit Service 20376f
Packit Service 20376f
	fnflags = FNM_PATHNAME;
Packit Service 20376f
	if (match->flags & GIT_ATTR_FNMATCH_ICASE)
Packit Service 20376f
		fnflags |= FNM_IGNORECASE;
Packit Service 20376f
Packit Service 20376f
	/* path of the file relative to the workdir, so we match the rules in subdirs */
Packit Service 20376f
	if (match->containing_dir) {
Packit Service 20376f
		git_buf_puts(&buf, match->containing_dir);
Packit Service 20376f
	}
Packit Service 20376f
	if (git_buf_puts(&buf, match->pattern) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	path = git_buf_detach(&buf;;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(rules, i, rule) {
Packit Service 20376f
		if (!(rule->flags & GIT_ATTR_FNMATCH_HASWILD)) {
Packit Service 20376f
			if (does_negate_pattern(rule, match)) {
Packit Service 20376f
				error = 0;
Packit Service 20376f
				*out = 1;
Packit Service 20376f
				goto out;
Packit Service 20376f
			}
Packit Service 20376f
			else
Packit Service 20376f
				continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * When dealing with a directory, we add '/<star>' so
Packit Service 20376f
		 * p_fnmatch() honours FNM_PATHNAME. Checking for LEADINGDIR
Packit Service 20376f
		 * alone isn't enough as that's also set for nagations, so we
Packit Service 20376f
		 * need to check that NEGATIVE is off.
Packit Service 20376f
		 */
Packit Service 20376f
		git_buf_clear(&buf;;
Packit Service 20376f
		if (rule->containing_dir) {
Packit Service 20376f
			git_buf_puts(&buf, rule->containing_dir);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		error = git_buf_puts(&buf, rule->pattern);
Packit Service 20376f
Packit Service 20376f
		if ((rule->flags & (GIT_ATTR_FNMATCH_LEADINGDIR | GIT_ATTR_FNMATCH_NEGATIVE)) == GIT_ATTR_FNMATCH_LEADINGDIR)
Packit Service 20376f
			error = git_buf_PUTS(&buf, "/*");
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto out;
Packit Service 20376f
Packit Service 20376f
		if ((error = p_fnmatch(git_buf_cstr(&buf), path, fnflags)) < 0) {
Packit Service 20376f
			giterr_set(GITERR_INVALID, "error matching pattern");
Packit Service 20376f
			goto out;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* if we found a match, we want to keep this rule */
Packit Service 20376f
		if (error != FNM_NOMATCH) {
Packit Service 20376f
			*out = 1;
Packit Service 20376f
			error = 0;
Packit Service 20376f
			goto out;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git__free(path);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_ignore_file(
Packit Service 20376f
	git_repository *repo, git_attr_file *attrs, const char *data)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	int ignore_case = false;
Packit Service 20376f
	const char *scan = data, *context = NULL;
Packit Service 20376f
	git_attr_fnmatch *match = NULL;
Packit Service 20376f
Packit Service 20376f
	if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
	/* if subdir file path, convert context for file paths */
Packit Service 20376f
	if (attrs->entry &&
Packit Service 20376f
		git_path_root(attrs->entry->path) < 0 &&
Packit Service 20376f
		!git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE))
Packit Service 20376f
		context = attrs->entry->path;
Packit Service 20376f
Packit Service 20376f
	if (git_mutex_lock(&attrs->lock) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to lock ignore file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	while (!error && *scan) {
Packit Service 20376f
		int valid_rule = 1;
Packit Service 20376f
Packit Service 20376f
		if (!match && !(match = git__calloc(1, sizeof(*match)))) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG;
Packit Service 20376f
Packit Service 20376f
		if (!(error = git_attr_fnmatch__parse(
Packit Service 20376f
			match, &attrs->pool, context, &scan)))
Packit Service 20376f
		{
Packit Service 20376f
			match->flags |= GIT_ATTR_FNMATCH_IGNORE;
Packit Service 20376f
Packit Service 20376f
			if (ignore_case)
Packit Service 20376f
				match->flags |= GIT_ATTR_FNMATCH_ICASE;
Packit Service 20376f
Packit Service 20376f
			while (match->length > 0) {
Packit Service 20376f
				if (match->pattern[match->length - 1] == ' ' ||
Packit Service 20376f
				    match->pattern[match->length - 1] == '\t') {
Packit Service 20376f
					match->pattern[match->length - 1] = 0;
Packit Service 20376f
					match->length --;
Packit Service 20376f
				} else {
Packit Service 20376f
					break;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			scan = git__next_line(scan);
Packit Service 20376f
Packit Service 20376f
			/*
Packit Service 20376f
			 * If a negative match doesn't actually do anything,
Packit Service 20376f
			 * throw it away. As we cannot always verify whether a
Packit Service 20376f
			 * rule containing wildcards negates another rule, we
Packit Service 20376f
			 * do not optimize away these rules, though.
Packit Service 20376f
			 * */
Packit Service 20376f
			if (match->flags & GIT_ATTR_FNMATCH_NEGATIVE
Packit Service 20376f
			    && !(match->flags & GIT_ATTR_FNMATCH_HASWILD))
Packit Service 20376f
				error = does_negate_rule(&valid_rule, &attrs->rules, match);
Packit Service 20376f
Packit Service 20376f
			if (!error && valid_rule)
Packit Service 20376f
				error = git_vector_insert(&attrs->rules, match);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (error != 0 || !valid_rule) {
Packit Service 20376f
			match->pattern = NULL;
Packit Service 20376f
Packit Service 20376f
			if (error == GIT_ENOTFOUND)
Packit Service 20376f
				error = 0;
Packit Service 20376f
		} else {
Packit Service 20376f
			match = NULL; /* vector now "owns" the match */
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_mutex_unlock(&attrs->lock);
Packit Service 20376f
	git__free(match);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int push_ignore_file(
Packit Service 20376f
	git_ignores *ignores,
Packit Service 20376f
	git_vector *which_list,
Packit Service 20376f
	const char *base,
Packit Service 20376f
	const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_attr_file *file = NULL;
Packit Service 20376f
Packit Service 20376f
	error = git_attr_cache__get(
Packit Service 20376f
		&file, ignores->repo, NULL, GIT_ATTR_FILE__FROM_FILE,
Packit Service 20376f
		base, filename, parse_ignore_file);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (file != NULL) {
Packit Service 20376f
		if ((error = git_vector_insert(which_list, file)) < 0)
Packit Service 20376f
			git_attr_file__free(file);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int push_one_ignore(void *payload, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_ignores *ign = payload;
Packit Service 20376f
	ign->depth++;
Packit Service 20376f
	return push_ignore_file(ign, &ign->ign_path, path, GIT_IGNORE_FILE);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int get_internal_ignores(git_attr_file **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_attr_cache__init(repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = git_attr_cache__get(
Packit Service 20376f
		out, repo, NULL, GIT_ATTR_FILE__IN_MEMORY, NULL, GIT_IGNORE_INTERNAL, NULL);
Packit Service 20376f
Packit Service 20376f
	/* if internal rules list is empty, insert default rules */
Packit Service 20376f
	if (!error && !(*out)->rules.length)
Packit Service 20376f
		error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore__for_path(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	git_ignores *ignores)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	const char *workdir = git_repository_workdir(repo);
Packit Service 20376f
	git_buf infopath = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(repo && ignores && path);
Packit Service 20376f
Packit Service 20376f
	memset(ignores, 0, sizeof(*ignores));
Packit Service 20376f
	ignores->repo = repo;
Packit Service 20376f
Packit Service 20376f
	/* Read the ignore_case flag */
Packit Service 20376f
	if ((error = git_repository__cvar(
Packit Service 20376f
			&ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_attr_cache__init(repo)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* given a unrooted path in a non-bare repo, resolve it */
Packit Service 20376f
	if (workdir && git_path_root(path) < 0) {
Packit Service 20376f
		git_buf local = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_path_dirname_r(&local, path)) < 0 ||
Packit Service 20376f
		    (error = git_path_resolve_relative(&local, 0)) < 0 ||
Packit Service 20376f
		    (error = git_path_to_dir(&local)) < 0 ||
Packit Service 20376f
		    (error = git_buf_joinpath(&ignores->dir, workdir, local.ptr)) < 0)
Packit Service 20376f
		{;} /* Nothing, we just want to stop on the first error */
Packit Service 20376f
		git_buf_free(&local);
Packit Service 20376f
	} else {
Packit Service 20376f
		error = git_buf_joinpath(&ignores->dir, path, "");
Packit Service 20376f
	}
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (workdir && !git__prefixcmp(ignores->dir.ptr, workdir))
Packit Service 20376f
		ignores->dir_root = strlen(workdir);
Packit Service 20376f
Packit Service 20376f
	/* set up internals */
Packit Service 20376f
	if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* load .gitignore up the path */
Packit Service 20376f
	if (workdir != NULL) {
Packit Service 20376f
		error = git_path_walk_up(
Packit Service 20376f
			&ignores->dir, workdir, push_one_ignore, ignores);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_item_path(&infopath,
Packit Service 20376f
			repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* load .git/info/exclude */
Packit Service 20376f
	error = push_ignore_file(
Packit Service 20376f
		ignores, &ignores->ign_global,
Packit Service 20376f
		infopath.ptr, GIT_IGNORE_FILE_INREPO);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* load core.excludesfile */
Packit Service 20376f
	if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
Packit Service 20376f
		error = push_ignore_file(
Packit Service 20376f
			ignores, &ignores->ign_global, NULL,
Packit Service 20376f
			git_repository_attr_cache(repo)->cfg_excl_file);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&infopath);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		git_ignore__free(ignores);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore__push_dir(git_ignores *ign, const char *dir)
Packit Service 20376f
{
Packit Service 20376f
	if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	ign->depth++;
Packit Service 20376f
Packit Service 20376f
	return push_ignore_file(
Packit Service 20376f
		ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore__pop_dir(git_ignores *ign)
Packit Service 20376f
{
Packit Service 20376f
	if (ign->ign_path.length > 0) {
Packit Service 20376f
		git_attr_file *file = git_vector_last(&ign->ign_path);
Packit Service 20376f
		const char *start = file->entry->path, *end;
Packit Service 20376f
Packit Service 20376f
		/* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
Packit Service 20376f
		 * - file->path looks something like "a/b/.gitignore
Packit Service 20376f
		 *
Packit Service 20376f
		 * We are popping the last directory off ign->dir.  We also want
Packit Service 20376f
		 * to remove the file from the vector if the popped directory
Packit Service 20376f
		 * matches the ignore path.  We need to test if the "a/b" part of
Packit Service 20376f
		 * the file key matches the path we are about to pop.
Packit Service 20376f
		 */
Packit Service 20376f
Packit Service 20376f
		if ((end = strrchr(start, '/')) != NULL) {
Packit Service 20376f
			size_t dirlen = (end - start) + 1;
Packit Service 20376f
			const char *relpath = ign->dir.ptr + ign->dir_root;
Packit Service 20376f
			size_t pathlen = ign->dir.size - ign->dir_root;
Packit Service 20376f
Packit Service 20376f
			if (pathlen == dirlen && !memcmp(relpath, start, dirlen)) {
Packit Service 20376f
				git_vector_pop(&ign->ign_path);
Packit Service 20376f
				git_attr_file__free(file);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (--ign->depth > 0) {
Packit Service 20376f
		git_buf_rtruncate_at_char(&ign->dir, '/');
Packit Service 20376f
		git_path_to_dir(&ign->dir);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_ignore__free(git_ignores *ignores)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	git_attr_file *file;
Packit Service 20376f
Packit Service 20376f
	git_attr_file__free(ignores->ign_internal);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&ignores->ign_path, i, file) {
Packit Service 20376f
		git_attr_file__free(file);
Packit Service 20376f
		ignores->ign_path.contents[i] = NULL;
Packit Service 20376f
	}
Packit Service 20376f
	git_vector_free(&ignores->ign_path);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&ignores->ign_global, i, file) {
Packit Service 20376f
		git_attr_file__free(file);
Packit Service 20376f
		ignores->ign_global.contents[i] = NULL;
Packit Service 20376f
	}
Packit Service 20376f
	git_vector_free(&ignores->ign_global);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&ignores->dir);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool ignore_lookup_in_rules(
Packit Service 20376f
	int *ignored, git_attr_file *file, git_attr_path *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t j;
Packit Service 20376f
	git_attr_fnmatch *match;
Packit Service 20376f
Packit Service 20376f
	git_vector_rforeach(&file->rules, j, match) {
Packit Service 20376f
		if (git_attr_fnmatch__match(match, path)) {
Packit Service 20376f
			*ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) ?
Packit Service 20376f
				GIT_IGNORE_TRUE : GIT_IGNORE_FALSE;
Packit Service 20376f
			return true;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore__lookup(
Packit Service 20376f
	int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	git_attr_file *file;
Packit Service 20376f
	git_attr_path path;
Packit Service 20376f
Packit Service 20376f
	*out = GIT_IGNORE_NOTFOUND;
Packit Service 20376f
Packit Service 20376f
	if (git_attr_path__init(
Packit Service 20376f
		&path, pathname, git_repository_workdir(ignores->repo), dir_flag) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* first process builtins - success means path was found */
Packit Service 20376f
	if (ignore_lookup_in_rules(out, ignores->ign_internal, &path))
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* next process files in the path */
Packit Service 20376f
	git_vector_foreach(&ignores->ign_path, i, file) {
Packit Service 20376f
		if (ignore_lookup_in_rules(out, file, &path))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* last process global ignores */
Packit Service 20376f
	git_vector_foreach(&ignores->ign_global, i, file) {
Packit Service 20376f
		if (ignore_lookup_in_rules(out, file, &path))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_attr_path__free(&path);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore_add_rule(git_repository *repo, const char *rules)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_attr_file *ign_internal = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = parse_ignore_file(repo, ign_internal, rules);
Packit Service 20376f
	git_attr_file__free(ign_internal);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore_clear_internal_rules(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_attr_file *ign_internal;
Packit Service 20376f
Packit Service 20376f
	if ((error = get_internal_ignores(&ign_internal, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_attr_file__clear_rules(ign_internal, true)))
Packit Service 20376f
		error = parse_ignore_file(
Packit Service 20376f
			repo, ign_internal, GIT_IGNORE_DEFAULT_RULES);
Packit Service 20376f
Packit Service 20376f
	git_attr_file__free(ign_internal);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore_path_is_ignored(
Packit Service 20376f
	int *ignored,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *pathname)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	const char *workdir;
Packit Service 20376f
	git_attr_path path;
Packit Service 20376f
	git_ignores ignores;
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	git_attr_file *file;
Packit Service 20376f
Packit Service 20376f
	assert(repo && ignored && pathname);
Packit Service 20376f
Packit Service 20376f
	workdir = git_repository_workdir(repo);
Packit Service 20376f
Packit Service 20376f
	memset(&path, 0, sizeof(path));
Packit Service 20376f
	memset(&ignores, 0, sizeof(ignores));
Packit Service 20376f
Packit Service 20376f
	if ((error = git_attr_path__init(&path, pathname, workdir, GIT_DIR_FLAG_UNKNOWN)) < 0 ||
Packit Service 20376f
		(error = git_ignore__for_path(repo, path.path, &ignores)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	while (1) {
Packit Service 20376f
		/* first process builtins - success means path was found */
Packit Service 20376f
		if (ignore_lookup_in_rules(ignored, ignores.ign_internal, &path))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		/* next process files in the path */
Packit Service 20376f
		git_vector_foreach(&ignores.ign_path, i, file) {
Packit Service 20376f
			if (ignore_lookup_in_rules(ignored, file, &path))
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* last process global ignores */
Packit Service 20376f
		git_vector_foreach(&ignores.ign_global, i, file) {
Packit Service 20376f
			if (ignore_lookup_in_rules(ignored, file, &path))
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* move up one directory */
Packit Service 20376f
		if (path.basename == path.path)
Packit Service 20376f
			break;
Packit Service 20376f
		path.basename[-1] = '\0';
Packit Service 20376f
		while (path.basename > path.path && *path.basename != '/')
Packit Service 20376f
			path.basename--;
Packit Service 20376f
		if (path.basename > path.path)
Packit Service 20376f
			path.basename++;
Packit Service 20376f
		path.is_dir = 1;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_ignore__pop_dir(&ignores)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*ignored = 0;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_attr_path__free(&path);
Packit Service 20376f
	git_ignore__free(&ignores);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_ignore__check_pathspec_for_exact_ignores(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_vector *vspec,
Packit Service 20376f
	bool no_fnmatch)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_attr_fnmatch *match;
Packit Service 20376f
	int ignored;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	const char *wd, *filename;
Packit Service 20376f
	git_index *idx;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository__ensure_not_bare(
Packit Service 20376f
			repo, "validate pathspec")) < 0 ||
Packit Service 20376f
		(error = git_repository_index(&idx, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	wd = git_repository_workdir(repo);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(vspec, i, match) {
Packit Service 20376f
		/* skip wildcard matches (if they are being used) */
Packit Service 20376f
		if ((match->flags & GIT_ATTR_FNMATCH_HASWILD) != 0 &&
Packit Service 20376f
			!no_fnmatch)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		filename = match->pattern;
Packit Service 20376f
Packit Service 20376f
		/* if file is already in the index, it's fine */
Packit Service 20376f
		if (git_index_get_bypath(idx, filename, 0) != NULL)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_joinpath(&path, wd, filename)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		/* is there a file on disk that matches this exactly? */
Packit Service 20376f
		if (!git_path_isfile(path.ptr))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* is that file ignored? */
Packit Service 20376f
		if ((error = git_ignore_path_is_ignored(&ignored, repo, filename)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (ignored) {
Packit Service 20376f
			giterr_set(GITERR_INVALID, "pathspec contains ignored file '%s'",
Packit Service 20376f
				filename);
Packit Service 20376f
			error = GIT_EINVALIDSPEC;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_index_free(idx);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f