submodule: refuse to add repository with no commits
authorKyle Meyer <kyle@kyleam.com>
Tue, 9 Apr 2019 23:07:35 +0000 (19:07 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Apr 2019 03:52:48 +0000 (12:52 +0900)
When the path given to 'git submodule add' is an existing repository
that is not in the index, the repository is passed to 'git add'. If
this repository doesn't have a commit checked out, we don't get a
useful result: there is no subproject OID to track, and any untracked
files in the sub-repository are added as blobs in the top-level
repository.

To avoid getting into this state, abort if the path is a repository
that doesn't have a commit checked out. Note that this check must
come before the 'git add --dry-run' check because the next commit will
make 'git add' fail when given a repository that doesn't have a commit
checked out.

Signed-off-by: Kyle Meyer <kyle@kyleam.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-submodule.sh
t/t7400-submodule-basic.sh
index b5f2beee60ab59a04b5b236da8df39403009db1b..16b49f1419f8431725edd841893753c4fddeb78d 100755 (executable)
@@ -230,6 +230,13 @@ cmd_add()
                die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
        fi
 
+       if test -d "$sm_path" &&
+               test -z $(git -C "$sm_path" rev-parse --show-cdup 2>/dev/null)
+       then
+           git -C "$sm_path" rev-parse --verify -q HEAD >/dev/null ||
+           die "$(eval_gettext "'\$sm_path' does not have a commit checked out")"
+       fi
+
        if test -z "$force" &&
                ! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
        then
index aba2d4d6ee415ab5528ed67d518cfb26412f1142..a208cb26e1dfd4b1a147f620747c025600ce7181 100755 (executable)
@@ -46,6 +46,15 @@ test_expect_success 'submodule update aborts on missing gitmodules url' '
        test_must_fail git submodule init
 '
 
+test_expect_success 'add aborts on repository with no commits' '
+       cat >expect <<-\EOF &&
+       '"'repo-no-commits'"' does not have a commit checked out
+       EOF
+       git init repo-no-commits &&
+       test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
+       test_i18ncmp expect actual
+'
+
 test_expect_success 'setup - repository in init subdirectory' '
        mkdir init &&
        (
@@ -809,7 +818,7 @@ test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.gi
                cp pristine-.git-config .git/config &&
                cp pristine-.gitmodules .gitmodules &&
                mkdir -p a/b/c &&
-               (cd a/b/c && git init) &&
+               (cd a/b/c && git init && test_commit msg) &&
                git config remote.origin.url ../foo/bar.git &&
                git submodule add ../bar/a/b/c ./a/b/c &&
                git submodule init &&