Web lists-archives.com

[PATCH 1/4] 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 any commits, we don't get a useful
result: there is no subproject OID to track, and any untracked files
in the sub-repository are added to the current repository.

Detect if the path is a repository with no commits and abort to avoid
getting into this state unless --force is used.  Reacting to --force
isn't very useful, especially because an upcoming commit will make
'git add' fail in this situation, but it allows us to use the same
'git add --dry-run' condition as the ignored path case.

Signed-off-by: Kyle Meyer <kyle@xxxxxxxxxx>
---
 git-submodule.sh           | 12 ++++++++++--
 t/t7400-submodule-basic.sh | 11 ++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index 514ede2596..6c74656027 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -234,10 +234,18 @@ cmd_add()
 	if test -z "$force" &&
 		! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
 	then
-		eval_gettextln "The following path is ignored by one of your .gitignore files:
+		if test -d "$sm_path" &&
+			test -z $(git -C "$sm_path" rev-parse --show-cdup 2>/dev/null) &&
+			! git -C "$sm_path" rev-parse --verify -q HEAD >/dev/null
+		then
+			die "$(eval_gettext "'\$sm_path' does not have any commits")"
+		else
+			eval_gettextln "\
+The following path is ignored by one of your .gitignore files:
 \$sm_path
 Use -f if you really want to add it." >&2
-		exit 1
+			exit 1
+		fi
 	fi
 
 	if test -n "$custom_name"
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index aba2d4d6ee..6adf640143 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -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 >expected <<-\EOF &&
+	'"'repo-no-commits'"' does not have any commits
+	EOF
+	git init repo-no-commits &&
+	test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
+	test_i18ncmp expected 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 &&
-- 
2.21.0