Skip to content

Commit 26cf48f

Browse files
committed
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.
1 parent fcb0d84 commit 26cf48f

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

include/git2/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ typedef enum {
6464
typedef struct git_config_entry {
6565
const char *name; /**< Name of the entry (normalised) */
6666
const char *value; /**< String value of the entry */
67+
unsigned int include_depth; /**< Depth of includes where this variable was found */
6768
git_config_level_t level; /**< Which config file this was found in */
6869
void (*free)(struct git_config_entry *entry); /**< Free function for this entry */
6970
void *payload; /**< Opaque value for the free function. Do not read or write */

src/config_file.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
typedef struct cvar_t {
2727
struct cvar_t *next;
2828
git_config_entry *entry;
29-
bool included; /* whether this is part of [include] */
3029
} cvar_t;
3130

3231
typedef struct git_config_file_iter {
@@ -118,7 +117,7 @@ int git_config_file_normalize_section(char *start, char *end)
118117
}
119118

120119
/* Add or append the new config option */
121-
static int append_entry(git_strmap *values, git_config_entry *entry, bool included)
120+
static int append_entry(git_strmap *values, git_config_entry *entry)
122121
{
123122
git_strmap_iter pos;
124123
cvar_t *existing, *var;
@@ -127,7 +126,6 @@ static int append_entry(git_strmap *values, git_config_entry *entry, bool includ
127126
var = git__calloc(1, sizeof(cvar_t));
128127
GITERR_CHECK_ALLOC(var);
129128
var->entry = entry;
130-
var->included = included;
131129

132130
pos = git_strmap_lookup_index(values, entry->name);
133131
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
444442
goto out;
445443
}
446444

447-
if (existing->included) {
445+
if (existing->entry->include_depth) {
448446
giterr_set(GITERR_CONFIG, "modifying included variable is not supported");
449447
ret = -1;
450448
goto out;
@@ -584,7 +582,7 @@ static int config_delete(git_config_backend *cfg, const char *name)
584582
var = git_strmap_value_at(values, pos);
585583
refcounted_strmap_free(map);
586584

587-
if (var->included) {
585+
if (var->entry->include_depth) {
588586
giterr_set(GITERR_CONFIG, "cannot delete included variable");
589587
return -1;
590588
}
@@ -884,7 +882,7 @@ struct parse_data {
884882
const char *file_path;
885883
git_strmap *values;
886884
git_config_level_t level;
887-
int depth;
885+
unsigned int depth;
888886
};
889887

890888
static int parse_include(git_config_parser *reader,
@@ -1053,8 +1051,9 @@ static int read_on_variable(
10531051
entry->name = git_buf_detach(&buf);
10541052
entry->value = var_value;
10551053
entry->level = parse_data->level;
1054+
entry->include_depth = parse_data->depth;
10561055

1057-
if ((result = append_entry(parse_data->values, entry, !!parse_data->depth)) < 0)
1056+
if ((result = append_entry(parse_data->values, entry)) < 0)
10581057
return result;
10591058

10601059
result = 0;

0 commit comments

Comments
 (0)