]> git.proxmox.com Git - libgit2.git/commitdiff
config: write out section headers with subsections correctly
authorCarlos Martín Nieto <carlos@cmartin.tk>
Fri, 9 Mar 2012 19:38:32 +0000 (20:38 +0100)
committerCarlos Martín Nieto <carlos@cmartin.tk>
Fri, 9 Mar 2012 19:43:17 +0000 (20:43 +0100)
write_section() mistakenly treated is input as the whole variable name
instead of simply the section (and possibly subsection) and would
confuse "section.subsection" as a section plus variable name and
produce a wrong section header.

Fix this and include a test for writing "section.subsection.var" and
reading it from the file.

src/config_file.c
tests-clar/config/write.c

index 3c7c593ec58c5cf8a9006d6882a3be21661baa6d..e1f4ef932adbe854cd70c0edc9276aab48d521e4 100644 (file)
@@ -925,22 +925,18 @@ static int config_parse(diskfile_backend *cfg_file)
 static int write_section(git_filebuf *file, const char *key)
 {
        int error;
-       const char *fdot, *ldot;
+       const char *dot;
        git_buf buf = GIT_BUF_INIT;
 
        /* All of this just for [section "subsection"] */
-       fdot = strchr(key, '.');
+       dot = strchr(key, '.');
        git_buf_putc(&buf, '[');
-       if (fdot == NULL)
+       if (dot == NULL) {
                git_buf_puts(&buf, key);
-       else
-               git_buf_put(&buf, key, fdot - key);
-       ldot = strrchr(key, '.');
-       if (fdot != ldot && fdot != NULL) {
-               git_buf_putc(&buf, '"');
+       } else {
+               git_buf_put(&buf, key, dot - key);
                /* TODO: escape  */
-               git_buf_put(&buf, fdot + 1, ldot - fdot - 1);
-               git_buf_putc(&buf, '"');
+               git_buf_printf(&buf, " \"%s\"", dot + 1);
        }
        git_buf_puts(&buf, "]\n");
        if (git_buf_oom(&buf))
index d22c6f2cf1d2450acc4526204f4a0b060e74b69b..f25bf5a91ae97196e09955ad3e88f0b30f6bf06d 100644 (file)
@@ -67,6 +67,21 @@ void test_config_write__delete_value(void)
        git_config_free(cfg);
 }
 
+void test_config_write__write_subsection(void)
+{
+       git_config *cfg;
+       const char *str;
+
+       cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
+       cl_git_pass(git_config_set_string(cfg, "my.own.var", "works"));
+       git_config_free(cfg);
+
+       cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
+       cl_git_pass(git_config_get_string(cfg, "my.own.var", &str));
+       cl_git_pass(strcmp(str, "works"));
+       git_config_free(cfg);
+}
+
 void test_config_write__delete_inexistent(void)
 {
        git_config *cfg;