Skip to content

Commit dbeadf8

Browse files
committed
config_parse: provide parser init and dispose functions
Right now, all configuration file backends are expected to directly mess with the configuration parser's internals in order to set it up. Let's avoid doing that by implementing both a `git_config_parser_init` and `git_config_parser_dispose` function to clearly define the interface between configuration backends and the parser. Ideally, we would make the `git_config_parser` structure definition private to its implementation. But as that would require an additional memory allocation that was not required before we just live with it being visible to others.
1 parent 3215752 commit dbeadf8

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

src/config_file.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,9 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
11761176
{
11771177
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
11781178
git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT;
1179+
git_config_parser parser = GIT_CONFIG_PARSER_INIT;
11791180
git_filebuf file = GIT_FILEBUF_INIT;
11801181
struct write_data write_data;
1181-
git_config_parser reader;
11821182
int error;
11831183

11841184
memset(&write_data, 0, sizeof(write_data));
@@ -1196,8 +1196,8 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
11961196
if (error < 0 && error != GIT_ENOTFOUND)
11971197
goto done;
11981198

1199-
reader.path = cfg->file.path;
1200-
git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
1199+
if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0)
1200+
goto done;
12011201

12021202
ldot = strrchr(key, '.');
12031203
name = ldot + 1;
@@ -1217,7 +1217,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
12171217
write_data.preg = preg;
12181218
write_data.value = value;
12191219

1220-
if ((error = git_config_parse(&reader, write_on_section, write_on_variable,
1220+
if ((error = git_config_parse(&parser, write_on_section, write_on_variable,
12211221
write_on_comment, write_on_eof, &write_data)) < 0)
12221222
goto done;
12231223

@@ -1243,7 +1243,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
12431243
git_buf_dispose(&buf);
12441244
git_buf_dispose(&contents);
12451245
git_filebuf_cleanup(&file);
1246-
git_parse_ctx_clear(&reader.ctx);
1246+
git_config_parser_dispose(&parser);
12471247

12481248
return error;
12491249
}

src/config_mem.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,24 @@ static int read_variable_cb(
7878
static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo)
7979
{
8080
config_memory_backend *memory_backend = (config_memory_backend *) backend;
81+
git_config_parser parser = GIT_PARSE_CTX_INIT;
8182
config_memory_parse_data parse_data;
82-
git_config_parser reader;
83+
int error;
8384

8485
GIT_UNUSED(repo);
8586

86-
if (memory_backend->cfg.size == 0)
87-
return 0;
88-
89-
git_parse_ctx_init(&reader.ctx, memory_backend->cfg.ptr, memory_backend->cfg.size);
90-
reader.path = "in-memory";
87+
if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr,
88+
memory_backend->cfg.size)) < 0)
89+
goto out;
9190
parse_data.entries = memory_backend->entries;
9291
parse_data.level = level;
9392

94-
return git_config_parse(&reader, NULL, read_variable_cb, NULL, NULL, &parse_data);
93+
if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0)
94+
goto out;
95+
96+
out:
97+
git_config_parser_dispose(&parser);
98+
return error;
9599
}
96100

97101
static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out)

src/config_parse.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,17 @@ static int parse_variable(git_config_parser *reader, char **var_name, char **var
474474
return error;
475475
}
476476

477+
int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen)
478+
{
479+
out->path = path;
480+
return git_parse_ctx_init(&out->ctx, data, datalen);
481+
}
482+
483+
void git_config_parser_dispose(git_config_parser *parser)
484+
{
485+
git_parse_ctx_clear(&parser->ctx);
486+
}
487+
477488
int git_config_parse(
478489
git_config_parser *parser,
479490
git_config_parser_section_cb on_section,

src/config_parse.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ typedef struct {
2222
git_parse_ctx ctx;
2323
} git_config_parser;
2424

25+
#define GIT_CONFIG_PARSER_INIT { NULL, GIT_PARSE_CTX_INIT }
26+
2527
typedef int (*git_config_parser_section_cb)(
2628
git_config_parser *parser,
2729
const char *current_section,
@@ -49,6 +51,9 @@ typedef int (*git_config_parser_eof_cb)(
4951
const char *current_section,
5052
void *payload);
5153

54+
int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen);
55+
void git_config_parser_dispose(git_config_parser *parser);
56+
5257
int git_config_parse(
5358
git_config_parser *parser,
5459
git_config_parser_section_cb on_section,

src/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ typedef struct {
2323
size_t line_num;
2424
} git_parse_ctx;
2525

26+
#define GIT_PARSE_CTX_INIT { 0 }
27+
2628
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
2729
void git_parse_ctx_clear(git_parse_ctx *ctx);
2830

0 commit comments

Comments
 (0)