summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-06-27 08:54:51 +0200
committerPatrick Steinhardt <ps@pks.im>2019-07-11 08:31:26 +0200
commita0dc3027f118119d3336db08251c646291f2c098 (patch)
tree443311db7eb1895cf2ff80736c056e1b78d149c9
parent985f5cdf7c9ae8bc5111941b0f9b9ebbca2c4cb2 (diff)
downloadlibgit2-a0dc3027f118119d3336db08251c646291f2c098.tar.gz
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`.
-rw-r--r--src/config_file.c52
1 files changed, 30 insertions, 22 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 77edf132c..aee17448d 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -170,48 +170,56 @@ out:
return error;
}
-static int config_refresh(git_config_backend *cfg)
+static int config_set_entries(git_config_backend *cfg, git_config_entries *entries)
{
diskfile_backend *b = (diskfile_backend *)cfg;
- git_config_entries *entries = NULL, *tmp;
+ git_config_entries *old = NULL;
git_config_file *include;
- int error, modified;
- uint32_t i;
+ int error;
+ size_t i;
if (b->header.parent.readonly)
return config_error_readonly();
- error = config_is_modified(&modified, &b->file);
- if (error < 0 && error != GIT_ENOTFOUND)
- goto out;
-
- if (!modified)
- return 0;
-
- if ((error = git_config_entries_new(&entries)) < 0)
- goto out;
-
- /* Reparse the current configuration */
- git_array_foreach(b->file.includes, i, include) {
+ git_array_foreach(b->file.includes, i, include)
config_file_clear(include);
- }
git_array_clear(b->file.includes);
- if ((error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0)
- goto out;
-
if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) {
git_error_set(GIT_ERROR_OS, "failed to lock config backend");
goto out;
}
- tmp = b->header.entries;
+ old = b->header.entries;
b->header.entries = entries;
- entries = tmp;
git_mutex_unlock(&b->header.values_mutex);
out:
+ git_config_entries_free(old);
+ return error;
+}
+
+static int config_refresh(git_config_backend *cfg)
+{
+ diskfile_backend *b = (diskfile_backend *)cfg;
+ git_config_entries *entries = NULL;
+ int error, modified;
+
+ error = config_is_modified(&modified, &b->file);
+ if (error < 0 && error != GIT_ENOTFOUND)
+ goto out;
+
+ if (!modified)
+ return 0;
+
+ if ((error = git_config_entries_new(&entries)) < 0 ||
+ (error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0 ||
+ (error = config_set_entries(cfg, entries)) < 0)
+ goto out;
+
+ entries = NULL;
+out:
git_config_entries_free(entries);
return (error == GIT_ENOTFOUND) ? 0 : error;