summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-08-16 10:27:49 +0200
committerGitHub <noreply@github.com>2018-08-16 10:27:49 +0200
commit43e7bf78aa28b8a5bc43f0f3ba91b06d98ca6b70 (patch)
tree60eb7b52c61759fdebc6023d93dee834dc8604fd
parent227ace0f0c9f86807d0c1ec20e2076b8c4234b5d (diff)
parent6698e0562d0f782903f28c224c879da7c2abf674 (diff)
downloadlibgit2-43e7bf78aa28b8a5bc43f0f3ba91b06d98ca6b70.tar.gz
Merge pull request #4750 from nelhage/nelhage-config-no-section
config_file: Don't crash on options without a section
-rw-r--r--src/config_file.c11
-rw-r--r--tests/config/read.c33
-rw-r--r--tests/resources/config/config-nosection1
3 files changed, 43 insertions, 2 deletions
diff --git a/src/config_file.c b/src/config_file.c
index ea72648aa..050bcfc92 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1035,8 +1035,15 @@ static int read_on_variable(
GIT_UNUSED(line);
GIT_UNUSED(line_len);
- git_buf_puts(&buf, current_section);
- git_buf_putc(&buf, '.');
+ if (current_section) {
+ /* TODO: Once warnings lang, 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));
diff --git a/tests/config/read.c b/tests/config/read.c
index ddc5c8ce6..e9a39e5ad 100644
--- a/tests/config/read.c
+++ b/tests/config/read.c
@@ -758,3 +758,36 @@ void test_config_read__bom(void)
git_config_free(cfg);
git_buf_dispose(&buf);
}
+
+static int read_nosection_cb(const git_config_entry *entry, void *payload) {
+ int *seen = (int*)payload;
+ if (strcmp(entry->name, "key") == 0) {
+ (*seen)++;
+ }
+ return 0;
+}
+
+/* This would ideally issue a warning, if we had a way to do so. */
+void test_config_read__nosection(void)
+{
+ git_config *cfg;
+ git_buf buf = GIT_BUF_INIT;
+ int seen = 0;
+
+ cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-nosection")));
+
+ /*
+ * Given a key with no section, we do not allow reading it,
+ * but we do include it in an iteration over the config
+ * store. This appears to match how git's own APIs (and
+ * git-config(1)) behave.
+ */
+
+ cl_git_fail_with(git_config_get_string_buf(&buf, cfg, "key"), GIT_EINVALIDSPEC);
+
+ cl_git_pass(git_config_foreach(cfg, read_nosection_cb, &seen));
+ cl_assert_equal_i(seen, 1);
+
+ git_buf_dispose(&buf);
+ git_config_free(cfg);
+}
diff --git a/tests/resources/config/config-nosection b/tests/resources/config/config-nosection
new file mode 100644
index 000000000..dd2ee0857
--- /dev/null
+++ b/tests/resources/config/config-nosection
@@ -0,0 +1 @@
+key = value