Skip to content

Commit 286a676

Browse files
authored
Merge pull request libgit2#4522 from csware/submodules-should-report-parse-errors
Submodules-API should report .gitmodules parse errors instead of ignoring them
2 parents e5f32e8 + e55b537 commit 286a676

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/submodule.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ __KHASH_IMPL(
9191

9292
static int submodule_alloc(git_submodule **out, git_repository *repo, const char *name);
9393
static git_config_backend *open_gitmodules(git_repository *repo, int gitmod);
94-
static git_config *gitmodules_snapshot(git_repository *repo);
94+
static int gitmodules_snapshot(git_config **snap, git_repository *repo);
9595
static int get_url_base(git_buf *url, git_repository *repo);
9696
static int lookup_head_remote_key(git_buf *remote_key, git_repository *repo);
9797
static int lookup_default_remote(git_remote **remote, git_repository *repo);
@@ -554,8 +554,11 @@ int git_submodule__map(git_repository *repo, git_strmap *map)
554554
data.map = map;
555555
data.repo = repo;
556556

557-
if ((mods = gitmodules_snapshot(repo)) == NULL)
557+
if ((error = gitmodules_snapshot(&mods, repo)) < 0) {
558+
if (error == GIT_ENOTFOUND)
559+
error = 0;
558560
goto cleanup;
561+
}
559562

560563
data.mods = mods;
561564
if ((error = git_config_foreach(
@@ -1565,7 +1568,8 @@ int git_submodule_reload(git_submodule *sm, int force)
15651568

15661569
if (!git_repository_is_bare(sm->repo)) {
15671570
/* refresh config data */
1568-
mods = gitmodules_snapshot(sm->repo);
1571+
if ((error = gitmodules_snapshot(&mods, sm->repo)) < 0 && error != GIT_ENOTFOUND)
1572+
return error;
15691573
if (mods != NULL) {
15701574
error = submodule_read_config(sm, mods);
15711575
git_config_free(mods);
@@ -1969,32 +1973,37 @@ static int submodule_load_from_wd_lite(git_submodule *sm)
19691973
}
19701974

19711975
/**
1972-
* Returns a snapshot of $WORK_TREE/.gitmodules.
1976+
* Requests a snapshot of $WORK_TREE/.gitmodules.
19731977
*
1974-
* We ignore any errors and just pretend the file isn't there.
1978+
* Returns GIT_ENOTFOUND in case no .gitmodules file exist
19751979
*/
1976-
static git_config *gitmodules_snapshot(git_repository *repo)
1980+
static int gitmodules_snapshot(git_config **snap, git_repository *repo)
19771981
{
19781982
const char *workdir = git_repository_workdir(repo);
1979-
git_config *mods = NULL, *snap = NULL;
1983+
git_config *mods = NULL;
19801984
git_buf path = GIT_BUF_INIT;
1985+
int error;
19811986

1982-
if (workdir != NULL) {
1983-
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0)
1984-
return NULL;
1987+
if (!workdir)
1988+
return GIT_ENOTFOUND;
19851989

1986-
if (git_config_open_ondisk(&mods, path.ptr) < 0)
1987-
mods = NULL;
1988-
}
1990+
if ((error = git_buf_joinpath(&path, workdir, GIT_MODULES_FILE)) < 0)
1991+
return error;
19891992

1990-
git_buf_free(&path);
1993+
if ((error = git_config_open_ondisk(&mods, path.ptr)) < 0)
1994+
goto cleanup;
1995+
1996+
if ((error = git_config_snapshot(snap, mods)) < 0)
1997+
goto cleanup;
1998+
1999+
error = 0;
19912000

1992-
if (mods) {
1993-
git_config_snapshot(&snap, mods);
2001+
cleanup:
2002+
if (mods)
19942003
git_config_free(mods);
1995-
}
2004+
git_buf_free(&path);
19962005

1997-
return snap;
2006+
return error;
19982007
}
19992008

20002009
static git_config_backend *open_gitmodules(

tests/submodule/lookup.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,19 @@ void test_submodule_lookup__foreach_in_bare_repository_fails(void)
445445

446446
cl_git_fail(git_submodule_foreach(g_repo, foreach_cb, NULL));
447447
}
448+
449+
void test_submodule_lookup__fail_invalid_gitmodules(void)
450+
{
451+
git_submodule *sm;
452+
sm_lookup_data data;
453+
memset(&data, 0, sizeof(data));
454+
455+
cl_git_rewritefile("submod2/.gitmodules",
456+
"[submodule \"Test_App\"\n"
457+
" path = Test_App\n"
458+
" url = ../Test_App\n");
459+
460+
cl_git_fail(git_submodule_lookup(&sm, g_repo, "Test_App"));
461+
462+
cl_git_fail(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
463+
}

0 commit comments

Comments
 (0)