config: introduce new read-only in-memory backend Now that we have abstracted away how to store and retrieve config entries, it became trivial to implement a new in-memory backend by making use of this. And thus we do so. This commit implements a new read-only in-memory backend that can parse a chunk of memory into a `git_config_backend` structure.
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
diff --git a/src/config_backend.h b/src/config_backend.h
index ea2beaf..2451f9a 100644
--- a/src/config_backend.h
+++ b/src/config_backend.h
@@ -25,6 +25,14 @@
*/
extern int git_config_backend_from_file(git_config_backend **out, const char *path);
+/**
+ * Create an in-memory configuration file backend
+ *
+ * @param out the new backend
+ * @param cfg the configuration that is to be parsed
+ */
+extern int git_config_backend_from_string(git_config_backend **out, const char *cfg);
+
GIT_INLINE(int) git_config_backend_open(git_config_backend *cfg, unsigned int level, const git_repository *repo)
{
return cfg->open(cfg, level, repo);
diff --git a/src/config_mem.c b/src/config_mem.c
new file mode 100644
index 0000000..fbb6373
--- /dev/null
+++ b/src/config_mem.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "config.h"
+
+#include "config_backend.h"
+#include "config_parse.h"
+#include "config_entries.h"
+
+typedef struct {
+ git_config_backend parent;
+ git_config_entries *entries;
+ git_buf cfg;
+} config_memory_backend;
+
+typedef struct {
+ git_config_entries *entries;
+ git_config_level_t level;
+} config_memory_parse_data;
+
+static int config_error_readonly(void)
+{
+ giterr_set(GITERR_CONFIG, "this backend is read-only");
+ return -1;
+}
+
+static int read_variable_cb(
+ git_config_parser *reader,
+ const char *current_section,
+ const char *var_name,
+ const char *var_value,
+ const char *line,
+ size_t line_len,
+ void *payload)
+{
+ config_memory_parse_data *parse_data = (config_memory_parse_data *) payload;
+ git_buf buf = GIT_BUF_INIT;
+ git_config_entry *entry;
+ const char *c;
+ int result;
+
+ GIT_UNUSED(reader);
+ GIT_UNUSED(line);
+ GIT_UNUSED(line_len);
+
+ if (current_section) {
+ /* TODO: Once warnings land, we should likely warn
+ * here. Git appears to warn in most cases if it sees
+ * un-namespaced config options.
+ */
+ git_buf_puts(&buf, current_section);
+ git_buf_putc(&buf, '.');
+ }
+
+ for (c = var_name; *c; c++)
+ git_buf_putc(&buf, git__tolower(*c));
+
+ if (git_buf_oom(&buf))
+ return -1;
+
+ entry = git__calloc(1, sizeof(git_config_entry));
+ GITERR_CHECK_ALLOC(entry);
+ entry->name = git_buf_detach(&buf);
+ entry->value = var_value ? git__strdup(var_value) : NULL;
+ entry->level = parse_data->level;
+ entry->include_depth = 0;
+
+ if ((result = git_config_entries_append(parse_data->entries, entry)) < 0)
+ return result;
+
+ return result;
+}
+
+static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo)
+{
+ config_memory_backend *memory_backend = (config_memory_backend *) backend;
+ config_memory_parse_data parse_data;
+ git_config_parser reader;
+
+ GIT_UNUSED(repo);
+
+ if (memory_backend->cfg.size == 0)
+ return 0;
+
+ git_parse_ctx_init(&reader.ctx, memory_backend->cfg.ptr, memory_backend->cfg.size);
+ reader.file = NULL;
+ parse_data.entries = memory_backend->entries;
+ parse_data.level = level;
+
+ return git_config_parse(&reader, NULL, read_variable_cb, NULL, NULL, &parse_data);
+}
+
+static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out)
+{
+ config_memory_backend *memory_backend = (config_memory_backend *) backend;
+ return git_config_entries_get(out, memory_backend->entries, key);
+}
+
+static int config_memory_iterator(
+ git_config_iterator **iter,
+ git_config_backend *backend)
+{
+ config_memory_backend *memory_backend = (config_memory_backend *) backend;
+ git_config_entries *entries;
+ int error;
+
+ if ((error = git_config_entries_dup(&entries, memory_backend->entries)) < 0)
+ goto out;
+
+ if ((error = git_config_entries_iterator_new(iter, entries)) < 0)
+ goto out;
+
+out:
+ /* Let iterator delete duplicated entries when it's done */
+ git_config_entries_free(entries);
+ return error;
+}
+
+static int config_memory_set(git_config_backend *backend, const char *name, const char *value)
+{
+ GIT_UNUSED(backend);
+ GIT_UNUSED(name);
+ GIT_UNUSED(value);
+ return config_error_readonly();
+}
+
+static int config_memory_set_multivar(
+ git_config_backend *backend, const char *name, const char *regexp, const char *value)
+{
+ GIT_UNUSED(backend);
+ GIT_UNUSED(name);
+ GIT_UNUSED(regexp);
+ GIT_UNUSED(value);
+ return config_error_readonly();
+}
+
+static int config_memory_delete(git_config_backend *backend, const char *name)
+{
+ GIT_UNUSED(backend);
+ GIT_UNUSED(name);
+ return config_error_readonly();
+}
+
+static int config_memory_delete_multivar(git_config_backend *backend, const char *name, const char *regexp)
+{
+ GIT_UNUSED(backend);
+ GIT_UNUSED(name);
+ GIT_UNUSED(regexp);
+ return config_error_readonly();
+}
+
+static int config_memory_lock(git_config_backend *backend)
+{
+ GIT_UNUSED(backend);
+ return config_error_readonly();
+}
+
+static int config_memory_unlock(git_config_backend *backend, int success)
+{
+ GIT_UNUSED(backend);
+ GIT_UNUSED(success);
+ return config_error_readonly();
+}
+
+static int config_memory_snapshot(git_config_backend **out, git_config_backend *backend)
+{
+ GIT_UNUSED(out);
+ GIT_UNUSED(backend);
+ giterr_set(GITERR_CONFIG, "this backend does not support snapshots");
+ return -1;
+}
+
+static void config_memory_free(git_config_backend *_backend)
+{
+ config_memory_backend *backend = (config_memory_backend *)_backend;
+
+ if (backend == NULL)
+ return;
+
+ git_config_entries_free(backend->entries);
+ git_buf_dispose(&backend->cfg);
+ git__free(backend);
+}
+
+int git_config_backend_from_string(git_config_backend **out, const char *cfg)
+{
+ config_memory_backend *backend;
+
+ backend = git__calloc(1, sizeof(config_memory_backend));
+ GITERR_CHECK_ALLOC(backend);
+
+ if (git_config_entries_new(&backend->entries) < 0) {
+ git__free(backend);
+ return -1;
+ }
+
+ if (git_buf_sets(&backend->cfg, cfg) < 0) {
+ git_config_entries_free(backend->entries);
+ git__free(backend);
+ return -1;
+ }
+
+ backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
+ backend->parent.readonly = 1;
+ backend->parent.open = config_memory_open;
+ backend->parent.get = config_memory_get;
+ backend->parent.set = config_memory_set;
+ backend->parent.set_multivar = config_memory_set_multivar;
+ backend->parent.del = config_memory_delete;
+ backend->parent.del_multivar = config_memory_delete_multivar;
+ backend->parent.iterator = config_memory_iterator;
+ backend->parent.lock = config_memory_lock;
+ backend->parent.unlock = config_memory_unlock;
+ backend->parent.snapshot = config_memory_snapshot;
+ backend->parent.free = config_memory_free;
+
+ *out = (git_config_backend *)backend;
+
+ return 0;
+}
diff --git a/src/config_parse.c b/src/config_parse.c
index 6e85bbe..0d70549 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -16,8 +16,9 @@ const char *git_config_escaped = "\n\t\b\"\\";
static void set_parse_error(git_config_parser *reader, int col, const char *error_str)
{
+ const char *file = reader->file ? reader->file->path : "in-memory";
giterr_set(GITERR_CONFIG, "failed to parse config file: %s (in %s:%"PRIuZ", column %d)",
- error_str, reader->file->path, reader->ctx.line_num, col);
+ error_str, file, reader->ctx.line_num, col);
}
diff --git a/tests/config/memory.c b/tests/config/memory.c
new file mode 100644
index 0000000..aed221c
--- /dev/null
+++ b/tests/config/memory.c
@@ -0,0 +1,138 @@
+#include "clar_libgit2.h"
+
+#include "config_backend.h"
+
+static git_config_backend *backend;
+
+void test_config_memory__initialize(void)
+{
+ backend = NULL;
+}
+
+void test_config_memory__cleanup(void)
+{
+ git_config_backend_free(backend);
+}
+
+static void assert_config_contains(git_config_backend *backend,
+ const char *name, const char *value)
+{
+ git_config_entry *entry;
+ cl_git_pass(git_config_backend_get_string(&entry, backend, name));
+ cl_assert_equal_s(entry->value, value);
+}
+
+struct expected_entry {
+ const char *name;
+ const char *value;
+ int seen;
+};
+
+static int contains_all_cb(const git_config_entry *entry, void *payload)
+{
+ struct expected_entry *entries = (struct expected_entry *) payload;
+ int i;
+
+ for (i = 0; entries[i].name; i++) {
+ if (strcmp(entries[i].name, entry->name) ||
+ strcmp(entries[i].value , entry->value))
+ continue;
+
+ if (entries[i].seen)
+ cl_fail("Entry seen more than once");
+ entries[i].seen = 1;
+ return 0;
+ }
+
+ cl_fail("Unexpected entry");
+ return -1;
+}
+
+static void assert_config_contains_all(git_config_backend *backend,
+ struct expected_entry *entries)
+{
+ int i;
+
+ cl_git_pass(git_config_backend_foreach(backend, contains_all_cb, entries));
+
+ for (i = 0; entries[i].name; i++)
+ cl_assert(entries[i].seen);
+}
+
+static void setup_backend(const char *cfg)
+{
+ cl_git_pass(git_config_backend_from_string(&backend, cfg));
+ cl_git_pass(git_config_backend_open(backend, 0, NULL));
+}
+
+void test_config_memory__write_operations_fail(void)
+{
+ setup_backend("");
+ cl_git_fail(git_config_backend_set_string(backend, "general.foo", "var"));
+ cl_git_fail(git_config_backend_delete(backend, "general.foo"));
+ cl_git_fail(git_config_backend_lock(backend));
+ cl_git_fail(git_config_backend_unlock(backend, 0));
+}
+
+void test_config_memory__simple(void)
+{
+ setup_backend(
+ "[general]\n"
+ "foo=bar\n");
+
+ assert_config_contains(backend, "general.foo", "bar");
+}
+
+void test_config_memory__malformed_fails_to_open(void)
+{
+ cl_git_pass(git_config_backend_from_string(&backend,
+ "[general\n"
+ "foo=bar\n"));
+ cl_git_fail(git_config_backend_open(backend, 0, NULL));
+}
+
+void test_config_memory__multiple_vars(void)
+{
+ setup_backend(
+ "[general]\n"
+ "foo=bar\n"
+ "key=value\n");
+ assert_config_contains(backend, "general.foo", "bar");
+ assert_config_contains(backend, "general.key", "value");
+}
+
+void test_config_memory__multiple_sections(void)
+{
+ setup_backend(
+ "[general]\n"
+ "foo=bar\n"
+ "\n"
+ "[other]\n"
+ "key=value\n");
+ assert_config_contains(backend, "general.foo", "bar");
+ assert_config_contains(backend, "other.key", "value");
+}
+
+void test_config_memory__multivar_gets_correct_string(void)
+{
+ setup_backend(
+ "[general]\n"
+ "foo=bar1\n"
+ "foo=bar2\n");
+ assert_config_contains(backend, "general.foo", "bar2");
+}
+
+void test_config_memory__foreach_sees_multivar(void)
+{
+ struct expected_entry entries[] = {
+ { "general.foo", "bar1", 0 },
+ { "general.foo", "bar2", 0 },
+ { NULL, NULL, 0 },
+ };
+
+ setup_backend(
+ "[general]\n"
+ "foo=bar1\n"
+ "foo=bar2\n");
+ assert_config_contains_all(backend, entries);
+}