Commit 0700ca1a74b58b73c4fb9ececffeaa80046c2f58

Russell Belfer 2013-05-23T16:11:53

More config code checks and cleanups

diff --git a/src/config.c b/src/config.c
index 49b9123..e436a31 100644
--- a/src/config.c
+++ b/src/config.c
@@ -339,17 +339,6 @@ int git_config_foreach_match(
 	return ret;
 }
 
-int git_config_delete_entry(git_config *cfg, const char *name)
-{
-	git_config_backend *file;
-	file_internal *internal;
-
-	internal = git_vector_get(&cfg->files, 0);
-	file = internal->file;
-
-	return file->del(file, name);
-}
-
 /**************
  * Setters
  **************/
@@ -361,6 +350,19 @@ static int config_error_nofiles(const char *name)
 	return GIT_ENOTFOUND;
 }
 
+int git_config_delete_entry(git_config *cfg, const char *name)
+{
+	git_config_backend *file;
+	file_internal *internal;
+
+	internal = git_vector_get(&cfg->files, 0);
+	if (!internal || !internal->file)
+		return config_error_nofiles(name);
+	file = internal->file;
+
+	return file->del(file, name);
+}
+
 int git_config_set_int64(git_config *cfg, const char *name, int64_t value)
 {
 	char str_value[32]; /* All numbers should fit in here */
@@ -390,7 +392,7 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
 	}
 
 	internal = git_vector_get(&cfg->files, 0);
-	if (!internal)
+	if (!internal || !internal->file)
 		return config_error_nofiles(name);
 	file = internal->file;
 
@@ -468,7 +470,7 @@ static int get_string(const char **out, const git_config *cfg, const char *name)
 	int res;
 
 	git_vector_foreach(&cfg->files, i, internal) {
-		if (!internal || !internal->file || !internal->file->get)
+		if (!internal || !internal->file)
 			continue;
 
 		res = get_string_at_file(out, internal->file, name);
@@ -506,12 +508,17 @@ int git_config_get_entry(const git_config_entry **out, const git_config *cfg, co
 {
 	file_internal *internal;
 	unsigned int i;
+	git_config_backend *file;
+	int ret;
 
 	*out = NULL;
 
 	git_vector_foreach(&cfg->files, i, internal) {
-		git_config_backend *file = internal->file;
-		int ret = file->get(file, name, out);
+		if (!internal || !internal->file)
+			continue;
+		file = internal->file;
+
+		ret = file->get(file, name, out);
 		if (ret != GIT_ENOTFOUND)
 			return ret;
 	}
@@ -519,8 +526,9 @@ int git_config_get_entry(const git_config_entry **out, const git_config *cfg, co
 	return config_error_notfound(name);
 }
 
-int git_config_get_multivar(const git_config *cfg, const char *name, const char *regexp,
-		git_config_foreach_cb cb, void *payload)
+int git_config_get_multivar(
+	const git_config *cfg, const char *name, const char *regexp,
+	git_config_foreach_cb cb, void *payload)
 {
 	file_internal *internal;
 	git_config_backend *file;
@@ -533,7 +541,10 @@ int git_config_get_multivar(const git_config *cfg, const char *name, const char 
 	 */
 	for (i = cfg->files.length; i > 0; --i) {
 		internal = git_vector_get(&cfg->files, i - 1);
+		if (!internal || !internal->file)
+			continue;
 		file = internal->file;
+
 		ret = file->get_multivar(file, name, regexp, cb, payload);
 		if (ret < 0 && ret != GIT_ENOTFOUND)
 			return ret;
@@ -548,7 +559,7 @@ int git_config_set_multivar(git_config *cfg, const char *name, const char *regex
 	file_internal *internal;
 
 	internal = git_vector_get(&cfg->files, 0);
-	if (!internal)
+	if (!internal || !internal->file)
 		return config_error_nofiles(name);
 	file = internal->file;
 
@@ -643,13 +654,12 @@ int git_config_open_default(git_config **out)
 	git_config *cfg = NULL;
 	git_buf buf = GIT_BUF_INIT;
 
-	error = git_config_new(&cfg);
+	if ((error = git_config_new(&cfg)) < 0)
+		return error;
 
-	if (!error && (!git_config_find_global_r(&buf) ||
-		       !git_config__global_location(&buf))) {
+	if (!git_config_find_global_r(&buf) || !git_config__global_location(&buf)) {
 		error = git_config_add_file_ondisk(cfg, buf.ptr,
 			GIT_CONFIG_LEVEL_GLOBAL, 0);
-	} else {
 	}
 
 	if (!error && !git_config_find_xdg_r(&buf))
@@ -662,7 +672,7 @@ int git_config_open_default(git_config **out)
 
 	git_buf_free(&buf);
 
-	if (error && cfg) {
+	if (error) {
 		git_config_free(cfg);
 		cfg = NULL;
 	}
@@ -675,6 +685,7 @@ int git_config_open_default(git_config **out)
 /***********
  * Parsers
  ***********/
+
 int git_config_lookup_map_value(
 	int *out,
 	const git_cvar_map *maps,