summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.c16
-rw-r--r--src/config_file.c37
-rw-r--r--src/config_file.h17
3 files changed, 59 insertions, 11 deletions
diff --git a/src/config.c b/src/config.c
index d18b85c30..98fb3b20d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -136,17 +136,27 @@ int git_config_add_file(git_config *cfg, git_config_file *file, int priority)
* Loop over all the variables
*/
-int git_config_foreach(git_config *cfg, int (*fn)(const char *, const char *, void *), void *data)
+int git_config_foreach(
+ git_config *cfg, int (*fn)(const char *, const char *, void *), void *data)
+{
+ return git_config_foreach_match(cfg, NULL, fn, data);
+}
+
+int git_config_foreach_match(
+ git_config *cfg,
+ const char *regexp,
+ int (*fn)(const char *, const char *, void *),
+ void *data)
{
int ret = 0;
unsigned int i;
file_internal *internal;
git_config_file *file;
- for(i = 0; i < cfg->files.length && ret == 0; ++i) {
+ for (i = 0; i < cfg->files.length && ret == 0; ++i) {
internal = git_vector_get(&cfg->files, i);
file = internal->file;
- ret = file->foreach(file, fn, data);
+ ret = file->foreach(file, regexp, fn, data);
}
return ret;
diff --git a/src/config_file.c b/src/config_file.c
index fd1aa8d08..1f3ebfca9 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -188,25 +188,46 @@ static void backend_free(git_config_file *_backend)
git__free(backend);
}
-static int file_foreach(git_config_file *backend, int (*fn)(const char *, const char *, void *), void *data)
+static int file_foreach(
+ git_config_file *backend,
+ const char *regexp,
+ int (*fn)(const char *, const char *, void *),
+ void *data)
{
diskfile_backend *b = (diskfile_backend *)backend;
cvar_t *var;
const char *key;
+ regex_t regex;
+ int result = 0;
if (!b->values)
return 0;
+ if (regexp != NULL) {
+ if ((result = regcomp(&regex, regexp, REG_EXTENDED)) < 0) {
+ giterr_set_regex(&regex, result);
+ regfree(&regex);
+ return -1;
+ }
+ }
+
git_strmap_foreach(b->values, key, var,
- do {
- if (fn(key, var->value, data) < 0)
- break;
+ for (; var != NULL; var = CVAR_LIST_NEXT(var)) {
+ /* skip non-matching keys if regexp was provided */
+ if (regexp && regexec(&regex, key, 0, NULL, 0) != 0)
+ continue;
- var = CVAR_LIST_NEXT(var);
- } while (var != NULL);
+ /* abort iterator on non-zero return value */
+ if ((result = fn(key, var->value, data)) != 0)
+ goto cleanup;
+ }
);
- return 0;
+cleanup:
+ if (regexp != NULL)
+ regfree(&regex);
+
+ return result;
}
static int config_set(git_config_file *cfg, const char *name, const char *value)
@@ -337,6 +358,7 @@ static int config_get_multivar(
result = regcomp(&regex, regex_str, REG_EXTENDED);
if (result < 0) {
giterr_set_regex(&regex, result);
+ regfree(&regex);
return -1;
}
@@ -396,6 +418,7 @@ static int config_set_multivar(
if (result < 0) {
git__free(key);
giterr_set_regex(&preg, result);
+ regfree(&preg);
return -1;
}
diff --git a/src/config_file.h b/src/config_file.h
index 0080b5713..c31292881 100644
--- a/src/config_file.h
+++ b/src/config_file.h
@@ -19,12 +19,27 @@ GIT_INLINE(void) git_config_file_free(git_config_file *cfg)
cfg->free(cfg);
}
+GIT_INLINE(int) git_config_file_set_string(
+ git_config_file *cfg, const char *name, const char *value)
+{
+ return cfg->set(cfg, name, value);
+}
+
GIT_INLINE(int) git_config_file_foreach(
git_config_file *cfg,
int (*fn)(const char *key, const char *value, void *data),
void *data)
{
- return cfg->foreach(cfg, fn, data);
+ return cfg->foreach(cfg, NULL, fn, data);
+}
+
+GIT_INLINE(int) git_config_file_foreach_match(
+ git_config_file *cfg,
+ const char *regexp,
+ int (*fn)(const char *key, const char *value, void *data),
+ void *data)
+{
+ return cfg->foreach(cfg, regexp, fn, data);
}
#endif