Skip to content

Commit fcb0d84

Browse files
committed
config_file: move cvar handling into append_entry
The code appending new configuration entries to our current list first allocates the `cvar` structure and then passes it to `append_entry`. As we want to extend `append_entry` to store configuration entries in a map as well as in a list for ordered iteration, we will have to create two `cvar` structures, though. As such, the future change will become much easier when allocation of the `cvar` structure is doen in `append_entry` itself.
1 parent dfcd923 commit fcb0d84

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/config_file.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,20 @@ int git_config_file_normalize_section(char *start, char *end)
118118
}
119119

120120
/* Add or append the new config option */
121-
static int append_entry(git_strmap *values, cvar_t *var)
121+
static int append_entry(git_strmap *values, git_config_entry *entry, bool included)
122122
{
123123
git_strmap_iter pos;
124-
cvar_t *existing;
124+
cvar_t *existing, *var;
125125
int error = 0;
126126

127-
pos = git_strmap_lookup_index(values, var->entry->name);
127+
var = git__calloc(1, sizeof(cvar_t));
128+
GITERR_CHECK_ALLOC(var);
129+
var->entry = entry;
130+
var->included = included;
131+
132+
pos = git_strmap_lookup_index(values, entry->name);
128133
if (!git_strmap_valid_index(values, pos)) {
129-
git_strmap_insert(values, var->entry->name, var, &error);
134+
git_strmap_insert(values, entry->name, var, &error);
130135
} else {
131136
existing = git_strmap_value_at(values, pos);
132137
while (existing->next != NULL) {
@@ -1028,7 +1033,7 @@ static int read_on_variable(
10281033
{
10291034
struct parse_data *parse_data = (struct parse_data *)data;
10301035
git_buf buf = GIT_BUF_INIT;
1031-
cvar_t *var;
1036+
git_config_entry *entry;
10321037
int result = 0;
10331038

10341039
GIT_UNUSED(line);
@@ -1043,28 +1048,24 @@ static int read_on_variable(
10431048
return -1;
10441049
}
10451050

1046-
var = git__calloc(1, sizeof(cvar_t));
1047-
GITERR_CHECK_ALLOC(var);
1048-
var->entry = git__calloc(1, sizeof(git_config_entry));
1049-
GITERR_CHECK_ALLOC(var->entry);
1050-
1051-
var->entry->name = git_buf_detach(&buf);
1052-
var->entry->value = var_value;
1053-
var->entry->level = parse_data->level;
1054-
var->included = !!parse_data->depth;
1051+
entry = git__calloc(1, sizeof(git_config_entry));
1052+
GITERR_CHECK_ALLOC(entry);
1053+
entry->name = git_buf_detach(&buf);
1054+
entry->value = var_value;
1055+
entry->level = parse_data->level;
10551056

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

10591060
result = 0;
10601061

10611062
/* Add or append the new config option */
1062-
if (!git__strcmp(var->entry->name, "include.path"))
1063-
result = parse_include(reader, parse_data, var->entry->value);
1064-
else if (!git__prefixcmp(var->entry->name, "includeif.") &&
1065-
!git__suffixcmp(var->entry->name, ".path"))
1063+
if (!git__strcmp(entry->name, "include.path"))
1064+
result = parse_include(reader, parse_data, entry->value);
1065+
else if (!git__prefixcmp(entry->name, "includeif.") &&
1066+
!git__suffixcmp(entry->name, ".path"))
10661067
result = parse_conditional_include(reader, parse_data,
1067-
var->entry->name, var->entry->value);
1068+
entry->name, entry->value);
10681069

10691070

10701071
return result;

0 commit comments

Comments
 (0)