Skip to content

Commit e55b537

Browse files
committed
Submodule API should report .gitmodules parse errors
Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 45f5840 commit e55b537

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);
@@ -509,8 +509,11 @@ int git_submodule__map(git_repository *repo, git_strmap *map)
509509
data.map = map;
510510
data.repo = repo;
511511

512-
if ((mods = gitmodules_snapshot(repo)) == NULL)
512+
if ((error = gitmodules_snapshot(&mods, repo)) < 0) {
513+
if (error == GIT_ENOTFOUND)
514+
error = 0;
513515
goto cleanup;
516+
}
514517

515518
data.mods = mods;
516519
if ((error = git_config_foreach(
@@ -1511,7 +1514,8 @@ int git_submodule_reload(git_submodule *sm, int force)
15111514

15121515
if (!git_repository_is_bare(sm->repo)) {
15131516
/* refresh config data */
1514-
mods = gitmodules_snapshot(sm->repo);
1517+
if ((error = gitmodules_snapshot(&mods, sm->repo)) < 0 && error != GIT_ENOTFOUND)
1518+
return error;
15151519
if (mods != NULL) {
15161520
error = submodule_read_config(sm, mods);
15171521
git_config_free(mods);
@@ -1915,32 +1919,37 @@ static int submodule_load_from_wd_lite(git_submodule *sm)
19151919
}
19161920

19171921
/**
1918-
* Returns a snapshot of $WORK_TREE/.gitmodules.
1922+
* Requests a snapshot of $WORK_TREE/.gitmodules.
19191923
*
1920-
* We ignore any errors and just pretend the file isn't there.
1924+
* Returns GIT_ENOTFOUND in case no .gitmodules file exist
19211925
*/
1922-
static git_config *gitmodules_snapshot(git_repository *repo)
1926+
static int gitmodules_snapshot(git_config **snap, git_repository *repo)
19231927
{
19241928
const char *workdir = git_repository_workdir(repo);
1925-
git_config *mods = NULL, *snap = NULL;
1929+
git_config *mods = NULL;
19261930
git_buf path = GIT_BUF_INIT;
1931+
int error;
19271932

1928-
if (workdir != NULL) {
1929-
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0)
1930-
return NULL;
1933+
if (!workdir)
1934+
return GIT_ENOTFOUND;
19311935

1932-
if (git_config_open_ondisk(&mods, path.ptr) < 0)
1933-
mods = NULL;
1934-
}
1936+
if ((error = git_buf_joinpath(&path, workdir, GIT_MODULES_FILE)) < 0)
1937+
return error;
19351938

1936-
git_buf_free(&path);
1939+
if ((error = git_config_open_ondisk(&mods, path.ptr)) < 0)
1940+
goto cleanup;
1941+
1942+
if ((error = git_config_snapshot(snap, mods)) < 0)
1943+
goto cleanup;
1944+
1945+
error = 0;
19371946

1938-
if (mods) {
1939-
git_config_snapshot(&snap, mods);
1947+
cleanup:
1948+
if (mods)
19401949
git_config_free(mods);
1941-
}
1950+
git_buf_free(&path);
19421951

1943-
return snap;
1952+
return error;
19441953
}
19451954

19461955
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)