Commit 26cf48fcf8cf0ecd2b098a41c8873a75e0186577

Patrick Steinhardt 2018-02-09T11:35:16

config_file: move include depth into config entry In order to reject writes to included configuration entries, we need to keep track of whether an entry was included via another configuration file or not. This information is being stored in the `cvar` structure, which is a rather weird location, as it is only used to create a list structure of config entries. Move the include depth into the structure `git_config_entry` instead. While this fixes the layering issue, it enables users of libgit2 to access the depth, too.

diff --git a/include/git2/config.h b/include/git2/config.h
index d9da65b..7bcca7d 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -64,6 +64,7 @@ typedef enum {
 typedef struct git_config_entry {
 	const char *name; /**< Name of the entry (normalised) */
 	const char *value; /**< String value of the entry */
+	unsigned int include_depth; /**< Depth of includes where this variable was found */
 	git_config_level_t level; /**< Which config file this was found in */
 	void (*free)(struct git_config_entry *entry); /**< Free function for this entry */
 	void *payload; /**< Opaque value for the free function. Do not read or write */
diff --git a/src/config_file.c b/src/config_file.c
index 4cf322f..d4e1c0a 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -26,7 +26,6 @@
 typedef struct cvar_t {
 	struct cvar_t *next;
 	git_config_entry *entry;
-	bool included; /* whether this is part of [include] */
 } cvar_t;
 
 typedef struct git_config_file_iter {
@@ -118,7 +117,7 @@ int git_config_file_normalize_section(char *start, char *end)
 }
 
 /* Add or append the new config option */
-static int append_entry(git_strmap *values, git_config_entry *entry, bool included)
+static int append_entry(git_strmap *values, git_config_entry *entry)
 {
 	git_strmap_iter pos;
 	cvar_t *existing, *var;
@@ -127,7 +126,6 @@ static int append_entry(git_strmap *values, git_config_entry *entry, bool includ
 	var = git__calloc(1, sizeof(cvar_t));
 	GITERR_CHECK_ALLOC(var);
 	var->entry = entry;
-	var->included = included;
 
 	pos = git_strmap_lookup_index(values, entry->name);
 	if (!git_strmap_valid_index(values, pos)) {
@@ -444,7 +442,7 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
 			goto out;
 		}
 
-		if (existing->included) {
+		if (existing->entry->include_depth) {
 			giterr_set(GITERR_CONFIG, "modifying included variable is not supported");
 			ret = -1;
 			goto out;
@@ -584,7 +582,7 @@ static int config_delete(git_config_backend *cfg, const char *name)
 	var = git_strmap_value_at(values, pos);
 	refcounted_strmap_free(map);
 
-	if (var->included) {
+	if (var->entry->include_depth) {
 		giterr_set(GITERR_CONFIG, "cannot delete included variable");
 		return -1;
 	}
@@ -884,7 +882,7 @@ struct parse_data {
 	const char *file_path;
 	git_strmap *values;
 	git_config_level_t level;
-	int depth;
+	unsigned int depth;
 };
 
 static int parse_include(git_config_parser *reader,
@@ -1053,8 +1051,9 @@ static int read_on_variable(
 	entry->name = git_buf_detach(&buf);
 	entry->value = var_value;
 	entry->level = parse_data->level;
+	entry->include_depth = parse_data->depth;
 
-	if ((result = append_entry(parse_data->values, entry, !!parse_data->depth)) < 0)
+	if ((result = append_entry(parse_data->values, entry)) < 0)
 		return result;
 
 	result = 0;