Commit 5dc8513b by Vicent Martí

Merge pull request #1864 from libgit2/minimize-regex-usage

Minimize regex usage
parents d89b8b60 106c12f1
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#include "refspec.h" #include "refspec.h"
#include "fetchhead.h" #include "fetchhead.h"
#include <regex.h>
static int add_refspec(git_remote *remote, const char *string, bool is_fetch) static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
{ {
git_refspec *spec; git_refspec *spec;
...@@ -1075,35 +1073,28 @@ void git_remote_free(git_remote *remote) ...@@ -1075,35 +1073,28 @@ void git_remote_free(git_remote *remote)
git__free(remote); git__free(remote);
} }
struct cb_data { static int remote_list_cb(const git_config_entry *entry, void *payload)
git_vector *list;
regex_t *preg;
};
static int remote_list_cb(const git_config_entry *entry, void *data_)
{ {
struct cb_data *data = (struct cb_data *)data_; git_vector *list = payload;
size_t nmatch = 2; const char *name = entry->name + strlen("remote.");
regmatch_t pmatch[2]; size_t namelen = strlen(name);
const char *name = entry->name; char *remote_name;
if (!regexec(data->preg, name, nmatch, pmatch, 0)) { /* we know name matches "remote.<stuff>.(push)?url" */
char *remote_name = git__strndup(&name[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so);
GITERR_CHECK_ALLOC(remote_name);
if (git_vector_insert(data->list, remote_name) < 0) if (!strcmp(&name[namelen - 4], ".url"))
return -1; remote_name = git__strndup(name, namelen - 4); /* strip ".url" */
} else
remote_name = git__strndup(name, namelen - 8); /* strip ".pushurl" */
GITERR_CHECK_ALLOC(remote_name);
return 0; return git_vector_insert(list, remote_name);
} }
int git_remote_list(git_strarray *remotes_list, git_repository *repo) int git_remote_list(git_strarray *remotes_list, git_repository *repo)
{ {
git_config *cfg; git_config *cfg;
git_vector list; git_vector list;
regex_t preg;
struct cb_data data;
int error; int error;
if (git_repository_config__weakptr(&cfg, repo) < 0) if (git_repository_config__weakptr(&cfg, repo) < 0)
...@@ -1112,18 +1103,13 @@ int git_remote_list(git_strarray *remotes_list, git_repository *repo) ...@@ -1112,18 +1103,13 @@ int git_remote_list(git_strarray *remotes_list, git_repository *repo)
if (git_vector_init(&list, 4, git__strcmp_cb) < 0) if (git_vector_init(&list, 4, git__strcmp_cb) < 0)
return -1; return -1;
if (regcomp(&preg, "^remote\\.(.*)\\.(push)?url$", REG_EXTENDED) < 0) { error = git_config_foreach_match(
giterr_set(GITERR_OS, "Remote catch regex failed to compile"); cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &list);
return -1;
}
data.list = &list;
data.preg = &preg;
error = git_config_foreach(cfg, remote_list_cb, &data);
regfree(&preg);
if (error < 0) { if (error < 0) {
size_t i; size_t i;
char *elem; char *elem;
git_vector_foreach(&list, i, elem) { git_vector_foreach(&list, i, elem) {
git__free(elem); git__free(elem);
} }
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
#include "git2/revparse.h" #include "git2/revparse.h"
#include "merge.h" #include "merge.h"
#include <regex.h>
git_commit_list_node *git_revwalk__commit_lookup( git_commit_list_node *git_revwalk__commit_lookup(
git_revwalk *walk, const git_oid *oid) git_revwalk *walk, const git_oid *oid)
{ {
...@@ -181,48 +179,35 @@ static int push_glob_cb(const char *refname, void *data_) ...@@ -181,48 +179,35 @@ static int push_glob_cb(const char *refname, void *data_)
static int push_glob(git_revwalk *walk, const char *glob, int hide) static int push_glob(git_revwalk *walk, const char *glob, int hide)
{ {
int error = 0;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
struct push_cb_data data; struct push_cb_data data;
regex_t preg; size_t wildcard;
assert(walk && glob); assert(walk && glob);
/* refs/ is implied if not given in the glob */ /* refs/ is implied if not given in the glob */
if (strncmp(glob, GIT_REFS_DIR, strlen(GIT_REFS_DIR))) { if (git__prefixcmp(glob, GIT_REFS_DIR) != 0)
git_buf_printf(&buf, GIT_REFS_DIR "%s", glob); git_buf_joinpath(&buf, GIT_REFS_DIR, glob);
} else { else
git_buf_puts(&buf, glob); git_buf_puts(&buf, glob);
}
/* If no '?', '*' or '[' exist, we append '/ *' to the glob */ /* If no '?', '*' or '[' exist, we append '/ *' to the glob */
memset(&preg, 0x0, sizeof(regex_t)); wildcard = strcspn(glob, "?*[");
if (regcomp(&preg, "[?*[]", REG_EXTENDED)) { if (!glob[wildcard])
giterr_set(GITERR_OS, "Regex failed to compile"); git_buf_put(&buf, "/*", 2);
git_buf_free(&buf);
return -1;
}
if (regexec(&preg, glob, 0, NULL, 0))
git_buf_puts(&buf, "/*");
if (git_buf_oom(&buf))
goto on_error;
data.walk = walk; data.walk = walk;
data.hide = hide; data.hide = hide;
if (git_reference_foreach_glob( if (git_buf_oom(&buf))
walk->repo, git_buf_cstr(&buf), push_glob_cb, &data) < 0) error = -1;
goto on_error; else
error = git_reference_foreach_glob(
regfree(&preg); walk->repo, git_buf_cstr(&buf), push_glob_cb, &data);
git_buf_free(&buf);
return 0;
on_error:
regfree(&preg);
git_buf_free(&buf); git_buf_free(&buf);
return -1; return error;
} }
int git_revwalk_push_glob(git_revwalk *walk, const char *glob) int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
......
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