config: create a function to format section headers

Factor out the logic which creates section headers in the config file, e.g. the 'branch.foo' key will be turned into '[branch "foo"]'. This introduces no function changes, but is needed for a later change which adds support for copying branch sections in the config file. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Sahil Dua <sahildua2305@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sahil Dua committed Jun 18, 2017 at 23:16 UTC 5463caab15fb795da2aab01827419b094146d1c4
1 file changed +11 -2
config.c
+11 -2
@@ -2169,10 +2169,10 @@ static int write_error(const char *filename)
2169 return 4;
2170 }
2171
2172 -static int store_write_section(int fd, const char *key)
2172 +static struct strbuf store_create_section(const char *key)
2173 {
2174 const char *dot;
2175 - int i, success;
2175 + int i;
2176 struct strbuf sb = STRBUF_INIT;
2177
2178 dot = memchr(key, '.', store.baselen);
@@ -2188,6 +2188,15 @@ static int store_write_section(int fd, const char *key)
2188 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
2189 }
2190
2191 + return sb;
2192 +}
2193 +
2194 +static int store_write_section(int fd, const char *key)
2195 +{
2196 + int success;
2197 +
2198 + struct strbuf sb = store_create_section(key);
2199 +
2200 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
2201 strbuf_release(&sb);
2202