Commit a0dc3027f118119d3336db08251c646291f2c098

Patrick Steinhardt 2019-06-27T08:54:51

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`.

diff --git a/src/config_file.c b/src/config_file.c
index 77edf13..aee1744 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;