{"thread":{"id":"42998","subject":"[PATCHv4 6/7] submodule--helper: add remote-branch helper","startedAt":"2016-08-03T20:45:27Z","lastAt":"2016-08-03T23:23:00Z","messageCount":6,"participants":["Stefan Beller","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"292989","messageId":"20160803204404.3356-2-sbeller@google.com","threadId":"42998","inReplyTo":"20160803204404.3356-1-sbeller@google.com","subject":"[PATCHv4 6/7] submodule--helper: add remote-branch helper","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-08-03T20:44:03Z","receivedAt":"2016-08-03T20:45:27Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"In a later patch we want to enhance the logic for the branch selection.\nRewrite the current logic to be in C, so we can directly use C when\nwe enhance the logic.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/submodule--helper.c | 36 +++++++++++++++++++++++++++++++++++-\n git-submodule.sh            |  2 +-\n 2 files changed, 36 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex fb90c64..9be2c75 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -899,6 +899,39 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefix\n\treturn 0;\n }\n \n+static const char *remote_submodule_branch(const char *path)\n+{\n+\tconst struct submodule *sub;\n+\tgitmodules_config();\n+\tgit_config(submodule_config, NULL);\n+\n+\tsub = submodule_from_path(null_sha1, path);\n+\tif (!sub)\n+\t\treturn NULL;\n+\n+\tif (!sub->branch)\n+\t\treturn \"master\";\n+\n+\treturn sub->branch;\n+}\n+\n+static int resolve_remote_submodule_branch(int argc, const char **argv,\n+\t\tconst char *prefix)\n+{\n+\tconst char *ret;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tif (argc != 2)\n+\t\tdie(\"submodule--helper remote-branch takes exactly one arguments, got %d\", argc);\n+\n+\tret = remote_submodule_branch(argv[1]);\n+\tif (!ret)\n+\t\tdie(\"submodule %s doesn't exist\", argv[1]);\n+\n+\tprintf(\"%s\", ret);\n+\tstrbuf_release(&sb);\n+\treturn 0;\n+}\n+\n struct cmd_struct {\n \tconst char *cmd;\n \tint (*fn)(int, const char **, const char *);\n@@ -912,7 +945,8 @@ static struct cmd_struct commands[] = {\n \t{\"relative-path\", resolve_relative_path},\n \t{\"resolve-relative-url\", resolve_relative_url},\n \t{\"resolve-relative-url-test\", resolve_relative_url_test},\n-\t{\"init\", module_init}\n+\t{\"init\", module_init},\n+\t{\"remote-branch\", resolve_remote_submodule_branch}\n };\n \n int cmd_submodule__helper(int argc, const char **argv, const char *prefix)\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 41aff65..8c5e898 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -614,7 +614,7 @@ cmd_update()\n \n \t\tif test -n \"$remote\"\n \t\tthen\n-\t\t\tbranch=$(get_submodule_config \"$name\" branch master)\n+\t\t\tbranch=$(git submodule--helper remote-branch \"$sm_path\")\n \t\t\tif test -z \"$nofetch\"\n \t\t\tthen\n \t\t\t\t# Fetch remote before determining tracking $sha1\n-- \n2.9.2.524.gdbd1860\n\n"},{"id":"292990","messageId":"20160803204404.3356-1-sbeller@google.com","threadId":"42998","inReplyTo":null,"subject":"[PATCHv4 {6,7}/7] submodule update: allow '.' for branch value","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-08-03T20:44:02Z","receivedAt":"2016-08-03T20:45:34Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"This replaces the last two commits of  sb/submodule-update-dot-branch.\n\nThanks Jeff for pointing out the possible segfault.\nIn the last commit I removed commented code in the test.\n\nThanks,\nStefan\n\ndiff to sb/submodule-update-dot-branch:\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex ae88eff..f1acc4d 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -906,6 +906,9 @@ static const char *remote_submodule_branch(const char *path)\n        git_config(submodule_config, NULL);\n \n        sub = submodule_from_path(null_sha1, path);\n+       if (!sub)\n+               return NULL;\n+\n        if (!sub->branch)\n                return \"master\";\n \n@@ -933,11 +936,16 @@ static const char *remote_submodule_branch(const char *path)\n static int resolve_remote_submodule_branch(int argc, const char **argv,\n                const char *prefix)\n {\n+       const char *ret;\n        struct strbuf sb = STRBUF_INIT;\n        if (argc != 2)\n                die(\"submodule--helper remote-branch takes exactly one arguments, got %d\", argc);\n \n-       printf(\"%s\", remote_submodule_branch(argv[1]));\n+       ret = remote_submodule_branch(argv[1]);\n+       if (!ret)\n+               die(\"submodule %s doesn't exist\", argv[1]);\n+\n+       printf(\"%s\", ret);\n        strbuf_release(&sb);\n        return 0;\n }\ndiff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh\nindex 1c4c1f2..d7983cf 100755\n--- a/t/t7406-submodule-update.sh\n+++ b/t/t7406-submodule-update.sh\n@@ -238,7 +238,6 @@ test_expect_success 'submodule update --remote should fetch upstream changes wit\n                test_cmp expect actual &&\n                git checkout master &&\n                git branch -d test-branch &&\n-               #~ git -C ../submodule branch -d test-branch &&\n                git reset --hard HEAD^\n        )\n '\n\nStefan Beller (2):\n  submodule--helper: add remote-branch helper\n  submodule update: allow '.' for branch value\n\n builtin/submodule--helper.c | 54 ++++++++++++++++++++++++++++++++++++++++++++-\n git-submodule.sh            |  2 +-\n t/t7406-submodule-update.sh | 35 ++++++++++++++++++++++++++++-\n 3 files changed, 88 insertions(+), 3 deletions(-)\n\n-- \n2.9.2.524.gdbd1860\n\n"},{"id":"292991","messageId":"20160803204404.3356-3-sbeller@google.com","threadId":"42998","inReplyTo":"20160803204404.3356-1-sbeller@google.com","subject":"[PATCHv4 6/7] submodule update: allow '.' for branch value","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-08-03T20:44:04Z","receivedAt":"2016-08-03T20:45:42Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"Gerrit has a \"superproject subscription\" feature[1], that triggers a\ncommit in a superproject that is subscribed to its submodules.\nConceptually this Gerrit feature can be done on the client side with\nGit via (except for raciness, error handling etc):\n\n  while [ true ]; do\n    git -C <superproject> submodule update --remote --force\n    git -C <superproject> commit -a -m \"Update submodules\"\n    git -C <superproject> push\n  done\n\nfor each branch in the superproject. To ease the configuration in Gerrit\na special value of \".\" has been introduced for the submodule.<name>.branch\nto mean the same branch as the superproject[2], such that you can create a\nnew branch on both superproject and the submodule and this feature\ncontinues to work on that new branch.\n\nNow we find projects in the wild with such a .gitmodules file.\nThe .gitmodules used in these Gerrit projects do not conform\nto Gits understanding of how .gitmodules should look like.\nThis teaches Git to deal gracefully with this syntax as well.\n\nThe redefinition of \".\" does no harm to existing projects unaware of\nthis change, as \".\" is an invalid branch name in Git, so we do not\nexpect such projects to exist.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/submodule--helper.c | 18 ++++++++++++++++++\n t/t7406-submodule-update.sh | 35 ++++++++++++++++++++++++++++++++++-\n 2 files changed, 52 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 9be2c75..f1acc4d 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -912,6 +912,24 @@ static const char *remote_submodule_branch(const char *path)\n \tif (!sub->branch)\n \t\treturn \"master\";\n \n+\tif (!strcmp(sub->branch, \".\")) {\n+\t\tunsigned char sha1[20];\n+\t\tconst char *refname = resolve_ref_unsafe(\"HEAD\", 0, sha1, NULL);\n+\n+\t\tif (!refname)\n+\t\t\tdie(_(\"No such ref: %s\"), \"HEAD\");\n+\n+\t\t/* detached HEAD */\n+\t\tif (!strcmp(refname, \"HEAD\"))\n+\t\t\tdie(_(\"Submodule (%s) branch configured to inherit \"\n+\t\t\t      \"branch from superproject, but the superproject \"\n+\t\t\t      \"is not on any branch\"), sub->name);\n+\n+\t\tif (!skip_prefix(refname, \"refs/heads/\", &refname))\n+\t\t\tdie(_(\"Expecting a full ref name, got %s\"), refname);\n+\t\treturn refname;\n+\t}\n+\n \treturn sub->branch;\n }\n \ndiff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh\nindex 1bb1f43..d7983cf 100755\n--- a/t/t7406-submodule-update.sh\n+++ b/t/t7406-submodule-update.sh\n@@ -209,9 +209,42 @@ test_expect_success 'submodule update --remote should fetch upstream changes' '\n \t)\n '\n \n+test_expect_success 'submodule update --remote should fetch upstream changes with .' '\n+\t(\n+\t\tcd super &&\n+\t\tgit config -f .gitmodules submodule.\"submodule\".branch \".\" &&\n+\t\tgit add .gitmodules &&\n+\t\tgit commit -m \"submodules: update from the respective superproject branch\"\n+\t) &&\n+\t(\n+\t\tcd submodule &&\n+\t\techo line4a >> file &&\n+\t\tgit add file &&\n+\t\ttest_tick &&\n+\t\tgit commit -m \"upstream line4a\" &&\n+\t\tgit checkout -b test-branch &&\n+\t\ttest_commit on-test-branch\n+\t) &&\n+\t(\n+\t\tcd super &&\n+\t\tgit submodule update --remote --force submodule &&\n+\t\tgit -C submodule log -1 --oneline >actual\n+\t\tgit -C ../submodule log -1 --oneline master >expect\n+\t\ttest_cmp expect actual &&\n+\t\tgit checkout -b test-branch &&\n+\t\tgit submodule update --remote --force submodule &&\n+\t\tgit -C submodule log -1 --oneline >actual\n+\t\tgit -C ../submodule log -1 --oneline test-branch >expect\n+\t\ttest_cmp expect actual &&\n+\t\tgit checkout master &&\n+\t\tgit branch -d test-branch &&\n+\t\tgit reset --hard HEAD^\n+\t)\n+'\n+\n test_expect_success 'local config should override .gitmodules branch' '\n \t(cd submodule &&\n-\t git checkout -b test-branch &&\n+\t git checkout test-branch &&\n \t echo line5 >> file &&\n \t git add file &&\n \t test_tick &&\n-- \n2.9.2.524.gdbd1860\n\n"},{"id":"293046","messageId":"xmqq8twd78fm.fsf@gitster.mtv.corp.google.com","threadId":"42998","inReplyTo":"20160803204404.3356-2-sbeller@google.com","subject":"Re: [PATCHv4 6/7] submodule--helper: add remote-branch helper","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-03T23:13:17Z","receivedAt":"2016-08-03T23:14:37Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\n> index fb90c64..9be2c75 100644\n> --- a/builtin/submodule--helper.c\n> +++ b/builtin/submodule--helper.c\n> @@ -899,6 +899,39 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefix\n> \treturn 0;\n\nI wonder who lost the leading SP before the tab here.  Will manually\nfix, so this is not a reason to force resending, but you may want to\nmake sure there is no systemic cause to corrupt your future patches.\n\nThanks.\n"},{"id":"293047","messageId":"xmqq4m7178dg.fsf@gitster.mtv.corp.google.com","threadId":"42998","inReplyTo":"20160803204404.3356-3-sbeller@google.com","subject":"Re: [PATCHv4 6/7] submodule update: allow '.' for branch value","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-03T23:14:35Z","receivedAt":"2016-08-03T23:16:14Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n> Gerrit has a \"superproject subscription\" feature[1], that triggers a\n> commit in a superproject that is subscribed to its submodules.\n> Conceptually this Gerrit feature can be done on the client side with\n> Git via (except for raciness, error handling etc):\n\nThis would replace 7/7 not 6/7 obviously ;-)\n\nThanks.\n"},{"id":"293049","messageId":"CAGZ79kaVB_aSuUK-jzAaq_ZHMh-P8hrjGjkkQwr0eH7u9xZj-Q@mail.gmail.com","threadId":"42998","inReplyTo":"xmqq8twd78fm.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCHv4 6/7] submodule--helper: add remote-branch helper","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-08-03T23:22:42Z","receivedAt":"2016-08-03T23:23:00Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Wed, Aug 3, 2016 at 4:13 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Stefan Beller <sbeller@google.com> writes:\n>\n>> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\n>> index fb90c64..9be2c75 100644\n>> --- a/builtin/submodule--helper.c\n>> +++ b/builtin/submodule--helper.c\n>> @@ -899,6 +899,39 @@ static int resolve_relative_path(int argc, const char **argv, const char *prefix\n>>       return 0;\n>\n> I wonder who lost the leading SP before the tab here.  Will manually\n> fix, so this is not a reason to force resending, but you may want to\n> make sure there is no systemic cause to corrupt your future patches.\n\nIt's not a systematic issue, it was user error.\n(I started writing out a story, but it got long)\n\nThanks,\nStefan\n\n>\n> Thanks.\n"}]}