Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a13c1ec

Browse files
author
Edward Thomson
committed
config: don't write section header if we're in it
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.
1 parent e8d5df9 commit a13c1ec

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/config_file.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ static int config_parse(
14721472
int (*on_section)(struct reader **reader, const char *current_section, const char *line, size_t line_len, void *data),
14731473
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),
14741474
int (*on_comment)(struct reader **reader, const char *line, size_t line_len, void *data),
1475-
int (*on_eof)(struct reader **reader, void *data),
1475+
int (*on_eof)(struct reader **reader, const char *current_section, void *data),
14761476
void *data)
14771477
{
14781478
char *current_section = NULL, *var_name, *var_value, *line_start;
@@ -1523,7 +1523,7 @@ static int config_parse(
15231523
}
15241524

15251525
if (on_eof)
1526-
result = on_eof(&reader, data);
1526+
result = on_eof(&reader, current_section, data);
15271527

15281528
git__free(current_section);
15291529
return result;
@@ -1839,7 +1839,8 @@ static int write_on_comment(struct reader **reader, const char *line, size_t lin
18391839
return write_line_to(&write_data->buffered_comment, line, line_len);
18401840
}
18411841

1842-
static int write_on_eof(struct reader **reader, void *data)
1842+
static int write_on_eof(
1843+
struct reader **reader, const char *current_section, void *data)
18431844
{
18441845
struct write_data *write_data = (struct write_data *)data;
18451846
int result = 0;
@@ -1858,7 +1859,11 @@ static int write_on_eof(struct reader **reader, void *data)
18581859
* value.
18591860
*/
18601861
if ((!write_data->preg || !write_data->preg_replaced) && write_data->value) {
1861-
if ((result = write_section(write_data->buf, write_data->section)) == 0)
1862+
/* write the section header unless we're already in it */
1863+
if (!current_section || strcmp(current_section, write_data->section))
1864+
result = write_section(write_data->buf, write_data->section);
1865+
1866+
if (!result)
18621867
result = write_value(write_data);
18631868
}
18641869

0 commit comments

Comments
 (0)