git-clone: Add option --branch to override initial branch
PATCH 1/1 git-clone: Add option --branch to override initial branch
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone. Normally this is the active branch of the remote repository, which is also the fallback if the selected branch is not found.
--- Documentation/git-clone.txt | 5 +++++ builtin-clone.c | 33 +++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-)
Documentation/git-clone.txt +5 -0
@@ -119,6 +119,11 @@ then the cloned repository will become corrupt. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name> instead. +--branch <name>:: +-b <name>:: + Instead of using the remote repository's active branch as the + initial branch, use <name> instead. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +29 -4
@@ -38,6 +38,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;static int option_local, option_no_hardlinks, option_shared;static char *option_template, *option_reference, *option_depth;static char *option_origin =NULL ; +static char *option_branch =NULL ;static char *option_upload_pack ="git-upload-pack" ;static int option_verbose;
@@ -66,6 +67,8 @@ static struct option builtin_clone_options[] = { "path to git-upload-pack on the remote"),OPT_STRING (0 ,"depth" , &option_depth,"depth" ,"create a shallow clone of that depth" ), +OPT_STRING ('b' ,"branch" , &option_branch,"branch" , +"initial remote branch to check out" ), OPT_END() };
@@ -372,7 +375,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)const char *repo_name, *repo, *work_tree, *git_dir;char *path, *dir;int dest_exists; -const struct ref *refs, *head_points_at, *remote_head, *mapped_refs; +const struct ref *refs, *mapped_refs; +const struct ref *remote_head =NULL ; +const struct ref *head_points_at =NULL ;struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;
@@ -545,12 +550,32 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, &refspec, reflog_msg.buf); - head_points_at =locate_head (refs, mapped_refs, &remote_head); +if (option_branch) { +const int offset =11 ; +const char *branch = option_branch; +if (!prefixcmp (branch,"refs/heads/" )) + branch += offset; + +const struct ref *r; +for (r = mapped_refs; r; r = r->next) { +if (!strcmp (r->name + offset, branch)) { +/* Override initial branch */ + head_points_at = r; + remote_head = r; +break ; + } + } + +if (!head_points_at) +warning ("remote has no branch named '%s', " +"falling back to default." , option_branch); + } + +if (!head_points_at) + head_points_at =locate_head (refs, mapped_refs, &remote_head); }else {warning ("You appear to have cloned an empty repository." ); - head_points_at =NULL ; - remote_head =NULL ; option_no_checkout =1 ;if (!option_bare) install_branch_config("master", option_origin,
Signature · 1 line
1.6.2.rc2.16.gf474c.dirty
Hi,
Quoted 4 lines · Tor Arne Vestbø
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone. Normally this is the active branch of the remote repository, which is also the fallback if the selected branch is not found.
I do not think that falling back if the selected branch is not found is a wise choice.
Besides, the common way to check out something different than the remote's HEAD is like this:
$ git clone -n $URL $ cd $DIR $ git checkout -t origin/$BRANCH
I am undecided if that is good enough, or your patch is needed.
Ciao,
Dscho
PATCH v2 1/1 git-clone: Add option --branch to override initial branch
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone (normally this is the active branch of the remote repository).
If the selected branch is not found the operation aborts.
---
Something like this?
Documentation/git-clone.txt | 5 +++++
builtin-clone.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-)
Documentation/git-clone.txt +5 -0
@@ -119,6 +119,11 @@ then the cloned repository will become corrupt. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name> instead. +--branch <name>:: +-b <name>:: + Instead of using the remote repository's active branch as the + initial branch, use <name> instead. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +28 -4
@@ -38,6 +38,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;static int option_local, option_no_hardlinks, option_shared;static char *option_template, *option_reference, *option_depth;static char *option_origin =NULL ; +static char *option_branch =NULL ;static char *option_upload_pack ="git-upload-pack" ;static int option_verbose;
@@ -66,6 +67,8 @@ static struct option builtin_clone_options[] = { "path to git-upload-pack on the remote"),OPT_STRING (0 ,"depth" , &option_depth,"depth" ,"create a shallow clone of that depth" ), +OPT_STRING ('b' ,"branch" , &option_branch,"branch" , +"initial remote branch to check out" ), OPT_END() };
@@ -372,7 +375,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)const char *repo_name, *repo, *work_tree, *git_dir;char *path, *dir;int dest_exists; -const struct ref *refs, *head_points_at, *remote_head, *mapped_refs; +const struct ref *refs, *mapped_refs; +const struct ref *remote_head =NULL ; +const struct ref *head_points_at =NULL ;struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;
@@ -545,12 +550,31 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, &refspec, reflog_msg.buf); - head_points_at =locate_head (refs, mapped_refs, &remote_head); +if (option_branch) { +const int offset =11 ; +const char *branch = option_branch; +if (!prefixcmp (branch,"refs/heads/" )) + branch += offset; + +const struct ref *r; +for (r = mapped_refs; r; r = r->next) { +if (!strcmp (r->name + offset, branch)) { +/* Override initial branch */ + head_points_at = r; + remote_head = r; +break ; + } + } + +if (!head_points_at) +die ("remote has no branch named '%s'." , option_branch); + + }else { + head_points_at =locate_head (refs, mapped_refs, &remote_head); + } }else {warning ("You appear to have cloned an empty repository." ); - head_points_at =NULL ; - remote_head =NULL ; option_no_checkout =1 ;if (!option_bare) install_branch_config("master", option_origin,
Signature · 1 line
1.6.2.rc2.17.g2aa38
Quoted 7 lines · Tor Arne Vestbø
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone (normally this is the active branch of the remote repository).
If the selected branch is not found the operation aborts.
Signed-off-by Tor Arne Vestbø <[email protected]>
The semantics and desirability of the new feature have been already discussed, and I am not convinced that it is necessary, in the sense that I do not think I likely ever use this myself, but I am just one of git users so that is not a strong basis for rejection.
I'll let others discuss more about the design issues, and will only talk about code in this message.
builtin-clone.c static int option_quiet, option_no_checkout, option_bare, option_mirror; · line 41
Quoted 9 lines
builtin-clone.c +1
-0@@ -38,6 +38,7 @@ static int option_quiet, option_no_checkout, option_bare, option_mirror;static int option_local, option_no_hardlinks, option_shared;static char *option_template, *option_reference, *option_depth;static char *option_origin =NULL ; +static char *option_branch =NULL ;
I see this was copied from the line immediately above, but please do not initialize static variables to 0 or NULL. BSS will take care of it.
Quoted 12 lines
builtin-clone.c +3
-1@@ -372,7 +375,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)const char *repo_name, *repo, *work_tree, *git_dir;char *path, *dir;int dest_exists; -const struct ref *refs, *head_points_at, *remote_head, *mapped_refs; +const struct ref *refs, *mapped_refs; +const struct ref *remote_head =NULL ; +const struct ref *head_points_at =NULL ;struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 555
Quoted 7 lines
builtin-clone.c +3
-1@@ -545,12 +550,31 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, &refspec, reflog_msg.buf); - head_points_at =locate_head (refs, mapped_refs, &remote_head); +if (option_branch) { +const int offset =11 ; +const char *branch = option_branch;
One indent level in git code equals a HT, i.e. 8 places.
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 557
Quoted 2 lines
builtin-clone.c +2
-0+if (!prefixcmp (branch,"refs/heads/" )) + branch += offset;
I suspect that you are trying to protect your code against somebody miscounting the length of "refs/heads/" (perhaps when updating this codepath in git version 47 that keeps local branches somewhere else, such as "refs/local-heads/"), but this "const int offset" does not buy you anything. He will likely to leave "offset" to 11 just the same.
It is a different story if it were done like this:
static const char heads_prefix[] = "refs/heads/";
if (!prefixcmp(branch, heads_prefix))
branch += strlen(heads_prefix);to let the compiler notice heads_prefix is a constant and optimize the strlen() out, but I personally think it is overkill.
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 559
Quoted 1 line
builtin-clone.c +1
-0+const struct ref *r;
We do not tolerate decl-after-statement.
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 567
Quoted 8 lines
builtin-clone.c +8
-0+for (r = mapped_refs; r; r = r->next) { +if (!strcmp (r->name + offset, branch)) { +/* Override initial branch */ + head_points_at = r; + remote_head = r; +break ; + } + }
This duplicates major part of what locate_head() does but with a different target other than "master", doesn't it?
You would want to refactor this, but I think 'next/pu' already has some refactoring of the locate_head() logic, so you may want to look at it and either build your changes on top of it, or wait until that other topic to stabilize.
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 574
Quoted 6 lines
builtin-clone.c +6
-0+if (!head_points_at) +die ("remote has no branch named '%s'." , option_branch); + + }else { + head_points_at =locate_head (refs, mapped_refs, &remote_head); + }
This falls into more personal taste than coding guideline, but it often is easier to read to arrange your code:
if (... condition ...) {
shorter codepath
} else {
much
longer
code
path
}For one thing, it is much easier to miss a short "else" clause hanging at the end of loooong "if" part.
Quoted 2 lines · Junio C Hamano
I'll let others discuss more about the design issues, and will only talk about code in this message.
[...snip...]
Great feedback, much appreciated! :) I'll work up a new patch as soon as I have some free cycles. Thanks!
Tor Arne
Hi,
Quoted 11 lines · Junio C Hamano
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 557
Quoted 2 lines · Tor Arne Vestbø
builtin-clone.c +2
-0+if (!prefixcmp (branch,"refs/heads/" )) + branch += offset;I suspect that you are trying to protect your code against somebody miscounting the length of "refs/heads/" (perhaps when updating this codepath in git version 47 that keeps local branches somewhere else, such as "refs/local-heads/"), but this "const int offset" does not buy you anything. He will likely to leave "offset" to 11 just the same.
Quoted 8 lines
It is a different story if it were done like this:
static const char heads_prefix[] = "refs/heads/"; if (!prefixcmp(branch, heads_prefix)) branch += strlen(heads_prefix);to let the compiler notice
heads_prefixis a constant and optimize thestrlen()out, but I personally think it is overkill.
Of course you could also do this instead (which I personally think would not be overkill):
branch = skip_prefix(branch, "refs/heads/");
Ciao,
Dscho
Quoted 7 lines · Junio C Hamano <gitster <at> pobox.com> writes:
Quoted 4 lines · Tor Arne Vestbø <torarnv <at> gmail.com> writes:
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone (normally this is the active branch of the remote repository).
Quoted 8 lines
Quoted 3 lines
If the selected branch is not found the operation aborts.
Signed-off-by Tor Arne Vestbø <torarnv <at> gmail.com>The semantics and desirability of the new feature have been already discussed, and I am not convinced that it is necessary, in the sense that I do not think I likely ever use this myself, but I am just one of git users so that is not a strong basis for rejection.
I wrote a comment about the --branch approach a couple of days ago, dunno why but this thread never reached my inbox (replying via gmame web interface).
http://thread.gmane.org/gmane.comp.version-control.git/112527
As I wrote in my post a friend of mine, new to git, was looking for the possibility of cloning a repo and automatically checkout a specific branch.
Regards,
Paolo
Quoted 18 lines · Paolo Ciarrocchi
Quoted 7 lines · Junio C Hamano <gitster <at> pobox.com> writes:
Quoted 4 lines · Tor Arne Vestbø <torarnv <at> gmail.com> writes:
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone (normally this is the active branch of the remote repository).
Quoted 8 lines
Quoted 3 lines
If the selected branch is not found the operation aborts.
Signed-off-by Tor Arne Vestbø <torarnv <at> gmail.com>The semantics and desirability of the new feature have been already discussed, and I am not convinced that it is necessary, in the sense that I do not think I likely ever use this myself, but I am just one of git users so that is not a strong basis for rejection.
Quoted 7 lines
I wrote a comment about the --branch approach a couple of days ago, dunno why but this thread never reached my inbox (replying via gmame web interface).
http://thread.gmane.org/gmane.comp.version-control.git/112527
As I wrote in my post a friend of mine, new to git, was looking for the possibility of cloning a repo and automatically checkout a specific branch.
Yeah, I also would like this option... one-liner for people that don't know git at all.
me: you want my code? just run this command.
Signature · 1 line
Felipe Contreras
[restored the CC list]
Quoted 2 lines · Felipe Contreras
On Mon, Mar 9, 2009 at 4:39 PM, Paolo Ciarrocchi
<[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
I wrote a comment about the --branch approach a couple of days ago, dunno why but this thread never reached my inbox (replying via gmame web interface).
http://thread.gmane.org/gmane.comp.version-control.git/112527
Quoted 7 lines
Quoted 2 lines
As I wrote in my post a friend of mine, new to git, was looking for the possibility of cloning a repo and automatically checkout a specific branch.
Yeah, I also would like this option... one-liner for people that don't know git at all.
me: you want my code? just run this command.
Yes, that is what my friend was lookin for.
I told him to use the following procedure:
$ git clone -n URL $ git checkout -b foo origin/bar
He is now an almost happy git user :-).
That being said, I see the following command as an improvement over the actual GIT UI:
$ git clone git://URI -b bar
Ciao,
Signature · 3 lines
Paolo http://paolo.ciarrocchi.googlepages.com/ http://mypage.vodafone.it/
Quoted 7 lines · Paolo Ciarrocchi
$ git clone -n URL $ git checkout -b foo origin/barThat being said, I see the following command as an improvement over the actual GIT UI:
$ git clone git://URI -b bar
Note that in your original advice, foo and bar can be different, and it's not clear to me what "-b bar" should do...
Personally I frequently use foo == bar (no local master branch), but I think another common pattern is foo != bar, but foo or bar == "master".
Maybe a syntax similar to push, like "-b LOCAL_BR:REMOTE_BR", with "-b BR" being shorthand for "-b BR:BR"?
-Miles
Signature · 2 lines
Happiness, n. An agreeable sensation arising from contemplating the misery of another.
Quoted 9 lines · Miles Bader
Quoted 7 lines · Paolo Ciarrocchi
$ git clone -n URL $ git checkout -b foo origin/barThat being said, I see the following command as an improvement over the actual GIT UI:
$ git clone git://URI -b bar
Quoted 8 lines
Note that in your original advice, foo and bar can be different, and it's not clear to me what "-b bar" should do...
Personally I frequently use foo == bar (no local master branch), but I think another common pattern is foo != bar, but foo or bar == "master".
Maybe a syntax similar to push, like "-b LOCAL_BR:REMOTE_BR", with "-b BR" being shorthand for "-b BR:BR"?
Yes, makes sense.
Ciao,
Signature · 3 lines
Paolo http://paolo.ciarrocchi.googlepages.com/ http://mypage.vodafone.it/
Quoted 13 lines · Paolo Ciarrocchi
Quoted 12 lines · Miles Bader
Quoted 7 lines · Paolo Ciarrocchi
$ git clone -n URL $ git checkout -b foo origin/barThat being said, I see the following command as an improvement over the actual GIT UI:
$ git clone git://URI -b barNote that in your original advice, foo and bar can be different, and it's not clear to me what "-b bar" should do...
Quoted 7 lines
Quoted 5 lines
Personally I frequently use foo == bar (no local master branch), but I think another common pattern is foo != bar, but foo or bar == "master".
Maybe a syntax similar to push, like "-b LOCAL_BR:REMOTE_BR", with "-b BR" being shorthand for "-b BR:BR"?
Yes, makes sense.
+1
Signature · 1 line
Felipe Contreras
Hi,
Quoted 1 line · Tor Arne Vestbø
Something like this?
Leaving unnecessary initialization and funny indentation aside for a moment, what about the objection that it might not be necessary?
Keep in mind: your change (as every change) bears the potential to introduce bugs and to complicate the user interface. The change must be worth those risks.
So could you make a case (if you resubmit a patch, in the commit message, please) why your change is desirable?
Thanks,
Dscho
Quoted 1 line · Johannes Schindelin
Leaving unnecessary initialization and funny indentation aside for a moment,
I do appreciate the feedback though. C is not my primary language, and I'm happy to learn from my mistakes :-)
Quoted 1 line
Keep in mind: your change (as every change) bears the potential to introduce bugs and to complicate the user interface. The change must be worth those risks.
I fully understand. Here is my rationale for why it's worth the risk:
Imagine you have a project called Foo, which has active development on the 'master' branch, and not quite so active development on the more stable version branch '1.6' (which v1.6.0 and v1.6.1 was tagged from).
Now, you want to put up info on the project web page / wiki on how to contribute to project Foo. This information is for new contributors -- who may be unfamiliar with git and it's inner workings. You write:
"To get started contributing to project Foo, please clone using:
$ git clone git://git.foo.com/project.git
"
This looks nice and inviting.
You also want to provide instructions for those who would like to contribute to the more stable branch of project Foo, 1.6:
"If you would like to contribute to the stable 1.6 branch, do:
$ git clone -n git://git.foo.com/project.git $ cd project $ git checkout -t origin/1.6 $ git branch -D master
"
Which is not so nice and inviting. At least not compared to:
"If you would like to contribute to the stable 1.6 branch, do:
$ git clone git://git.foo.com/project.git --branch 1.6
"
Remember these are new contributors, unfamiliar with git. Presenting them with a list of four commands that have to be run to get started (commands which incidentally also are the first-ones new users mix up), is not ideal. "What does -n do?", "What does -t do?", "What's a tracking branch?", "Origin? What's that?", "What does -D do?", "Delete?! Will I delete the main development line!?", etc.. :)
Also, remember that these commands are not something that can be scripted or put into an alias, because these users have not cloned anything yet.
I know Subversions is perhaps not the best ideal, but to contrast:
$ svn import http://svn.foo.bar/project/trunk $ svn import http://svn.foo.bar/project/branches/1.6
Easy to get to a different branch without having to dive into the full feature set of the SCM.
So, to conclude, I see this as a usability-feature of git-clone, which outweighs the possible risk of introducing new bugs. It's not a feature I will personally use that often, but it's one that I think new users will appreciate.
Tor Arne
Quoted 6 lines · Tor Arne Vestbø
If you would like to contribute to the stable 1.6 branch, do:
$ git clone -n git://git.foo.com/project.git $ cd project $ git checkout -t origin/1.6 $ git branch -D master Which is not so nice and inviting.
If you are working on 1.6 maintenance track, why discard 'master'? If the upstream project calls it 1.6, you can call your fork 1.6 and keep that checked out.
IOW, _you_ are make it not nice.
Quoted 10 lines · Junio C Hamano
Quoted 6 lines · Tor Arne Vestbø
If you would like to contribute to the stable 1.6 branch, do:
$ git clone -n git://git.foo.com/project.git $ cd project $ git checkout -t origin/1.6 $ git branch -D master Which is not so nice and inviting.If you are working on 1.6 maintenance track, why discard 'master'?
One example I can think of is if master is moving a lot faster than the maintenance track, and you are not interested in master.
[box:/tmp/downstream] $ git branch
* 1.6
master
[box:/tmp/downstream] $ git pull --rebase
Current branch 1.6 is up to date.
[box:/tmp/downstream] $ git push
To file:///tmp/upstream
! [rejected] master -> master (non-fast forward)
error: failed to push some refs to 'file:///tmp/upstream'
In that case you would either have to ff master all the time (requiring a checkout or rebase magic), or do an explicit "git push origin 1.6".
Neither good options when you are trying to teach people that git push is the way you submit changes.
Tor Arne
Quoted 3 lines · Tor Arne Vestbø
In that case you would either have to ff master all the time (requiring a checkout or rebase magic), or do an explicit "git push origin 1.6".
or do something like:
$ cat >>.git/config <<\EOF
[remote "there"]
push = HEAD
EOFjust once.
Quoted 8 lines · Johannes Schindelin
Quoted 4 lines · Tor Arne Vestbø
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone. Normally this is the active branch of the remote repository, which is also the fallback if the selected branch is not found.
I do not think that falling back if the selected branch is not found is a wise choice.
Ah, was not sure what the proper response would be. I'll resubmit with a die() instead.
Quoted 6 lines
Besides, the common way to check out something different than the remote's HEAD is like this:
$ git clone -n $URL $ cd $DIR $ git checkout -t origin/$BRANCH
Yepp, plus removing the original branch:
$ git branch -D $ORIGINAL_ACTIVE_BRANCH # typically master
Quoted 1 line
I am undecided if that is good enough, or your patch is needed.
The idea was to be able to tell someone "hey, if you want to hack on some feature for next, do the following:"
$ git clone git://git.kernel.org/pub/scm/git/git.git -b next
Maybe next is not such a good example, since it does not diverge that much from master and pu, but imagine a repository with a master, plus other branches that over time diverge from master (where you would typically use git-new-workdir to have them in a separate working tree).
In that situation it would be nice to be able to tell someone, hey, if you want to work on this odd branch which is not master, just do -b.
Tor Arne
Quoted 12 lines · Johannes Schindelin
Hi,
Quoted 4 lines · Tor Arne Vestbø
The options --branch and -b allow the user to override the initial branch created and checked out by git-clone. Normally this is the active branch of the remote repository, which is also the fallback if the selected branch is not found.
I do not think that falling back if the selected branch is not found is a wise choice.
Quoted 8 lines
Besides, the common way to check out something different than the remote's HEAD is like this:
$ git clone -n $URL $ cd $DIR $ git checkout -t origin/$BRANCHI am undecided if that is good enough, or your patch is needed.
I am fairly negative on this one, if it matters.