Skip to content

Commit e138111

Browse files
kyleamgitster
authored andcommitted
submodule: refuse to add repository with no commits
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>
1 parent aeb582a commit e138111

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

git-submodule.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ cmd_add()
230230
die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
231231
fi
232232

233+
if test -d "$sm_path" &&
234+
test -z $(git -C "$sm_path" rev-parse --show-cdup 2>/dev/null)
235+
then
236+
git -C "$sm_path" rev-parse --verify -q HEAD >/dev/null ||
237+
die "$(eval_gettext "'\$sm_path' does not have a commit checked out")"
238+
fi
239+
233240
if test -z "$force" &&
234241
! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
235242
then

t/t7400-submodule-basic.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ test_expect_success 'submodule update aborts on missing gitmodules url' '
4646
test_must_fail git submodule init
4747
'
4848

49+
test_expect_success 'add aborts on repository with no commits' '
50+
cat >expect <<-\EOF &&
51+
'"'repo-no-commits'"' does not have a commit checked out
52+
EOF
53+
git init repo-no-commits &&
54+
test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
55+
test_i18ncmp expect actual
56+
'
57+
4958
test_expect_success 'setup - repository in init subdirectory' '
5059
mkdir init &&
5160
(
@@ -809,7 +818,7 @@ test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.gi
809818
cp pristine-.git-config .git/config &&
810819
cp pristine-.gitmodules .gitmodules &&
811820
mkdir -p a/b/c &&
812-
(cd a/b/c && git init) &&
821+
(cd a/b/c && git init && test_commit msg) &&
813822
git config remote.origin.url ../foo/bar.git &&
814823
git submodule add ../bar/a/b/c ./a/b/c &&
815824
git submodule init &&

0 commit comments

Comments
 (0)