{"thread":{"id":"8626","subject":"[PATCH] git-submodule: provide easy way of adding new submodules","startedAt":"2007-06-17T15:23:16Z","lastAt":"2007-06-17T15:23:16Z","messageCount":1,"participants":["Sven Verdoolaege"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"45223","messageId":"20070617152315.GA3916MdfPADPa@greensroom.kotnet.org","threadId":"8626","inReplyTo":null,"subject":"[PATCH] git-submodule: provide easy way of adding new submodules","fromName":"Sven Verdoolaege","fromEmail":"skimo@kotnet.org","sentAt":"2007-06-17T15:23:16Z","receivedAt":"2007-06-17T15:23:16Z","isPatch":true,"sender":{"key":"skimo@kotnet.org","avatar":null},"body":"To make a submodule effectively usable, the path and\na URL where the submodule can be cloned need to be stored\nin .gitmodules.  This subcommand takes care of setting\nthis information after cloning the new submodule.\nOnly the index is updated, so, if needed, the user may still\nchange the URL or switch to a different branch of the submodule\nbefore committing.\n\nSigned-off-by: Sven Verdoolaege <skimo@kotnet.org>\n---\n Documentation/git-submodule.txt |    8 +++++\n git-submodule.sh                |   65 +++++++++++++++++++++++++++++++++++---\n 2 files changed, 67 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt\nindex f8fb80f..bf9cbf9 100644\n--- a/Documentation/git-submodule.txt\n+++ b/Documentation/git-submodule.txt\n@@ -8,11 +8,19 @@ git-submodule - Initialize, update or inspect submodules\n \n SYNOPSIS\n --------\n+'git-submodule' [--quiet] add <repository> [<path>]\n 'git-submodule' [--quiet] [--cached] [status|init|update] [--] [<path>...]\n \n \n COMMANDS\n --------\n+add::\n+\tAdd the given repository as a submodule at the given path\n+\tto the changeset to be committed next.  In particular, the\n+\trepository is cloned at the specified path, added to the\n+\tchangeset and registered in .gitmodules.   If no path is\n+\tspecified, the path is deduced from the repository specification.\n+\n status::\n \tShow the status of the submodules. This will print the SHA-1 of the\n \tcurrently checked out commit for each submodule, along with the\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 89a3885..85ae275 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -1,13 +1,14 @@\n #!/bin/sh\n #\n-# git-submodules.sh: init, update or list git submodules\n+# git-submodules.sh: add, init, update or list git submodules\n #\n # Copyright (c) 2007 Lars Hjemli\n \n-USAGE='[--quiet] [--cached] [status|init|update] [--] [<path>...]'\n+USAGE='[--quiet] [--cached] [add <repo>|status|init|update] [--] [<path>...]'\n . git-sh-setup\n require_work_tree\n \n+add=\n init=\n update=\n status=\n@@ -25,6 +26,17 @@ say()\n \tfi\n }\n \n+get_repo_base() {\n+\t(\n+\t\tcd \"`/bin/pwd`\" &&\n+\t\tcd \"$1\" || cd \"$1.git\" &&\n+\t\t{\n+\t\t\tcd .git\n+\t\t\tpwd\n+\t\t}\n+\t) 2>/dev/null\n+}\n+\n #\n # Map submodule path to submodule name\n #\n@@ -66,6 +78,41 @@ module_clone()\n }\n \n #\n+# Add a new submodule to the working tree, .gitmodules and the index\n+#\n+# $@ = repo [path]\n+#\n+module_add()\n+{\n+\trepo=$1\n+\tpath=$2\n+\n+\t# Turn the source into an absolute path if\n+\t# it is local\n+\tif base=$(get_repo_base \"$repo\"); then\n+\t\trepo=\"$base\"\n+\tfi\n+\n+\t# Guess path from repo if not specified or strip trailing slashes\n+\tif test -z \"$path\"; then\n+\t\tpath=$(echo \"$repo\" | sed -e 's|/*$||' -e 's|:*/*\\.git$||' -e 's|.*[/:]||g')\n+\telse\n+\t\tpath=$(echo \"$path\" | sed -e 's|/*$||')\n+\tfi\n+\n+\tmodule_clone \"$path\" \"$repo\" || exit\n+\t(unset GIT_DIR && cd \"$path\" && git checkout -q) ||\n+\tdie \"Unable to checkout submodule '$path'\"\n+\tgit add \"$path\" ||\n+\tdie \"Failed to add submodule '$path'\"\n+\n+\tGIT_CONFIG=.gitmodules git config submodule.\"$path\".path \"$path\" &&\n+\tGIT_CONFIG=.gitmodules git config submodule.\"$path\".url \"$repo\" &&\n+\tgit add .gitmodules ||\n+\tdie \"Failed to register submodule '$path'\"\n+}\n+\n+#\n # Register submodules in .git/config\n #\n # $@ = requested paths (default to all)\n@@ -173,6 +220,9 @@ modules_list()\n while case \"$#\" in 0) break ;; esac\n do\n \tcase \"$1\" in\n+\tadd)\n+\t\tadd=1\n+\t\t;;\n \tinit)\n \t\tinit=1\n \t\t;;\n@@ -201,14 +251,17 @@ do\n \tshift\n done\n \n-case \"$init,$update,$status,$cached\" in\n-1,,,)\n+case \"$add,$init,$update,$status,$cached\" in\n+1,,,,)\n+\tmodule_add \"$@\"\n+\t;;\n+,1,,,)\n \tmodules_init \"$@\"\n \t;;\n-,1,,)\n+,,1,,)\n \tmodules_update \"$@\"\n \t;;\n-,,*,*)\n+,,,*,*)\n \tmodules_list \"$@\"\n \t;;\n *)\n-- \n1.5.2.1.1088.ge05f4-dirty\n"}]}