Merge pull request #4898 from pks-t/pks/config-parent-is-file config: fix adding files if their parent directory is a file
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
diff --git a/src/config.c b/src/config.c
index 042805e..569b69f 100644
--- a/src/config.c
+++ b/src/config.c
@@ -109,7 +109,7 @@ int git_config_add_file_ondisk(
assert(cfg && path);
res = p_stat(path, &st);
- if (res < 0 && errno != ENOENT) {
+ if (res < 0 && errno != ENOENT && errno != ENOTDIR) {
giterr_set(GITERR_CONFIG, "failed to stat '%s'", path);
return -1;
}
diff --git a/tests/config/read.c b/tests/config/read.c
index c770d68..ccc479b 100644
--- a/tests/config/read.c
+++ b/tests/config/read.c
@@ -534,6 +534,26 @@ void test_config_read__fallback_from_local_to_global_and_from_global_to_system(v
git_config_free(cfg);
}
+void test_config_read__parent_dir_is_file(void)
+{
+ git_config *cfg;
+ int count;
+
+ cl_git_pass(git_config_new(&cfg));
+ /*
+ * Verify we can add non-existing files when the parent directory is not
+ * a directory.
+ */
+ cl_git_pass(git_config_add_file_ondisk(cfg, "/dev/null/.gitconfig",
+ GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
+
+ count = 0;
+ cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
+ cl_assert_equal_i(0, count);
+
+ git_config_free(cfg);
+}
+
/*
* At the beginning of the test, config18 has:
* int32global = 28