submodule init: fail gracefully with a missing .gitmodules file
PATCH 1/1 submodule init: fail gracefully with a missing .gitmodules file
When there is no .gitmodules file availabe to initialize a submodule from, submodule_from_path just returns NULL. We need to check for that and abort gracefully. When submodule init was implemented in shell, a missing .gitmodules file would result in an error message
No url found for submodule path '%s' in .gitmodules
While that is technically true, I think we can broaden the error message and just say there is no section for the submodule in the .gitmodules file.
---
This goes on top of sb/submodule-init (I added the base-commit below, just in case anyone uses that feature already. Though I did that manually :)
Thanks,
Stefan
builtin/submodule--helper.c | 10 +++++++--- t/t7400-submodule-basic.sh | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-)
builtin/submodule--helper.c +7 -3
@@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)/* Only loads from .gitmodules, no overlay with .git/config */ gitmodules_config (); - sub =submodule_from_path (null_sha1, path); -if (prefix) {strbuf_addf (&sb,"%s%s" , prefix, path); displaypath =strbuf_detach (&sb,NULL ); }else - displaypath =xstrdup (sub->path); + displaypath =xstrdup (path); + + sub =submodule_from_path (null_sha1, path); + +if (!sub) +die (_ ("No section found for submodule path '%s' in .gitmodules" ), + displaypath);/* * Copy url setting when it is not set yet.
t/t7400-submodule-basic.sh +8 -0
@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' ' git branch initial ' +test_expect_success 'sane abort on missing .gitmodules file' ' + test_when_finished "git update-index --remove sub" && + git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && + # missing the .gitmodules file here + test_must_fail git submodule init 2>actual && + test_i18ngrep "No section found for submodule path" actual +' + test_expect_success 'configuration parsing' ' test_when_finished "rm -f .gitmodules" && cat >.gitmodules <<-\EOF &&
Signature · 3 lines
2.8.0.26.g3604242.dirty base-commit: 3604242f080a813d6f20a7394def422d1e55b30e
Quoted 9 lines · Stefan Beller
When there is no .gitmodules file availabe to initialize a submodule from,
submodule_from_pathjust returns NULL. We need to check for that and abort gracefully. Whensubmodule initwas implemented in shell, a missing .gitmodules file would result in an error messageNo url found for submodule path '%s' in .gitmodules
While that is technically true, I think we can broaden the error message and just say there is no section for the submodule in the .gitmodules file.
Actually I am not so sure any more about this part. In a resend I will use the old error message again.
This fix is also required for submodule--helper update-clone.
Quoted 63 lines
Signed-off-by Stefan Beller <[email protected]>---
This goes on top of sb/submodule-init (I added the base-commit below, just in case anyone uses that feature already. Though I did that manually :)
Thanks,
Stefanbuiltin/submodule--helper.c | 10 +++++++--- t/t7400-submodule-basic.sh | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-)builtin/submodule--helper.c +7
-3@@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)/* Only loads from .gitmodules, no overlay with .git/config */ gitmodules_config (); - sub =submodule_from_path (null_sha1, path); -if (prefix) {strbuf_addf (&sb,"%s%s" , prefix, path); displaypath =strbuf_detach (&sb,NULL ); }else - displaypath =xstrdup (sub->path); + displaypath =xstrdup (path); + + sub =submodule_from_path (null_sha1, path); + +if (!sub) +die (_ ("No section found for submodule path '%s' in .gitmodules" ), + displaypath);/* * Copy url setting when it is not set yet. t/t7400-submodule-basic.sh +8
-1@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' ' git branch initial ' +test_expect_success 'sane abort on missing .gitmodules file' ' + test_when_finished "git update-index --remove sub" && + git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && + # missing the .gitmodules file here + test_must_fail git submodule init 2>actual && + test_i18ngrep "No section found for submodule path" actual +' + test_expect_success 'configuration parsing' ' test_when_finished "rm -f .gitmodules" && cat >.gitmodules <<-\EOF && --2.8.0.26.g3604242.dirty
base-commit:
3604242f080a813d6f20a7394def422d1e55b30e