More config code checks and cleanups
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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,