Skip to content

Commit d92028a

Browse files
stefanbellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3604242 commit d92028a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

builtin/submodule--helper.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,17 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
314314
/* Only loads from .gitmodules, no overlay with .git/config */
315315
gitmodules_config();
316316

317-
sub = submodule_from_path(null_sha1, path);
318-
319317
if (prefix) {
320318
strbuf_addf(&sb, "%s%s", prefix, path);
321319
displaypath = strbuf_detach(&sb, NULL);
322320
} 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);
324328

325329
/*
326330
* Copy url setting when it is not set yet.

t/t7400-submodule-basic.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ test_expect_success 'setup - initial commit' '
1818
git branch initial
1919
'
2020

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+
2129
test_expect_success 'configuration parsing' '
2230
test_when_finished "rm -f .gitmodules" &&
2331
cat >.gitmodules <<-\EOF &&

0 commit comments

Comments
 (0)