Commit 727ae380a5de63e7b5856abb1f4dd191e2d5b678

John Fultz 2014-11-01T11:21:45

Make config reading continue after hitting a missing include file. For example, if you have [include] path = foo and foo didn't exist, git_config_open_ondisk() would just give up on the rest of the file. Now it ignores the unresolved include without error and continues reading the rest of the file.

diff --git a/src/config_file.c b/src/config_file.c
index 093e74a..195ad90 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1269,7 +1269,7 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
 				if ((result = git_path_dirname_r(&path, reader->file_path)) < 0)
 					break;
 
-				/* We need to know out index in the array, as the next config_parse call may realloc */
+				/* We need to know our index in the array, as the next config_parse call may realloc */
 				index = git_array_size(cfg_file->readers) - 1;
 				dir = git_buf_detach(&path);
 				result = included_path(&path, dir, var->entry->value);
@@ -1280,12 +1280,11 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
 
 				r->file_path = git_buf_detach(&path);
 				git_buf_init(&r->buffer, 0);
-				if ((result = git_futils_readbuffer_updated(&r->buffer, r->file_path, &r->file_mtime,
-									    &r->file_size, NULL)) < 0)
-					break;
-
-				result = config_parse(values, cfg_file, r, level, depth+1);
-				r = git_array_get(cfg_file->readers, index);
+				if (git_futils_readbuffer_updated(&r->buffer, r->file_path, &r->file_mtime,
+									    &r->file_size, NULL) == 0) {
+					result = config_parse(values, cfg_file, r, level, depth+1);
+					r = git_array_get(cfg_file->readers, index);
+				}
 				git_buf_free(&r->buffer);
 
 				if (result < 0)
diff --git a/tests/config/include.c b/tests/config/include.c
index 167814e..b98252d 100644
--- a/tests/config/include.c
+++ b/tests/config/include.c
@@ -86,3 +86,18 @@ void test_config_include__depth(void)
 	unlink("a");
 	unlink("b");
 }
+
+void test_config_include__missing(void)
+{
+	git_config *cfg;
+	const char *str;
+
+	cl_git_mkfile("included", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
+
+	cl_git_pass(git_config_open_ondisk(&cfg, "included"));
+	cl_git_pass(git_config_get_string(&str, cfg, "foo.bar"));
+	cl_assert_equal_s(str, "baz");
+
+	git_config_free(cfg);
+	unlink("included");
+}