submodule init: fail gracefully with a missing .gitmodules file
authorStefan Beller <sbeller@google.com>
Thu, 28 Apr 2016 20:02:45 +0000 (13:02 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 29 Apr 2016 17:05:24 +0000 (10:05 -0700)
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>
builtin/submodule--helper.c
t/t7400-submodule-basic.sh
index b6d4f27648c8c7f1c4d01e1c5060675e13d8cca2..ce9d11e8d367176a6f846d0a004f22a1c8b74d93 100644 (file)
@@ -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();
 
        /* 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
        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 url found for submodule path '%s' in .gitmodules"),
+                       displaypath);
 
        /*
         * Copy url setting when it is not set yet.
 
        /*
         * Copy url setting when it is not set yet.
index f99f674ac795b7b55c5fc678257bd7365c71e62d..df6b4da2ba46f479c035f77adee88591c54e1407 100755 (executable)
@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' '
        git branch initial
 '
 
        git branch initial
 '
 
+test_expect_success 'submodule init aborts 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 url found for submodule path" actual
+'
+
 test_expect_success 'configuration parsing' '
        test_when_finished "rm -f .gitmodules" &&
        cat >.gitmodules <<-\EOF &&
 test_expect_success 'configuration parsing' '
        test_when_finished "rm -f .gitmodules" &&
        cat >.gitmodules <<-\EOF &&