config: don't special-case the multivar iterator Build it on top of the normal iterator instead, which lets use re-use a lot of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
diff --git a/include/git2/config.h b/include/git2/config.h
index 2821646..f144151 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -350,7 +350,7 @@ GIT_EXTERN(int) git_config_get_multivar_foreach(const git_config *cfg, const cha
* @param regexp regular expression to filter which variables we're
* interested in. Use NULL to indicate all
*/
-GIT_EXTERN(int) git_config_get_multivar(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp);
+GIT_EXTERN(int) git_config_multivar_iterator_new(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp);
/**
* Return the current entry and advance the iterator
diff --git a/include/git2/sys/config.h b/include/git2/sys/config.h
index e369fb8..7572ace 100644
--- a/include/git2/sys/config.h
+++ b/include/git2/sys/config.h
@@ -58,8 +58,6 @@ struct git_config_backend {
/* Open means open the file/database and parse if necessary */
int (*open)(struct git_config_backend *, git_config_level_t level);
int (*get)(const struct git_config_backend *, const char *key, const git_config_entry **entry);
- int (*get_multivar_foreach)(struct git_config_backend *, const char *key, const char *regexp, git_config_foreach_cb callback, void *payload);
- int (*get_multivar)(git_config_iterator **, struct git_config_backend *, const char *name, const char *regexp);
int (*set)(struct git_config_backend *, const char *key, const char *value);
int (*set_multivar)(git_config_backend *cfg, const char *name, const char *regexp, const char *value);
int (*del)(struct git_config_backend *, const char *key);
diff --git a/src/config.c b/src/config.c
index ae4e481..c98d6a5 100644
--- a/src/config.c
+++ b/src/config.c
@@ -747,7 +747,7 @@ int git_config_get_multivar_foreach(
git_config_iterator *iter;
git_config_entry *entry;
- if ((err = git_config_get_multivar(&iter, cfg, name, regexp)) < 0)
+ if ((err = git_config_multivar_iterator_new(&iter, cfg, name, regexp)) < 0)
return err;
found = 0;
@@ -771,92 +771,82 @@ int git_config_get_multivar_foreach(
typedef struct {
git_config_iterator parent;
- git_config_iterator *current;
+ git_config_iterator *iter;
char *name;
- char *regexp;
- const git_config *cfg;
- size_t i;
+ regex_t regex;
+ int have_regex;
} multivar_iter;
static int multivar_iter_next(git_config_entry **entry, git_config_iterator *_iter)
{
multivar_iter *iter = (multivar_iter *) _iter;
- git_config_iterator *current = iter->current;
- file_internal *internal;
- git_config_backend *backend;
- size_t i;
int error = 0;
- if (current != NULL &&
- (error = current->next(entry, current)) == 0) {
- return 0;
- }
-
- if (error < 0 && error != GIT_ITEROVER)
- return error;
-
- do {
- if (find_next_backend(&i, iter->cfg, iter->i) < 0)
- return GIT_ITEROVER;
-
- internal = git_vector_get(&iter->cfg->files, i - 1);
- backend = internal->file;
- iter->i = i - 1;
-
- if (iter->current)
- iter->current->free(current);
-
- iter->current = NULL;
- error = backend->get_multivar(&iter->current, backend, iter->name, iter->regexp);
- if (error == GIT_ENOTFOUND)
+ while ((error = iter->iter->next(entry, iter->iter)) == 0) {
+ if (git__strcmp(iter->name, (*entry)->name))
continue;
- if (error < 0)
- return error;
-
- return iter->current->next(entry, iter->current);
+ if (!iter->have_regex)
+ return 0;
- } while(1);
+ if (regexec(&iter->regex, (*entry)->value, 0, NULL, 0) == 0)
+ return 0;
+ }
- return GIT_ITEROVER;
+ return error;
}
void multivar_iter_free(git_config_iterator *_iter)
{
multivar_iter *iter = (multivar_iter *) _iter;
- if (iter->current)
- iter->current->free(iter->current);
+ iter->iter->free(iter->iter);
git__free(iter->name);
- git__free(iter->regexp);
+ regfree(&iter->regex);
git__free(iter);
}
-int git_config_get_multivar(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp)
+int git_config_multivar_iterator_new(git_config_iterator **out, const git_config *cfg, const char *name, const char *regexp)
{
- multivar_iter *iter;
+ multivar_iter *iter = NULL;
+ git_config_iterator *inner = NULL;
+ int error;
+
+ if ((error = git_config_iterator_new(&inner, cfg)) < 0)
+ return error;
iter = git__calloc(1, sizeof(multivar_iter));
GITERR_CHECK_ALLOC(iter);
- iter->name = git__strdup(name);
- GITERR_CHECK_ALLOC(iter->name);
+ if ((error = git_config__normalize_name(name, &iter->name)) < 0)
+ goto on_error;
if (regexp != NULL) {
- iter->regexp = git__strdup(regexp);
- GITERR_CHECK_ALLOC(iter->regexp);
+ error = regcomp(&iter->regex, regexp, REG_EXTENDED);
+ if (error < 0) {
+ giterr_set_regex(&iter->regex, error);
+ error = -1;
+ regfree(&iter->regex);
+ goto on_error;
+ }
+
+ iter->have_regex = 1;
}
+ iter->iter = inner;
iter->parent.free = multivar_iter_free;
iter->parent.next = multivar_iter_next;
- iter->i = cfg->files.length;
- iter->cfg = cfg;
-
*out = (git_config_iterator *) iter;
return 0;
+
+on_error:
+
+ inner->free(inner);
+ git__free(iter);
+ return error;
}
int git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value)
@@ -1125,6 +1115,41 @@ fail_parse:
return -1;
}
+/* Take something the user gave us and make it nice for our hash function */
+int git_config__normalize_name(const char *in, char **out)
+{
+ char *name, *fdot, *ldot;
+
+ assert(in && out);
+
+ name = git__strdup(in);
+ GITERR_CHECK_ALLOC(name);
+
+ fdot = strchr(name, '.');
+ ldot = strrchr(name, '.');
+
+ if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1])
+ goto invalid;
+
+ /* Validate and downcase up to first dot and after last dot */
+ if (git_config_file_normalize_section(name, fdot) < 0 ||
+ git_config_file_normalize_section(ldot + 1, NULL) < 0)
+ goto invalid;
+
+ /* If there is a middle range, make sure it doesn't have newlines */
+ while (fdot < ldot)
+ if (*fdot++ == '\n')
+ goto invalid;
+
+ *out = name;
+ return 0;
+
+invalid:
+ git__free(name);
+ giterr_set(GITERR_CONFIG, "Invalid config item name '%s'", in);
+ return GIT_EINVALIDSPEC;
+}
+
struct rename_data {
git_config *config;
git_buf *name;
diff --git a/src/config.h b/src/config.h
index c5c11ae..85db5e3 100644
--- a/src/config.h
+++ b/src/config.h
@@ -49,4 +49,7 @@ extern int git_config_rename_section(
*/
extern int git_config_file__ondisk(struct git_config_backend **out, const char *path);
+extern int git_config__normalize_name(const char *in, char **out);
+
+
#endif
diff --git a/src/config_file.c b/src/config_file.c
index 7c22be4..21dc223 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -136,41 +136,6 @@ int git_config_file_normalize_section(char *start, char *end)
return 0;
}
-/* Take something the user gave us and make it nice for our hash function */
-static int normalize_name(const char *in, char **out)
-{
- char *name, *fdot, *ldot;
-
- assert(in && out);
-
- name = git__strdup(in);
- GITERR_CHECK_ALLOC(name);
-
- fdot = strchr(name, '.');
- ldot = strrchr(name, '.');
-
- if (fdot == NULL || fdot == name || ldot == NULL || !ldot[1])
- goto invalid;
-
- /* Validate and downcase up to first dot and after last dot */
- if (git_config_file_normalize_section(name, fdot) < 0 ||
- git_config_file_normalize_section(ldot + 1, NULL) < 0)
- goto invalid;
-
- /* If there is a middle range, make sure it doesn't have newlines */
- while (fdot < ldot)
- if (*fdot++ == '\n')
- goto invalid;
-
- *out = name;
- return 0;
-
-invalid:
- git__free(name);
- giterr_set(GITERR_CONFIG, "Invalid config item name '%s'", in);
- return GIT_EINVALIDSPEC;
-}
-
static void free_vars(git_strmap *values)
{
cvar_t *var = NULL;
@@ -314,7 +279,7 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
khiter_t pos;
int rval, ret;
- if ((rval = normalize_name(name, &key)) < 0)
+ if ((rval = git_config__normalize_name(name, &key)) < 0)
return rval;
/*
@@ -397,7 +362,7 @@ static int config_get(const git_config_backend *cfg, const char *name, const git
khiter_t pos;
int error;
- if ((error = normalize_name(name, &key)) < 0)
+ if ((error = git_config__normalize_name(name, &key)) < 0)
return error;
pos = git_strmap_lookup_index(b->values, key);
@@ -412,162 +377,6 @@ static int config_get(const git_config_backend *cfg, const char *name, const git
return 0;
}
-typedef struct {
- git_config_iterator parent;
- cvar_t *var;
- regex_t regex;
- int have_regex;
-} foreach_iter;
-
-static void foreach_iter_free(git_config_iterator *_iter)
-{
- foreach_iter *iter = (foreach_iter *) _iter;
-
- if (iter->have_regex)
- regfree(&iter->regex);
-
- git__free(iter);
-}
-
-static int foreach_iter_next(git_config_entry **out, git_config_iterator *_iter)
-{
- foreach_iter *iter = (foreach_iter *) _iter;
-
- cvar_t* var = iter->var;
-
-
- if (var == NULL)
- return GIT_ITEROVER;
-
- if (!iter->have_regex) {
- *out = var->entry;
- iter->var = var->next;
- return 0;
- }
-
- /* For the regex case, we must loop until we find something we like */
- do {
- git_config_entry *entry = var->entry;
- regex_t *regex = &iter->regex;;
- if (regexec(regex, entry->value, 0, NULL, 0) == 0) {
- *out = entry;
- iter->var = var->next;
- return 0;
- }
- var = var->next;
- } while(var != NULL);
-
- return GIT_ITEROVER;
-}
-
-static int config_get_multivar(git_config_iterator **out, git_config_backend *_backend,
- const char *name, const char *regexp)
-{
- foreach_iter *iter;
- diskfile_backend *b = (diskfile_backend *) _backend;
-
- char *key;
- khiter_t pos;
- int error = 0;
-
- if ((error = normalize_name(name, &key)) < 0)
- return error;
-
- pos = git_strmap_lookup_index(b->values, key);
- git__free(key);
-
- if (!git_strmap_valid_index(b->values, pos))
- return GIT_ENOTFOUND;
-
- iter = git__calloc(1, sizeof(foreach_iter));
- GITERR_CHECK_ALLOC(iter);
-
- iter->var = git_strmap_value_at(b->values, pos);
-
- if (regexp != NULL) {
- int result;
-
- result = regcomp(&iter->regex, regexp, REG_EXTENDED);
- if (result < 0) {
- giterr_set_regex(&iter->regex, result);
- regfree(&iter->regex);
- return -1;
- }
- iter->have_regex = 1;
- }
-
- iter->parent.free = foreach_iter_free;
- iter->parent.next = foreach_iter_next;
-
- *out = (git_config_iterator *) iter;
-
- return 0;
- }
-
-static int config_get_multivar_foreach(
- git_config_backend *cfg,
- const char *name,
- const char *regex_str,
- int (*fn)(const git_config_entry *, void *),
- void *data)
-{
- cvar_t *var;
- diskfile_backend *b = (diskfile_backend *)cfg;
- char *key;
- khiter_t pos;
- int error;
-
- if ((error = normalize_name(name, &key)) < 0)
- return error;
-
- pos = git_strmap_lookup_index(b->values, key);
- git__free(key);
-
- if (!git_strmap_valid_index(b->values, pos))
- return GIT_ENOTFOUND;
-
- var = git_strmap_value_at(b->values, pos);
-
- if (regex_str != NULL) {
- regex_t regex;
- int result;
-
- /* regex matching; build the regex */
- result = regcomp(®ex, regex_str, REG_EXTENDED);
- if (result < 0) {
- giterr_set_regex(®ex, result);
- regfree(®ex);
- return -1;
- }
-
- /* and throw the callback only on the variables that
- * match the regex */
- do {
- if (regexec(®ex, var->entry->value, 0, NULL, 0) == 0) {
- /* early termination by the user is not an error;
- * just break and return successfully */
- if (fn(var->entry, data))
- break;
- }
-
- var = var->next;
- } while (var != NULL);
- regfree(®ex);
- } else {
- /* no regex; go through all the variables */
- do {
- /* early termination by the user is not an error;
- * just break and return successfully */
- if (fn(var->entry, data) < 0)
- break;
-
- var = var->next;
- } while (var != NULL);
- }
-
- return 0;
-}
-
static int config_set_multivar(
git_config_backend *cfg, const char *name, const char *regexp, const char *value)
{
@@ -581,7 +390,7 @@ static int config_set_multivar(
assert(regexp);
- if ((result = normalize_name(name, &key)) < 0)
+ if ((result = git_config__normalize_name(name, &key)) < 0)
return result;
pos = git_strmap_lookup_index(b->values, key);
@@ -654,7 +463,7 @@ static int config_delete(git_config_backend *cfg, const char *name)
int result;
khiter_t pos;
- if ((result = normalize_name(name, &key)) < 0)
+ if ((result = git_config__normalize_name(name, &key)) < 0)
return result;
pos = git_strmap_lookup_index(b->values, key);
@@ -694,8 +503,6 @@ int git_config_file__ondisk(git_config_backend **out, const char *path)
backend->parent.open = config_open;
backend->parent.get = config_get;
- backend->parent.get_multivar_foreach = config_get_multivar_foreach;
- backend->parent.get_multivar = config_get_multivar;
backend->parent.set = config_set;
backend->parent.set_multivar = config_set_multivar;
backend->parent.del = config_delete;
diff --git a/tests-clar/config/multivar.c b/tests-clar/config/multivar.c
index afb993c..0d552d6 100644
--- a/tests-clar/config/multivar.c
+++ b/tests-clar/config/multivar.c
@@ -76,7 +76,7 @@ static void check_get_multivar(git_config *cfg, int expected)
git_config_entry *entry;
int n = 0;
- cl_git_pass(git_config_get_multivar(&iter, cfg, _name, NULL));
+ cl_git_pass(git_config_multivar_iterator_new(&iter, cfg, _name, NULL));
while (git_config_next(&entry, iter) == 0)
n++;