Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de0c455

Browse files
committed
Merge pull request libgit2#2679 from jfultz/missing-include
Make config reading continue after hitting a missing include file.
2 parents 0a62918 + ebc13b2 commit de0c455

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/config_file.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
12691269
if ((result = git_path_dirname_r(&path, reader->file_path)) < 0)
12701270
break;
12711271

1272-
/* We need to know out index in the array, as the next config_parse call may realloc */
1272+
/* We need to know our index in the array, as the next config_parse call may realloc */
12731273
index = git_array_size(cfg_file->readers) - 1;
12741274
dir = git_buf_detach(&path);
12751275
result = included_path(&path, dir, var->entry->value);
@@ -1280,12 +1280,18 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
12801280

12811281
r->file_path = git_buf_detach(&path);
12821282
git_buf_init(&r->buffer, 0);
1283-
if ((result = git_futils_readbuffer_updated(&r->buffer, r->file_path, &r->file_mtime,
1284-
&r->file_size, NULL)) < 0)
1285-
break;
1283+
result = git_futils_readbuffer_updated(&r->buffer, r->file_path, &r->file_mtime,
1284+
&r->file_size, NULL);
1285+
1286+
if (result == 0) {
1287+
result = config_parse(values, cfg_file, r, level, depth+1);
1288+
r = git_array_get(cfg_file->readers, index);
1289+
}
1290+
else if (result == GIT_ENOTFOUND) {
1291+
giterr_clear();
1292+
result = 0;
1293+
}
12861294

1287-
result = config_parse(values, cfg_file, r, level, depth+1);
1288-
r = git_array_get(cfg_file->readers, index);
12891295
git_buf_free(&r->buffer);
12901296

12911297
if (result < 0)

tests/config/include.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ void test_config_include__depth(void)
8686
unlink("a");
8787
unlink("b");
8888
}
89+
90+
void test_config_include__missing(void)
91+
{
92+
git_config *cfg;
93+
const char *str;
94+
95+
cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
96+
97+
giterr_clear();
98+
cl_git_pass(git_config_open_ondisk(&cfg, "including"));
99+
cl_assert(giterr_last() == NULL);
100+
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar"));
101+
cl_assert_equal_s(str, "baz");
102+
103+
git_config_free(cfg);
104+
}

0 commit comments

Comments
 (0)