Commit 6e6da75fdb3c5d53c8df94d551aa9e6b9f4d8957

Patrick Steinhardt 2019-07-11T11:00:05

config_parse: remove use of `git_config_file` The config parser code needs to keep track of the current parsed file's name so that we are able to provide proper error messages to the user. Right now, we do that by storing a `git_config_file` in the parser structure, but as that is a specific backend and the parser aims to be generic, it is a layering violation. Switch over to use a simple string to fix that.

diff --git a/src/config_file.c b/src/config_file.c
index 8cadeba..89da829 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -889,7 +889,7 @@ static int config_read_buffer(
 	}
 
 	/* Initialize the reading position */
-	reader.file = file;
+	reader.path = file->path;
 	git_parse_ctx_init(&reader.ctx, buf, buflen);
 
 	/* If the file is empty, there's nothing for us to do */
@@ -1175,7 +1175,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char 
 	struct write_data write_data;
 
 	memset(&reader, 0, sizeof(reader));
-	reader.file = &cfg->file;
+	reader.path = cfg->file.path;
 
 	if (cfg->locked) {
 		result = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content) == NULL ? "" : git_buf_cstr(&cfg->locked_content));
diff --git a/src/config_mem.c b/src/config_mem.c
index c2ecfda..b563a97 100644
--- a/src/config_mem.c
+++ b/src/config_mem.c
@@ -87,7 +87,7 @@ static int config_memory_open(git_config_backend *backend, git_config_level_t le
 		return 0;
 
 	git_parse_ctx_init(&reader.ctx, memory_backend->cfg.ptr, memory_backend->cfg.size);
-	reader.file = NULL;
+	reader.path = "in-memory";
 	parse_data.entries = memory_backend->entries;
 	parse_data.level = level;
 
diff --git a/src/config_parse.c b/src/config_parse.c
index 6354f52..bda1840 100644
--- a/src/config_parse.c
+++ b/src/config_parse.c
@@ -16,16 +16,14 @@ const char *git_config_escaped = "\n\t\b\"\\";
 
 static void set_parse_error(git_config_parser *reader, int col, const char *error_str)
 {
-	const char *file = reader->file ? reader->file->path : "in-memory";
-
 	if (col)
 		git_error_set(GIT_ERROR_CONFIG,
 		              "failed to parse config file: %s (in %s:%"PRIuZ", column %d)",
-		              error_str, file, reader->ctx.line_num, col);
+		              error_str, reader->path, reader->ctx.line_num, col);
 	else
 		git_error_set(GIT_ERROR_CONFIG,
 		              "failed to parse config file: %s (in %s:%"PRIuZ")",
-		              error_str, file, reader->ctx.line_num);
+		              error_str, reader->path, reader->ctx.line_num);
 }
 
 
diff --git a/src/config_parse.h b/src/config_parse.h
index 3eef7d3..a25da9d 100644
--- a/src/config_parse.h
+++ b/src/config_parse.h
@@ -25,7 +25,7 @@ typedef struct config_file {
 } git_config_file;
 
 typedef struct {
-	git_config_file *file;
+	const char *path;
 	git_parse_ctx ctx;
 } git_config_parser;