{"thread":{"id":"20723","subject":"[PATCH] Add option -b/--branch to clone for select a new HEAD","startedAt":"2009-08-24T20:42:48Z","lastAt":"2009-08-25T19:00:39Z","messageCount":6,"participants":["Kirill A. Korinskiy","Jeff King","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"121674","messageId":"1251146568-25248-1-git-send-email-catap@catap.ru","threadId":"20723","inReplyTo":null,"subject":"[PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Kirill A. Korinskiy","fromEmail":"catap@catap.ru","sentAt":"2009-08-24T20:42:48Z","receivedAt":"2009-08-24T20:42:48Z","isPatch":true,"sender":{"key":"catap@catap.ru","avatar":"https://gravatar.com/avatar/ea0ab2c29579606bd684eccdf786c666f4425bedc4e599b698a175f12737b1c5?d=mp&s=160"},"body":"Sometimes (especially on production systems) we need to use only one\nremote branch for building software. It really annoying to clone\norigin and then swith branch by hand everytime. So this patch provide\nfunctionality to clone remote branch with one command without using\ncheckout after clone.\n---\n Documentation/git-clone.txt |    4 ++++\n builtin-clone.c             |   26 +++++++++++++++++++++++---\n 2 files changed, 27 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 2c63a0f..50446d2 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository.\n \tInstead of using the remote name 'origin' to keep track\n \tof the upstream repository, use <name>.\n \n+--branch <name>::\n+-b <name>::\n+\tInstead of using the remote HEAD as master, use <name> branch.\n+\n --upload-pack <upload-pack>::\n -u <upload-pack>::\n \tWhen given, and the repository to clone from is accessed\ndiff --git a/builtin-clone.c b/builtin-clone.c\nindex 32dea74..4420c68 100644\n--- a/builtin-clone.c\n+++ b/builtin-clone.c\n@@ -41,6 +41,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;\n static int option_local, option_no_hardlinks, option_shared;\n static char *option_template, *option_reference, *option_depth;\n static char *option_origin = NULL;\n+static char *option_branch = NULL;\n static char *option_upload_pack = \"git-upload-pack\";\n static int option_verbose;\n \n@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = {\n \t\t   \"reference repository\"),\n \tOPT_STRING('o', \"origin\", &option_origin, \"branch\",\n \t\t   \"use <branch> instead of 'origin' to track upstream\"),\n+\tOPT_STRING('b', \"branch\", &option_branch, \"branch\",\n+\t\t   \"use <branch> from 'origin' as HEAD\"),\n \tOPT_STRING('u', \"upload-pack\", &option_upload_pack, \"path\",\n \t\t   \"path to git-upload-pack on the remote\"),\n \tOPT_STRING(0, \"depth\", &option_depth, \"depth\",\n@@ -347,8 +350,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \tconst char *repo_name, *repo, *work_tree, *git_dir;\n \tchar *path, *dir;\n \tint dest_exists;\n-\tconst struct ref *refs, *head_points_at, *remote_head, *mapped_refs;\n-\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n+\tconst struct ref *refs, *head_points_at, *remote_head = NULL, *mapped_refs;\n+\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT, branch_head = STRBUF_INIT;\n \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n \tstruct transport *transport = NULL;\n \tchar *src_ref_prefix = \"refs/heads/\";\n@@ -372,6 +375,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \t\tif (option_origin)\n \t\t\tdie(\"--bare and --origin %s options are incompatible.\",\n \t\t\t    option_origin);\n+\t\tif (option_branch)\n+\t\t\tdie(\"--bare and --branch %s options are incompatible.\",\n+\t\t\t    option_branch);\n \t\toption_no_checkout = 1;\n \t}\n \n@@ -518,7 +524,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \t\tmapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf);\n \n-\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\tif (option_branch) {\n+\t\t\tstrbuf_addf(&branch_head, \"%s%s\", src_ref_prefix, option_branch);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, branch_head.buf);\n+\t\t}\n+\n+\t\tif (!remote_head) {\n+\t\t\tif (option_branch)\n+\t\t\t\twarning(\"Remote branch %s not found in upstream %s\"\n+\t\t\t\t\t\", using HEAD instead\",\n+\t\t\t\t\toption_branch, option_origin);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\t}\n+\n \t\thead_points_at = guess_remote_head(remote_head, mapped_refs, 0);\n \t}\n \telse {\n-- \n1.6.2\n"},{"id":"121693","messageId":"20090825015726.GB7655@coredump.intra.peff.net","threadId":"20723","inReplyTo":"1251146568-25248-1-git-send-email-catap@catap.ru","subject":"Re: [PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-08-25T01:57:26Z","receivedAt":"2009-08-25T01:57:26Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Aug 25, 2009 at 12:42:48AM +0400, Kirill A. Korinskiy wrote:\n\n> Sometimes (especially on production systems) we need to use only one\n> remote branch for building software. It really annoying to clone\n> origin and then swith branch by hand everytime. So this patch provide\n> functionality to clone remote branch with one command without using\n> checkout after clone.\n\nIf you are doing this a lot, it is probably a sign that you should\nrepoint the \"HEAD\" of the parent repository.\n\nThat being said, you may want one branch half the time, and another\nbranch the other half. So I think this is a good feature.\n\nA few comments:\n\n> ---\n>  Documentation/git-clone.txt |    4 ++++\n>  builtin-clone.c             |   26 +++++++++++++++++++++++---\n>  2 files changed, 27 insertions(+), 3 deletions(-)\n\nTests?\n\n> -\tconst struct ref *refs, *head_points_at, *remote_head, *mapped_refs;\n> -\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n> +\tconst struct ref *refs, *head_points_at, *remote_head = NULL, *mapped_refs;\n> +\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT, branch_head = STRBUF_INIT;\n\nStyle nit: I don't know if we have a style guideline for declaring\nvariables, but I find these \"many variables on a line\" declarations\nannoying for reviewing, since it is hard to see what actually changed\n(and yes, you only added a declaration on one, so I am partially\ncomplaining about the person who came before you :) ).\n\n> +\t\tif (option_branch)\n> +\t\t\tdie(\"--bare and --branch %s options are incompatible.\",\n> +\t\t\t    option_branch);\n\nHmm. Would it perhaps make sense to have \"--bare --branch foo\" point the\nHEAD of the newly created bare repo, but not impact the (nonexistent)\nworking tree?\n\n-Peff\n"},{"id":"121696","messageId":"7vfxbgvdx8.fsf@alter.siamese.dyndns.org","threadId":"20723","inReplyTo":"20090825015726.GB7655@coredump.intra.peff.net","subject":"Re: [PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-08-25T02:13:23Z","receivedAt":"2009-08-25T02:13:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> On Tue, Aug 25, 2009 at 12:42:48AM +0400, Kirill A. Korinskiy wrote:\n>\n>> Sometimes (especially on production systems) we need to use only one\n>> remote branch for building software. It really annoying to clone\n>> origin and then swith branch by hand everytime. So this patch provide\n>> functionality to clone remote branch with one command without using\n>> checkout after clone.\n>\n> If you are doing this a lot, it is probably a sign that you should\n> repoint the \"HEAD\" of the parent repository.\n>\n> That being said, you may want one branch half the time, and another\n> branch the other half. So I think this is a good feature.\n>\n> A few comments:\n\nVery good review snipped, as I agree with everything you said (except that\nyou did not point out the lack of sign-off).\n"},{"id":"121723","messageId":"1251203445-3740-1-git-send-email-catap@catap.ru","threadId":"20723","inReplyTo":"20090825015726.GB7655@coredump.intra.peff.net","subject":"[PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Kirill A. Korinskiy","fromEmail":"catap@catap.ru","sentAt":"2009-08-25T12:30:45Z","receivedAt":"2009-08-25T12:30:45Z","isPatch":true,"sender":{"key":"catap@catap.ru","avatar":"https://gravatar.com/avatar/ea0ab2c29579606bd684eccdf786c666f4425bedc4e599b698a175f12737b1c5?d=mp&s=160"},"body":"Sometimes (especially on production systems) we need to use only one\nremote branch for building software. It really annoying to clone\norigin and then swith branch by hand everytime. So this patch provide\nfunctionality to clone remote branch with one command without using\ncheckout after clone.\n---\n Documentation/git-clone.txt |    4 ++++\n builtin-clone.c             |   23 ++++++++++++++++++++---\n t/t5706-clone-brnach.sh     |   31 +++++++++++++++++++++++++++++++\n 3 files changed, 55 insertions(+), 3 deletions(-)\n create mode 100755 t/t5706-clone-brnach.sh\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 2c63a0f..50446d2 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository.\n \tInstead of using the remote name 'origin' to keep track\n \tof the upstream repository, use <name>.\n \n+--branch <name>::\n+-b <name>::\n+\tInstead of using the remote HEAD as master, use <name> branch.\n+\n --upload-pack <upload-pack>::\n -u <upload-pack>::\n \tWhen given, and the repository to clone from is accessed\ndiff --git a/builtin-clone.c b/builtin-clone.c\nindex 32dea74..9cea056 100644\n--- a/builtin-clone.c\n+++ b/builtin-clone.c\n@@ -41,6 +41,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;\n static int option_local, option_no_hardlinks, option_shared;\n static char *option_template, *option_reference, *option_depth;\n static char *option_origin = NULL;\n+static char *option_branch = NULL;\n static char *option_upload_pack = \"git-upload-pack\";\n static int option_verbose;\n \n@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = {\n \t\t   \"reference repository\"),\n \tOPT_STRING('o', \"origin\", &option_origin, \"branch\",\n \t\t   \"use <branch> instead of 'origin' to track upstream\"),\n+\tOPT_STRING('b', \"branch\", &option_branch, \"branch\",\n+\t\t   \"use <branch> from 'origin' as HEAD\"),\n \tOPT_STRING('u', \"upload-pack\", &option_upload_pack, \"path\",\n \t\t   \"path to git-upload-pack on the remote\"),\n \tOPT_STRING(0, \"depth\", &option_depth, \"depth\",\n@@ -347,8 +350,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \tconst char *repo_name, *repo, *work_tree, *git_dir;\n \tchar *path, *dir;\n \tint dest_exists;\n-\tconst struct ref *refs, *head_points_at, *remote_head, *mapped_refs;\n-\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n+\tconst struct ref *refs, *head_points_at, *remote_head = NULL, *mapped_refs;\n+\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT, branch_head = STRBUF_INIT;\n \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n \tstruct transport *transport = NULL;\n \tchar *src_ref_prefix = \"refs/heads/\";\n@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \t\tmapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf);\n \n-\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\tif (option_branch) {\n+\t\t\tstrbuf_addf(&branch_head, \"%s%s\", src_ref_prefix, option_branch);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, branch_head.buf);\n+\t\t}\n+\n+\t\tif (!remote_head) {\n+\t\t\tif (option_branch)\n+\t\t\t\twarning(\"Remote branch %s not found in upstream %s\"\n+\t\t\t\t\t\", using HEAD instead\",\n+\t\t\t\t\toption_branch, option_origin);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\t}\n+\n \t\thead_points_at = guess_remote_head(remote_head, mapped_refs, 0);\n \t}\n \telse {\ndiff --git a/t/t5706-clone-brnach.sh b/t/t5706-clone-brnach.sh\nnew file mode 100755\nindex 0000000..1f2704b\n--- /dev/null\n+++ b/t/t5706-clone-brnach.sh\n@@ -0,0 +1,31 @@\n+#!/bin/sh\n+\n+test_description='branch clone options'\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\n+\tmkdir parent &&\n+\t(cd parent && git init &&\n+\t echo one >file && git add file &&\n+\t git commit -m one && git checkout -b two &&\n+\t echo two >f && git add f && git commit -m two &&\n+\t git checkout master)\n+\n+'\n+\n+test_expect_success 'clone' '\n+\n+\tgit clone parent clone &&\n+\t(cd clone && git rev-parse --verify refs/remotes/origin/master)\n+\n+'\n+\n+test_expect_success 'clone -b' '\n+\n+\tgit clone -b two parent clone-b &&\n+\t(cd clone && git rev-parse --verify refs/remotes/origin/two)\n+\n+'\n+\n+test_done\n-- \n1.6.2\n"},{"id":"121734","messageId":"1251220806-17607-1-git-send-email-catap@catap.ru","threadId":"20723","inReplyTo":"7vfxbgvdx8.fsf@alter.siamese.dyndns.org","subject":"[PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Kirill A. Korinskiy","fromEmail":"catap@catap.ru","sentAt":"2009-08-25T17:20:06Z","receivedAt":"2009-08-25T17:20:06Z","isPatch":true,"sender":{"key":"catap@catap.ru","avatar":"https://gravatar.com/avatar/ea0ab2c29579606bd684eccdf786c666f4425bedc4e599b698a175f12737b1c5?d=mp&s=160"},"body":"Sometimes (especially on production systems) we need to use only one\nremote branch for building software. It really annoying to clone\norigin and then swith branch by hand everytime. So this patch provide\nfunctionality to clone remote branch with one command without using\ncheckout after clone.\n\nSigned-off-by: Kirill A. Korinskiy <catap@catap.ru>\n---\n Documentation/git-clone.txt |    4 ++++\n builtin-clone.c             |   23 ++++++++++++++++++++---\n t/t5706-clone-brnach.sh     |   31 +++++++++++++++++++++++++++++++\n 3 files changed, 55 insertions(+), 3 deletions(-)\n create mode 100755 t/t5706-clone-brnach.sh\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 2c63a0f..50446d2 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository.\n \tInstead of using the remote name 'origin' to keep track\n \tof the upstream repository, use <name>.\n \n+--branch <name>::\n+-b <name>::\n+\tInstead of using the remote HEAD as master, use <name> branch.\n+\n --upload-pack <upload-pack>::\n -u <upload-pack>::\n \tWhen given, and the repository to clone from is accessed\ndiff --git a/builtin-clone.c b/builtin-clone.c\nindex 32dea74..9cea056 100644\n--- a/builtin-clone.c\n+++ b/builtin-clone.c\n@@ -41,6 +41,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;\n static int option_local, option_no_hardlinks, option_shared;\n static char *option_template, *option_reference, *option_depth;\n static char *option_origin = NULL;\n+static char *option_branch = NULL;\n static char *option_upload_pack = \"git-upload-pack\";\n static int option_verbose;\n \n@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = {\n \t\t   \"reference repository\"),\n \tOPT_STRING('o', \"origin\", &option_origin, \"branch\",\n \t\t   \"use <branch> instead of 'origin' to track upstream\"),\n+\tOPT_STRING('b', \"branch\", &option_branch, \"branch\",\n+\t\t   \"use <branch> from 'origin' as HEAD\"),\n \tOPT_STRING('u', \"upload-pack\", &option_upload_pack, \"path\",\n \t\t   \"path to git-upload-pack on the remote\"),\n \tOPT_STRING(0, \"depth\", &option_depth, \"depth\",\n@@ -347,8 +350,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \tconst char *repo_name, *repo, *work_tree, *git_dir;\n \tchar *path, *dir;\n \tint dest_exists;\n-\tconst struct ref *refs, *head_points_at, *remote_head, *mapped_refs;\n-\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n+\tconst struct ref *refs, *head_points_at, *remote_head = NULL, *mapped_refs;\n+\tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT, branch_head = STRBUF_INIT;\n \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n \tstruct transport *transport = NULL;\n \tchar *src_ref_prefix = \"refs/heads/\";\n@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \t\tmapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf);\n \n-\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\tif (option_branch) {\n+\t\t\tstrbuf_addf(&branch_head, \"%s%s\", src_ref_prefix, option_branch);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, branch_head.buf);\n+\t\t}\n+\n+\t\tif (!remote_head) {\n+\t\t\tif (option_branch)\n+\t\t\t\twarning(\"Remote branch %s not found in upstream %s\"\n+\t\t\t\t\t\", using HEAD instead\",\n+\t\t\t\t\toption_branch, option_origin);\n+\n+\t\t\tremote_head = find_ref_by_name(refs, \"HEAD\");\n+\t\t}\n+\n \t\thead_points_at = guess_remote_head(remote_head, mapped_refs, 0);\n \t}\n \telse {\ndiff --git a/t/t5706-clone-brnach.sh b/t/t5706-clone-brnach.sh\nnew file mode 100755\nindex 0000000..1f2704b\n--- /dev/null\n+++ b/t/t5706-clone-brnach.sh\n@@ -0,0 +1,31 @@\n+#!/bin/sh\n+\n+test_description='branch clone options'\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\n+\tmkdir parent &&\n+\t(cd parent && git init &&\n+\t echo one >file && git add file &&\n+\t git commit -m one && git checkout -b two &&\n+\t echo two >f && git add f && git commit -m two &&\n+\t git checkout master)\n+\n+'\n+\n+test_expect_success 'clone' '\n+\n+\tgit clone parent clone &&\n+\t(cd clone && git rev-parse --verify refs/remotes/origin/master)\n+\n+'\n+\n+test_expect_success 'clone -b' '\n+\n+\tgit clone -b two parent clone-b &&\n+\t(cd clone && git rev-parse --verify refs/remotes/origin/two)\n+\n+'\n+\n+test_done\n-- \n1.6.2\n"},{"id":"121747","messageId":"20090825190039.GD23731@coredump.intra.peff.net","threadId":"20723","inReplyTo":"1251220806-17607-1-git-send-email-catap@catap.ru","subject":"Re: [PATCH] Add option -b/--branch to clone for select a new HEAD","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-08-25T19:00:39Z","receivedAt":"2009-08-25T19:00:39Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Thanks for revising, it is looking a bit better. A few comments still,\nthough:\n\nOn Tue, Aug 25, 2009 at 09:20:06PM +0400, Kirill A. Korinskiy wrote:\n\n> Sometimes (especially on production systems) we need to use only one\n> remote branch for building software. It really annoying to clone\n> origin and then swith branch by hand everytime. So this patch provide\n> functionality to clone remote branch with one command without using\n> checkout after clone.\n\nTypos:\n  s/It/It's/\n  s/swith/switch/\n  s/provide/provides/\n  s/clone remote/clone a remote/\n\n>  t/t5706-clone-brnach.sh     |   31 +++++++++++++++++++++++++++++++\n\nTypo: s/brnach/branch/ :)\n\n> +test_expect_success 'clone' '\n> +\n> +\tgit clone parent clone &&\n> +\t(cd clone && git rev-parse --verify refs/remotes/origin/master)\n> +\n> +'\n> +\n> +test_expect_success 'clone -b' '\n> +\n> +\tgit clone -b two parent clone-b &&\n> +\t(cd clone && git rev-parse --verify refs/remotes/origin/two)\n> +\n> +'\n\nIs this really testing the right thing? Shouldn't you always have\nrefs/remotes/origin/*, no matter what \"-b\" says? The difference should\nbe that HEAD in the first test will point to 'master', and to 'two' in\nthe second test.\n\n-Peff\n"}]}