Skip to content

Commit de70bb4

Browse files
committed
global: convert trivial fnmatch users to use wildcard
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).
1 parent 451df79 commit de70bb4

File tree

8 files changed

+38
-38
lines changed

8 files changed

+38
-38
lines changed

src/describe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
#include "commit.h"
1616
#include "commit_list.h"
17-
#include "fnmatch.h"
1817
#include "oidmap.h"
1918
#include "refs.h"
2019
#include "repository.h"
2120
#include "revwalk.h"
2221
#include "tag.h"
2322
#include "vector.h"
23+
#include "wildmatch.h"
2424

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

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

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

src/ignore.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "attrcache.h"
1313
#include "path.h"
1414
#include "config.h"
15-
#include "fnmatch.h"
15+
#include "wildmatch.h"
1616

1717
#define GIT_IGNORE_INTERNAL "[internal]exclude"
1818

@@ -101,17 +101,17 @@ static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg)
101101
*/
102102
static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match)
103103
{
104-
int error = 0, fnflags;
104+
int error = 0, wildmatch_flags;
105105
size_t i;
106106
git_attr_fnmatch *rule;
107107
char *path;
108108
git_buf buf = GIT_BUF_INIT;
109109

110110
*out = 0;
111111

112-
fnflags = FNM_PATHNAME;
112+
wildmatch_flags = WM_PATHNAME;
113113
if (match->flags & GIT_ATTR_FNMATCH_ICASE)
114-
fnflags |= FNM_IGNORECASE;
114+
wildmatch_flags |= WM_CASEFOLD;
115115

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

144-
if ((error = p_fnmatch(git_buf_cstr(&buf), path, fnflags)) < 0) {
144+
if ((error = wildmatch(git_buf_cstr(&buf), path, wildmatch_flags)) < 0) {
145145
git_error_set(GIT_ERROR_INVALID, "error matching pattern");
146146
goto out;
147147
}
148148

149149
/* if we found a match, we want to keep this rule */
150-
if (error != FNM_NOMATCH) {
150+
if (error != WM_NOMATCH) {
151151
*out = 1;
152152
error = 0;
153153
goto out;

src/pathspec.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "index.h"
1717
#include "bitvec.h"
1818
#include "diff.h"
19-
#include "fnmatch.h"
19+
#include "wildmatch.h"
2020

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

113113
struct pathspec_match_context {
114-
int fnmatch_flags;
114+
int wildmatch_flags;
115115
int (*strcomp)(const char *, const char *);
116116
int (*strncomp)(const char *, const char *, size_t);
117117
};
@@ -122,11 +122,11 @@ static void pathspec_match_context_init(
122122
bool casefold)
123123
{
124124
if (disable_fnmatch)
125-
ctxt->fnmatch_flags = -1;
125+
ctxt->wildmatch_flags = -1;
126126
else if (casefold)
127-
ctxt->fnmatch_flags = FNM_CASEFOLD;
127+
ctxt->wildmatch_flags = WM_CASEFOLD;
128128
else
129-
ctxt->fnmatch_flags = 0;
129+
ctxt->wildmatch_flags = 0;
130130

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

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

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

153153
/* if we didn't match, look for exact dirname prefix match */
154-
if (result == FNM_NOMATCH &&
154+
if (result == WM_NOMATCH &&
155155
(match->flags & GIT_ATTR_FNMATCH_HASWILD) == 0 &&
156156
ctxt->strncomp(path, match->pattern, match->length) == 0 &&
157157
path[match->length] == '/')
@@ -160,7 +160,7 @@ static int pathspec_match_one(
160160
/* if we didn't match and this is a negative match, check for exact
161161
* match of filename with leading '!'
162162
*/
163-
if (result == FNM_NOMATCH &&
163+
if (result == WM_NOMATCH &&
164164
(match->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 &&
165165
*path == '!' &&
166166
ctxt->strncomp(path + 1, match->pattern, match->length) == 0 &&

src/refdb_fs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "iterator.h"
1919
#include "sortedcache.h"
2020
#include "signature.h"
21-
#include "fnmatch.h"
21+
#include "wildmatch.h"
2222

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

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

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

619619
if (ref->flags & PACKREF_SHADOWED)
620620
continue;
621-
if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
621+
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
622622
continue;
623623

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

662662
if (ref->flags & PACKREF_SHADOWED)
663663
continue;
664-
if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0)
664+
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
665665
continue;
666666

667667
*out = ref->name;

src/refspec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
#include "git2/errors.h"
1111

12-
#include "util.h"
13-
#include "fnmatch.h"
1412
#include "refs.h"
13+
#include "util.h"
1514
#include "vector.h"
15+
#include "wildmatch.h"
1616

1717
int git_refspec__parse(git_refspec *refspec, const char *input, bool is_fetch)
1818
{
@@ -213,15 +213,15 @@ int git_refspec_src_matches(const git_refspec *refspec, const char *refname)
213213
if (refspec == NULL || refspec->src == NULL)
214214
return false;
215215

216-
return (p_fnmatch(refspec->src, refname, 0) == 0);
216+
return (wildmatch(refspec->src, refname, 0) == 0);
217217
}
218218

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

224-
return (p_fnmatch(refspec->dst, refname, 0) == 0);
224+
return (wildmatch(refspec->dst, refname, 0) == 0);
225225
}
226226

227227
static int refspec_transform(

src/status.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "repository.h"
1717
#include "ignore.h"
1818
#include "index.h"
19-
#include "fnmatch.h"
19+
#include "wildmatch.h"
2020

2121
#include "git2/diff.h"
2222
#include "diff.h"
@@ -457,7 +457,7 @@ struct status_file_info {
457457
char *expected;
458458
unsigned int count;
459459
unsigned int status;
460-
int fnm_flags;
460+
int wildmatch_flags;
461461
int ambiguous;
462462
};
463463

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

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

474474
if (sfi->count > 1 ||
475475
(strcomp(sfi->expected, path) != 0 &&
476-
p_fnmatch(sfi->expected, path, sfi->fnm_flags) != 0))
476+
wildmatch(sfi->expected, path, sfi->wildmatch_flags) != 0))
477477
{
478478
sfi->ambiguous = true;
479479
return GIT_EAMBIGUOUS; /* git_error_set will be done by caller */
@@ -500,7 +500,7 @@ int git_status_file(
500500
if ((sfi.expected = git__strdup(path)) == NULL)
501501
return -1;
502502
if (index->ignore_case)
503-
sfi.fnm_flags = FNM_CASEFOLD;
503+
sfi.wildmatch_flags = WM_CASEFOLD;
504504

505505
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
506506
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |

src/tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include "tag.h"
99

1010
#include "commit.h"
11-
#include "fnmatch.h"
1211
#include "signature.h"
1312
#include "message.h"
13+
#include "wildmatch.h"
1414
#include "git2/object.h"
1515
#include "git2/repository.h"
1616
#include "git2/signature.h"
@@ -476,7 +476,7 @@ static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
476476
GIT_UNUSED(oid);
477477

478478
if (!*filter->pattern ||
479-
p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
479+
wildmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
480480
{
481481
char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN);
482482
GIT_ERROR_CHECK_ALLOC(matched);

tests/describe/describe_helpers.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "describe_helpers.h"
22

3-
#include "fnmatch.h"
3+
#include "wildmatch.h"
44

55
void assert_describe(
66
const char *expected_output,
@@ -18,7 +18,7 @@ void assert_describe(
1818
cl_git_pass(git_describe_commit(&result, object, opts));
1919
cl_git_pass(git_describe_format(&label, result, fmt_opts));
2020

21-
cl_must_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
21+
cl_must_pass(wildmatch(expected_output, git_buf_cstr(&label), 0));
2222

2323
git_describe_result_free(result);
2424
git_object_free(object);
@@ -37,7 +37,7 @@ void assert_describe_workdir(
3737
cl_git_pass(git_describe_workdir(&result, repo, opts));
3838
cl_git_pass(git_describe_format(&label, result, fmt_opts));
3939

40-
cl_must_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
40+
cl_must_pass(wildmatch(expected_output, git_buf_cstr(&label), 0));
4141

4242
git_describe_result_free(result);
4343
git_buf_dispose(&label);

0 commit comments

Comments
 (0)