Merge pull request #1604 from arrbee/config-file-checks Extend checking for config with no files
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
diff --git a/src/config.c b/src/config.c
index dc8c702..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;
@@ -465,10 +467,13 @@ static int get_string(const char **out, const git_config *cfg, const char *name)
{
file_internal *internal;
unsigned int i;
+ int res;
git_vector_foreach(&cfg->files, i, internal) {
- int res = get_string_at_file(out, internal->file, name);
+ if (!internal || !internal->file)
+ continue;
+ res = get_string_at_file(out, internal->file, name);
if (res != GIT_ENOTFOUND)
return res;
}
@@ -503,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;
}
@@ -516,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;
@@ -530,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;
@@ -545,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;
@@ -640,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))
@@ -659,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;
}
@@ -672,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,
diff --git a/tests-clar/repo/config.c b/tests-clar/repo/config.c
index 086fb5e..b8971bb 100644
--- a/tests-clar/repo/config.c
+++ b/tests-clar/repo/config.c
@@ -13,14 +13,15 @@ void test_repo_config__initialize(void)
cl_must_pass(p_mkdir("alternate", 0777));
cl_git_pass(git_path_prettify(&path, "alternate", NULL));
-
}
void test_repo_config__cleanup(void)
{
+ cl_git_pass(git_path_prettify(&path, "alternate", NULL));
cl_git_pass(git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES));
-
git_buf_free(&path);
+ cl_assert(!git_path_isdir("alternate"));
+
cl_fixture_cleanup("empty_standard_repo");
}
@@ -73,3 +74,127 @@ void test_repo_config__open_missing_global_with_separators(void)
git_config_free(config);
git_repository_free(repo);
}
+
+#include "repository.h"
+
+void test_repo_config__read_no_configs(void)
+{
+ git_repository *repo;
+ int val;
+
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
+
+ /* with none */
+
+ cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
+ cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
+ git_repository_free(repo);
+
+ /* with just system */
+
+ cl_must_pass(p_mkdir("alternate/1", 0777));
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, "1"));
+ cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(10, val);
+ git_repository_free(repo);
+
+ /* with xdg + system */
+
+ cl_must_pass(p_mkdir("alternate/2", 0777));
+ path.ptr[path.size - 1] = '2';
+ cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(20, val);
+ git_repository_free(repo);
+
+ /* with global + xdg + system */
+
+ cl_must_pass(p_mkdir("alternate/3", 0777));
+ path.ptr[path.size - 1] = '3';
+ cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
+ cl_git_pass(git_libgit2_opts(
+ GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(30, val);
+ git_repository_free(repo);
+
+ /* with all configs */
+
+ cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(40, val);
+ git_repository_free(repo);
+
+ /* with all configs but delete the files ? */
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(40, val);
+
+ cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
+ cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+
+ cl_must_pass(p_unlink("alternate/1/gitconfig"));
+ cl_assert(!git_path_isfile("alternate/1/gitconfig"));
+
+ cl_must_pass(p_unlink("alternate/2/config"));
+ cl_assert(!git_path_isfile("alternate/2/config"));
+
+ cl_must_pass(p_unlink("alternate/3/.gitconfig"));
+ cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
+
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(40, val);
+ git_repository_free(repo);
+
+ /* reopen */
+
+ cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
+ cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
+
+ cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
+ git_repository__cvar_cache_clear(repo);
+ val = -1;
+ cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
+ cl_assert_equal_i(7, val);
+ git_repository_free(repo);
+
+ cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
+ cl_assert(!git_path_exists("alternate/3/.gitconfig"));
+}