mirror of
https://github.com/libgit2/libgit2.git
synced 2026-06-22 06:26:26 +00:00
submodule: refuse lookup in bare repositories
While it is technically possible to look up submodules inside of a bare repository by reading the submodule configuration of a specific commit, we do not offer this functionality right now. As such, calling both `git_submodule_lookup` and `git_submodule_foreach` should error out early when these functions encounter a bare repository. While `git_submodule_lookup` already does return an error due to not being able to parse the configuration, `git_submodule_foreach` simply returns success and never invokes the callback function. Fix the issue by having both functions check whether the repository is bare and returning an error in that case.
This commit is contained in:
@@ -209,6 +209,11 @@ int git_submodule_lookup(
|
||||
|
||||
assert(repo && name);
|
||||
|
||||
if (repo->is_bare) {
|
||||
giterr_set(GITERR_SUBMODULE, "cannot get submodules without a working tree");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (repo->submodule_cache != NULL) {
|
||||
khiter_t pos = git_strmap_lookup_index(repo->submodule_cache, name);
|
||||
if (git_strmap_valid_index(repo->submodule_cache, pos)) {
|
||||
@@ -549,6 +554,11 @@ int git_submodule_foreach(
|
||||
int error;
|
||||
size_t i;
|
||||
|
||||
if (repo->is_bare) {
|
||||
giterr_set(GITERR_SUBMODULE, "cannot get submodules without a working tree");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((error = git_strmap_alloc(&submodules)) < 0)
|
||||
return error;
|
||||
|
||||
|
||||
1
tests/resources/submodules.git/HEAD
Normal file
1
tests/resources/submodules.git/HEAD
Normal file
@@ -0,0 +1 @@
|
||||
ref: refs/heads/master
|
||||
4
tests/resources/submodules.git/config
Normal file
4
tests/resources/submodules.git/config
Normal file
@@ -0,0 +1,4 @@
|
||||
[core]
|
||||
repositoryformatversion = 0
|
||||
filemode = true
|
||||
bare = true
|
||||
@@ -0,0 +1,2 @@
|
||||
x%‰=
|
||||
€0F]í)Š0à"ÃIŒ*•|Éý-t{?œ2ÇilV8¿ùô$±«Øm¡ýv»ãkk*FDAÊ=(=|=6<> ¬DAv=ÛÍA}™&'…Oò$=
|
||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
xќЋ[
|
||||
В0EэО*fК¤S“ €€KР¤йЌъђ4ЭїwаЧ…Г9pУ2MCҐFфP@ќгЬu.„.¶pЪ!ІOYбѓdiYUН'М•8XпbPnјфК6
|
||||
Д§Фћ“¶1[qоМ}0q«пҐРc[WЊ#Э1fєДR:ац›SZ¦+Y‘Ж+{µtdПlvє¬»юOmћЁu<D081>_ґ}и5Фiй·«щ` Kж
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
tests/resources/submodules.git/objects/info/packs
Normal file
2
tests/resources/submodules.git/objects/info/packs
Normal file
@@ -0,0 +1,2 @@
|
||||
P pack-b69d04bb39ac274669e2184e45bd90015d02ef5b.pack
|
||||
|
||||
Binary file not shown.
Binary file not shown.
1
tests/resources/submodules.git/refs/heads/master
Normal file
1
tests/resources/submodules.git/refs/heads/master
Normal file
@@ -0,0 +1 @@
|
||||
97896810b3210244a62a82458b8e0819ecfc6850
|
||||
@@ -419,3 +419,29 @@ void test_submodule_lookup__cached(void)
|
||||
git_submodule_free(sm);
|
||||
git_submodule_free(sm2);
|
||||
}
|
||||
|
||||
void test_submodule_lookup__lookup_in_bare_repository_fails(void)
|
||||
{
|
||||
git_submodule *sm;
|
||||
|
||||
cl_git_sandbox_cleanup();
|
||||
g_repo = cl_git_sandbox_init("submodules.git");
|
||||
|
||||
cl_git_fail(git_submodule_lookup(&sm, g_repo, "nonexisting"));
|
||||
}
|
||||
|
||||
static int foreach_cb(git_submodule *sm, const char *name, void *payload)
|
||||
{
|
||||
GIT_UNUSED(sm);
|
||||
GIT_UNUSED(name);
|
||||
GIT_UNUSED(payload);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void test_submodule_lookup__foreach_in_bare_repository_fails(void)
|
||||
{
|
||||
cl_git_sandbox_cleanup();
|
||||
g_repo = cl_git_sandbox_init("submodules.git");
|
||||
|
||||
cl_git_fail(git_submodule_foreach(g_repo, foreach_cb, NULL));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user