{"thread":{"id":"8550","subject":"[PATCH 4/5] git-submodule: give submodules proper names","startedAt":"2007-06-11T00:03:12Z","lastAt":"2007-06-11T00:03:12Z","messageCount":1,"participants":["Lars Hjemli"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"44672","messageId":"op.ttqc3mis9pspc6@localhost","threadId":"8550","inReplyTo":null,"subject":"[PATCH 4/5] git-submodule: give submodules proper names","fromName":"Lars Hjemli","fromEmail":"hjemli@gmail.com","sentAt":"2007-06-11T00:03:12Z","receivedAt":"2007-06-11T00:03:12Z","isPatch":true,"sender":{"key":"hjemli@gmail.com","avatar":null},"body":"This changes the way git-submodule uses .gitmodules: Subsections no longer\nspecify the submodule path, they now specify the submodule name. The\nsubmodule path is found under the new key \"submodule.<name>.path\", which is\na required key.\n\nWith this change a submodule can be moved between different 'checkout paths'\nwithout upsetting git-submodule.\n\nSigned-off-by: Lars Hjemli <hjemli@gmail.com>\n---\n  git-submodule.sh           |   45 ++++++++++++++++++++++++++++++-------------\n  t/t7400-submodule-basic.sh |   20 +++++++++++++++---\n  2 files changed, 47 insertions(+), 18 deletions(-)\n\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 6c83c52..89a3885 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -25,6 +25,19 @@ say()\n  \tfi\n  }\n\n+#\n+# Map submodule path to submodule name\n+#\n+# $1 = path\n+#\n+module_name()\n+{\n+       name=$(GIT_CONFIG=.gitmodules git-config --get-regexp '^submodule\\..*\\.path$' \"$1\" |\n+       sed -nre 's/^submodule\\.(.+)\\.path .+$/\\1/p')\n+       test -z \"$name\" &&\n+       die \"No submodule mapping found in .gitmodules for path '$path'\"\n+       echo \"$name\"\n+}\n\n  #\n  # Clone a submodule\n@@ -49,7 +62,7 @@ module_clone()\n  \tdie \"A file already exist at path '$path'\"\n\n  \tgit-clone -n \"$url\" \"$path\" ||\n-\tdie \"Clone of submodule '$path' failed\"\n+\tdie \"Clone of '$url' into submodule path '$path' failed\"\n  }\n\n  #\n@@ -63,17 +76,18 @@ modules_init()\n  \twhile read mode sha1 stage path\n  \tdo\n  \t\t# Skip already registered paths\n-\t\turl=$(git-config submodule.\"$path\".url)\n+\t\tname=$(module_name \"$path\") || exit\n+\t\turl=$(git-config submodule.\"$name\".url)\n  \t\ttest -z \"$url\" || continue\n\n-\t\turl=$(GIT_CONFIG=.gitmodules git-config submodule.\"$path\".url)\n+\t\turl=$(GIT_CONFIG=.gitmodules git-config submodule.\"$name\".url)\n  \t\ttest -z \"$url\" &&\n-\t\tdie \"No url found for submodule '$path' in .gitmodules\"\n+\t\tdie \"No url found for submodule path '$path' in .gitmodules\"\n\n-\t\tgit-config submodule.\"$path\".url \"$url\" ||\n-\t\tdie \"Failed to register url for submodule '$path'\"\n+\t\tgit-config submodule.\"$name\".url \"$url\" ||\n+\t\tdie \"Failed to register url for submodule path '$path'\"\n\n-\t\tsay \"Submodule '$path' registered with url '$url'\"\n+\t\tsay \"Submodule '$name' ($url) registered for path '$path'\"\n  \tdone\n  }\n\n@@ -87,13 +101,14 @@ modules_update()\n  \tgit ls-files --stage -- \"$@\" | grep -e '^160000 ' |\n  \twhile read mode sha1 stage path\n  \tdo\n-\t\turl=$(git-config submodule.\"$path\".url)\n+\t\tname=$(module_name \"$path\") || exit\n+\t\turl=$(git-config submodule.\"$name\".url)\n  \t\tif test -z \"$url\"\n  \t\tthen\n  \t\t\t# Only mention uninitialized submodules when its\n  \t\t\t# path have been specified\n  \t\t\ttest \"$#\" != \"0\" &&\n-\t\t\tsay \"Submodule '$path' not initialized\"\n+\t\t\tsay \"Submodule path '$path' not initialized\"\n  \t\t\tcontinue\n  \t\tfi\n\n@@ -104,22 +119,22 @@ modules_update()\n  \t\telse\n  \t\t\tsubsha1=$(unset GIT_DIR && cd \"$path\" &&\n  \t\t\t\tgit-rev-parse --verify HEAD) ||\n-\t\t\tdie \"Unable to find current revision of submodule '$path'\"\n+\t\t\tdie \"Unable to find current revision in submodule path '$path'\"\n  \t\tfi\n\n  \t\tif test \"$subsha1\" != \"$sha1\"\n  \t\tthen\n  \t\t\t(unset GIT_DIR && cd \"$path\" && git-fetch &&\n  \t\t\t\tgit-checkout -q \"$sha1\") ||\n-\t\t\tdie \"Unable to checkout '$sha1' in submodule '$path'\"\n+\t\t\tdie \"Unable to checkout '$sha1' in submodule path '$path'\"\n\n-\t\t\tsay \"Submodule '$path': checked out '$sha1'\"\n+\t\t\tsay \"Submodule path '$path': checked out '$sha1'\"\n  \t\tfi\n  \tdone\n  }\n\n  #\n-# List all registered submodules, prefixed with:\n+# List all submodules, prefixed with:\n  #  - submodule not initialized\n  #  + different revision checked out\n  #\n@@ -133,7 +148,9 @@ modules_list()\n  \tgit ls-files --stage -- \"$@\" | grep -e '^160000 ' |\n  \twhile read mode sha1 stage path\n  \tdo\n-\t\tif ! test -d \"$path\"/.git\n+\t\tname=$(module_name \"$path\") || exit\n+\t\turl=$(git-config submodule.\"$name\".url)\n+\t\tif test -z \"url\" || ! test -d \"$path\"/.git\n  \t\tthen\n  \t\t\tsay \"-$sha1 $path\"\n  \t\t\tcontinue;\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex 9f2d4f9..7a9b505 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -18,7 +18,7 @@ subcommands of git-submodule.\n  #  -add directory lib to 'superproject', this creates a DIRLINK entry\n  #  -add a couple of regular files to enable testing of submodule filtering\n  #  -mv lib subrepo\n-#  -add an entry to .gitmodules for path 'lib'\n+#  -add an entry to .gitmodules for submodule 'example'\n  #\n  test_expect_success 'Prepare submodule testing' '\n  \tmkdir lib &&\n@@ -40,7 +40,19 @@ test_expect_success 'Prepare submodule testing' '\n  \tgit-add a lib z &&\n  \tgit-commit -m \"super commit 1\" &&\n  \tmv lib .subrepo &&\n-\tGIT_CONFIG=.gitmodules git-config submodule.lib.url git://example.com/lib.git\n+\tGIT_CONFIG=.gitmodules git-config submodule.example.url git://example.com/lib.git\n+'\n+\n+test_expect_success 'status should fail for unmapped paths' '\n+\tif git-submodule status\n+\tthen\n+\t\techo \"[OOPS] submodule status succeeded\"\n+\t\tfalse\n+\telif ! GIT_CONFIG=.gitmodules git-config submodule.example.path lib\n+\tthen\n+\t\techo \"[OOPS] git-config failed to update .gitmodules\"\n+\t\tfalse\n+\tfi\n  '\n\n  test_expect_success 'status should only print one line' '\n@@ -54,12 +66,12 @@ test_expect_success 'status should initially be \"missing\"' '\n\n  test_expect_success 'init should register submodule url in .git/config' '\n  \tgit-submodule init &&\n-\turl=$(git-config submodule.lib.url) &&\n+\turl=$(git-config submodule.example.url) &&\n  \tif test \"$url\" != \"git://example.com/lib.git\"\n  \tthen\n  \t\techo \"[OOPS] init succeeded but submodule url is wrong\"\n  \t\tfalse\n-\telif ! git-config submodule.lib.url ./.subrepo\n+\telif ! git-config submodule.example.url ./.subrepo\n  \tthen\n  \t\techo \"[OOPS] init succeeded but update of url failed\"\n  \t\tfalse\n-- \n1.5.2.1.914.gbd3a7\n"}]}