git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jens Lehmann <Jens.Lehmann@web.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jonathan Johnson <me@jondavidjohn.com>,
	Git Mailing List <git@vger.kernel.org>,
	Heiko Voigt <hvoigt@hvoigt.net>
Subject: [PATCH v2 2/2] submodule add: Fail when .git/modules/<name> already exists unless forced
Date: Sun, 30 Sep 2012 23:01:29 +0200	[thread overview]
Message-ID: <5068B329.7040302@web.de> (raw)
In-Reply-To: <50689B5F.3060308@web.de>

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>
---

Am 30.09.2012 21:19, schrieb Jens Lehmann:
> Am 30.09.2012 06:47, schrieb Junio C Hamano:
>> I think failing with a better error message is a good idea. It
>> should suggest to either resurrect the submodule that is stashed
>> away in "$GIT_DIR/modules/$name" if it indeed is the same, or to
>> give it a different name (perhaps "kernel" used to be pointing at
>> the Linux kernel history, then the user is replacing it with a
>> totally different implementation that is really from different
>> origin and do not share any history, perhaps BSD).  In such a case,
>> the user may want to pick bsd-kernel or something as its name, to
>> differentiate it.
> 
> Good point! I will add a more detailed error message (including
> the url of the default remote which is configured for the already
> present submodule repo) and teach --force to skip the test and
> resurrect that submodule repo.

The message when "git submodule add" finds .git/modules/<name> is:

A git directory for '<name>' is found locally with remote(s):
  origin	<url(s) from .git/modules/<name>>
If you want to reuse this local git directory instead of cloning again from
  <url given on command line>
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.

When run with the --force option the following message is printed:

Reactivating local git directory for submodule '<name>'.


 git-submodule.sh            | 15 ++++++++++++++-
 t/t7400-submodule-basic.sh  | 30 ++++++++++++++++++++++++++++++
 t/t7406-submodule-update.sh |  2 +-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index 22febb1..e8112c8 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -359,7 +359,20 @@ Use -f if you really want to add it." >&2
 		fi

 	else
-
+		if test -d ".git/modules/$sm_name"
+		then
+			if test -z "$force"
+			then
+				echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
+				GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^,"  ", -e s,' (fetch)',, >&2
+				echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
+				echo >&2 "  $realrepo"
+				echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
+				die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
+			else
+				echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
+			fi
+		fi
 		module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
 		(
 			clear_local_git_env
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 78bf739..f1a94f7 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -726,4 +726,34 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
 	)
 '

+test_expect_success 'submodule add with an existing name fails unless forced' '
+	(
+		cd addtest2 &&
+		rm -rf repo &&
+		git rm repo &&
+		test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
+		test ! -d repo &&
+		echo "repo" >expect &&
+		git config -f .gitmodules submodule.repo_new.path >actual &&
+		test_cmp expect actual&&
+		echo "$submodurl/bare.git" >expect &&
+		git config -f .gitmodules submodule.repo_new.url >actual &&
+		test_cmp expect actual &&
+		echo "$submodurl/bare.git" >expect &&
+		git config submodule.repo_new.url >actual &&
+		test_cmp expect actual &&
+		git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
+		test -d repo &&
+		echo "repo" >expect &&
+		git config -f .gitmodules submodule.repo_new.path >actual &&
+		test_cmp expect actual&&
+		echo "$submodurl/repo.git" >expect &&
+		git config -f .gitmodules submodule.repo_new.url >actual &&
+		test_cmp expect actual &&
+		echo "$submodurl/repo.git" >expect &&
+		git config submodule.repo_new.url >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 1542653..feaec6c 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'
 	(cd super &&
 	 git reset --hard master &&
 	 rm -rf deeper/ &&
-	 git submodule add ../submodule deeper/submodule
+	 git submodule add --force ../submodule deeper/submodule
 	)
 '

-- 
1.7.12.1.430.gd057107

  reply	other threads:[~2012-09-30 21:01 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-26  4:18 Bug in Submodule add Jonathan Johnson
2012-09-26 20:56 ` Jens Lehmann
2012-09-29 23:04   ` [PATCH 0/2] Let "git submodule add" fail when .git/modules/<name> already exists Jens Lehmann
2012-09-29 23:05     ` [PATCH 1/2] Teach "git submodule add" the --name option Jens Lehmann
2012-09-29 23:07     ` [PATCH 2/2] submodule add: Fail when .git/modules/<name> already exists Jens Lehmann
2012-09-30  4:47     ` [PATCH 0/2] Let "git submodule add" fail " Junio C Hamano
2012-09-30 19:19       ` Jens Lehmann
2012-09-30 21:01         ` Jens Lehmann [this message]
2012-10-01  0:06           ` [PATCH v2 2/2] submodule add: Fail when .git/modules/<name> already exists unless forced Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5068B329.7040302@web.de \
    --to=jens.lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=hvoigt@hvoigt.net \
    --cc=me@jondavidjohn.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).