Commit 943700ec by yorah

Return the matched pathspec pattern in `git_pathspec_match_path`

Instead of returning directly the pattern as the return value, I used an
out parameter, because the function also tests if the passed pathspecs
vector is empty. If yes, it considers that the path "matches", but in
that case there is no matched pattern per se.
parent 41713ec1
......@@ -224,7 +224,7 @@ static int checkout_action_wd_only(
if (!git_pathspec_match_path(
pathspec, wd->path,
(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
git_iterator_ignore_case(workdir)))
git_iterator_ignore_case(workdir), NULL))
return 0;
/* check if item is tracked in the index but not in the checkout diff */
......
......@@ -47,6 +47,7 @@ static int diff_delta__from_one(
const git_index_entry *entry)
{
git_diff_delta *delta;
const char *matched_pathspec;
if (status == GIT_DELTA_IGNORED &&
(diff->opts.flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
......@@ -59,7 +60,7 @@ static int diff_delta__from_one(
if (!git_pathspec_match_path(
&diff->pathspec, entry->path,
(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
return 0;
delta = diff_delta__alloc(diff, status, entry->path);
......@@ -419,13 +420,14 @@ static int maybe_modified(
unsigned int omode = oitem->mode;
unsigned int nmode = nitem->mode;
bool new_is_workdir = (new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
const char *matched_pathspec;
GIT_UNUSED(old_iter);
if (!git_pathspec_match_path(
&diff->pathspec, oitem->path,
(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
return 0;
/* on platforms with no symlinks, preserve mode of existing symlinks */
......
......@@ -1710,7 +1710,8 @@ int git_index_read_tree_match(
goto cleanup;
while (entry != NULL) {
if (git_pathspec_match_path(&pathspec, entry->path, false, false) &&
if (git_pathspec_match_path(
&pathspec, entry->path, false, false, NULL) &&
(error = git_index_add(index, entry)) < 0)
goto cleanup;
......
......@@ -106,14 +106,21 @@ void git_pathspec_free(git_vector *vspec)
/* match a path against the vectorized pathspec */
bool git_pathspec_match_path(
git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold)
git_vector *vspec,
const char *path,
bool disable_fnmatch,
bool casefold,
const char **matched_pathspec)
{
unsigned int i;
size_t i;
git_attr_fnmatch *match;
int fnmatch_flags = 0;
int (*use_strcmp)(const char *, const char *);
int (*use_strncmp)(const char *, const char *, size_t);
if (matched_pathspec)
*matched_pathspec = NULL;
if (!vspec || !vspec->length)
return true;
......@@ -143,8 +150,12 @@ bool git_pathspec_match_path(
path[match->length] == '/')
result = 0;
if (result == 0)
if (result == 0) {
if (matched_pathspec)
*matched_pathspec = match->pattern;
return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? false : true;
}
}
return false;
......
......@@ -25,8 +25,16 @@ extern int git_pathspec_init(
/* free data from the pathspec vector */
extern void git_pathspec_free(git_vector *vspec);
/* match a path against the vectorized pathspec */
/*
* Match a path against the vectorized pathspec.
* The matched pathspec is passed back into the `matched_pathspec` parameter,
* unless it is passed as NULL by the caller.
*/
extern bool git_pathspec_match_path(
git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold);
git_vector *vspec,
const char *path,
bool disable_fnmatch,
bool casefold,
const char **matched_pathspec);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment