]> git.proxmox.com Git - libgit2.git/commitdiff
config: don't write section header if we're in it
authorEdward Thomson <ethomson@github.com>
Mon, 28 Mar 2016 15:13:51 +0000 (11:13 -0400)
committerEdward Thomson <ethomson@github.com>
Mon, 28 Mar 2016 15:13:51 +0000 (11:13 -0400)
If we hit the EOF while trying to write a new value, it may be that
we're already in the section that we were looking for.  If so, do not
write a (duplicate) section header, just write the value.

src/config_file.c

index 584b9fa823cc06b430eea7a409faa45f40f943bc..b2d21b00c5e6f5cdf94697175253c6372602da60 100644 (file)
@@ -1500,7 +1500,7 @@ static int config_parse(
        int (*on_section)(struct reader **reader, const char *current_section, const char *line, size_t line_len, void *data),
        int (*on_variable)(struct reader **reader, const char *current_section, char *var_name, char *var_value, const char *line, size_t line_len, void *data),
        int (*on_comment)(struct reader **reader, const char *line, size_t line_len, void *data),
-       int (*on_eof)(struct reader **reader, void *data),
+       int (*on_eof)(struct reader **reader, const char *current_section, void *data),
        void *data)
 {
        char *current_section = NULL, *var_name, *var_value, *line_start;
@@ -1551,7 +1551,7 @@ static int config_parse(
        }
 
        if (on_eof)
-               result = on_eof(&reader, data);
+               result = on_eof(&reader, current_section, data);
 
        git__free(current_section);
        return result;
@@ -1867,7 +1867,8 @@ static int write_on_comment(struct reader **reader, const char *line, size_t lin
        return write_line_to(&write_data->buffered_comment, line, line_len);
 }
 
-static int write_on_eof(struct reader **reader, void *data)
+static int write_on_eof(
+       struct reader **reader, const char *current_section, void *data)
 {
        struct write_data *write_data = (struct write_data *)data;
        int result = 0;
@@ -1886,7 +1887,11 @@ static int write_on_eof(struct reader **reader, void *data)
         * value.
         */
        if ((!write_data->preg || !write_data->preg_replaced) && write_data->value) {
-               if ((result = write_section(write_data->buf, write_data->section)) == 0)
+               /* write the section header unless we're already in it */
+               if (!current_section || strcmp(current_section, write_data->section))
+                       result = write_section(write_data->buf, write_data->section);
+
+               if (!result)
                        result = write_value(write_data);
        }