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 <me@jondavidjohn.com> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lehmann committed Sep 30, 2012 at 23:01 UTC 4b7c286ec74c7bb88e9e7ac2d283d86f64b6b0ea
3 files changed +45 -2
git-submodule.sh
+14 -1
@@ -359,7 +359,20 @@ Use -f if you really want to add it." >&2
359 fi
360
361 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
376 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
377 (
378 clear_local_git_env
t/t7400-submodule-basic.sh
+30
@@ -726,4 +726,34 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
726 )
727 '
728
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 +
759 test_done
t/t7406-submodule-update.sh
+1 -1
@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'
627 (cd super &&
628 git reset --hard master &&
629 rm -rf deeper/ &&
630 - git submodule add ../submodule deeper/submodule
630 + git submodule add --force ../submodule deeper/submodule
631 )
632 '
633