summaryrefslogtreecommitdiff
path: root/src/config_file.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-07-10 15:22:39 -0700
committerRussell Belfer <rb@github.com>2012-07-10 23:19:47 -0700
commitb3ff1dab317dc9194c4bc124afd95ae9be2ad57b (patch)
treec031792209b9758395610720b8dbfb776165f783 /src/config_file.c
parentc3a875c975cde11ffd18947c419b1cd38205e6c3 (diff)
downloadlibgit2-b3ff1dab317dc9194c4bc124afd95ae9be2ad57b.tar.gz
Adding git_config_foreach_match() iteration fn
Adding a new config iteration function that let's you iterate over just the config entries that match a particular regular expression. The old foreach becomes a simple use of this with an empty pattern. This also fixes an apparent bug in the existing `git_config_foreach` where returning a non-zero value from the iteration callback was not correctly aborting the iteration and the returned value was not being propogated back to the caller of foreach. Added to tests to cover all these changes.
Diffstat (limited to 'src/config_file.c')
-rw-r--r--src/config_file.c37
1 files changed, 30 insertions, 7 deletions
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;
}