{"thread":{"id":"31651","subject":"Bug in Submodule add","startedAt":"2012-09-26T04:18:05Z","lastAt":"2012-10-01T00:06:58Z","messageCount":9,"participants":["Jonathan Johnson","Jens Lehmann","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"199924","messageId":"BC634E06939C44239106E7A8DD229130@gmail.com","threadId":"31651","inReplyTo":null,"subject":"Bug in Submodule add","fromName":"Jonathan Johnson","fromEmail":"me@jondavidjohn.com","sentAt":"2012-09-26T04:18:05Z","receivedAt":"2012-09-26T04:18:05Z","isPatch":false,"sender":{"key":"me@jondavidjohn.com","avatar":null},"body":"I believe I have found an issue with the way `submodule add` detects a submodule that already exists in the repository. \n\nTo reproduce\n\n1) add a git submodule in a specific location (we'll say it's at `./submodule/location`)\n2) go through the normal steps of removing a submodule, as listed here - https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial\n3) Now the submodule is completely removed and there is no reference to it in .gitmodules or .git/config\n4) Re-add a different repository at the same location (`./submodule/location`)\n\nExpected - The new submodule repository will be set up at ./submodule/location and have the new repository as its origin\n\nWhat Actually Happens - The new submodule uses the existing `$gitdir` (old repository) as the actual backing repository to the submodule, but the new repository is reflected in .gitmodules and .git/config.\n\nSo to recap, the result is that `git remote show origin`  in the submodule shows a different origin than is in .gitmodules and .git/config\n\nOne simple step to remedy this would be to add the deletion of the backing repository from the .git/modules directory, but again, I think an actual command to take care of all of these steps is in order anyways.  Not sure you want to encourage people poking around in the .git directory.\n\nIf this is already resolved in the newest versions, please disregard\n\nThanks!\n\nJonathan Johnson\n\nhttp://jondavidjohn.com | me@jondavidjohn.com\n"},{"id":"199976","messageId":"50636C00.6080906@web.de","threadId":"31651","inReplyTo":"BC634E06939C44239106E7A8DD229130@gmail.com","subject":"Re: Bug in Submodule add","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-26T20:56:32Z","receivedAt":"2012-09-26T20:56:32Z","isPatch":false,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 26.09.2012 06:18, schrieb Jonathan Johnson:\n> I believe I have found an issue with the way `submodule add` detects a submodule that already exists in the repository. \n\nYes, this is an issue and thanks for the detailed report.\n\n> To reproduce\n> \n> 1) add a git submodule in a specific location (we'll say it's at `./submodule/location`)\n> 2) go through the normal steps of removing a submodule, as listed here - https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial\n> 3) Now the submodule is completely removed and there is no reference to it in .gitmodules or .git/config\n> 4) Re-add a different repository at the same location (`./submodule/location`)\n> \n> Expected - The new submodule repository will be set up at ./submodule/location and have the new repository as its origin\n> \n> What Actually Happens - The new submodule uses the existing `$gitdir` (old repository) as the actual backing repository to the submodule, but the new repository is reflected in .gitmodules and .git/config.\n> \n> So to recap, the result is that `git remote show origin`  in the submodule shows a different origin than is in .gitmodules and .git/config\n> \n> One simple step to remedy this would be to add the deletion of the backing repository from the .git/modules directory, but again, I think an actual command to take care of all of these steps is in order anyways.  Not sure you want to encourage people poking around in the .git directory.\n\nUnfortunately just throwing away the old repository under .git/modules,\nwhether manually or by a git command, is no real solution here: it would\nmake it impossible to go back to a commit which records the old submodule\nand check that out again.\n\nThe reason for this issue is that the submodule path is used as its name\nby \"git submodule add\". While we could check this type of conflict locally,\nwe can't really avoid it due to the distributed nature of git (somebody\nelse could add a different repo under the same path - and thus the same\nname - in another clone of the repo).\n\nThe only long term solution I can think of is to use some kind of UUID for\nthe name, so that the names of newly added submodules won't have a chance\nto clash anymore. For the short term aborting \"git submodule add\" when a\nsubmodule of that name already exists in .git/modules of the superproject\ntogether with the ability to provide a custom name might at least solve\nthe local clashes.\n"},{"id":"200184","messageId":"50677E76.1050204@web.de","threadId":"31651","inReplyTo":"50636C00.6080906@web.de","subject":"[PATCH 0/2] Let \"git submodule add\" fail when .git/modules/<name> already exists","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-29T23:04:22Z","receivedAt":"2012-09-29T23:04:22Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 26.09.2012 22:56, schrieb Jens Lehmann:\n> Am 26.09.2012 06:18, schrieb Jonathan Johnson:\n>> To reproduce\n>>\n>> 1) add a git submodule in a specific location (we'll say it's at `./submodule/location`)\n>> 2) go through the normal steps of removing a submodule, as listed here - https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial\n>> 3) Now the submodule is completely removed and there is no reference to it in .gitmodules or .git/config\n>> 4) Re-add a different repository at the same location (`./submodule/location`)\n>>\n>> Expected - The new submodule repository will be set up at ./submodule/location and have the new repository as its origin\n>>\n>> What Actually Happens - The new submodule uses the existing `$gitdir` (old repository) as the actual backing repository to the submodule, but the new repository is reflected in .gitmodules and .git/config.\n>>\n>> So to recap, the result is that `git remote show origin`  in the submodule shows a different origin than is in .gitmodules and .git/config\n>>\n>> One simple step to remedy this would be to add the deletion of the backing repository from the .git/modules directory, but again, I think an actual command to take care of all of these steps is in order anyways.  Not sure you want to encourage people poking around in the .git directory.\n> \n> Unfortunately just throwing away the old repository under .git/modules,\n> whether manually or by a git command, is no real solution here: it would\n> make it impossible to go back to a commit which records the old submodule\n> and check that out again.\n> \n> The reason for this issue is that the submodule path is used as its name\n> by \"git submodule add\". While we could check this type of conflict locally,\n> we can't really avoid it due to the distributed nature of git (somebody\n> else could add a different repo under the same path - and thus the same\n> name - in another clone of the repo).\n> \n> The only long term solution I can think of is to use some kind of UUID for\n> the name, so that the names of newly added submodules won't have a chance\n> to clash anymore. For the short term aborting \"git submodule add\" when a\n> submodule of that name already exists in .git/modules of the superproject\n> together with the ability to provide a custom name might at least solve\n> the local clashes.\n\nThis two patch series implements the short term solution described above.\n\nUsing some kind of UUID can easily be added in a subsequent patch, we just\nhave to replace 'sm_name=\"$sm_path\"' with 'sm_name=$(<generate uuid>)' in\nline 348 of git-submodule.sh. I think it'll be the best solution to just\nuse a random UUID for that, as doing anything clever (like using the SHA1\nof the url to avoid copies of the same remote repo) might lead to subtle\nbreakages (e.g. because it assumes the url stays unique forever, which it\nsometimes won't). But maybe the short term solution is sufficient as most\nof the time people won't produce submodule name conflicts (and names\nderived from paths are much more readable that UUIDs). Thoughts?\n\n\nJens Lehmann (2):\n  Teach \"git submodule add\" the --name option\n  submodule add: Fail when .git/modules/<name> already exists\n\n Documentation/git-submodule.txt |  7 ++++-\n Documentation/gitmodules.txt    |  4 ++-\n git-submodule.sh                | 35 ++++++++++++++++-------\n t/t7400-submodule-basic.sh      | 63 +++++++++++++++++++++++++++++++++++++++++\n t/t7406-submodule-update.sh     |  2 +-\n 5 files changed, 97 insertions(+), 14 deletions(-)\n\n-- \n1.7.12.1.430.g4fd6dc4\n"},{"id":"200185","messageId":"50677ED6.1030001@web.de","threadId":"31651","inReplyTo":"50677E76.1050204@web.de","subject":"[PATCH 1/2] Teach \"git submodule add\" the --name option","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-29T23:05:58Z","receivedAt":"2012-09-29T23:05:58Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"\"git submodule add\" initializes the name of a submodule to its path. This\nwas ok as long as the .git directory lived inside the submodule's work\ntree, but since 1.7.8 it is stored in the .git/modules/<name> directory of\nthe superproject, making the submodule name survive the removal of the\nsubmodule's work tree. This leads to problems when the user tries to add a\ndifferent submodule at the same path - and thus the same name - later, as\nthat will happily try to restore the submodule from the old repository\ninstead of the one the user specified and will lead to a checkout of the\nwrong repository.\n\nAdd the new \"--name\" option to let the user provide a name for the\nsubmodule. This enables the user to solve this conflict without having to\nremove .git/modules/<name> by hand (which is no viable solution as it\nmakes it impossible to checkout a commit that records the old submodule\nand populate it, as that will still check out the new submodule for the\nsame reason).\n\nTo achieve that the submodule's name is added to the parameter list of\nthe module_clone() helper function. This makes it possible to remove the\ncall of module_name() there because both callers of module_clone() already\nknow the name and can provide it as argument number two.\n\nReported-by: Jonathan Johnson <me@jondavidjohn.com>\nSigned-off-by: Jens Lehmann <Jens.Lehmann@web.de>\n---\n Documentation/git-submodule.txt |  7 ++++++-\n Documentation/gitmodules.txt    |  4 +++-\n git-submodule.sh                | 32 ++++++++++++++++++++---------\n t/t7400-submodule-basic.sh      | 45 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 76 insertions(+), 12 deletions(-)\n\ndiff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt\nindex 2de7bf0..22efca0 100644\n--- a/Documentation/git-submodule.txt\n+++ b/Documentation/git-submodule.txt\n@@ -9,7 +9,7 @@ git-submodule - Initialize, update or inspect submodules\n SYNOPSIS\n --------\n [verse]\n-'git submodule' [--quiet] add [-b branch] [-f|--force]\n+'git submodule' [--quiet] add [-b branch] [-f|--force] [--name <name>]\n \t      [--reference <repository>] [--] <repository> [<path>]\n 'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]\n 'git submodule' [--quiet] init [--] [<path>...]\n@@ -266,6 +266,11 @@ OPTIONS\n \tInitialize all submodules for which \"git submodule init\" has not been\n \tcalled so far before updating.\n\n+--name::\n+\tThis option is only valid for the add command. It sets the submodule's\n+\tname to the given string instead of defaulting to its path. The name\n+\tmust be valid as a directory name and may not end with a '/'.\n+\n --reference <repository>::\n \tThis option is only valid for add and update commands.  These\n \tcommands sometimes need to clone a remote repository. In this case,\ndiff --git a/Documentation/gitmodules.txt b/Documentation/gitmodules.txt\nindex 4effd78..ab3e91c 100644\n--- a/Documentation/gitmodules.txt\n+++ b/Documentation/gitmodules.txt\n@@ -18,7 +18,9 @@ working tree, is a text file with a syntax matching the requirements\n of linkgit:git-config[1].\n\n The file contains one subsection per submodule, and the subsection value\n-is the name of the submodule. Each submodule section also contains the\n+is the name of the submodule. The name is set to the path where the\n+submodule has been added unless it was customized with the '--name'\n+option of 'git submodule add'. Each submodule section also contains the\n following required keys:\n\n submodule.<name>.path::\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 3e2045e..22febb1 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -5,7 +5,7 @@\n # Copyright (c) 2007 Lars Hjemli\n\n dashless=$(basename \"$0\" | sed -e 's/-/ /')\n-USAGE=\"[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]\n+USAGE=\"[--quiet] add [-b branch] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]\n    or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]\n    or: $dashless [--quiet] init [--] [<path>...]\n    or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]\n@@ -29,6 +29,7 @@ files=\n nofetch=\n update=\n prefix=\n+custom_name=\n\n # The function takes at most 2 arguments. The first argument is the\n # URL that navigates to the submodule origin repo. When relative, this URL\n@@ -179,8 +180,9 @@ module_name()\n module_clone()\n {\n \tsm_path=$1\n-\turl=$2\n-\treference=\"$3\"\n+\tname=$2\n+\turl=$3\n+\treference=\"$4\"\n \tquiet=\n \tif test -n \"$GIT_QUIET\"\n \tthen\n@@ -189,8 +191,6 @@ module_clone()\n\n \tgitdir=\n \tgitdir_base=\n-\tname=$(module_name \"$sm_path\" 2>/dev/null)\n-\ttest -n \"$name\" || name=\"$sm_path\"\n \tbase_name=$(dirname \"$name\")\n\n \tgitdir=$(git rev-parse --git-dir)\n@@ -272,6 +272,11 @@ cmd_add()\n \t\t\treference=\"$1\"\n \t\t\tshift\n \t\t\t;;\n+\t\t--name)\n+\t\t\tcase \"$2\" in '') usage ;; esac\n+\t\t\tcustom_name=$2\n+\t\t\tshift\n+\t\t\t;;\n \t\t--)\n \t\t\tshift\n \t\t\tbreak\n@@ -336,6 +341,13 @@ Use -f if you really want to add it.\" >&2\n \t\texit 1\n \tfi\n\n+\tif test -n \"$custom_name\"\n+\tthen\n+\t\tsm_name=\"$custom_name\"\n+\telse\n+\t\tsm_name=\"$sm_path\"\n+\tfi\n+\n \t# perhaps the path exists and is already a git repo, else clone it\n \tif test -e \"$sm_path\"\n \tthen\n@@ -348,7 +360,7 @@ Use -f if you really want to add it.\" >&2\n\n \telse\n\n-\t\tmodule_clone \"$sm_path\" \"$realrepo\" \"$reference\" || exit\n+\t\tmodule_clone \"$sm_path\" \"$sm_name\" \"$realrepo\" \"$reference\" || exit\n \t\t(\n \t\t\tclear_local_git_env\n \t\t\tcd \"$sm_path\" &&\n@@ -359,13 +371,13 @@ Use -f if you really want to add it.\" >&2\n \t\t\tesac\n \t\t) || die \"$(eval_gettext \"Unable to checkout submodule '\\$sm_path'\")\"\n \tfi\n-\tgit config submodule.\"$sm_path\".url \"$realrepo\"\n+\tgit config submodule.\"$sm_name\".url \"$realrepo\"\n\n \tgit add $force \"$sm_path\" ||\n \tdie \"$(eval_gettext \"Failed to add submodule '\\$sm_path'\")\"\n\n-\tgit config -f .gitmodules submodule.\"$sm_path\".path \"$sm_path\" &&\n-\tgit config -f .gitmodules submodule.\"$sm_path\".url \"$repo\" &&\n+\tgit config -f .gitmodules submodule.\"$sm_name\".path \"$sm_path\" &&\n+\tgit config -f .gitmodules submodule.\"$sm_name\".url \"$repo\" &&\n \tgit add --force .gitmodules ||\n \tdie \"$(eval_gettext \"Failed to register submodule '\\$sm_path'\")\"\n }\n@@ -594,7 +606,7 @@ Maybe you want to use 'update --init'?\")\"\n\n \t\tif ! test -d \"$sm_path\"/.git -o -f \"$sm_path\"/.git\n \t\tthen\n-\t\t\tmodule_clone \"$sm_path\" \"$url\" \"$reference\"|| exit\n+\t\t\tmodule_clone \"$sm_path\" \"$name\" \"$url\" \"$reference\" || exit\n \t\t\tcloned_modules=\"$cloned_modules;$name\"\n \t\t\tsubsha1=\n \t\telse\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex 56a81cd..78bf739 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -681,4 +681,49 @@ test_expect_success 'moving the superproject does not break submodules' '\n \t)\n '\n\n+test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '\n+\t(\n+\t\tcd addtest2 &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\techo \"$submodurl/repo\" >expect &&\n+\t\t\tgit config remote.origin.url >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo \"gitdir: ../.git/modules/repo\" >expect &&\n+\t\t\ttest_cmp expect .git\n+\t\t) &&\n+\t\trm -rf repo &&\n+\t\tgit rm repo &&\n+\t\tgit submodule add -q --name repo_new \"$submodurl/bare.git\" repo >actual &&\n+\t\ttest ! -s actual &&\n+\t\techo \"gitdir: ../.git/modules/submod\" >expect &&\n+\t\ttest_cmp expect submod/.git &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\t\tgit config remote.origin.url >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo \"gitdir: ../.git/modules/repo_new\" >expect &&\n+\t\t\ttest_cmp expect .git\n+\t\t) &&\n+\t\techo \"repo\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo.path >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config -f .gitmodules submodule.repo_new.path >actual &&\n+\t\ttest_cmp expect actual&&\n+\t\techo \"$submodurl/repo\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/repo\" >expect &&\n+\t\tgit config submodule.repo.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\n-- \n1.7.12.1.430.g4fd6dc4\n"},{"id":"200186","messageId":"50677F2A.6060807@web.de","threadId":"31651","inReplyTo":"50677E76.1050204@web.de","subject":"[PATCH 2/2] submodule add: Fail when .git/modules/<name> already exists","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-29T23:07:22Z","receivedAt":"2012-09-29T23:07:22Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"When adding a new submodule it can happen that .git/modules/<name> already\ncontains a submodule repo, e.g. when a submodule is removed from the work\ntree and another submodule is added at the same path. But then the work\ntree of the submodule will be populated using the existing repository and\nnot the one the user provided.\n\nError out in that case and tell the user she should use a different name\nfor the submodule with the \"--name\" option to avoid this problem. In one\ntest in t7406 the --name option had to be added to \"git submodule add\", as\nthat test re-adds a formerly removed submodule.\n\nReported-by: Jonathan Johnson <me@jondavidjohn.com>\nSigned-off-by: Jens Lehmann <Jens.Lehmann@web.de>\n---\n git-submodule.sh            |  3 ++-\n t/t7400-submodule-basic.sh  | 18 ++++++++++++++++++\n t/t7406-submodule-update.sh |  2 +-\n 3 files changed, 21 insertions(+), 2 deletions(-)\n\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 22febb1..58cd053 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -359,7 +359,8 @@ Use -f if you really want to add it.\" >&2\n \t\tfi\n\n \telse\n-\n+\t\ttest ! -d \".git/modules/$sm_name\" ||\n+\t\tdie \"$(eval_gettext \"Submodule name '\\$sm_name' is already used. Please choose another name with the '--name' option.\")\"\n \t\tmodule_clone \"$sm_path\" \"$sm_name\" \"$realrepo\" \"$reference\" || exit\n \t\t(\n \t\t\tclear_local_git_env\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex 78bf739..a031a27 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -726,4 +726,22 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano\n \t)\n '\n\n+test_expect_success 'submodule add with an existing name fails' '\n+\t(\n+\t\tcd addtest2 &&\n+\t\trm -rf repo &&\n+\t\ttest_must_fail git submodule add -q --name repo_new \"$submodurl/bare.git\" repo &&\n+\t\ttest ! -d repo &&\n+\t\techo \"repo\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.path >actual &&\n+\t\ttest_cmp expect actual&&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\ndiff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh\nindex 1542653..2d44c51 100755\n--- a/t/t7406-submodule-update.sh\n+++ b/t/t7406-submodule-update.sh\n@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'\n \t(cd super &&\n \t git reset --hard master &&\n \t rm -rf deeper/ &&\n-\t git submodule add ../submodule deeper/submodule\n+\t git submodule add --name deeper/submodule2 ../submodule deeper/submodule\n \t)\n '\n\n-- \n1.7.12.1.430.g4fd6dc4\n"},{"id":"200192","messageId":"7vtxugglqy.fsf@alter.siamese.dyndns.org","threadId":"31651","inReplyTo":"50677E76.1050204@web.de","subject":"Re: [PATCH 0/2] Let \"git submodule add\" fail when .git/modules/<name> already exists","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-09-30T04:47:33Z","receivedAt":"2012-09-30T04:47:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jens Lehmann <Jens.Lehmann@web.de> writes:\n\n>> The only long term solution I can think of is to use some kind of UUID for\n>> the name, so that the names of newly added submodules won't have a chance\n>> to clash anymore. For the short term aborting \"git submodule add\" when a\n>> submodule of that name already exists in .git/modules of the superproject\n>> together with the ability to provide a custom name might at least solve\n>> the local clashes.\n\nThat assumes that the addition of the submodule for the second time\nis to add a completely different submodule at the same location and\nis done on purpose, but is that a sensible assumption?\n\nIf a superproject that is about an embedded appliance used to have a\nsubmodule A bound at its path \"kernel\", but for some reason stopped\nshipping with \"kernel\" and then later reintroduced the directory\n\"kernel\" bound to some submodule B, my gut feeling is that it is\njust as likely (if not more likely) that A and B are indeed the same\nsubmodule (i.e. it shares the same history) as they are totally\nunrelated.\n\nCould it be that it is a user error combined with the immaturity of\n\"git submodule\" tool that does not yet support \"it used to be here,\nbut it disappears for a while and then it reappears in the history\nof the superproject\" very well that caused the user to manually add\na \"new\" submodule which in fact is the same submodule at the same\npath?\n\nI think failing with a better error message is a good idea. It\nshould suggest to either resurrect the submodule that is stashed\naway in \"$GIT_DIR/modules/$name\" if it indeed is the same, or to\ngive it a different name (perhaps \"kernel\" used to be pointing at\nthe Linux kernel history, then the user is replacing it with a\ntotally different implementation that is really from different\norigin and do not share any history, perhaps BSD).  In such a case,\nthe user may want to pick bsd-kernel or something as its name, to\ndifferentiate it.\n\n> Using some kind of UUID can easily be added in a subsequent patch,...\n\nI would suggest thinking really long and hard before saying UUID.\nIt is an easy cop-out to ensure uniqueness, but risks to allow two\npeople (or one person at two different time) to give two unrelated\nnames to a single thing that actually is the same.\n\nA better alternative might be to use the commit object name at the\nroot of the history of the submodule, which would catch the simplest\nand most common case of the mistake, I would think.\n"},{"id":"200220","messageId":"50689B5F.3060308@web.de","threadId":"31651","inReplyTo":"7vtxugglqy.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 0/2] Let \"git submodule add\" fail when .git/modules/<name> already exists","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-30T19:19:59Z","receivedAt":"2012-09-30T19:19:59Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 30.09.2012 06:47, schrieb Junio C Hamano:\n> Jens Lehmann <Jens.Lehmann@web.de> writes:\n> \n>>> The only long term solution I can think of is to use some kind of UUID for\n>>> the name, so that the names of newly added submodules won't have a chance\n>>> to clash anymore. For the short term aborting \"git submodule add\" when a\n>>> submodule of that name already exists in .git/modules of the superproject\n>>> together with the ability to provide a custom name might at least solve\n>>> the local clashes.\n> \n> That assumes that the addition of the submodule for the second time\n> is to add a completely different submodule at the same location and\n> is done on purpose, but is that a sensible assumption?\n> \n> If a superproject that is about an embedded appliance used to have a\n> submodule A bound at its path \"kernel\", but for some reason stopped\n> shipping with \"kernel\" and then later reintroduced the directory\n> \"kernel\" bound to some submodule B, my gut feeling is that it is\n> just as likely (if not more likely) that A and B are indeed the same\n> submodule (i.e. it shares the same history) as they are totally\n> unrelated.\n> \n> Could it be that it is a user error combined with the immaturity of\n> \"git submodule\" tool that does not yet support \"it used to be here,\n> but it disappears for a while and then it reappears in the history\n> of the superproject\" very well that caused the user to manually add\n> a \"new\" submodule which in fact is the same submodule at the same\n> path?\n> \n> I think failing with a better error message is a good idea. It\n> should suggest to either resurrect the submodule that is stashed\n> away in \"$GIT_DIR/modules/$name\" if it indeed is the same, or to\n> give it a different name (perhaps \"kernel\" used to be pointing at\n> the Linux kernel history, then the user is replacing it with a\n> totally different implementation that is really from different\n> origin and do not share any history, perhaps BSD).  In such a case,\n> the user may want to pick bsd-kernel or something as its name, to\n> differentiate it.\n\nGood point! I will add a more detailed error message (including\nthe url of the default remote which is configured for the already\npresent submodule repo) and teach --force to skip the test and\nresurrect that submodule repo.\n\n>> Using some kind of UUID can easily be added in a subsequent patch,...\n> \n> I would suggest thinking really long and hard before saying UUID.\n\nAbsolutely.\n\n> It is an easy cop-out to ensure uniqueness, but risks to allow two\n> people (or one person at two different time) to give two unrelated\n> names to a single thing that actually is the same.\n\nI'm not too worried about that (even though it would be good for\nthe disk footprint). And I couldn't come up with a better way to\nsolve the problem we currently have when the same name is used\nfor two different submodule repos.\n\n> A better alternative might be to use the commit object name at the\n> root of the history of the submodule, which would catch the simplest\n> and most common case of the mistake, I would think.\n\nThis won't work well e.g. when one uses a fork of another repo,\nthat will contain different commits while still having the same\nroot commit. I was also thinking about hashing the URL, but that\nwill break when the user reconfigures the URL to somewhere else.\nAfter playing with some ideas I couldn't find a way to let the\nsubmodule's repo provide sufficient uniqueness.\n\nI'd say for now we go with the detection of name clashes and let\nthe user choose if he wants to resurrect that submodule repo or\nif he wants to choose another name. But if we notice further down\nthe road that collisions are a problem in real life, we can think\nagain if UUIDs - or something else - might be a solution.\n"},{"id":"200224","messageId":"5068B329.7040302@web.de","threadId":"31651","inReplyTo":"50689B5F.3060308@web.de","subject":"[PATCH v2 2/2] submodule add: Fail when .git/modules/<name> already exists unless forced","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2012-09-30T21:01:29Z","receivedAt":"2012-09-30T21:01:29Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"When adding a new submodule it can happen that .git/modules/<name> already\ncontains a submodule repo, e.g. when a submodule is removed from the work\ntree and another submodule is added at the same path. But then the work\ntree of the submodule will be populated using the existing repository and\nnot the one the user provided, which results in an incorrect work tree. On\nthe other hand the user might reactivate a submodule removed earlier, then\nreusing that .git directory is the Right Thing to do.\n\nAs git can't decide what is the case, error out and tell the user she\nshould use either use a different name for the submodule with the \"--name\"\noption or can reuse the .git directory for the newly added submodule by\nproviding the --force option (which only makes sense when the upstream\nmatches, so the error message lists all remotes of .git/modules/<name>).\n\nIn one test in t7406 the --force option had to be added to \"git submodule\nadd\", as that test re-adds a formerly removed submodule.\n\nReported-by: Jonathan Johnson <me@jondavidjohn.com>\nSigned-off-by: Jens Lehmann <Jens.Lehmann@web.de>\n---\n\nAm 30.09.2012 21:19, schrieb Jens Lehmann:\n> Am 30.09.2012 06:47, schrieb Junio C Hamano:\n>> I think failing with a better error message is a good idea. It\n>> should suggest to either resurrect the submodule that is stashed\n>> away in \"$GIT_DIR/modules/$name\" if it indeed is the same, or to\n>> give it a different name (perhaps \"kernel\" used to be pointing at\n>> the Linux kernel history, then the user is replacing it with a\n>> totally different implementation that is really from different\n>> origin and do not share any history, perhaps BSD).  In such a case,\n>> the user may want to pick bsd-kernel or something as its name, to\n>> differentiate it.\n> \n> Good point! I will add a more detailed error message (including\n> the url of the default remote which is configured for the already\n> present submodule repo) and teach --force to skip the test and\n> resurrect that submodule repo.\n\nThe message when \"git submodule add\" finds .git/modules/<name> is:\n\nA git directory for '<name>' is found locally with remote(s):\n  origin\t<url(s) from .git/modules/<name>>\nIf you want to reuse this local git directory instead of cloning again from\n  <url given on command line>\nuse the '--force' option. If the local git directory is not the correct repo\nor you are unsure what this means choose another name with the '--name' option.\n\nWhen run with the --force option the following message is printed:\n\nReactivating local git directory for submodule '<name>'.\n\n\n git-submodule.sh            | 15 ++++++++++++++-\n t/t7400-submodule-basic.sh  | 30 ++++++++++++++++++++++++++++++\n t/t7406-submodule-update.sh |  2 +-\n 3 files changed, 45 insertions(+), 2 deletions(-)\n\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 22febb1..e8112c8 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -359,7 +359,20 @@ Use -f if you really want to add it.\" >&2\n \t\tfi\n\n \telse\n-\n+\t\tif test -d \".git/modules/$sm_name\"\n+\t\tthen\n+\t\t\tif test -z \"$force\"\n+\t\t\tthen\n+\t\t\t\techo >&2 \"$(eval_gettext \"A git directory for '\\$sm_name' is found locally with remote(s):\")\"\n+\t\t\t\tGIT_DIR=\".git/modules/$sm_name\" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^,\"  \", -e s,' (fetch)',, >&2\n+\t\t\t\techo >&2 \"$(eval_gettext \"If you want to reuse this local git directory instead of cloning again from\")\"\n+\t\t\t\techo >&2 \"  $realrepo\"\n+\t\t\t\techo >&2 \"$(eval_gettext \"use the '--force' option. If the local git directory is not the correct repo\")\"\n+\t\t\t\tdie \"$(eval_gettext \"or you are unsure what this means choose another name with the '--name' option.\")\"\n+\t\t\telse\n+\t\t\t\techo \"$(eval_gettext \"Reactivating local git directory for submodule '\\$sm_name'.\")\"\n+\t\t\tfi\n+\t\tfi\n \t\tmodule_clone \"$sm_path\" \"$sm_name\" \"$realrepo\" \"$reference\" || exit\n \t\t(\n \t\t\tclear_local_git_env\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex 78bf739..f1a94f7 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -726,4 +726,34 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano\n \t)\n '\n\n+test_expect_success 'submodule add with an existing name fails unless forced' '\n+\t(\n+\t\tcd addtest2 &&\n+\t\trm -rf repo &&\n+\t\tgit rm repo &&\n+\t\ttest_must_fail git submodule add -q --name repo_new \"$submodurl/repo.git\" repo &&\n+\t\ttest ! -d repo &&\n+\t\techo \"repo\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.path >actual &&\n+\t\ttest_cmp expect actual&&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/bare.git\" >expect &&\n+\t\tgit config submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit submodule add -f -q --name repo_new \"$submodurl/repo.git\" repo &&\n+\t\ttest -d repo &&\n+\t\techo \"repo\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.path >actual &&\n+\t\ttest_cmp expect actual&&\n+\t\techo \"$submodurl/repo.git\" >expect &&\n+\t\tgit config -f .gitmodules submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$submodurl/repo.git\" >expect &&\n+\t\tgit config submodule.repo_new.url >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\ndiff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh\nindex 1542653..feaec6c 100755\n--- a/t/t7406-submodule-update.sh\n+++ b/t/t7406-submodule-update.sh\n@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'\n \t(cd super &&\n \t git reset --hard master &&\n \t rm -rf deeper/ &&\n-\t git submodule add ../submodule deeper/submodule\n+\t git submodule add --force ../submodule deeper/submodule\n \t)\n '\n\n-- \n1.7.12.1.430.gd057107\n"},{"id":"200226","messageId":"7v7grbf42l.fsf@alter.siamese.dyndns.org","threadId":"31651","inReplyTo":"5068B329.7040302@web.de","subject":"Re: [PATCH v2 2/2] submodule add: Fail when .git/modules/<name> already exists unless forced","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-10-01T00:06:58Z","receivedAt":"2012-10-01T00:06:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jens Lehmann <Jens.Lehmann@web.de> writes:\n\n>> Good point! I will add a more detailed error message (including\n>> the url of the default remote which is configured for the already\n>> present submodule repo) and teach --force to skip the test and\n>> resurrect that submodule repo.\n>\n> The message when \"git submodule add\" finds .git/modules/<name> is:\n>\n> A git directory for '<name>' is found locally with remote(s):\n>   origin\t<url(s) from .git/modules/<name>>\n> If you want to reuse this local git directory instead of cloning again from\n>   <url given on command line>\n> use the '--force' option. If the local git directory is not the correct repo\n> or you are unsure what this means choose another name with the '--name' option.\n>\n> When run with the --force option the following message is printed:\n>\n> Reactivating local git directory for submodule '<name>'.\n\nThanks, will re-queue.\n\nThe approach \"submodule rm\" takes when removing a project is to\ntreat the removed submodule as not necessary for the current commit\nin the superproject, but it is considered necessary elsewhere in the\nhistory of the superproject, and that is why we stash away the\nrepository in $GIT_DIR/modules of the superproject.\n\nWe may however want to think about another mode of user error where\nthe user runs \"submodule add $path\" for a wrong repository, realizes\nthe mistake _before_ making any commit and try to repoint the $path\nto a correct repository.  The behaviour of \"submodule add\" in this\npatch, and the behaviour of existing \"submodule rm\", assumes that\nthe user is not stupid and won't make such a mistake, but to recover,\nthe user may need a way to really nuke the submodule repository that\nwas added by the earlier misteake (which is not needed anywhere in\nthe history of the superproject) and $GIT_DIR/module/$name really\nreplaced with the updated one.\n\nI don't know how important to support a recovery procedure from such\nmistakes, though.\n"}]}