Skip to content

Commit 4b7c286

Browse files
jlehmanngitster
authored andcommitted
submodule add: Fail when .git/modules/<name> already exists unless forced
When adding a new submodule it can happen that .git/modules/<name> already contains a submodule repo, e.g. when a submodule is removed from the work tree and another submodule is added at the same path. But then the work tree of the submodule will be populated using the existing repository and not the one the user provided, which results in an incorrect work tree. On the other hand the user might reactivate a submodule removed earlier, then reusing that .git directory is the Right Thing to do. As git can't decide what is the case, error out and tell the user she should use either use a different name for the submodule with the "--name" option or can reuse the .git directory for the newly added submodule by providing the --force option (which only makes sense when the upstream matches, so the error message lists all remotes of .git/modules/<name>). In one test in t7406 the --force option had to be added to "git submodule add", as that test re-adds a formerly removed submodule. Reported-by: Jonathan Johnson <[email protected]> Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73b0898 commit 4b7c286

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

git-submodule.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,20 @@ Use -f if you really want to add it." >&2
359359
fi
360360

361361
else
362-
362+
if test -d ".git/modules/$sm_name"
363+
then
364+
if test -z "$force"
365+
then
366+
echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
367+
GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
368+
echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
369+
echo >&2 " $realrepo"
370+
echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
371+
die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
372+
else
373+
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
374+
fi
375+
fi
363376
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
364377
(
365378
clear_local_git_env

t/t7400-submodule-basic.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,34 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
726726
)
727727
'
728728

729+
test_expect_success 'submodule add with an existing name fails unless forced' '
730+
(
731+
cd addtest2 &&
732+
rm -rf repo &&
733+
git rm repo &&
734+
test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
735+
test ! -d repo &&
736+
echo "repo" >expect &&
737+
git config -f .gitmodules submodule.repo_new.path >actual &&
738+
test_cmp expect actual&&
739+
echo "$submodurl/bare.git" >expect &&
740+
git config -f .gitmodules submodule.repo_new.url >actual &&
741+
test_cmp expect actual &&
742+
echo "$submodurl/bare.git" >expect &&
743+
git config submodule.repo_new.url >actual &&
744+
test_cmp expect actual &&
745+
git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
746+
test -d repo &&
747+
echo "repo" >expect &&
748+
git config -f .gitmodules submodule.repo_new.path >actual &&
749+
test_cmp expect actual&&
750+
echo "$submodurl/repo.git" >expect &&
751+
git config -f .gitmodules submodule.repo_new.url >actual &&
752+
test_cmp expect actual &&
753+
echo "$submodurl/repo.git" >expect &&
754+
git config submodule.repo_new.url >actual &&
755+
test_cmp expect actual
756+
)
757+
'
758+
729759
test_done

t/t7406-submodule-update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'
627627
(cd super &&
628628
git reset --hard master &&
629629
rm -rf deeper/ &&
630-
git submodule add ../submodule deeper/submodule
630+
git submodule add --force ../submodule deeper/submodule
631631
)
632632
'
633633

0 commit comments

Comments
 (0)