Re: [PATCH 0/2] Let "git submodule add" fail when .git/modules/<name> already exists

3 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH 0/2] Let "git submodule add" fail when .git/modules/<name> already exists

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:54:54

Jens Lehmann [off-list ref] writes:
quoted
The only long term solution I can think of is to use some kind of UUID for
the name, so that the names of newly added submodules won't have a chance
to clash anymore. For the short term aborting "git submodule add" when a
submodule of that name already exists in .git/modules of the superproject
together with the ability to provide a custom name might at least solve
the local clashes.
That assumes that the addition of the submodule for the second time
is to add a completely different submodule at the same location and
is done on purpose, but is that a sensible assumption?

If a superproject that is about an embedded appliance used to have a
submodule A bound at its path "kernel", but for some reason stopped
shipping with "kernel" and then later reintroduced the directory
"kernel" bound to some submodule B, my gut feeling is that it is
just as likely (if not more likely) that A and B are indeed the same
submodule (i.e. it shares the same history) as they are totally
unrelated.

Could it be that it is a user error combined with the immaturity of
"git submodule" tool that does not yet support "it used to be here,
but it disappears for a while and then it reappears in the history
of the superproject" very well that caused the user to manually add
a "new" submodule which in fact is the same submodule at the same
path?

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.
Using some kind of UUID can easily be added in a subsequent patch,...
I would suggest thinking really long and hard before saying UUID.
It is an easy cop-out to ensure uniqueness, but risks to allow two
people (or one person at two different time) to give two unrelated
names to a single thing that actually is the same.

A better alternative might be to use the commit object name at the
root of the history of the submodule, which would catch the simplest
and most common case of the mistake, I would think.

Re: [PATCH 0/2] Let "git submodule add" fail when .git/modules/<name> already exists

From: Jens Lehmann <hidden>
Date: 2016-06-15 22:54:54

Am 30.09.2012 06:47, schrieb Junio C Hamano:
Jens Lehmann [off-list ref] writes:
quoted
quoted
The only long term solution I can think of is to use some kind of UUID for
the name, so that the names of newly added submodules won't have a chance
to clash anymore. For the short term aborting "git submodule add" when a
submodule of that name already exists in .git/modules of the superproject
together with the ability to provide a custom name might at least solve
the local clashes.
That assumes that the addition of the submodule for the second time
is to add a completely different submodule at the same location and
is done on purpose, but is that a sensible assumption?

If a superproject that is about an embedded appliance used to have a
submodule A bound at its path "kernel", but for some reason stopped
shipping with "kernel" and then later reintroduced the directory
"kernel" bound to some submodule B, my gut feeling is that it is
just as likely (if not more likely) that A and B are indeed the same
submodule (i.e. it shares the same history) as they are totally
unrelated.

Could it be that it is a user error combined with the immaturity of
"git submodule" tool that does not yet support "it used to be here,
but it disappears for a while and then it reappears in the history
of the superproject" very well that caused the user to manually add
a "new" submodule which in fact is the same submodule at the same
path?

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.
quoted
Using some kind of UUID can easily be added in a subsequent patch,...
I would suggest thinking really long and hard before saying UUID.
Absolutely.
It is an easy cop-out to ensure uniqueness, but risks to allow two
people (or one person at two different time) to give two unrelated
names to a single thing that actually is the same.
I'm not too worried about that (even though it would be good for
the disk footprint). And I couldn't come up with a better way to
solve the problem we currently have when the same name is used
for two different submodule repos.
A better alternative might be to use the commit object name at the
root of the history of the submodule, which would catch the simplest
and most common case of the mistake, I would think.
This won't work well e.g. when one uses a fork of another repo,
that will contain different commits while still having the same
root commit. I was also thinking about hashing the URL, but that
will break when the user reconfigures the URL to somewhere else.
After playing with some ideas I couldn't find a way to let the
submodule's repo provide sufficient uniqueness.

I'd say for now we go with the detection of name clashes and let
the user choose if he wants to resurrect that submodule repo or
if he wants to choose another name. But if we notice further down
the road that collisions are a problem in real life, we can think
again if UUIDs - or something else - might be a solution.

[PATCH v2 2/2] submodule add: Fail when .git/modules/<name> already exists unless forced

From: Jens Lehmann <hidden>
Date: 2016-06-15 22:54:54

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 <redacted>
Signed-off-by: Jens Lehmann <redacted>
---

Am 30.09.2012 21:19, schrieb Jens Lehmann:
Am 30.09.2012 06:47, schrieb Junio C Hamano:
quoted
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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help