Skip to content

Commit a0dc302

Browse files
committed
config_file: split out function that sets config entries
Updating a config file backend's config entries is a bit more involved, as it requires clearing of the old config entries as well as handling locking correctly. As we will need this functionality in a future patch to refresh config entries from a buffer, let's extract this into its own function `config_set_entries`.
1 parent 985f5cd commit a0dc302

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/config_file.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,47 +170,55 @@ static int config_is_modified(int *modified, git_config_file *file)
170170
return error;
171171
}
172172

173-
static int config_refresh(git_config_backend *cfg)
173+
static int config_set_entries(git_config_backend *cfg, git_config_entries *entries)
174174
{
175175
diskfile_backend *b = (diskfile_backend *)cfg;
176-
git_config_entries *entries = NULL, *tmp;
176+
git_config_entries *old = NULL;
177177
git_config_file *include;
178-
int error, modified;
179-
uint32_t i;
178+
int error;
179+
size_t i;
180180

181181
if (b->header.parent.readonly)
182182
return config_error_readonly();
183183

184-
error = config_is_modified(&modified, &b->file);
185-
if (error < 0 && error != GIT_ENOTFOUND)
186-
goto out;
187-
188-
if (!modified)
189-
return 0;
190-
191-
if ((error = git_config_entries_new(&entries)) < 0)
192-
goto out;
193-
194-
/* Reparse the current configuration */
195-
git_array_foreach(b->file.includes, i, include) {
184+
git_array_foreach(b->file.includes, i, include)
196185
config_file_clear(include);
197-
}
198186
git_array_clear(b->file.includes);
199187

200-
if ((error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0)
201-
goto out;
202-
203188
if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) {
204189
git_error_set(GIT_ERROR_OS, "failed to lock config backend");
205190
goto out;
206191
}
207192

208-
tmp = b->header.entries;
193+
old = b->header.entries;
209194
b->header.entries = entries;
210-
entries = tmp;
211195

212196
git_mutex_unlock(&b->header.values_mutex);
213197

198+
out:
199+
git_config_entries_free(old);
200+
return error;
201+
}
202+
203+
static int config_refresh(git_config_backend *cfg)
204+
{
205+
diskfile_backend *b = (diskfile_backend *)cfg;
206+
git_config_entries *entries = NULL;
207+
int error, modified;
208+
209+
error = config_is_modified(&modified, &b->file);
210+
if (error < 0 && error != GIT_ENOTFOUND)
211+
goto out;
212+
213+
if (!modified)
214+
return 0;
215+
216+
if ((error = git_config_entries_new(&entries)) < 0 ||
217+
(error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0 ||
218+
(error = config_set_entries(cfg, entries)) < 0)
219+
goto out;
220+
221+
entries = NULL;
214222
out:
215223
git_config_entries_free(entries);
216224

0 commit comments

Comments
 (0)