{"thread":{"id":"42171","subject":"[PATCH] submodule init: fail gracefully with a missing .gitmodules file","startedAt":"2016-04-28T18:28:39Z","lastAt":"2016-04-28T19:12:56Z","messageCount":2,"participants":["Stefan Beller"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"284855","messageId":"1461868119-9146-1-git-send-email-sbeller@google.com","threadId":"42171","inReplyTo":null,"subject":"[PATCH] submodule init: fail gracefully with a missing .gitmodules file","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-28T18:28:39Z","receivedAt":"2016-04-28T18:28:39Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"When there is no .gitmodules file availabe to initialize a submodule\nfrom, `submodule_from_path` just returns NULL. We need to check for\nthat and abort gracefully. When `submodule init` was implemented in shell,\na missing .gitmodules file would result in an error message\n\n    No url found for submodule path '%s' in .gitmodules\n\nWhile that is technically true, I think we can broaden the error message\nand just say there is no section for the submodule in the .gitmodules file.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n\n This goes on top of sb/submodule-init (I added the base-commit\n below, just in case anyone uses that feature already. Though I did\n that manually :)\n \n Thanks,\n Stefan\n\n builtin/submodule--helper.c | 10 +++++++---\n t/t7400-submodule-basic.sh  |  8 ++++++++\n 2 files changed, 15 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex b6d4f27..af5406e 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)\n \t/* Only loads from .gitmodules, no overlay with .git/config */\n \tgitmodules_config();\n \n-\tsub = submodule_from_path(null_sha1, path);\n-\n \tif (prefix) {\n \t\tstrbuf_addf(&sb, \"%s%s\", prefix, path);\n \t\tdisplaypath = strbuf_detach(&sb, NULL);\n \t} else\n-\t\tdisplaypath = xstrdup(sub->path);\n+\t\tdisplaypath = xstrdup(path);\n+\n+\tsub = submodule_from_path(null_sha1, path);\n+\n+\tif (!sub)\n+\t\tdie(_(\"No section found for submodule path '%s' in .gitmodules\"),\n+\t\t\tdisplaypath);\n \n \t/*\n \t * Copy url setting when it is not set yet.\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex f99f674..f2b3519 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' '\n \tgit branch initial\n '\n \n+test_expect_success 'sane abort on missing .gitmodules file' '\n+\ttest_when_finished \"git update-index --remove sub\" &&\n+\tgit update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&\n+\t# missing the .gitmodules file here\n+\ttest_must_fail git submodule init 2>actual &&\n+\ttest_i18ngrep \"No section found for submodule path\" actual\n+'\n+\n test_expect_success 'configuration parsing' '\n \ttest_when_finished \"rm -f .gitmodules\" &&\n \tcat >.gitmodules <<-\\EOF &&\n-- \n2.8.0.26.g3604242.dirty\n\nbase-commit: 3604242f080a813d6f20a7394def422d1e55b30e\n"},{"id":"284859","messageId":"CAGZ79kairv4jXsaRfqj_TA9ejqOyinOVwrj+-ATMTraaiQsvBA@mail.gmail.com","threadId":"42171","inReplyTo":"1461868119-9146-1-git-send-email-sbeller@google.com","subject":"Re: [PATCH] submodule init: fail gracefully with a missing .gitmodules file","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-28T19:12:56Z","receivedAt":"2016-04-28T19:12:56Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Thu, Apr 28, 2016 at 11:28 AM, Stefan Beller <sbeller@google.com> wrote:\n> When there is no .gitmodules file availabe to initialize a submodule\n> from, `submodule_from_path` just returns NULL. We need to check for\n> that and abort gracefully. When `submodule init` was implemented in shell,\n> a missing .gitmodules file would result in an error message\n>\n>     No url found for submodule path '%s' in .gitmodules\n>\n> While that is technically true, I think we can broaden the error message\n> and just say there is no section for the submodule in the .gitmodules file.\n\nActually I am not so sure any more about this part. In a resend I will use\nthe old error message again.\n\nThis fix is also required for `submodule--helper update-clone`.\n\n>\n> Signed-off-by: Stefan Beller <sbeller@google.com>\n> ---\n>\n>  This goes on top of sb/submodule-init (I added the base-commit\n>  below, just in case anyone uses that feature already. Though I did\n>  that manually :)\n>\n>  Thanks,\n>  Stefan\n>\n>  builtin/submodule--helper.c | 10 +++++++---\n>  t/t7400-submodule-basic.sh  |  8 ++++++++\n>  2 files changed, 15 insertions(+), 3 deletions(-)\n>\n> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\n> index b6d4f27..af5406e 100644\n> --- a/builtin/submodule--helper.c\n> +++ b/builtin/submodule--helper.c\n> @@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)\n>         /* Only loads from .gitmodules, no overlay with .git/config */\n>         gitmodules_config();\n>\n> -       sub = submodule_from_path(null_sha1, path);\n> -\n>         if (prefix) {\n>                 strbuf_addf(&sb, \"%s%s\", prefix, path);\n>                 displaypath = strbuf_detach(&sb, NULL);\n>         } else\n> -               displaypath = xstrdup(sub->path);\n> +               displaypath = xstrdup(path);\n> +\n> +       sub = submodule_from_path(null_sha1, path);\n> +\n> +       if (!sub)\n> +               die(_(\"No section found for submodule path '%s' in .gitmodules\"),\n> +                       displaypath);\n>\n>         /*\n>          * Copy url setting when it is not set yet.\n> diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\n> index f99f674..f2b3519 100755\n> --- a/t/t7400-submodule-basic.sh\n> +++ b/t/t7400-submodule-basic.sh\n> @@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' '\n>         git branch initial\n>  '\n>\n> +test_expect_success 'sane abort on missing .gitmodules file' '\n> +       test_when_finished \"git update-index --remove sub\" &&\n> +       git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&\n> +       # missing the .gitmodules file here\n> +       test_must_fail git submodule init 2>actual &&\n> +       test_i18ngrep \"No section found for submodule path\" actual\n> +'\n> +\n>  test_expect_success 'configuration parsing' '\n>         test_when_finished \"rm -f .gitmodules\" &&\n>         cat >.gitmodules <<-\\EOF &&\n> --\n> 2.8.0.26.g3604242.dirty\n>\n> base-commit: 3604242f080a813d6f20a7394def422d1e55b30e\n"}]}