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 Replicate that error message for now. When the .gitmodules file is missing we can probably fail even earlier for all of the submodules with an improved error message. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Apr 28, 2016 at 13:02 UTC d92028a575dde9c325e23f89c3d2b24f13868c57
2 files changed +15 -3
builtin/submodule--helper.c
+7 -3
@@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
314 /* Only loads from .gitmodules, no overlay with .git/config */
315 gitmodules_config();
316
317 - sub = submodule_from_path(null_sha1, path);
318 -
317 if (prefix) {
318 strbuf_addf(&sb, "%s%s", prefix, path);
319 displaypath = strbuf_detach(&sb, NULL);
320 } else
323 - displaypath = xstrdup(sub->path);
321 + displaypath = xstrdup(path);
322 +
323 + sub = submodule_from_path(null_sha1, path);
324 +
325 + if (!sub)
326 + die(_("No url found for submodule path '%s' in .gitmodules"),
327 + displaypath);
328
329 /*
330 * Copy url setting when it is not set yet.
t/t7400-submodule-basic.sh
+8
@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' '
18 git branch initial
19 '
20
21 +test_expect_success 'submodule init aborts on missing .gitmodules file' '
22 + test_when_finished "git update-index --remove sub" &&
23 + git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
24 + # missing the .gitmodules file here
25 + test_must_fail git submodule init 2>actual &&
26 + test_i18ngrep "No url found for submodule path" actual
27 +'
28 +
29 test_expect_success 'configuration parsing' '
30 test_when_finished "rm -f .gitmodules" &&
31 cat >.gitmodules <<-\EOF &&