Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/libgit2/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,20 @@ int git_config_open_default(git_config **out)
int error;
git_config *config = NULL;
git_str buf = GIT_STR_INIT;
int found_global = 0;

if ((error = git_config_new(&config)) < 0)
return error;

if (!git_config__find_global(&buf) ||
!git_config__global_location(&buf)) {
/* GIT_CONFIG_GLOBAL overrides the global configuration file */
if (git__getenv(&buf, "GIT_CONFIG_GLOBAL") == 0) {
found_global = 1;
} else if (!git_config__find_global(&buf) ||
!git_config__global_location(&buf)) {
found_global = 1;
}

if (found_global) {
error = git_config_add_file_ondisk(config, buf.ptr,
GIT_CONFIG_LEVEL_GLOBAL, NULL, 0);
}
Expand All @@ -1314,9 +1322,31 @@ int git_config_open_default(git_config **out)
error = git_config_add_file_ondisk(config, buf.ptr,
GIT_CONFIG_LEVEL_XDG, NULL, 0);

if (!error && !git_config__find_system(&buf))
error = git_config_add_file_ondisk(config, buf.ptr,
GIT_CONFIG_LEVEL_SYSTEM, NULL, 0);
/* GIT_CONFIG_NOSYSTEM and GIT_CONFIG_SYSTEM override system config */
if (!error) {
git_str no_system_buf = GIT_STR_INIT;
int no_system = 0;
int env_error;

env_error = git__getenv(&no_system_buf, "GIT_CONFIG_NOSYSTEM");

if (env_error && env_error != GIT_ENOTFOUND) {
git_str_dispose(&no_system_buf);
error = env_error;
} else {
git_config_parse_bool(&no_system, no_system_buf.ptr);
git_str_dispose(&no_system_buf);

if (!no_system) {
if (git__getenv(&buf, "GIT_CONFIG_SYSTEM") != 0)
git_config__find_system(&buf);

if (git_str_len(&buf))
error = git_config_add_file_ondisk(config, buf.ptr,
GIT_CONFIG_LEVEL_SYSTEM, NULL, 0);
}
}
}

if (!error && !git_config__find_programdata(&buf))
error = git_config_add_file_ondisk(config, buf.ptr,
Expand Down
68 changes: 68 additions & 0 deletions tests/libgit2/config/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,71 @@ void test_config_global__open_programdata(void)
git_repository_free(repo);
cl_fixture_cleanup("./foo.git");
}

void test_config_global__respects_GIT_CONFIG_GLOBAL_env(void)
{
git_config *cfg;
int32_t value;
git_str path = GIT_STR_INIT;

cl_git_pass(git_futils_mkdir_r("custom", 0777));
cl_git_mkfile("custom/my-gitconfig", "[envtest]\n value = 12345\n");
cl_git_pass(git_fs_path_prettify(&path, "custom/my-gitconfig", NULL));

cl_setenv("GIT_CONFIG_GLOBAL", path.ptr);

cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_int32(&value, cfg, "envtest.value"));
cl_assert_equal_i(12345, value);

git_config_free(cfg);
cl_setenv("GIT_CONFIG_GLOBAL", NULL);
git_str_dispose(&path);
cl_git_pass(git_futils_rmdir_r("custom", NULL, GIT_RMDIR_REMOVE_FILES));
}

void test_config_global__respects_GIT_CONFIG_SYSTEM_env(void)
{
git_config *cfg;
int32_t value;
git_str path = GIT_STR_INIT;

cl_git_pass(git_futils_mkdir_r("customsys", 0777));
cl_git_mkfile("customsys/my-sysconfig", "[systest]\n value = 67890\n");
cl_git_pass(git_fs_path_prettify(&path, "customsys/my-sysconfig", NULL));

cl_setenv("GIT_CONFIG_SYSTEM", path.ptr);

cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_int32(&value, cfg, "systest.value"));
cl_assert_equal_i(67890, value);

git_config_free(cfg);
cl_setenv("GIT_CONFIG_SYSTEM", NULL);
git_str_dispose(&path);
cl_git_pass(git_futils_rmdir_r("customsys", NULL, GIT_RMDIR_REMOVE_FILES));
}

void test_config_global__respects_GIT_CONFIG_NOSYSTEM_env(void)
{
git_config *cfg;
git_str path = GIT_STR_INIT;
git_config_entry *entry;
int error;

cl_git_pass(git_futils_mkdir_r("customsys", 0777));
cl_git_mkfile("customsys/my-sysconfig", "[nosystest]\n value = 11111\n");
cl_git_pass(git_fs_path_prettify(&path, "customsys/my-sysconfig", NULL));
cl_setenv("GIT_CONFIG_SYSTEM", path.ptr);
cl_setenv("GIT_CONFIG_NOSYSTEM", "true");

cl_git_pass(git_config_open_default(&cfg));
error = git_config_get_entry(&entry, cfg, "nosystest.value");
cl_assert_equal_i(GIT_ENOTFOUND, error);

git_config_free(cfg);
cl_setenv("GIT_CONFIG_NOSYSTEM", NULL);
cl_setenv("GIT_CONFIG_SYSTEM", NULL);
git_str_dispose(&path);
cl_git_pass(git_futils_rmdir_r("customsys", NULL, GIT_RMDIR_REMOVE_FILES));
}