Blame src/pathspec.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "git2/pathspec.h"
Packit Service 20376f
#include "git2/diff.h"
Packit Service 20376f
#include "pathspec.h"
Packit Service 20376f
#include "buf_text.h"
Packit Service 20376f
#include "attr_file.h"
Packit Service 20376f
#include "iterator.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
#include "bitvec.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
Packit Service 20376f
/* what is the common non-wildcard prefix for all items in the pathspec */
Packit Service 20376f
char *git_pathspec_prefix(const git_strarray *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	git_buf prefix = GIT_BUF_INIT;
Packit Service 20376f
	const char *scan;
Packit Service 20376f
Packit Service 20376f
	if (!pathspec || !pathspec->count ||
Packit Service 20376f
		git_buf_text_common_prefix(&prefix, pathspec) < 0)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	/* diff prefix will only be leading non-wildcards */
Packit Service 20376f
	for (scan = prefix.ptr; *scan; ++scan) {
Packit Service 20376f
		if (git__iswildcard(*scan) &&
Packit Service 20376f
			(scan == prefix.ptr || (*(scan - 1) != '\\')))
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
	git_buf_truncate(&prefix, scan - prefix.ptr);
Packit Service 20376f
Packit Service 20376f
	if (prefix.size <= 0) {
Packit Service 20376f
		git_buf_free(&prefix);
Packit Service 20376f
		return NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_text_unescape(&prefix);
Packit Service 20376f
Packit Service 20376f
	return git_buf_detach(&prefix);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* is there anything in the spec that needs to be filtered on */
Packit Service 20376f
bool git_pathspec_is_empty(const git_strarray *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	if (pathspec == NULL)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < pathspec->count; ++i) {
Packit Service 20376f
		const char *str = pathspec->strings[i];
Packit Service 20376f
Packit Service 20376f
		if (str && str[0])
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* build a vector of fnmatch patterns to evaluate efficiently */
Packit Service 20376f
int git_pathspec__vinit(
Packit Service 20376f
	git_vector *vspec, const git_strarray *strspec, git_pool *strpool)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	memset(vspec, 0, sizeof(*vspec));
Packit Service 20376f
Packit Service 20376f
	if (git_pathspec_is_empty(strspec))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (git_vector_init(vspec, strspec->count, NULL) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < strspec->count; ++i) {
Packit Service 20376f
		int ret;
Packit Service 20376f
		const char *pattern = strspec->strings[i];
Packit Service 20376f
		git_attr_fnmatch *match = git__calloc(1, sizeof(git_attr_fnmatch));
Packit Service 20376f
		if (!match)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE |
Packit Service 20376f
			GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_NOLEADINGDIR;
Packit Service 20376f
Packit Service 20376f
		ret = git_attr_fnmatch__parse(match, strpool, NULL, &pattern);
Packit Service 20376f
		if (ret == GIT_ENOTFOUND) {
Packit Service 20376f
			git__free(match);
Packit Service 20376f
			continue;
Packit Service 20376f
		} else if (ret < 0) {
Packit Service 20376f
			git__free(match);
Packit Service 20376f
			return ret;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (git_vector_insert(vspec, match) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* free data from the pathspec vector */
Packit Service 20376f
void git_pathspec__vfree(git_vector *vspec)
Packit Service 20376f
{
Packit Service 20376f
	git_vector_free_deep(vspec);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
struct pathspec_match_context {
Packit Service 20376f
	int fnmatch_flags;
Packit Service 20376f
	int (*strcomp)(const char *, const char *);
Packit Service 20376f
	int (*strncomp)(const char *, const char *, size_t);
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static void pathspec_match_context_init(
Packit Service 20376f
	struct pathspec_match_context *ctxt,
Packit Service 20376f
	bool disable_fnmatch,
Packit Service 20376f
	bool casefold)
Packit Service 20376f
{
Packit Service 20376f
	if (disable_fnmatch)
Packit Service 20376f
		ctxt->fnmatch_flags = -1;
Packit Service 20376f
	else if (casefold)
Packit Service 20376f
		ctxt->fnmatch_flags = FNM_CASEFOLD;
Packit Service 20376f
	else
Packit Service 20376f
		ctxt->fnmatch_flags = 0;
Packit Service 20376f
Packit Service 20376f
	if (casefold) {
Packit Service 20376f
		ctxt->strcomp  = git__strcasecmp;
Packit Service 20376f
		ctxt->strncomp = git__strncasecmp;
Packit Service 20376f
	} else {
Packit Service 20376f
		ctxt->strcomp  = git__strcmp;
Packit Service 20376f
		ctxt->strncomp = git__strncmp;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int pathspec_match_one(
Packit Service 20376f
	const git_attr_fnmatch *match,
Packit Service 20376f
	struct pathspec_match_context *ctxt,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int result = (match->flags & GIT_ATTR_FNMATCH_MATCH_ALL) ? 0 : FNM_NOMATCH;
Packit Service 20376f
Packit Service 20376f
	if (result == FNM_NOMATCH)
Packit Service 20376f
		result = ctxt->strcomp(match->pattern, path) ? FNM_NOMATCH : 0;
Packit Service 20376f
Packit Service 20376f
	if (ctxt->fnmatch_flags >= 0 && result == FNM_NOMATCH)
Packit Service 20376f
		result = p_fnmatch(match->pattern, path, ctxt->fnmatch_flags);
Packit Service 20376f
Packit Service 20376f
	/* if we didn't match, look for exact dirname prefix match */
Packit Service 20376f
	if (result == FNM_NOMATCH &&
Packit Service 20376f
		(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
Packit Service 20376f
		ctxt->strncomp(path, match->pattern, match->length) == 0 &&
Packit Service 20376f
		path[match->length] == '/')
Packit Service 20376f
		result = 0;
Packit Service 20376f
Packit Service 20376f
	/* if we didn't match and this is a negative match, check for exact
Packit Service 20376f
	 * match of filename with leading '!'
Packit Service 20376f
	 */
Packit Service 20376f
	if (result == FNM_NOMATCH &&
Packit Service 20376f
		(match->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 &&
Packit Service 20376f
		*path == '!' &&
Packit Service 20376f
		ctxt->strncomp(path + 1, match->pattern, match->length) == 0 &&
Packit Service 20376f
		(!path[match->length + 1] || path[match->length + 1] == '/'))
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (result == 0)
Packit Service 20376f
		return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? 0 : 1;
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int git_pathspec__match_at(
Packit Service 20376f
	size_t *matched_at,
Packit Service 20376f
	const git_vector *vspec,
Packit Service 20376f
	struct pathspec_match_context *ctxt,
Packit Service 20376f
	const char *path0,
Packit Service 20376f
	const char *path1)
Packit Service 20376f
{
Packit Service 20376f
	int result = GIT_ENOTFOUND;
Packit Service 20376f
	size_t i = 0;
Packit Service 20376f
	const git_attr_fnmatch *match;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(vspec, i, match) {
Packit Service 20376f
		if (path0 && (result = pathspec_match_one(match, ctxt, path0)) >= 0)
Packit Service 20376f
			break;
Packit Service 20376f
		if (path1 && (result = pathspec_match_one(match, ctxt, path1)) >= 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*matched_at = i;
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* match a path against the vectorized pathspec */
Packit Service 20376f
bool git_pathspec__match(
Packit Service 20376f
	const git_vector *vspec,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	bool disable_fnmatch,
Packit Service 20376f
	bool casefold,
Packit Service 20376f
	const char **matched_pathspec,
Packit Service 20376f
	size_t *matched_at)
Packit Service 20376f
{
Packit Service 20376f
	int result;
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	struct pathspec_match_context ctxt;
Packit Service 20376f
Packit Service 20376f
	if (matched_pathspec)
Packit Service 20376f
		*matched_pathspec = NULL;
Packit Service 20376f
	if (matched_at)
Packit Service 20376f
		*matched_at = GIT_PATHSPEC_NOMATCH;
Packit Service 20376f
Packit Service 20376f
	if (!vspec || !vspec->length)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	pathspec_match_context_init(&ctxt, disable_fnmatch, casefold);
Packit Service 20376f
Packit Service 20376f
	result = git_pathspec__match_at(&pos, vspec, &ctxt, path, NULL);
Packit Service 20376f
	if (result >= 0) {
Packit Service 20376f
		if (matched_pathspec) {
Packit Service 20376f
			const git_attr_fnmatch *match = git_vector_get(vspec, pos);
Packit Service 20376f
			*matched_pathspec = match->pattern;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (matched_at)
Packit Service 20376f
			*matched_at = pos;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (result > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
int git_pathspec__init(git_pathspec *ps, const git_strarray *paths)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	memset(ps, 0, sizeof(*ps));
Packit Service 20376f
Packit Service 20376f
	ps->prefix = git_pathspec_prefix(paths);
Packit Service 20376f
	git_pool_init(&ps->pool, 1);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_pathspec__vinit(&ps->pathspec, paths, &ps->pool)) < 0)
Packit Service 20376f
		git_pathspec__clear(ps);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_pathspec__clear(git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	git__free(ps->prefix);
Packit Service 20376f
	git_pathspec__vfree(&ps->pathspec);
Packit Service 20376f
	git_pool_clear(&ps->pool);
Packit Service 20376f
	memset(ps, 0, sizeof(*ps));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_new(git_pathspec **out, const git_strarray *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_pathspec *ps = git__malloc(sizeof(git_pathspec));
Packit Service 20376f
	GITERR_CHECK_ALLOC(ps);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_pathspec__init(ps, pathspec)) < 0) {
Packit Service 20376f
		git__free(ps);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(ps);
Packit Service 20376f
	*out = ps;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void pathspec_free(git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	git_pathspec__clear(ps);
Packit Service 20376f
	git__free(ps);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_pathspec_free(git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	if (!ps)
Packit Service 20376f
		return;
Packit Service 20376f
	GIT_REFCOUNT_DEC(ps, pathspec_free);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_matches_path(
Packit Service 20376f
	const git_pathspec *ps, uint32_t flags, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	bool no_fnmatch = (flags & GIT_PATHSPEC_NO_GLOB) != 0;
Packit Service 20376f
	bool casefold =  (flags & GIT_PATHSPEC_IGNORE_CASE) != 0;
Packit Service 20376f
Packit Service 20376f
	assert(ps && path);
Packit Service 20376f
Packit Service 20376f
	return (0 != git_pathspec__match(
Packit Service 20376f
		&ps->pathspec, path, no_fnmatch, casefold, NULL, NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void pathspec_match_free(git_pathspec_match_list *m)
Packit Service 20376f
{
Packit Service 20376f
	if (!m)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_pathspec_free(m->pathspec);
Packit Service 20376f
	m->pathspec = NULL;
Packit Service 20376f
Packit Service 20376f
	git_array_clear(m->matches);
Packit Service 20376f
	git_array_clear(m->failures);
Packit Service 20376f
	git_pool_clear(&m->pool);
Packit Service 20376f
	git__free(m);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_pathspec_match_list *pathspec_match_alloc(
Packit Service 20376f
	git_pathspec *ps, int datatype)
Packit Service 20376f
{
Packit Service 20376f
	git_pathspec_match_list *m = git__calloc(1, sizeof(git_pathspec_match_list));
Packit Service 20376f
	if (!m)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&m->pool, 1);
Packit Service 20376f
Packit Service 20376f
	/* need to keep reference to pathspec and increment refcount because
Packit Service 20376f
	 * failures array stores pointers to the pattern strings of the
Packit Service 20376f
	 * pathspec that had no matches
Packit Service 20376f
	 */
Packit Service 20376f
	GIT_REFCOUNT_INC(ps);
Packit Service 20376f
	m->pathspec = ps;
Packit Service 20376f
	m->datatype = datatype;
Packit Service 20376f
Packit Service 20376f
	return m;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(size_t) pathspec_mark_pattern(git_bitvec *used, size_t pos)
Packit Service 20376f
{
Packit Service 20376f
	if (!git_bitvec_get(used, pos)) {
Packit Service 20376f
		git_bitvec_set(used, pos, true);
Packit Service 20376f
		return 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static size_t pathspec_mark_remaining(
Packit Service 20376f
	git_bitvec *used,
Packit Service 20376f
	git_vector *patterns,
Packit Service 20376f
	struct pathspec_match_context *ctxt,
Packit Service 20376f
	size_t start,
Packit Service 20376f
	const char *path0,
Packit Service 20376f
	const char *path1)
Packit Service 20376f
{
Packit Service 20376f
	size_t count = 0;
Packit Service 20376f
Packit Service 20376f
	if (path1 == path0)
Packit Service 20376f
		path1 = NULL;
Packit Service 20376f
Packit Service 20376f
	for (; start < patterns->length; ++start) {
Packit Service 20376f
		const git_attr_fnmatch *pat = git_vector_get(patterns, start);
Packit Service 20376f
Packit Service 20376f
		if (git_bitvec_get(used, start))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if (path0 && pathspec_match_one(pat, ctxt, path0) > 0)
Packit Service 20376f
			count += pathspec_mark_pattern(used, start);
Packit Service 20376f
		else if (path1 && pathspec_match_one(pat, ctxt, path1) > 0)
Packit Service 20376f
			count += pathspec_mark_pattern(used, start);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return count;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int pathspec_build_failure_array(
Packit Service 20376f
	git_pathspec_string_array_t *failures,
Packit Service 20376f
	git_vector *patterns,
Packit Service 20376f
	git_bitvec *used,
Packit Service 20376f
	git_pool *pool)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	char **failed;
Packit Service 20376f
	const git_attr_fnmatch *pat;
Packit Service 20376f
Packit Service 20376f
	for (pos = 0; pos < patterns->length; ++pos) {
Packit Service 20376f
		if (git_bitvec_get(used, pos))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((failed = git_array_alloc(*failures)) == NULL)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		pat = git_vector_get(patterns, pos);
Packit Service 20376f
Packit Service 20376f
		if ((*failed = git_pool_strdup(pool, pat->pattern)) == NULL)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int pathspec_match_from_iterator(
Packit Service 20376f
	git_pathspec_match_list **out,
Packit Service 20376f
	git_iterator *iter,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_pathspec_match_list *m = NULL;
Packit Service 20376f
	const git_index_entry *entry = NULL;
Packit Service 20376f
	struct pathspec_match_context ctxt;
Packit Service 20376f
	git_vector *patterns = &ps->pathspec;
Packit Service 20376f
	bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0;
Packit Service 20376f
	bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0;
Packit Service 20376f
	size_t pos, used_ct = 0, found_files = 0;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	git_bitvec used_patterns;
Packit Service 20376f
	char **file;
Packit Service 20376f
Packit Service 20376f
	if (git_bitvec_init(&used_patterns, patterns->length) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (out) {
Packit Service 20376f
		*out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_STRINGS);
Packit Service 20376f
		GITERR_CHECK_ALLOC(m);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_reset_range(iter, ps->prefix, ps->prefix)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (git_iterator_type(iter) == GIT_ITERATOR_TYPE_WORKDIR &&
Packit Service 20376f
		(error = git_repository_index__weakptr(
Packit Service 20376f
			&index, git_iterator_owner(iter))) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	pathspec_match_context_init(
Packit Service 20376f
		&ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0,
Packit Service 20376f
		git_iterator_ignore_case(iter));
Packit Service 20376f
Packit Service 20376f
	while (!(error = git_iterator_advance(&entry, iter))) {
Packit Service 20376f
		/* search for match with entry->path */
Packit Service 20376f
		int result = git_pathspec__match_at(
Packit Service 20376f
			&pos, patterns, &ctxt, entry->path, NULL);
Packit Service 20376f
Packit Service 20376f
		/* no matches for this path */
Packit Service 20376f
		if (result < 0)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* if result was a negative pattern match, then don't list file */
Packit Service 20376f
		if (!result) {
Packit Service 20376f
			used_ct += pathspec_mark_pattern(&used_patterns, pos);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* check if path is ignored and untracked */
Packit Service 20376f
		if (index != NULL &&
Packit Service 20376f
			git_iterator_current_is_ignored(iter) &&
Packit Service 20376f
			git_index__find_pos(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* mark the matched pattern as used */
Packit Service 20376f
		used_ct += pathspec_mark_pattern(&used_patterns, pos);
Packit Service 20376f
		++found_files;
Packit Service 20376f
Packit Service 20376f
		/* if find_failures is on, check if any later patterns also match */
Packit Service 20376f
		if (find_failures && used_ct < patterns->length)
Packit Service 20376f
			used_ct += pathspec_mark_remaining(
Packit Service 20376f
				&used_patterns, patterns, &ctxt, pos + 1, entry->path, NULL);
Packit Service 20376f
Packit Service 20376f
		/* if only looking at failures, exit early or just continue */
Packit Service 20376f
		if (failures_only || !out) {
Packit Service 20376f
			if (used_ct == patterns->length)
Packit Service 20376f
				break;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* insert matched path into matches array */
Packit Service 20376f
		if ((file = (char **)git_array_alloc(m->matches)) == NULL ||
Packit Service 20376f
			(*file = git_pool_strdup(&m->pool, entry->path)) == NULL) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0 && error != GIT_ITEROVER)
Packit Service 20376f
		goto done;
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
	/* insert patterns that had no matches into failures array */
Packit Service 20376f
	if (find_failures && used_ct < patterns->length &&
Packit Service 20376f
		(error = pathspec_build_failure_array(
Packit Service 20376f
			&m->failures, patterns, &used_patterns, &m->pool)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* if every pattern failed to match, then we have failed */
Packit Service 20376f
	if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_files) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "no matching files were found");
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_bitvec_free(&used_patterns);
Packit Service 20376f
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		pathspec_match_free(m);
Packit Service 20376f
		if (out) *out = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_iterator_flag_t pathspec_match_iter_flags(uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	git_iterator_flag_t f = 0;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATHSPEC_IGNORE_CASE) != 0)
Packit Service 20376f
		f |= GIT_ITERATOR_IGNORE_CASE;
Packit Service 20376f
	else if ((flags & GIT_PATHSPEC_USE_CASE) != 0)
Packit Service 20376f
		f |= GIT_ITERATOR_DONT_IGNORE_CASE;
Packit Service 20376f
Packit Service 20376f
	return f;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_match_workdir(
Packit Service 20376f
	git_pathspec_match_list **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	git_iterator *iter;
Packit Service 20376f
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	iter_opts.flags = pathspec_match_iter_flags(flags);
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_iterator_for_workdir(&iter, repo, NULL, NULL, &iter_opts))) {
Packit Service 20376f
		error = pathspec_match_from_iterator(out, iter, flags, ps);
Packit Service 20376f
		git_iterator_free(iter);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_match_index(
Packit Service 20376f
	git_pathspec_match_list **out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	git_iterator *iter;
Packit Service 20376f
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	iter_opts.flags = pathspec_match_iter_flags(flags);
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_iterator_for_index(&iter, git_index_owner(index), index, &iter_opts))) {
Packit Service 20376f
		error = pathspec_match_from_iterator(out, iter, flags, ps);
Packit Service 20376f
		git_iterator_free(iter);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_match_tree(
Packit Service 20376f
	git_pathspec_match_list **out,
Packit Service 20376f
	git_tree *tree,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	git_iterator *iter;
Packit Service 20376f
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(tree);
Packit Service 20376f
Packit Service 20376f
	iter_opts.flags = pathspec_match_iter_flags(flags);
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_iterator_for_tree(&iter, tree, &iter_opts))) {
Packit Service 20376f
		error = pathspec_match_from_iterator(out, iter, flags, ps);
Packit Service 20376f
		git_iterator_free(iter);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pathspec_match_diff(
Packit Service 20376f
	git_pathspec_match_list **out,
Packit Service 20376f
	git_diff *diff,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	git_pathspec *ps)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_pathspec_match_list *m = NULL;
Packit Service 20376f
	struct pathspec_match_context ctxt;
Packit Service 20376f
	git_vector *patterns = &ps->pathspec;
Packit Service 20376f
	bool find_failures = out && (flags & GIT_PATHSPEC_FIND_FAILURES) != 0;
Packit Service 20376f
	bool failures_only = !out || (flags & GIT_PATHSPEC_FAILURES_ONLY) != 0;
Packit Service 20376f
	size_t i, pos, used_ct = 0, found_deltas = 0;
Packit Service 20376f
	const git_diff_delta *delta, **match;
Packit Service 20376f
	git_bitvec used_patterns;
Packit Service 20376f
Packit Service 20376f
	assert(diff);
Packit Service 20376f
Packit Service 20376f
	if (git_bitvec_init(&used_patterns, patterns->length) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (out) {
Packit Service 20376f
		*out = m = pathspec_match_alloc(ps, PATHSPEC_DATATYPE_DIFF);
Packit Service 20376f
		GITERR_CHECK_ALLOC(m);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	pathspec_match_context_init(
Packit Service 20376f
		&ctxt, (flags & GIT_PATHSPEC_NO_GLOB) != 0,
Packit Service 20376f
		git_diff_is_sorted_icase(diff));
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&diff->deltas, i, delta) {
Packit Service 20376f
		/* search for match with delta */
Packit Service 20376f
		int result = git_pathspec__match_at(
Packit Service 20376f
			&pos, patterns, &ctxt, delta->old_file.path, delta->new_file.path);
Packit Service 20376f
Packit Service 20376f
		/* no matches for this path */
Packit Service 20376f
		if (result < 0)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* mark the matched pattern as used */
Packit Service 20376f
		used_ct += pathspec_mark_pattern(&used_patterns, pos);
Packit Service 20376f
Packit Service 20376f
		/* if result was a negative pattern match, then don't list file */
Packit Service 20376f
		if (!result)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		++found_deltas;
Packit Service 20376f
Packit Service 20376f
		/* if find_failures is on, check if any later patterns also match */
Packit Service 20376f
		if (find_failures && used_ct < patterns->length)
Packit Service 20376f
			used_ct += pathspec_mark_remaining(
Packit Service 20376f
				&used_patterns, patterns, &ctxt, pos + 1,
Packit Service 20376f
				delta->old_file.path, delta->new_file.path);
Packit Service 20376f
Packit Service 20376f
		/* if only looking at failures, exit early or just continue */
Packit Service 20376f
		if (failures_only || !out) {
Packit Service 20376f
			if (used_ct == patterns->length)
Packit Service 20376f
				break;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* insert matched delta into matches array */
Packit Service 20376f
		if (!(match = (const git_diff_delta **)git_array_alloc(m->matches))) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		} else {
Packit Service 20376f
			*match = delta;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* insert patterns that had no matches into failures array */
Packit Service 20376f
	if (find_failures && used_ct < patterns->length &&
Packit Service 20376f
		(error = pathspec_build_failure_array(
Packit Service 20376f
			&m->failures, patterns, &used_patterns, &m->pool)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* if every pattern failed to match, then we have failed */
Packit Service 20376f
	if ((flags & GIT_PATHSPEC_NO_MATCH_ERROR) != 0 && !found_deltas) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "no matching deltas were found");
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_bitvec_free(&used_patterns);
Packit Service 20376f
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		pathspec_match_free(m);
Packit Service 20376f
		if (out) *out = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_pathspec_match_list_free(git_pathspec_match_list *m)
Packit Service 20376f
{
Packit Service 20376f
	if (m)
Packit Service 20376f
		pathspec_match_free(m);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_pathspec_match_list_entrycount(
Packit Service 20376f
	const git_pathspec_match_list *m)
Packit Service 20376f
{
Packit Service 20376f
	return m ? git_array_size(m->matches) : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_pathspec_match_list_entry(
Packit Service 20376f
	const git_pathspec_match_list *m, size_t pos)
Packit Service 20376f
{
Packit Service 20376f
	if (!m || m->datatype != PATHSPEC_DATATYPE_STRINGS ||
Packit Service 20376f
		!git_array_valid_index(m->matches, pos))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	return *((const char **)git_array_get(m->matches, pos));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_diff_delta *git_pathspec_match_list_diff_entry(
Packit Service 20376f
	const git_pathspec_match_list *m, size_t pos)
Packit Service 20376f
{
Packit Service 20376f
	if (!m || m->datatype != PATHSPEC_DATATYPE_DIFF ||
Packit Service 20376f
		!git_array_valid_index(m->matches, pos))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	return *((const git_diff_delta **)git_array_get(m->matches, pos));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_pathspec_match_list_failed_entrycount(
Packit Service 20376f
	const git_pathspec_match_list *m)
Packit Service 20376f
{
Packit Service 20376f
	return m ? git_array_size(m->failures) : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char * git_pathspec_match_list_failed_entry(
Packit Service 20376f
	const git_pathspec_match_list *m, size_t pos)
Packit Service 20376f
{
Packit Service 20376f
	char **entry = m ? git_array_get(m->failures, pos) : NULL;
Packit Service 20376f
Packit Service 20376f
	return entry ? *entry : NULL;
Packit Service 20376f
}