{"thread":{"id":"18097","subject":"[PATCH] git-clone: Add option --branch to override initial branch","startedAt":"2009-03-02T22:11:22Z","lastAt":"2009-03-12T09:12:16Z","messageCount":19,"participants":["Tor Arne Vestbø","Johannes Schindelin","Junio C Hamano","Paolo Ciarrocchi","Felipe Contreras","Miles Bader"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"106730","messageId":"1236031882-2052-1-git-send-email-torarnv@gmail.com","threadId":"18097","inReplyTo":null,"subject":"[PATCH] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-02T22:11:22Z","receivedAt":"2009-03-02T22:11:22Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"The options --branch and -b allow the user to override the initial\nbranch created and checked out by git-clone. Normally this is the\nactive branch of the remote repository, which is also the fallback\nif the selected branch is not found.\n\nSigned-off-by: Tor Arne Vestbø <torarnv@gmail.com>\n---\n Documentation/git-clone.txt |    5 +++++\n builtin-clone.c             |   33 +++++++++++++++++++++++++++++----\n 2 files changed, 34 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 95f08b9..e7feb4d 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -119,6 +119,11 @@ then the cloned repository will become corrupt.\n \tInstead of using the remote name 'origin' to keep track\n \tof the upstream repository, use <name> instead.\n \n+--branch <name>::\n+-b <name>::\n+\tInstead of using the remote repository's active branch as the\n+\tinitial branch, use <name> instead.\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 c338910..601c2c2 100644\n--- a/builtin-clone.c\n+++ b/builtin-clone.c\n@@ -38,6 +38,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@@ -66,6 +67,8 @@ static struct option builtin_clone_options[] = {\n \t\t   \"path to git-upload-pack on the remote\"),\n \tOPT_STRING(0, \"depth\", &option_depth, \"depth\",\n \t\t    \"create a shallow clone of that depth\"),\n+\tOPT_STRING('b', \"branch\", &option_branch, \"branch\",\n+\t\t    \"initial remote branch to check out\"),\n \n \tOPT_END()\n };\n@@ -372,7 +375,9 @@ 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+\tconst struct ref *refs, *mapped_refs;\n+\tconst struct ref *remote_head = NULL;\n+\tconst struct ref *head_points_at = NULL;\n \tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n \tstruct transport *transport = NULL;\n@@ -545,12 +550,32 @@ 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\thead_points_at = locate_head(refs, mapped_refs, &remote_head);\n+\t\tif (option_branch) {\n+\t\t    const int offset = 11;\n+\t\t    const char *branch = option_branch;\n+\t\t    if (!prefixcmp(branch, \"refs/heads/\"))\n+\t\t\tbranch += offset;\n+\n+\t\t    const struct ref *r;\n+\t\t    for (r = mapped_refs; r; r = r->next) {\n+\t\t\tif (!strcmp(r->name + offset, branch)) {\n+\t\t\t    /* Override initial branch */\n+\t\t\t    head_points_at = r;\n+\t\t\t    remote_head = r;\n+\t\t\t    break;\n+\t\t\t}\n+\t\t    }\n+\n+\t\t    if (!head_points_at)\n+\t\t\twarning(\"remote has no branch named '%s', \"\n+\t\t\t\t\"falling back to default.\", option_branch);\n+\t\t}\n+\n+\t\tif (!head_points_at)\n+\t\t    head_points_at = locate_head(refs, mapped_refs, &remote_head);\n \t}\n \telse {\n \t\twarning(\"You appear to have cloned an empty repository.\");\n-\t\thead_points_at = NULL;\n-\t\tremote_head = NULL;\n \t\toption_no_checkout = 1;\n \t\tif (!option_bare)\n \t\t\tinstall_branch_config(\"master\", option_origin,\n-- \n1.6.2.rc2.16.gf474c.dirty\n"},{"id":"106741","messageId":"alpine.DEB.1.00.0903030047130.10279@pacific.mpi-cbg.de","threadId":"18097","inReplyTo":"1236031882-2052-1-git-send-email-torarnv@gmail.com","subject":"Re: [PATCH] git-clone: Add option --branch to override initial branch","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-03-02T23:48:56Z","receivedAt":"2009-03-02T23:48:56Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 2 Mar 2009, Tor Arne Vestbø wrote:\n\n> The options --branch and -b allow the user to override the initial \n> branch created and checked out by git-clone. Normally this is the active \n> branch of the remote repository, which is also the fallback if the \n> selected branch is not found.\n\nI do not think that falling back if the selected branch is not found is a \nwise choice.\n\nBesides, the common way to check out something different than the remote's \nHEAD is like this:\n\n\t$ git clone -n $URL\n\t$ cd $DIR\n\t$ git checkout -t origin/$BRANCH\n\nI am undecided if that is good enough, or your patch is needed.\n\nCiao,\nDscho\n"},{"id":"106742","messageId":"7vk577lb5p.fsf@gitster.siamese.dyndns.org","threadId":"18097","inReplyTo":"alpine.DEB.1.00.0903030047130.10279@pacific.mpi-cbg.de","subject":"Re: [PATCH] git-clone: Add option --branch to override initial branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-03T00:09:38Z","receivedAt":"2009-03-03T00:09:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> Hi,\n>\n> On Mon, 2 Mar 2009, Tor Arne Vestbø wrote:\n>\n>> The options --branch and -b allow the user to override the initial \n>> branch created and checked out by git-clone. Normally this is the active \n>> branch of the remote repository, which is also the fallback if the \n>> selected branch is not found.\n>\n> I do not think that falling back if the selected branch is not found is a \n> wise choice.\n>\n> Besides, the common way to check out something different than the remote's \n> HEAD is like this:\n>\n> \t$ git clone -n $URL\n> \t$ cd $DIR\n> \t$ git checkout -t origin/$BRANCH\n>\n> I am undecided if that is good enough, or your patch is needed.\n\nI am fairly negative on this one, if it matters.\n"},{"id":"106743","messageId":"49AC75AF.4080003@gmail.com","threadId":"18097","inReplyTo":"alpine.DEB.1.00.0903030047130.10279@pacific.mpi-cbg.de","subject":"Re: [PATCH] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-03T00:11:27Z","receivedAt":"2009-03-03T00:11:27Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"Johannes Schindelin wrote:\n> On Mon, 2 Mar 2009, Tor Arne Vestbø wrote:\n>> The options --branch and -b allow the user to override the initial \n>> branch created and checked out by git-clone. Normally this is the active \n>> branch of the remote repository, which is also the fallback if the \n>> selected branch is not found.\n> \n> I do not think that falling back if the selected branch is not found is a \n> wise choice.\n\nAh, was not sure what the proper response would be. I'll resubmit with a\ndie() instead.\n\n> Besides, the common way to check out something different than the remote's \n> HEAD is like this:\n> \n> \t$ git clone -n $URL\n> \t$ cd $DIR\n> \t$ git checkout -t origin/$BRANCH\n\nYepp, plus removing the original branch:\n\n $ git branch -D $ORIGINAL_ACTIVE_BRANCH # typically master\n\n> I am undecided if that is good enough, or your patch is needed.\n\nThe idea was to be able to tell someone \"hey, if you want to hack on\nsome feature for next, do the following:\"\n\n $ git clone git://git.kernel.org/pub/scm/git/git.git -b next\n\nMaybe next is not such a good example, since it does not diverge that\nmuch from master and pu, but imagine a repository with a master, plus\nother branches that over time diverge from master (where you would\ntypically use git-new-workdir to have them in a separate working tree).\n\nIn that situation it would be nice to be able to tell someone, hey, if\nyou want to work on this odd branch which is not master, just do -b.\n\nTor Arne\n"},{"id":"106746","messageId":"1236040414-19089-1-git-send-email-torarnv@gmail.com","threadId":"18097","inReplyTo":"alpine.DEB.1.00.0903030047130.10279@pacific.mpi-cbg.de","subject":"[PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-03T00:33:34Z","receivedAt":"2009-03-03T00:33:34Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"The options --branch and -b allow the user to override the initial\nbranch created and checked out by git-clone (normally this is the\nactive branch of the remote repository).\n\nIf the selected branch is not found the operation aborts.\n\nSigned-off-by: Tor Arne Vestbø <torarnv@gmail.com>\n---\n\nSomething like this?\n\nDocumentation/git-clone.txt |    5 +++++\n builtin-clone.c             |   32 ++++++++++++++++++++++++++++----\n 2 files changed, 33 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt\nindex 95f08b9..e7feb4d 100644\n--- a/Documentation/git-clone.txt\n+++ b/Documentation/git-clone.txt\n@@ -119,6 +119,11 @@ then the cloned repository will become corrupt.\n \tInstead of using the remote name 'origin' to keep track\n \tof the upstream repository, use <name> instead.\n \n+--branch <name>::\n+-b <name>::\n+\tInstead of using the remote repository's active branch as the\n+\tinitial branch, use <name> instead.\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 c338910..5fc01ce 100644\n--- a/builtin-clone.c\n+++ b/builtin-clone.c\n@@ -38,6 +38,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@@ -66,6 +67,8 @@ static struct option builtin_clone_options[] = {\n \t\t   \"path to git-upload-pack on the remote\"),\n \tOPT_STRING(0, \"depth\", &option_depth, \"depth\",\n \t\t    \"create a shallow clone of that depth\"),\n+\tOPT_STRING('b', \"branch\", &option_branch, \"branch\",\n+\t\t    \"initial remote branch to check out\"),\n \n \tOPT_END()\n };\n@@ -372,7 +375,9 @@ 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+\tconst struct ref *refs, *mapped_refs;\n+\tconst struct ref *remote_head = NULL;\n+\tconst struct ref *head_points_at = NULL;\n \tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n \tstruct transport *transport = NULL;\n@@ -545,12 +550,31 @@ 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\thead_points_at = locate_head(refs, mapped_refs, &remote_head);\n+\t\tif (option_branch) {\n+\t\t    const int offset = 11;\n+\t\t    const char *branch = option_branch;\n+\t\t    if (!prefixcmp(branch, \"refs/heads/\"))\n+\t\t\tbranch += offset;\n+\n+\t\t    const struct ref *r;\n+\t\t    for (r = mapped_refs; r; r = r->next) {\n+\t\t\tif (!strcmp(r->name + offset, branch)) {\n+\t\t\t    /* Override initial branch */\n+\t\t\t    head_points_at = r;\n+\t\t\t    remote_head = r;\n+\t\t\t    break;\n+\t\t\t}\n+\t\t    }\n+\n+\t\t    if (!head_points_at)\n+\t\t\tdie(\"remote has no branch named '%s'.\", option_branch);\n+\n+\t\t} else {\n+\t\t    head_points_at = locate_head(refs, mapped_refs, &remote_head);\n+\t\t}\n \t}\n \telse {\n \t\twarning(\"You appear to have cloned an empty repository.\");\n-\t\thead_points_at = NULL;\n-\t\tremote_head = NULL;\n \t\toption_no_checkout = 1;\n \t\tif (!option_bare)\n \t\t\tinstall_branch_config(\"master\", option_origin,\n-- \n1.6.2.rc2.17.g2aa38\n"},{"id":"106800","messageId":"alpine.DEB.1.00.0903031004570.6399@intel-tinevez-2-302","threadId":"18097","inReplyTo":"1236040414-19089-1-git-send-email-torarnv@gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-03-03T09:07:31Z","receivedAt":"2009-03-03T09:07:31Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 3 Mar 2009, Tor Arne Vestbø wrote:\n\n> Something like this?\n\nLeaving unnecessary initialization and funny indentation aside for a \nmoment, what about the objection that it might not be necessary?\n\nKeep in mind: your change (as every change) bears the potential to \nintroduce bugs and to complicate the user interface.  The change must be \nworth those risks.\n\nSo could you make a case (if you resubmit a patch, in the commit message, \nplease) why your change is desirable?\n\nThanks,\nDscho"},{"id":"106878","messageId":"49AD5F0D.8000700@gmail.com","threadId":"18097","inReplyTo":"alpine.DEB.1.00.0903031004570.6399@intel-tinevez-2-302","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-03T16:47:09Z","receivedAt":"2009-03-03T16:47:09Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"Johannes Schindelin wrote:\n> Leaving unnecessary initialization and funny indentation aside for a \n> moment,\n\nI do appreciate the feedback though. C is not my primary language, and\nI'm happy to learn from my mistakes :-)\n\n> Keep in mind: your change (as every change) bears the potential to \n> introduce bugs and to complicate the user interface.  The change must be \n> worth those risks.\n\nI fully understand. Here is my rationale for why it's worth the risk:\n\nImagine you have a project called Foo, which has active development on \nthe 'master' branch, and not quite so active development on the more \nstable version branch '1.6' (which v1.6.0 and v1.6.1 was tagged from).\n\nNow, you want to put up info on the project web page / wiki on how to \ncontribute to project Foo. This information is for new contributors -- \nwho may be unfamiliar with git and it's inner workings. You write:\n\n\"To get started contributing to project Foo, please clone using:\n\n   $ git clone git://git.foo.com/project.git\n\n\"\n\nThis looks nice and inviting.\n\nYou also want to provide instructions for those who would like to \ncontribute to the more stable branch of project Foo, 1.6:\n\n\"If you would like to contribute to the stable 1.6 branch, do:\n\n   $ git clone -n git://git.foo.com/project.git\n   $ cd project\n   $ git checkout -t origin/1.6\n   $ git branch -D master\n\n\"\n\nWhich is not so nice and inviting. At least not compared to:\n\n\"If you would like to contribute to the stable 1.6 branch, do:\n\n   $ git clone git://git.foo.com/project.git --branch 1.6\n\n\"\n\nRemember these are new contributors, unfamiliar with git. Presenting \nthem with a list of four commands that have to be run to get started \n(commands which incidentally also are the first-ones new users mix up), \nis not ideal. \"What does -n do?\", \"What does -t do?\", \"What's a tracking\nbranch?\", \"Origin? What's that?\", \"What does -D do?\", \"Delete?! Will I \ndelete the main development line!?\", etc.. :)\n\nAlso, remember that these commands are not something that can be \nscripted or put into an alias, because these users have not cloned \nanything yet.\n\nI know Subversions is perhaps not the best ideal, but to contrast:\n\n   $ svn import http://svn.foo.bar/project/trunk\n   $ svn import http://svn.foo.bar/project/branches/1.6\n\nEasy to get to a different branch without having to dive into the full \nfeature set of the SCM.\n\nSo, to conclude, I see this as a usability-feature of git-clone, which \noutweighs the possible risk of introducing new bugs. It's not a feature \nI will personally use that often, but it's one that I think new users \nwill appreciate.\n\n\nTor Arne\n"},{"id":"106880","messageId":"7vr61eblde.fsf@gitster.siamese.dyndns.org","threadId":"18097","inReplyTo":"49AD5F0D.8000700@gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-03T16:51:25Z","receivedAt":"2009-03-03T16:51:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tor Arne Vestbø <torarnv@gmail.com> writes:\n\n> If you would like to contribute to the stable 1.6 branch, do:\n>   $ git clone -n git://git.foo.com/project.git\n>   $ cd project\n>   $ git checkout -t origin/1.6\n>   $ git branch -D master\n> Which is not so nice and inviting.\n\nIf you are working on 1.6 maintenance track, why discard 'master'?  If the\nupstream project calls it 1.6, you can call your fork 1.6 and keep that\nchecked out.\n\nIOW, _you_ are make it not nice.\n"},{"id":"106883","messageId":"49AD6305.8040909@gmail.com","threadId":"18097","inReplyTo":"7vr61eblde.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-03T17:04:05Z","receivedAt":"2009-03-03T17:04:05Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"Junio C Hamano wrote:\n> Tor Arne Vestbø <torarnv@gmail.com> writes:\n> \n>> If you would like to contribute to the stable 1.6 branch, do:\n>>   $ git clone -n git://git.foo.com/project.git\n>>   $ cd project\n>>   $ git checkout -t origin/1.6\n>>   $ git branch -D master\n>> Which is not so nice and inviting.\n> \n> If you are working on 1.6 maintenance track, why discard 'master'?\n\nOne example I can think of is if master is moving a lot faster than the \nmaintenance track, and you are not interested in master.\n\n[box:/tmp/downstream] $ git branch\n* 1.6\n   master\n\n[box:/tmp/downstream] $ git pull --rebase\nCurrent branch 1.6 is up to date.\n\n[box:/tmp/downstream] $ git push\nTo file:///tmp/upstream\n  ! [rejected]        master -> master (non-fast forward)\nerror: failed to push some refs to 'file:///tmp/upstream'\n\nIn that case you would either have to ff master all the time (requiring \na checkout or rebase magic), or do an explicit \"git push origin 1.6\".\n\nNeither good options when you are trying to teach people that git push \nis the way you submit changes.\n\nTor Arne\n"},{"id":"106884","messageId":"7vmyc2bkmc.fsf@gitster.siamese.dyndns.org","threadId":"18097","inReplyTo":"49AD6305.8040909@gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-03T17:07:39Z","receivedAt":"2009-03-03T17:07:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tor Arne Vestbø <torarnv@gmail.com> writes:\n\n> In that case you would either have to ff master all the time\n> (requiring a checkout or rebase magic), or do an explicit \"git push\n> origin 1.6\".\n\nor do something like:\n\n$ cat >>.git/config <<\\EOF\n[remote \"there\"]\n    push = HEAD\nEOF\n\njust once.\n"},{"id":"106948","messageId":"7vbpsh93q5.fsf@gitster.siamese.dyndns.org","threadId":"18097","inReplyTo":"1236040414-19089-1-git-send-email-torarnv@gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-04T06:55:30Z","receivedAt":"2009-03-04T06:55:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tor Arne Vestbø <torarnv@gmail.com> writes:\n\n> The options --branch and -b allow the user to override the initial\n> branch created and checked out by git-clone (normally this is the\n> active branch of the remote repository).\n>\n> If the selected branch is not found the operation aborts.\n>\n> Signed-off-by: Tor Arne Vestbø <torarnv@gmail.com>\n\nThe semantics and desirability of the new feature have been already\ndiscussed, and I am not convinced that it is necessary, in the sense that\nI do not think I likely ever use this myself, but I am just one of git\nusers so that is not a strong basis for rejection.\n\nI'll let others discuss more about the design issues, and will only talk\nabout code in this message.\n\n> diff --git a/builtin-clone.c b/builtin-clone.c\n> index c338910..5fc01ce 100644\n> --- a/builtin-clone.c\n> +++ b/builtin-clone.c\n> @@ -38,6 +38,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\nI see this was copied from the line immediately above, but please do not\ninitialize static variables to 0 or NULL.  BSS will take care of it.\n\n> @@ -372,7 +375,9 @@ 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> +\tconst struct ref *refs, *mapped_refs;\n> +\tconst struct ref *remote_head = NULL;\n> +\tconst struct ref *head_points_at = NULL;\n>  \tstruct strbuf key = STRBUF_INIT, value = STRBUF_INIT;\n>  \tstruct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;\n>  \tstruct transport *transport = NULL;\n> @@ -545,12 +550,31 @@ 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\thead_points_at = locate_head(refs, mapped_refs, &remote_head);\n> +\t\tif (option_branch) {\n> +\t\t    const int offset = 11;\n> +\t\t    const char *branch = option_branch;\n\nOne indent level in git code equals a HT, i.e. 8 places.\n\n> +\t\t    if (!prefixcmp(branch, \"refs/heads/\"))\n> +\t\t\tbranch += offset;\n\nI suspect that you are trying to protect your code against somebody\nmiscounting the length of \"refs/heads/\" (perhaps when updating this\ncodepath in git version 47 that keeps local branches somewhere else, such\nas \"refs/local-heads/\"), but this \"const int offset\" does not buy you\nanything.  He will likely to leave \"offset\" to 11 just the same.\n\nIt is a different story if it were done like this:\n\n\t\tstatic const char heads_prefix[] = \"refs/heads/\";\n                if (!prefixcmp(branch, heads_prefix))\n                \tbranch += strlen(heads_prefix);\n\nto let the compiler notice heads_prefix is a constant and optimize the\nstrlen() out, but I personally think it is overkill.\n\n> +\t\t    const struct ref *r;\n\nWe do not tolerate decl-after-statement.\n\n> +\t\t    for (r = mapped_refs; r; r = r->next) {\n> +\t\t\tif (!strcmp(r->name + offset, branch)) {\n> +\t\t\t    /* Override initial branch */\n> +\t\t\t    head_points_at = r;\n> +\t\t\t    remote_head = r;\n> +\t\t\t    break;\n> +\t\t\t}\n> +\t\t    }\n\nThis duplicates major part of what locate_head() does but with a different\ntarget other than \"master\", doesn't it?\n\nYou would want to refactor this, but I think 'next/pu' already has some\nrefactoring of the locate_head() logic, so you may want to look at it and\neither build your changes on top of it, or wait until that other topic to\nstabilize.\n\n> +\t\t    if (!head_points_at)\n> +\t\t\tdie(\"remote has no branch named '%s'.\", option_branch);\n> +\n> +\t\t} else {\n> +\t\t    head_points_at = locate_head(refs, mapped_refs, &remote_head);\n> +\t\t}\n\nThis falls into more personal taste than coding guideline, but it often is\neasier to read to arrange your code:\n\n\tif (... condition ...) {\n        \tshorter codepath\n\t} else {\n        \tmuch\n                longer\n                code\n                path\n\t}\n\nFor one thing, it is much easier to miss a short \"else\" clause hanging at\nthe end of loooong \"if\" part.\n"},{"id":"106953","messageId":"alpine.DEB.1.00.0903040953080.8549@intel-tinevez-2-302","threadId":"18097","inReplyTo":"7vbpsh93q5.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-03-04T08:56:03Z","receivedAt":"2009-03-04T08:56:03Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 3 Mar 2009, Junio C Hamano wrote:\n\n> Tor Arne Vestbø <torarnv@gmail.com> writes:\n> \n> > +\t\t    if (!prefixcmp(branch, \"refs/heads/\"))\n> > +\t\t\tbranch += offset;\n> \n> I suspect that you are trying to protect your code against somebody\n> miscounting the length of \"refs/heads/\" (perhaps when updating this\n> codepath in git version 47 that keeps local branches somewhere else, such\n> as \"refs/local-heads/\"), but this \"const int offset\" does not buy you\n> anything.  He will likely to leave \"offset\" to 11 just the same.\n> \n> It is a different story if it were done like this:\n> \n> \t\tstatic const char heads_prefix[] = \"refs/heads/\";\n>                 if (!prefixcmp(branch, heads_prefix))\n>                 \tbranch += strlen(heads_prefix);\n> \n> to let the compiler notice heads_prefix is a constant and optimize the\n> strlen() out, but I personally think it is overkill.\n\nOf course you could also do this instead (which I personally think would \nnot be overkill):\n\n\t\tbranch = skip_prefix(branch, \"refs/heads/\");\n\nCiao,\nDscho"},{"id":"106961","messageId":"49AE56BC.2000809@gmail.com","threadId":"18097","inReplyTo":"7vbpsh93q5.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Tor Arne Vestbø","fromEmail":"torarnv@gmail.com","sentAt":"2009-03-04T10:23:56Z","receivedAt":"2009-03-04T10:23:56Z","isPatch":true,"sender":{"key":"torarnv@gmail.com","avatar":"https://gravatar.com/avatar/847af0150a5a28ad86d9db1556a48aba04e7642c6a29a5a767f8c2bdf1720321?d=mp&s=160"},"body":"Junio C Hamano wrote:\n> I'll let others discuss more about the design issues, and will only talk\n> about code in this message.\n\n[...snip...]\n\nGreat feedback, much appreciated! :) I'll work up a new patch as soon as \nI have some free cycles. Thanks!\n\nTor Arne\n"},{"id":"107459","messageId":"loom.20090309T143413-334@post.gmane.org","threadId":"18097","inReplyTo":"7vbpsh93q5.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Paolo Ciarrocchi","fromEmail":"paolo.ciarrocchi@gmail.com","sentAt":"2009-03-09T14:39:54Z","receivedAt":"2009-03-09T14:39:54Z","isPatch":true,"sender":{"key":"paolo.ciarrocchi@gmail.com","avatar":null},"body":"Junio C Hamano <gitster <at> pobox.com> writes:\n\n> \n> Tor Arne Vestbø <torarnv <at> gmail.com> writes:\n> \n> > The options --branch and -b allow the user to override the initial\n> > branch created and checked out by git-clone (normally this is the\n> > active branch of the remote repository).\n> >\n> > If the selected branch is not found the operation aborts.\n> >\n> > Signed-off-by: Tor Arne Vestbø <torarnv <at> gmail.com>\n> \n> The semantics and desirability of the new feature have been already\n> discussed, and I am not convinced that it is necessary, in the sense that\n> I do not think I likely ever use this myself, but I am just one of git\n> users so that is not a strong basis for rejection.\n\nI wrote a comment about the --branch approach a couple of days ago, dunno why\nbut this thread never reached my inbox (replying via gmame web interface).\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/112527\n\nAs I wrote in my post a friend of mine, new to git, was looking for the\npossibility of cloning a repo and automatically checkout a specific branch.\n\nRegards,\n           Paolo\n"},{"id":"107471","messageId":"94a0d4530903090901o6e8c2a40k676387f90ee461b3@mail.gmail.com","threadId":"18097","inReplyTo":"loom.20090309T143413-334@post.gmane.org","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2009-03-09T16:01:54Z","receivedAt":"2009-03-09T16:01:54Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"On Mon, Mar 9, 2009 at 4:39 PM, Paolo Ciarrocchi\n<paolo.ciarrocchi@gmail.com> wrote:\n> Junio C Hamano <gitster <at> pobox.com> writes:\n>\n>>\n>> Tor Arne Vestbø <torarnv <at> gmail.com> writes:\n>>\n>> > The options --branch and -b allow the user to override the initial\n>> > branch created and checked out by git-clone (normally this is the\n>> > active branch of the remote repository).\n>> >\n>> > If the selected branch is not found the operation aborts.\n>> >\n>> > Signed-off-by: Tor Arne Vestbø <torarnv <at> gmail.com>\n>>\n>> The semantics and desirability of the new feature have been already\n>> discussed, and I am not convinced that it is necessary, in the sense that\n>> I do not think I likely ever use this myself, but I am just one of git\n>> users so that is not a strong basis for rejection.\n>\n> I wrote a comment about the --branch approach a couple of days ago, dunno why\n> but this thread never reached my inbox (replying via gmame web interface).\n>\n> http://thread.gmane.org/gmane.comp.version-control.git/112527\n>\n> As I wrote in my post a friend of mine, new to git, was looking for the\n> possibility of cloning a repo and automatically checkout a specific branch.\n\nYeah, I also would like this option... one-liner for people that don't\nknow git at all.\n\nme: you want my code? just run this command.\n\n-- \nFelipe Contreras\n"},{"id":"107653","messageId":"4d8e3fd30903110152m1b52de30ge630d6b9f6a4c7fe@mail.gmail.com","threadId":"18097","inReplyTo":"94a0d4530903090901o6e8c2a40k676387f90ee461b3@mail.gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Paolo Ciarrocchi","fromEmail":"paolo.ciarrocchi@gmail.com","sentAt":"2009-03-11T08:52:28Z","receivedAt":"2009-03-11T08:52:28Z","isPatch":true,"sender":{"key":"paolo.ciarrocchi@gmail.com","avatar":null},"body":"[restored the CC list]\n\nOn Mon, Mar 9, 2009 at 5:01 PM, Felipe Contreras\n<felipe.contreras@gmail.com> wrote:\n> On Mon, Mar 9, 2009 at 4:39 PM, Paolo Ciarrocchi\n> <paolo.ciarrocchi@gmail.com> wrote:\n[...]\n>> I wrote a comment about the --branch approach a couple of days ago, dunno why\n>> but this thread never reached my inbox (replying via gmame web interface).\n>>\n>> http://thread.gmane.org/gmane.comp.version-control.git/112527\n>>\n>> As I wrote in my post a friend of mine, new to git, was looking for the\n>> possibility of cloning a repo and automatically checkout a specific branch.\n>\n> Yeah, I also would like this option... one-liner for people that don't\n> know git at all.\n>\n> me: you want my code? just run this command.\n\nYes, that is what my friend was lookin for.\n\nI told him to use the following procedure:\n$ git clone -n URL\n$ git checkout -b foo origin/bar\n\nHe is now an almost happy git user :-).\n\nThat being said, I see the following command as an improvement over\nthe actual GIT UI:\n\n $ git clone git://URI -b bar\n\n\nCiao,\n-- \nPaolo\nhttp://paolo.ciarrocchi.googlepages.com/\nhttp://mypage.vodafone.it/\n"},{"id":"107761","messageId":"buoocw7s79y.fsf@dhlpc061.dev.necel.com","threadId":"18097","inReplyTo":"4d8e3fd30903110152m1b52de30ge630d6b9f6a4c7fe@mail.gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Miles Bader","fromEmail":"miles@gnu.org","sentAt":"2009-03-12T04:18:49Z","receivedAt":"2009-03-12T04:18:49Z","isPatch":true,"sender":{"key":"miles@gnu.org","avatar":"https://gravatar.com/avatar/01069b69593af7bff28e2f97afeb3644ae6fe2f5f56cb3a8cf34c5fb8c36efe5?d=mp&s=160"},"body":"Paolo Ciarrocchi <paolo.ciarrocchi@gmail.com> writes:\n> $ git clone -n URL\n> $ git checkout -b foo origin/bar\n>\n> That being said, I see the following command as an improvement over\n> the actual GIT UI:\n>\n>  $ git clone git://URI -b bar\n\nNote that in your original advice, foo and bar can be different, and\nit's not clear to me what \"-b bar\" should do...\n\nPersonally I frequently use foo == bar (no local master branch), but I\nthink another common pattern is foo != bar, but foo or bar == \"master\".\n\nMaybe a syntax similar to push, like \"-b LOCAL_BR:REMOTE_BR\",\nwith \"-b BR\" being shorthand for \"-b BR:BR\"?\n\n-Miles\n\n-- \nHappiness, n. An agreeable sensation arising from contemplating the misery of\nanother.\n"},{"id":"107781","messageId":"4d8e3fd30903120148u52164fe3offe665bf70ef6d8d@mail.gmail.com","threadId":"18097","inReplyTo":"buoocw7s79y.fsf@dhlpc061.dev.necel.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Paolo Ciarrocchi","fromEmail":"paolo.ciarrocchi@gmail.com","sentAt":"2009-03-12T08:48:29Z","receivedAt":"2009-03-12T08:48:29Z","isPatch":true,"sender":{"key":"paolo.ciarrocchi@gmail.com","avatar":null},"body":"On Thu, Mar 12, 2009 at 5:18 AM, Miles Bader <miles@gnu.org> wrote:\n> Paolo Ciarrocchi <paolo.ciarrocchi@gmail.com> writes:\n>> $ git clone -n URL\n>> $ git checkout -b foo origin/bar\n>>\n>> That being said, I see the following command as an improvement over\n>> the actual GIT UI:\n>>\n>>  $ git clone git://URI -b bar\n>\n> Note that in your original advice, foo and bar can be different, and\n> it's not clear to me what \"-b bar\" should do...\n>\n> Personally I frequently use foo == bar (no local master branch), but I\n> think another common pattern is foo != bar, but foo or bar == \"master\".\n>\n> Maybe a syntax similar to push, like \"-b LOCAL_BR:REMOTE_BR\",\n> with \"-b BR\" being shorthand for \"-b BR:BR\"?\n\nYes, makes sense.\n\nCiao,\n-- \nPaolo\nhttp://paolo.ciarrocchi.googlepages.com/\nhttp://mypage.vodafone.it/\n"},{"id":"107786","messageId":"94a0d4530903120212m360c1db8y763b69bc8610ae64@mail.gmail.com","threadId":"18097","inReplyTo":"4d8e3fd30903120148u52164fe3offe665bf70ef6d8d@mail.gmail.com","subject":"Re: [PATCH v2] git-clone: Add option --branch to override initial branch","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2009-03-12T09:12:16Z","receivedAt":"2009-03-12T09:12:16Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"On Thu, Mar 12, 2009 at 10:48 AM, Paolo Ciarrocchi\n<paolo.ciarrocchi@gmail.com> wrote:\n> On Thu, Mar 12, 2009 at 5:18 AM, Miles Bader <miles@gnu.org> wrote:\n>> Paolo Ciarrocchi <paolo.ciarrocchi@gmail.com> writes:\n>>> $ git clone -n URL\n>>> $ git checkout -b foo origin/bar\n>>>\n>>> That being said, I see the following command as an improvement over\n>>> the actual GIT UI:\n>>>\n>>>  $ git clone git://URI -b bar\n>>\n>> Note that in your original advice, foo and bar can be different, and\n>> it's not clear to me what \"-b bar\" should do...\n>>\n>> Personally I frequently use foo == bar (no local master branch), but I\n>> think another common pattern is foo != bar, but foo or bar == \"master\".\n>>\n>> Maybe a syntax similar to push, like \"-b LOCAL_BR:REMOTE_BR\",\n>> with \"-b BR\" being shorthand for \"-b BR:BR\"?\n>\n> Yes, makes sense.\n\n+1\n\n-- \nFelipe Contreras\n"}]}