Skip to content

Commit

Permalink
global: convert trivial fnmatch users to use wildcard
Browse files Browse the repository at this point in the history
Upstream git.git has converted its codebase to use wildcard in
favor of fnmatch in commit 70a8fc999d (stop using fnmatch (either
native or compat), 2014-02-15). To keep our own regex-matching in
line with what git does, convert all trivial instances of
`fnmatch` usage to use `wildcard`, instead. Trivial usage is
defined to be use of `fnmatch` with either no flags or flags that
have a 1:1 equivalent in wildmatch (PATHNAME, IGNORECASE).
  • Loading branch information
pks-t committed Jun 15, 2019
1 parent 451df79 commit de70bb4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

#include "commit.h"
#include "commit_list.h"
#include "fnmatch.h"
#include "oidmap.h"
#include "refs.h"
#include "repository.h"
#include "revwalk.h"
#include "tag.h"
#include "vector.h"
#include "wildmatch.h"

/* Ported from https://github.com/git/git/blob/89dde7882f71f846ccd0359756d27bebc31108de/builtin/describe.c */

Expand Down Expand Up @@ -215,7 +215,7 @@ static int get_name(const char *refname, void *payload)
return 0;

/* Accept only tags that match the pattern, if given */
if (data->opts->pattern && (!is_tag || p_fnmatch(data->opts->pattern,
if (data->opts->pattern && (!is_tag || wildmatch(data->opts->pattern,
refname + strlen(GIT_REFS_TAGS_DIR), 0)))
return 0;

Expand Down
12 changes: 6 additions & 6 deletions src/ignore.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "attrcache.h"
#include "path.h"
#include "config.h"
#include "fnmatch.h"
#include "wildmatch.h"

#define GIT_IGNORE_INTERNAL "[internal]exclude"

Expand Down Expand Up @@ -101,17 +101,17 @@ static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg)
*/
static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match)
{
int error = 0, fnflags;
int error = 0, wildmatch_flags;
size_t i;
git_attr_fnmatch *rule;
char *path;
git_buf buf = GIT_BUF_INIT;

*out = 0;

fnflags = FNM_PATHNAME;
wildmatch_flags = WM_PATHNAME;
if (match->flags & GIT_ATTR_FNMATCH_ICASE)
fnflags |= FNM_IGNORECASE;
wildmatch_flags |= WM_CASEFOLD;

/* path of the file relative to the workdir, so we match the rules in subdirs */
if (match->containing_dir) {
Expand Down Expand Up @@ -141,13 +141,13 @@ static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match
if (git_buf_oom(&buf))
goto out;

if ((error = p_fnmatch(git_buf_cstr(&buf), path, fnflags)) < 0) {
if ((error = wildmatch(git_buf_cstr(&buf), path, wildmatch_flags)) < 0) {
git_error_set(GIT_ERROR_INVALID, "error matching pattern");
goto out;
}

/* if we found a match, we want to keep this rule */
if (error != FNM_NOMATCH) {
if (error != WM_NOMATCH) {
*out = 1;
error = 0;
goto out;
Expand Down
24 changes: 12 additions & 12 deletions src/pathspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "index.h"
#include "bitvec.h"
#include "diff.h"
#include "fnmatch.h"
#include "wildmatch.h"

/* what is the common non-wildcard prefix for all items in the pathspec */
char *git_pathspec_prefix(const git_strarray *pathspec)
Expand Down Expand Up @@ -111,7 +111,7 @@ void git_pathspec__vfree(git_vector *vspec)
}

struct pathspec_match_context {
int fnmatch_flags;
int wildmatch_flags;
int (*strcomp)(const char *, const char *);
int (*strncomp)(const char *, const char *, size_t);
};
Expand All @@ -122,11 +122,11 @@ static void pathspec_match_context_init(
bool casefold)
{
if (disable_fnmatch)
ctxt->fnmatch_flags = -1;
ctxt->wildmatch_flags = -1;
else if (casefold)
ctxt->fnmatch_flags = FNM_CASEFOLD;
ctxt->wildmatch_flags = WM_CASEFOLD;
else
ctxt->fnmatch_flags = 0;
ctxt->wildmatch_flags = 0;

if (casefold) {
ctxt->strcomp = git__strcasecmp;
Expand All @@ -142,16 +142,16 @@ static int pathspec_match_one(
struct pathspec_match_context *ctxt,
const char *path)
{
int result = (match->flags & GIT_ATTR_FNMATCH_MATCH_ALL) ? 0 : FNM_NOMATCH;
int result = (match->flags & GIT_ATTR_FNMATCH_MATCH_ALL) ? 0 : WM_NOMATCH;

if (result == FNM_NOMATCH)
result = ctxt->strcomp(match->pattern, path) ? FNM_NOMATCH : 0;
if (result == WM_NOMATCH)
result = ctxt->strcomp(match->pattern, path) ? WM_NOMATCH : 0;

if (ctxt->fnmatch_flags >= 0 && result == FNM_NOMATCH)
result = p_fnmatch(match->pattern, path, ctxt->fnmatch_flags);
if (ctxt->wildmatch_flags >= 0 && result == WM_NOMATCH)
result = wildmatch(match->pattern, path, ctxt->wildmatch_flags);

/* if we didn't match, look for exact dirname prefix match */
if (result == FNM_NOMATCH &&
if (result == WM_NOMATCH &&
(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
ctxt->strncomp(path, match->pattern, match->length) == 0 &&
path[match->length] == '/')
Expand All @@ -160,7 +160,7 @@ static int pathspec_match_one(
/* if we didn't match and this is a negative match, check for exact
* match of filename with leading '!'
*/
if (result == FNM_NOMATCH &&
if (result == WM_NOMATCH &&
(match->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 &&
*path == '!' &&
ctxt->strncomp(path + 1, match->pattern, match->length) == 0 &&
Expand Down
8 changes: 4 additions & 4 deletions src/refdb_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "iterator.h"
#include "sortedcache.h"
#include "signature.h"
#include "fnmatch.h"
#include "wildmatch.h"

#include <git2/tag.h>
#include <git2/object.h>
Expand Down Expand Up @@ -572,7 +572,7 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
ref_name = git_buf_cstr(&path);

if (git__suffixcmp(ref_name, ".lock") == 0 ||
(iter->glob && p_fnmatch(iter->glob, ref_name, 0) != 0))
(iter->glob && wildmatch(iter->glob, ref_name, 0) != 0))
continue;

ref_dup = git_pool_strdup(&iter->pool, ref_name);
Expand Down Expand Up @@ -618,7 +618,7 @@ static int refdb_fs_backend__iterator_next(

if (ref->flags & PACKREF_SHADOWED)
continue;
if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
continue;

*out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
Expand Down Expand Up @@ -661,7 +661,7 @@ static int refdb_fs_backend__iterator_next_name(

if (ref->flags & PACKREF_SHADOWED)
continue;
if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
continue;

*out = ref->name;
Expand Down
8 changes: 4 additions & 4 deletions src/refspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

#include "git2/errors.h"

#include "util.h"
#include "fnmatch.h"
#include "refs.h"
#include "util.h"
#include "vector.h"
#include "wildmatch.h"

int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
{
Expand Down Expand Up @@ -213,15 +213,15 @@ int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
if (refspec == NULL || refspec->src == NULL)
return false;

return (p_fnmatch(refspec->src, refname, 0) == 0);
return (wildmatch(refspec->src, refname, 0) == 0);
}

int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
{
if (refspec == NULL || refspec->dst == NULL)
return false;

return (p_fnmatch(refspec->dst, refname, 0) == 0);
return (wildmatch(refspec->dst, refname, 0) == 0);
}

static int refspec_transform(
Expand Down
10 changes: 5 additions & 5 deletions src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "repository.h"
#include "ignore.h"
#include "index.h"
#include "fnmatch.h"
#include "wildmatch.h"

#include "git2/diff.h"
#include "diff.h"
Expand Down Expand Up @@ -457,7 +457,7 @@ struct status_file_info {
char *expected;
unsigned int count;
unsigned int status;
int fnm_flags;
int wildmatch_flags;
int ambiguous;
};

Expand All @@ -469,11 +469,11 @@ static int get_one_status(const char *path, unsigned int status, void *data)
sfi->count++;
sfi->status = status;

strcomp = (sfi->fnm_flags & FNM_CASEFOLD) ? git__strcasecmp : git__strcmp;
strcomp = (sfi->wildmatch_flags & WM_CASEFOLD) ? git__strcasecmp : git__strcmp;

if (sfi->count > 1 ||
(strcomp(sfi->expected, path) != 0 &&
p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0))
wildmatch(sfi->expected, path, sfi->wildmatch_flags) != 0))
{
sfi->ambiguous = true;
return GIT_EAMBIGUOUS; /* git_error_set will be done by caller */
Expand All @@ -500,7 +500,7 @@ int git_status_file(
if ((sfi.expected = git__strdup(path)) == NULL)
return -1;
if (index->ignore_case)
sfi.fnm_flags = FNM_CASEFOLD;
sfi.wildmatch_flags = WM_CASEFOLD;

opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
Expand Down
4 changes: 2 additions & 2 deletions src/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "tag.h"

#include "commit.h"
#include "fnmatch.h"
#include "signature.h"
#include "message.h"
#include "wildmatch.h"
#include "git2/object.h"
#include "git2/repository.h"
#include "git2/signature.h"
Expand Down Expand Up @@ -476,7 +476,7 @@ static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
GIT_UNUSED(oid);

if (!*filter->pattern ||
p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
wildmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
{
char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN);
GIT_ERROR_CHECK_ALLOC(matched);
Expand Down
6 changes: 3 additions & 3 deletions tests/describe/describe_helpers.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "describe_helpers.h"

#include "fnmatch.h"
#include "wildmatch.h"

void assert_describe(
const char *expected_output,
Expand All @@ -18,7 +18,7 @@ void assert_describe(
cl_git_pass(git_describe_commit(&result, object, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));

cl_must_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
cl_must_pass(wildmatch(expected_output, git_buf_cstr(&label), 0));

git_describe_result_free(result);
git_object_free(object);
Expand All @@ -37,7 +37,7 @@ void assert_describe_workdir(
cl_git_pass(git_describe_workdir(&result, repo, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));

cl_must_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
cl_must_pass(wildmatch(expected_output, git_buf_cstr(&label), 0));

git_describe_result_free(result);
git_buf_dispose(&label);
Expand Down

0 comments on commit de70bb4

Please sign in to comment.