{"thread":{"id":"11856","subject":"[PATCH 0 of 7] - Improve handling remotes, origin, and submodules","startedAt":"2008-02-03T17:20:42Z","lastAt":"2008-02-03T17:20:49Z","messageCount":8,"participants":["Mark Levedahl"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"67226","messageId":"1202059249-3532-1-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":null,"subject":"[PATCH 0 of 7] - Improve handling remotes, origin, and submodules","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:42Z","receivedAt":"2008-02-03T17:20:42Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"This patch series was inspired by several issues dealing with\nmultiple remotes and submodules. The tests added as the last\npatch demonstrate the specific use pattern to be supported,\nand that does not work currently.\n\n1 - user A creates a project on server \"frotz\" with a submodule.\n2 - user B clones the project, forks it, adds a submodule, and\npublishes the fork on server \"fork\"\n\nuser A adds the fork to his working tree, via git remote add +\ngit fetch. The desire is that:\n1 - git submodule init + git submodule update include the new\nsubmdule added in the fork.\n2 - The top-level project and all submodules use the same\nremote nickname to refer to the same server.\n\nBreakage in 1.5.4 without this patch series is in several places:\n\n1 - git submodule does not follow the top-level project's\nbranch.<name>.remote in fetching, so does not try to clone\nthe new submodule from remoe \"fork\" where the fork exists.\n2 - If 1 were fixed, git submodule still invokes clone without \"-o\n<remotenick>\" when cloning, so each submodule would have \"origin\"\npoint to a different remote.\n3 - If 2 were fixed, submodule update leaves submodules on detached\nheads, so the remotes machinery cannot supply a remote name, and thus\n\"origin\" is used for further updates. As origin may not be defined,\n(e.g., for the submodule fetched from \"fork\" this also fails.)\n\nThe underlying design issue is the assumption that all projects\nrevolve around a single server, so that *the* server can be called\n\"origin\" without confusion.  Given a project with multiple servers and\nno one server being *the* server, origin cannot be usefully defined.\nAbsent submodules, the remotes machinery largely masks this: origin is\nreferred to only as the default remote and it is easy to avoid using\nthe default remote.  With submodules, this becomes more of an issue.\n\nFollowing this patch series, the following is true:\n\n1) The existing workflow of top-level and all sub-modules always\ncloned from and interacting with a single default remote\nnick-named \"origin\" is unchanged.\n2) Via clone -o <nick> (or git confit core.origin <nick>), the\ndefault remote is changed from origin to <nick>, and that default\nis used instead of origin everywhere.\n3) git-submodule uses the top-level's branch.<name>.remote to\nchoose which remote to fetch from, uses that in all submodules,\nincluding defining that in existing submodules whose url is\nrelative (../* or ./*) indicating the submodule is maintained on\nsame server as top-level.\n\nThe penultimate patch addresses a basic usability issue, allowing\naddition of a submodule in place in a project, rather than requiring\nit be cloned from a remote.  This hole in the porcelain became evident\nin creating a test (t7401) for this series.\nsubmodules except by cloning).\n"},{"id":"67229","messageId":"1202059249-3532-2-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-1-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 1/7] Teach remote machinery about core.origin config variable","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:43Z","receivedAt":"2008-02-03T17:20:43Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"This introduces a new configuration variable, core.origin, that\ndefines the name of the default remote to be used. Traditionally, this\nis \"origin\", and could be overridden for a given branch. This change\nintroduces a way to redefine the default as desired and have that honored\nregardless of the currently checked out head (e.g., core.origin is\nused when on a detached head or any other non-tracking branch).\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n Documentation/config.txt |    6 ++++++\n git-parse-remote.sh      |    5 +++--\n remote.c                 |   11 ++++++++++-\n 3 files changed, 19 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 4e222f1..a890f5d 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -291,6 +291,12 @@ core.editor::\n \t`GIT_EDITOR` environment, `core.editor`, `VISUAL` and\n \t`EDITOR` environment variables and then finally `vi`.\n \n+core.origin::\n+\tThe name of the remote used by default for fetch / pull. If unset,\n+\torigin is assumed. This value is used whenever the current branch\n+\thas no corresponding branch.<name>.remote, such as when working on\n+\ta detached head.\n+\n core.pager::\n \tThe command that git will use to paginate output.  Can be overridden\n \twith the `GIT_PAGER` environment variable.\ndiff --git a/git-parse-remote.sh b/git-parse-remote.sh\nindex 695a409..c7ac7c7 100755\n--- a/git-parse-remote.sh\n+++ b/git-parse-remote.sh\n@@ -56,8 +56,9 @@ get_remote_url () {\n \n get_default_remote () {\n \tcurr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')\n-\torigin=$(git config --get \"branch.$curr_branch.remote\")\n-\techo ${origin:-origin}\n+\tgit config --get \"branch.$curr_branch.remote\" ||\n+\tgit config --get \"core.origin\" ||\n+\techo origin\n }\n \n get_remote_default_refs_for_push () {\ndiff --git a/remote.c b/remote.c\nindex 0e00680..302d499 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -10,6 +10,7 @@ static int allocated_branches;\n \n static struct branch *current_branch;\n static const char *default_remote_name;\n+static const char *core_origin;\n \n #define BUF_SIZE (2048)\n static char buffer[BUF_SIZE];\n@@ -233,6 +234,11 @@ static int handle_config(const char *key, const char *value)\n \t\t\tadd_merge(branch, xstrdup(value));\n \t\treturn 0;\n \t}\n+\tif (!strcmp(key, \"core.origin\")) {\n+\t\tif (value)\n+\t\t\tcore_origin = xstrdup(value);\n+\t\treturn 0;\n+\t}\n \tif (prefixcmp(key,  \"remote.\"))\n \t\treturn 0;\n \tname = key + 7;\n@@ -291,7 +297,6 @@ static void read_config(void)\n \tint flag;\n \tif (default_remote_name) // did this already\n \t\treturn;\n-\tdefault_remote_name = xstrdup(\"origin\");\n \tcurrent_branch = NULL;\n \thead_ref = resolve_ref(\"HEAD\", sha1, 0, &flag);\n \tif (head_ref && (flag & REF_ISSYMREF) &&\n@@ -300,6 +305,10 @@ static void read_config(void)\n \t\t\tmake_branch(head_ref + strlen(\"refs/heads/\"), 0);\n \t}\n \tgit_config(handle_config);\n+\tif (!default_remote_name) {\n+\t\tdefault_remote_name = core_origin ?\n+\t\tcore_origin : xstrdup(\"origin\");\n+\t}\n }\n \n struct refspec *parse_ref_spec(int nr_refspec, const char **refspec)\n-- \n1.5.4.18.g43c18\n"},{"id":"67232","messageId":"1202059249-3532-3-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-2-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 2/7] git-remote - Unset core.origin when deleting the default remote","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:44Z","receivedAt":"2008-02-03T17:20:44Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n git-remote.perl |    5 +++++\n 1 files changed, 5 insertions(+), 0 deletions(-)\n\ndiff --git a/git-remote.perl b/git-remote.perl\nindex d13e4c1..75d2371 100755\n--- a/git-remote.perl\n+++ b/git-remote.perl\n@@ -328,6 +328,11 @@ sub rm_remote {\n \n \t$git->command('config', '--remove-section', \"remote.$name\");\n \n+\tmy $defremote = $git->config(\"core.origin\");\n+\tif (defined $defremote && $defremote eq $name) {\n+\t       $git->command(\"config\", \"--unset\", \"core.origin\");\n+\t}\n+\n \teval {\n \t    my @trackers = $git->command('config', '--get-regexp',\n \t\t\t'branch.*.remote', $name);\n-- \n1.5.4.18.g43c18\n"},{"id":"67227","messageId":"1202059249-3532-4-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-3-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 3/7] git-clone - Set remotes.origin config variable","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:45Z","receivedAt":"2008-02-03T17:20:45Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"This records the users choice of default remote name (by default \"origin\")\nas given by the -o option.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n Documentation/git-clone.txt |    3 ++-\n git-clone.sh                |    1 +\n 2 files changed, 3 insertions(+), 1 deletions(-)\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 2341881..f15aecd 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -103,7 +103,8 @@ OPTIONS\n --origin <name>::\n -o <name>::\n \tInstead of using the remote name 'origin' to keep track\n-\tof the upstream repository, use <name> instead.\n+\tof the upstream repository, use <name> instead. The name\n+\tis recorded in the core.origin config variable.\n \n --upload-pack <upload-pack>::\n -u <upload-pack>::\ndiff --git a/git-clone.sh b/git-clone.sh\nindex b4e858c..7208d68 100755\n--- a/git-clone.sh\n+++ b/git-clone.sh\n@@ -242,6 +242,7 @@ fi &&\n export GIT_DIR &&\n GIT_CONFIG=\"$GIT_DIR/config\" git-init $quiet ${template+\"$template\"} || usage\n \n+git config core.origin $origin\n if test -n \"$bare\"\n then\n \tGIT_CONFIG=\"$GIT_DIR/config\" git config core.bare true\n-- \n1.5.4.18.g43c18\n"},{"id":"67231","messageId":"1202059249-3532-5-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-4-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 4/7] git-submodule - Possibly inherit parent's default remote on init/clone","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:46Z","receivedAt":"2008-02-03T17:20:46Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"For submodules defined relative to their parent, it is likely that the\nparent's defined default remote is correct for the child as well. This\nallows use of remote names other than \"origin\", important as managed\nsubmodules are typically checked out on a detached head and therefore\nsubmodule-update invokes git-fetch using the default remote. Without this\nchange, submodules effectively had to have a default remote of \"origin.\"\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n Documentation/git-submodule.txt |    8 +++++---\n git-submodule.sh                |   20 ++++++++++++++------\n 2 files changed, 19 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt\nindex e818e6e..b04b107 100644\n--- a/Documentation/git-submodule.txt\n+++ b/Documentation/git-submodule.txt\n@@ -37,9 +37,11 @@ status::\n \n init::\n \tInitialize the submodules, i.e. register in .git/config each submodule\n-\tname and url found in .gitmodules. The key used in .git/config is\n-\t`submodule.$name.url`. This command does not alter existing information\n-\tin .git/config.\n+\tname and url found in .gitmodules, along with the default remote origin.\n+\tFor submodules using a relative url, the default remote is inherited\n+\tfrom the parent project, for absolute urls the default \"origin\" is used.\n+\tThe key used in .git/config is submodule.$name.url`. This command does\n+\tnot alter existing information in .git/config.\n \n update::\n \tUpdate the registered submodules, i.e. clone missing submodules and\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex a6aaf40..bb0d265 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -7,6 +7,7 @@\n USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'\n OPTIONS_SPEC=\n . git-sh-setup\n+. git-parse-remote\n require_work_tree\n \n command=\n@@ -40,9 +41,7 @@ get_repo_base() {\n # Resolve relative url by appending to parent's url\n resolve_relative_url ()\n {\n-\tbranch=\"$(git symbolic-ref HEAD 2>/dev/null)\"\n-\tremote=\"$(git config branch.${branch#refs/heads/}.remote)\"\n-\tremote=\"${remote:-origin}\"\n+\tremote=\"$(get_default_remote)\"\n \tremoteurl=\"$(git config remote.$remote.url)\" ||\n \t\tdie \"remote ($remote) does not have a url in .git/config\"\n \turl=\"$1\"\n@@ -92,6 +91,7 @@ module_clone()\n {\n \tpath=$1\n \turl=$2\n+\torigin=$3\n \n \t# If there already is a directory at the submodule path,\n \t# expect it to be empty (since that is the default checkout\n@@ -107,7 +107,7 @@ module_clone()\n \ttest -e \"$path\" &&\n \tdie \"A file already exist at path '$path'\"\n \n-\tgit-clone -n \"$url\" \"$path\" ||\n+\tgit-clone -n -o \"$origin\" \"$url\" \"$path\" ||\n \tdie \"Clone of '$url' into submodule path '$path' failed\"\n }\n \n@@ -156,10 +156,12 @@ cmd_add()\n \tcase \"$repo\" in\n \t./*|../*)\n \t\t# dereference source url relative to parent's url\n+\t\torigin=$(get_default_remote)\n \t\trealrepo=\"$(resolve_relative_url $repo)\" ;;\n \t*)\n \t\t# Turn the source into an absolute path if\n \t\t# it is local\n+\t\torigin=origin\n \t\tif base=$(get_repo_base \"$repo\"); then\n \t\t\trepo=\"$base\"\n \t\tfi\n@@ -180,7 +182,7 @@ cmd_add()\n \tgit ls-files --error-unmatch \"$path\" > /dev/null 2>&1 &&\n \tdie \"'$path' already exists in the index\"\n \n-\tmodule_clone \"$path\" \"$realrepo\" || exit\n+\tmodule_clone \"$path\" \"$realrepo\" \"$origin\" || exit\n \t(unset GIT_DIR; cd \"$path\" && git checkout -q ${branch:+-b \"$branch\" \"origin/$branch\"}) ||\n \tdie \"Unable to checkout submodule '$path'\"\n \tgit add \"$path\" ||\n@@ -236,9 +238,14 @@ cmd_init()\n \t\tcase \"$url\" in\n \t\t./*|../*)\n \t\t\turl=\"$(resolve_relative_url \"$url\")\"\n+\t\t\torigin=$(get_default_remote)\n+\t\t\t;;\n+\t\t*)\n+\t\t\torigin=origin\n \t\t\t;;\n \t\tesac\n \n+\t\tgit config submodule.\"$name\".origin \"$origin\" &&\n \t\tgit config submodule.\"$name\".url \"$url\" ||\n \t\tdie \"Failed to register url for submodule path '$path'\"\n \n@@ -287,10 +294,11 @@ cmd_update()\n \t\t\tsay \"Submodule path '$path' not initialized\"\n \t\t\tcontinue\n \t\tfi\n+\t\torigin=$(git config submodule.\"$name\".origin || echo origin)\n \n \t\tif ! test -d \"$path\"/.git\n \t\tthen\n-\t\t\tmodule_clone \"$path\" \"$url\" || exit\n+\t\t\tmodule_clone \"$path\" \"$url\" \"$origin\" || exit\n \t\t\tsubsha1=\n \t\telse\n \t\t\tsubsha1=$(unset GIT_DIR; cd \"$path\" &&\n-- \n1.5.4.18.g43c18\n"},{"id":"67230","messageId":"1202059249-3532-6-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-5-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 5/7] Teach git-submodule to use top-level remote when updating subprojects","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:47Z","receivedAt":"2008-02-03T17:20:47Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"Modules that are defined using relative urls to the master project are\nassumed to be completely owned by the project. When running\n\"submodule update\" from the top level, it is reasonable that the entire\nproject exists at the remote given by top-level branch.<name>.remote.\nUsing the remote machinery, this remote can be different for each\nbranch and can be different than the current defaults in each submodule.\n\nThis teaches submodule to:\n\n1) Possibly define the current master's remote in each submodule, using\nthe same relative url used by submodule init.\n2) Fetch each submodule's updates from the master's remote.\n\nSubmodules defined using absolute urls (not relative to the parent) are\nnot touched by this logic. Such modules are assumed to be independent\nof the master project so submodule can do no better than to fetch from\ntheir currently defined default remotes as already done.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n git-submodule.sh |   20 +++++++++++++++++++-\n 1 files changed, 19 insertions(+), 1 deletions(-)\n\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex bb0d265..8f31ebe 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -255,6 +255,8 @@ cmd_init()\n \n #\n # Update each submodule path to correct revision, using clone and checkout as needed\n+# For owned submodules (defined using relative url), we use master project's remote\n+# and define that in each submodule if not already there\n #\n # $@ = requested paths (default to all)\n #\n@@ -281,6 +283,7 @@ cmd_update()\n \t\tshift\n \tdone\n \n+\tmaster_remote=$(get_default_remote)\n \tgit ls-files --stage -- \"$@\" | grep -e '^160000 ' |\n \twhile read mode sha1 stage path\n \tdo\n@@ -306,9 +309,24 @@ cmd_update()\n \t\t\tdie \"Unable to find current revision in submodule path '$path'\"\n \t\tfi\n \n+\t\tbaseurl=\"$(GIT_CONFIG=.gitmodules git config submodule.\"$name\".url)\"\n+\t\tcase \"$baseurl\" in\n+\t\t./*|../*)\n+\t\t\tfetch_remote=$master_remote\n+\t\t\t(unset GIT_DIR ; cd \"$path\" && git config remote.\"$fetch_remote\".url > nul) ||\n+\t\t\t(\n+\t\t\t\tabsurl=\"$(resolve_relative_url $baseurl)\"\n+\t\t\t\tunset GIT_DIR; cd \"$path\" && git remote add \"$master_remote\" \"$absurl\"\n+\t\t\t) || die \"Unable to define remote '$fetch_remote' in submodule path '$path'\"\n+\t\t\t;;\n+\t\t*)\n+\t\t\tfetch_remote=\n+\t\t\t;;\n+\t\tesac\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(unset GIT_DIR; cd \"$path\" && git-fetch $fetch_remote &&\n \t\t\t\tgit-checkout -q \"$sha1\") ||\n \t\t\tdie \"Unable to checkout '$sha1' in submodule path '$path'\"\n \n-- \n1.5.4.18.g43c18\n"},{"id":"67233","messageId":"1202059249-3532-7-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-6-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 6/7] git-submodule - Allow adding a submodule in-place","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:48Z","receivedAt":"2008-02-03T17:20:48Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"When working in top-level directory, it is natural to create a new\nsubmodule in a subdirectory, then add that submodule to top-level in\nplace. This allows \"git submodule add <intended url> subdir\" to add\nthe existing subdir to the current project. The presumption is the user\nwill later push / clone the subdir to the <intended url> so that future\nsubmodule init / updates will work.\n\nAbsent this patch, \"git submodule add\" insists upon cloning the subdir\nfrom the remote, which is fine for adding an existing project in but\nless useful when adding a new submodule from scratch to an existing\nproject. This functionality remains, and the clone is attempted if the\nsubdir does not already exist as a valid git repo.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n\nfix submodule again\n---\n Documentation/git-submodule.txt |    5 ++-\n git-submodule.sh                |   56 +++++++++++++++++++++++---------------\n 2 files changed, 37 insertions(+), 24 deletions(-)\n\ndiff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt\nindex b04b107..8ea3fc3 100644\n--- a/Documentation/git-submodule.txt\n+++ b/Documentation/git-submodule.txt\n@@ -18,8 +18,9 @@ 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+\tto the changeset to be committed next.  If path is a valid\n+\trepository within the project, it is added as is. Otherwise,\n+\trepository is cloned at the specified path. path is added to the\n \tchangeset and registered in .gitmodules.   If no path is\n \tspecified, the path is deduced from the repository specification.\n \tIf the repository url begins with ./ or ../, it is stored as\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 8f31ebe..47b5664 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -44,6 +44,7 @@ resolve_relative_url ()\n \tremote=\"$(get_default_remote)\"\n \tremoteurl=\"$(git config remote.$remote.url)\" ||\n \t\tdie \"remote ($remote) does not have a url in .git/config\"\n+\tremoteurl=\"${remoteurl%/.git}\"\n \turl=\"$1\"\n \twhile test -n \"$url\"\n \tdo\n@@ -153,22 +154,6 @@ cmd_add()\n \t\tusage\n \tfi\n \n-\tcase \"$repo\" in\n-\t./*|../*)\n-\t\t# dereference source url relative to parent's url\n-\t\torigin=$(get_default_remote)\n-\t\trealrepo=\"$(resolve_relative_url $repo)\" ;;\n-\t*)\n-\t\t# Turn the source into an absolute path if\n-\t\t# it is local\n-\t\torigin=origin\n-\t\tif base=$(get_repo_base \"$repo\"); then\n-\t\t\trepo=\"$base\"\n-\t\tfi\n-\t\trealrepo=$repo\n-\t\t;;\n-\tesac\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@@ -176,15 +161,42 @@ cmd_add()\n \t\tpath=$(echo \"$path\" | sed -e 's|/*$||')\n \tfi\n \n-\ttest -e \"$path\" &&\n-\tdie \"'$path' already exists\"\n-\n \tgit ls-files --error-unmatch \"$path\" > /dev/null 2>&1 &&\n \tdie \"'$path' already exists in the index\"\n \n-\tmodule_clone \"$path\" \"$realrepo\" \"$origin\" || exit\n-\t(unset GIT_DIR; cd \"$path\" && git checkout -q ${branch:+-b \"$branch\" \"origin/$branch\"}) ||\n-\tdie \"Unable to checkout submodule '$path'\"\n+\t# perhaps the path exists and is already a git repo, else clone it\n+\tif test -e \"$path\"\n+\tthen\n+\t\tif test -d \"$path/.git\" &&\n+\t\ttest \"$(unset GIT_DIR; cd $path; git rev-parse --git-dir)\" = \".git\"\n+\t\tthen\n+\t\t\techo \"Adding existing repo at '$path' to the index\"\n+\t\telse\n+\t\t\tdie \"'$path' already exists and is not a valid git repo\"\n+\t\tfi\n+\telse\n+\t\tcase \"$repo\" in\n+\t\t./*|../*)\n+\t\t\t# dereference source url relative to parent's url\n+\t\t\torigin=$(get_default_remote)\n+\t\t\trealrepo=\"$(resolve_relative_url $repo)\" ;;\n+\t\t*)\n+\t\t\t# Turn the source into an absolute path if\n+\t\t\t# it is local\n+\t\t\torigin=origin\n+\t\t\tif base=$(get_repo_base \"$repo\")\n+\t\t\tthen\n+\t\t\t\trepo=\"$base\"\n+\t\t\tfi\n+\t\t\trealrepo=$repo\n+\t\t\t;;\n+\t\tesac\n+\n+\t\tmodule_clone \"$path\" \"$realrepo\" \"$origin\" || exit\n+\t\t(unset GIT_DIR; cd \"$path\" && git checkout -q ${branch:+-b \"$branch\" \"origin/$branch\"}) ||\n+\t\tdie \"Unable to checkout submodule '$path'\"\n+\tfi\n+\n \tgit add \"$path\" ||\n \tdie \"Failed to add submodule '$path'\"\n \n-- \n1.5.4.18.g43c18\n"},{"id":"67228","messageId":"1202059249-3532-8-git-send-email-mlevedahl@gmail.com","threadId":"11856","inReplyTo":"1202059249-3532-7-git-send-email-mlevedahl@gmail.com","subject":"[PATCH 7/7] Add t/t7401 - test submodule interaction with remotes machinery","fromName":"Mark Levedahl","fromEmail":"mlevedahl@gmail.com","sentAt":"2008-02-03T17:20:49Z","receivedAt":"2008-02-03T17:20:49Z","isPatch":true,"sender":{"key":"mdl123@verizon.net","avatar":"https://avatars.githubusercontent.com/u/5302462?v=4"},"body":"This adds a sequence of tests to assure that the following two sequences\nwork:\n\n\tgit clone -o frotz <someurl> foo\n\tcd foo\n\tgit submodule init\n\tgit submodule update\n\nThis should result in the master and subproject having \"frotz\" as the\nname of the default remote (and origin undefined).\n\nThen, in the same working directory\n\tgit remote add fork <some url>\n\tgit fetch fork\n\tgit checkout --track -b fork fork/<somebranch>\n\tgit submodule init\n\tgit submodule update\n\nwill retrive new submodules from remote \"fork\", and define fork in the\nexisting modules. Origin remains undefined.\n\nNote: this latter case is a clear motivation for overriding \"origin\": after\nthe second test, the various submodules would have different ideas of\nremote \"origin\": these would point to different servers. This would\nentirely prevent the top-level branch.<name>.remote machinery from\ncontrolling the project as there is no uniform naming of remotes.\n\nSigned-off-by: Mark Levedahl <mlevedahl@gmail.com>\n---\n t/t7401-submodule-remote.sh |  104 +++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 104 insertions(+), 0 deletions(-)\n create mode 100755 t/t7401-submodule-remote.sh\n\ndiff --git a/t/t7401-submodule-remote.sh b/t/t7401-submodule-remote.sh\nnew file mode 100755\nindex 0000000..1a793db\n--- /dev/null\n+++ b/t/t7401-submodule-remote.sh\n@@ -0,0 +1,104 @@\n+#!/bin/sh\n+\n+test_description='Porcelain support for submodules with multiple remotes\n+\n+This test verifies operation of submodules using multiple remotes and\n+differing remote per top-level branch.  This includes ability to name the\n+default something other than origin, to follow the top-level\n+remote.<branch>, and to propagate definition of new remotes down to\n+submodules as needed.\n+'\n+\n+. ./test-lib.sh\n+\n+# the standard tests all work with one repo, but we need several..\n+rm -rf .git\n+\n+test_expect_success 'Prepare master repository with 1 submodule' '\n+\t(\n+\tmkdir master &&\n+\tcd master &&\n+\tgit init &&\n+\techo \"on master\" > master.txt &&\n+\tgit add master.txt &&\n+\tgit commit -m \"Add master.txt\" &&\n+\tmkdir submod1 &&\n+\tcd submod1 &&\n+\tgit init &&\n+\techo \"submod1\" > submod1.txt &&\n+\tgit add submod1.txt &&\n+\tgit commit -m \"Added submod1.txt\" &&\n+\tcd .. &&\n+\tgit submodule add ./submod1 submod1 &&\n+\tgit commit -m \"Added submodule submod1\"\n+\t)\n+'\n+\n+test_expect_success 'Clone master as fork' '\n+\t(\n+\tgit clone master fork &&\n+\tcd fork &&\n+\ttest \"$(git remote)\" = \"origin\" &&\n+\tgit submodule init &&\n+\tgit submodule update &&\n+\ttest -e submod1/.git\n+\t)\n+'\n+\n+test_expect_success 'Add second submodule in fork' '\n+\t(\n+\tcd fork &&\n+\tmkdir submod2 &&\n+\tcd submod2 &&\n+\tgit init &&\n+\techo \"submod2\" > submod2.txt &&\n+\tgit add submod2.txt &&\n+\tgit commit -m \"Added submod2.txt\" &&\n+\tcd .. &&\n+\tgit submodule add ./submod2 submod2 &&\n+\tgit commit -m \"Added submodule submod2 on fork\"\n+\t)\n+'\n+\n+test_expect_success 'Clone master using frotz instead of origin' '\n+\t(\n+\tgit clone -o frotz master worker &&\n+\tcd worker &&\n+\ttest \"$(git remote)\" = \"frotz\"\n+\t)\n+'\n+\n+test_expect_success 'Get submodules using frotz instead of origin' '\n+\t(\n+\tcd worker &&\n+\tgit submodule init &&\n+\tgit submodule update &&\n+\ttest -e submod1/.git &&\n+\tcd submod1 &&\n+\ttest \"$(git remote)\" = \"frotz\"\n+\t)\n+'\n+\n+test_expect_success 'Update using fork to get additional submodule' '\n+\t(\n+\tcd worker &&\n+\tgit remote add fork $(pwd)/../fork &&\n+\tgit fetch fork &&\n+\tgit checkout --track -b fork_master fork/master &&\n+\tgit submodule init &&\n+\tgit submodule update &&\n+\ttest -e submod2/.git &&\n+\tcd submod2 &&\n+\ttest \"$(git remote)\" = \"fork\" &&\n+\tcd ../submod1 &&\n+\tremotes1=$(git remote) &&\n+\tcase $remotes1 in\n+\t\tfork*frotz|frotz*fork)\n+\t\t\ttrue ;;\n+\t\t*)\n+\t\t\tfalse ;;\n+\tesac\n+\t)\n+'\n+\n+test_done\n-- \n1.5.4.18.g43c18\n"}]}