Add option -b/--branch to clone for select a new HEAD
PATCH 1/1 Add option -b/--branch to clone for select a new HEAD
In reply to a message not in the archiveSometimes (especially on production systems) we need to use only one remote branch for building software. It really annoying to clone origin and then swith branch by hand everytime. So this patch provide functionality to clone remote branch with one command without using checkout after clone.
--- Documentation/git-clone.txt | 4 ++++ builtin-clone.c | 23 ++++++++++++++++++++--- t/t5706-clone-branch.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100755 t/t5706-clone-branch.sh
Documentation/git-clone.txt +4 -0
@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>. +--branch <name>:: +-b <name>:: + Instead of using the remote HEAD as master, use <name> branch. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +20 -3
@@ -41,6 +41,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;
@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = { "reference repository"),OPT_STRING ('o' ,"origin" , &option_origin,"branch" ,"use <branch> instead of 'origin' to track upstream" ), +OPT_STRING ('b' ,"branch" , &option_branch,"branch" , +"use <branch> from 'origin' as HEAD" ),OPT_STRING ('u' ,"upload-pack" , &option_upload_pack,"path" ,"path to git-upload-pack on the remote" ), OPT_STRING(0, "depth", &option_depth, "depth",
@@ -347,8 +350,8 @@ 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; -struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ; +const struct ref *refs, *head_points_at, *remote_head =NULL , *mapped_refs; +struct strbuf key =STRBUF_INIT , value =STRBUF_INIT , branch_head =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;char *src_ref_prefix ="refs/heads/" ;
@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, refspec, reflog_msg.buf); - remote_head =find_ref_by_name (refs,"HEAD" ); +if (option_branch) { +strbuf_addf (&branch_head,"%s%s" , src_ref_prefix, option_branch); + + remote_head =find_ref_by_name (refs, branch_head.buf); + } + +if (!remote_head) { +if (option_branch) +warning ("Remote branch %s not found in upstream %s" +", using HEAD instead" , + option_branch, option_origin); + + remote_head =find_ref_by_name (refs,"HEAD" ); + } + head_points_at =guess_remote_head (remote_head, mapped_refs,0 ); }else {
t/t5706-clone-branch.sh new file +31 -0
@@ -0,0 +1,31 @@ +#!/bin/sh + +test_description='branch clone options' +. ./test-lib.sh + +test_expect_success 'setup' ' + + mkdir parent && + (cd parent && git init && + echo one >file && git add file && + git commit -m one && git checkout -b two && + echo two >f && git add f && git commit -m two && + git checkout master) + +' + +test_expect_success 'clone' ' + + git clone parent clone && + (cd clone && git rev-parse --verify refs/remotes/origin/master) + +' + +test_expect_success 'clone -b' ' + + git clone -b two parent clone-b && + (cd clone-b && test $(git rev-parse --verify HEAD) = $(git rev-parse --verify refs/remotes/origin/two)) + +' + +test_done
Signature · 1 line
1.6.2
PATCH 1/1 Add option -b/--branch to clone for select a new HEAD
Sometimes (especially on production systems) we need to use only one remote branch for building software. It's really annoying to clone origin and then switch branch by hand everytime. So this patch provides functionality to clone a remote branch with one command without using checkout after clone.
--- Documentation/git-clone.txt | 4 ++++ builtin-clone.c | 23 ++++++++++++++++++++--- t/t5706-clone-branch.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100755 t/t5706-clone-branch.sh
Documentation/git-clone.txt +4 -0
@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>. +--branch <name>:: +-b <name>:: + Instead of using the remote HEAD as master, use <name> branch. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +20 -3
@@ -41,6 +41,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;
@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = { "reference repository"),OPT_STRING ('o' ,"origin" , &option_origin,"branch" ,"use <branch> instead of 'origin' to track upstream" ), +OPT_STRING ('b' ,"branch" , &option_branch,"branch" , +"use <branch> from 'origin' as HEAD" ),OPT_STRING ('u' ,"upload-pack" , &option_upload_pack,"path" ,"path to git-upload-pack on the remote" ), OPT_STRING(0, "depth", &option_depth, "depth",
@@ -347,8 +350,8 @@ 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; -struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ; +const struct ref *refs, *head_points_at, *remote_head =NULL , *mapped_refs; +struct strbuf key =STRBUF_INIT , value =STRBUF_INIT , branch_head =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;char *src_ref_prefix ="refs/heads/" ;
@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, refspec, reflog_msg.buf); - remote_head =find_ref_by_name (refs,"HEAD" ); +if (option_branch) { +strbuf_addf (&branch_head,"%s%s" , src_ref_prefix, option_branch); + + remote_head =find_ref_by_name (refs, branch_head.buf); + } + +if (!remote_head) { +if (option_branch) +warning ("Remote branch %s not found in upstream %s" +", using HEAD instead" , + option_branch, option_origin); + + remote_head =find_ref_by_name (refs,"HEAD" ); + } + head_points_at =guess_remote_head (remote_head, mapped_refs,0 ); }else {
t/t5706-clone-branch.sh new file +31 -0
@@ -0,0 +1,31 @@ +#!/bin/sh + +test_description='branch clone options' +. ./test-lib.sh + +test_expect_success 'setup' ' + + mkdir parent && + (cd parent && git init && + echo one >file && git add file && + git commit -m one && git checkout -b two && + echo two >f && git add f && git commit -m two && + git checkout master) + +' + +test_expect_success 'clone' ' + + git clone parent clone && + (cd clone && git rev-parse --verify refs/remotes/origin/master) + +' + +test_expect_success 'clone -b' ' + + git clone -b two parent clone-b && + (cd clone-b && test $(git rev-parse --verify HEAD) = $(git rev-parse --verify refs/remotes/origin/two)) + +' + +test_done
Signature · 1 line
1.6.2
t/t5706-clone-branch.sh line 29
Quoted 13 lines · Kirill A. Korinskiy
t/t5706-clone-branch.sh new file +13
-0+test_expect_success 'clone' ' + + git clone parent clone && + (cd clone && git rev-parse --verify refs/remotes/origin/master) + +' + +test_expect_success 'clone -b' ' + + git clone -b two parent clone-b && + (cd clone-b && test $(git rev-parse --verify HEAD) = $(git rev-parse --verify refs/remotes/origin/two)) + +'
OK, I think that second test makes sense (though please wrap the very long line), but now what is the first one doing? Shouldn't it be:
(cd clone &&
test $(git rev-parse --verify HEAD) = \
$(git rev-parse --verify refs/remotes/origin/master)
)also?
-Peff
Quoted 19 lines · Jeff King
t/t5706-clone-branch.sh line 29
Quoted 13 lines · Kirill A. Korinskiy
t/t5706-clone-branch.sh new file +13
-0+test_expect_success 'clone' ' + + git clone parent clone && + (cd clone && git rev-parse --verify refs/remotes/origin/master) + +' + +test_expect_success 'clone -b' ' + + git clone -b two parent clone-b && + (cd clone-b && test $(git rev-parse --verify HEAD) = $(git rev-parse --verify refs/remotes/origin/two)) + +'OK, I think that second test makes sense (though please wrap the very long line), but now what is the first one doing? Shouldn't it be:
Quoted 6 lines
(cd clone && test $(git rev-parse --verify HEAD) = \ $(git rev-parse --verify refs/remotes/origin/master) )also?
Are you checking that the HEAD (whichever branch it points at) points at the same commit, or are you also interested in the _current branch_ to be a particular name as well? The suggested check only compares commits and HEAD can be pointing at a local branch whose name is xyzzy.
What is the semantics of this new -b option? When the remote repository has 'next' as its default branch (i.e. HEAD points at it), and if you run clone with "-b maint" against it, I expect that the checked out commit will be the 'maint' of remote repository, but what is the name of the current branch in the resulting clone on our end?
- Would we use 'master' as the name of our current branch, because that is the default?
- Would we use 'next' as the name of our current branch, because that is what the remote side uses?
- Would we use 'maint', because that is what -b gave us?
I am _hoping_ it is the last one, as otherwise you would also need to make sure that the branch that is different from 'maint' we set as the current branch must track 'maint' from the remote.
Oh, with -b, would we set up our 'maint' to track their 'maint'? Is it something you may want to verify as well?
Quoted 19 lines · Kirill A. Korinskiy
Sometimes (especially on production systems) we need to use only one remote branch for building software. It's really annoying to clone origin and then switch branch by hand everytime. So this patch provides functionality to clone a remote branch with one command without using checkout after clone.
Signed-off-by Kirill A. Korinskiy <[email protected]>--- Documentation/git-clone.txt | 4 ++++ builtin-clone.c | 23 ++++++++++++++++++++--- t/t5706-clone-branch.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100755 t/t5706-clone-branch.shDocumentation/git-clone.txt +0
-0
Documentation/git-clone.txt objects from the source repository into a pack in the cloned repository. · line 132
Quoted 6 lines
Documentation/git-clone.txt +3
-0@@ -127,6 +127,10 @@ objects from the source repository into a pack in the cloned repository. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>. +--branch <name>:: +-b <name>:: + Instead of using the remote HEAD as master, use <name> branch.
Hm, that's no good. The branch won't be called master, nor is HEAD used as "master" anyway. If the remote repo's HEAD references refs/heads/foo, you'll get refs/heads/foo locally as well, not "master", but see below.
Maybe: Create a local branch head for <name> instead of the branch referenced by the remote repo's HEAD.
Documentation/git-clone.txt static struct option builtin_clone_options[] = { · line 70
Quoted 6 lines
Documentation/git-clone.txt +2
-0@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = { "reference repository"), OPT_STRING('o', "origin", &option_origin, "branch", "use <branch> instead of 'origin' to track upstream"), + OPT_STRING('b', "branch", &option_branch, "branch", + "use <branch> from 'origin' as HEAD"),
Using 'origin' there is unfortunate, as using "--origin foo" would make clone call the remote "foo" instead of "origin". And it's not really used "as" HEAD, but instead of the remote repo's HEAD to determine which local branch head to create. Though I guess this affect the refs/remotes/<remote>/HEAD symref as well?
Quoted 11 lines
Documentation/git-clone.txt +6
-1@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs = write_remote_refs(refs, refspec, reflog_msg.buf); - remote_head = find_ref_by_name(refs, "HEAD"); + if (option_branch) { + strbuf_addf(&branch_head, "%s%s", src_ref_prefix, option_branch); + + remote_head = find_ref_by_name(refs, branch_head.buf); + } +
Documentation/git-clone.txt int cmd_clone(int argc, const char **argv, const char *prefix) · line 539
Quoted 10 lines
Documentation/git-clone.txt +9
-0+ if (!remote_head) { + if (option_branch) + warning("Remote branch %s not found in upstream %s" + ", using HEAD instead", + option_branch, option_origin); + + remote_head = find_ref_by_name(refs, "HEAD"); + } + head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
This would still pick refs/heads/master if refs/heads/master and refs/heads/<branch> reference the same commit. That's due to the check in guess_remote_head() which prefers refs/heads/master over all other refs. While this is acceptable for the HEAD lookup, I'd treat that as a bug for this new option.
Quoted 25 lines
Documentation/git-clone.txt +0
-0} else {t/t5706-clone-branch.sh new file +17
-0@@ -0,0 +1,31 @@ +#!/bin/sh + +test_description='branch clone options' +. ./test-lib.sh + +test_expect_success 'setup' ' + + mkdir parent && + (cd parent && git init && + echo one >file && git add file && + git commit -m one && git checkout -b two && + echo two >f && git add f && git commit -m two && + git checkout master) + +' + +test_expect_success 'clone' '
t/t5706-clone-branch.sh line 27
Quoted 10 lines
t/t5706-clone-branch.sh new file +10
-0+ + git clone parent clone && + (cd clone && git rev-parse --verify refs/remotes/origin/master) + +' + +test_expect_success 'clone -b' ' + + git clone -b two parent clone-b && + (cd clone-b && test $(git rev-parse --verify HEAD) = $(git rev-parse --verify refs/remotes/origin/two))
This should probably check not just that HEAD resolves to the same commit as refs/remotes/origin/two, but that HEAD references refs/heads/two as well, even if the remote's refs/heads/master references the same commit as refs/heads/two (see above).
Björn
Quoted 33 lines · Kirill A. Korinskiy
At Wed, 26 Aug 2009 00:36:37 +0200,
Quoted 19 lines · Björn Steinbrink
Quoted 19 lines
builtin-clone.c +14
-1@@ -518,7 +521,21 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, refspec, reflog_msg.buf); - remote_head =find_ref_by_name (refs,"HEAD" ); +if (option_branch) { +strbuf_addf (&branch_head,"%s%s" , src_ref_prefix, option_branch); + + remote_head =find_ref_by_name (refs, branch_head.buf); + } + +if (!remote_head) { +if (option_branch) +warning ("Remote branch %s not found in upstream %s" +", using HEAD instead" , + option_branch, option_origin); + + remote_head =find_ref_by_name (refs,"HEAD" ); + }Quoted 9 lines
Review int cmd_clone(int argc, const char **argv, const char *prefix) · line 539
Quoted 2 lines
(quoted diff) +1
-0+ head_points_at = guess_remote_head(remote_head, mapped_refs, 0);This would still pick refs/heads/master if refs/heads/master and refs/heads/<branch> reference the same commit. That's due to the check in
guess_remote_head()which prefers refs/heads/master over all other refs. While this is acceptable for the HEAD lookup, I'd treat that as a bug for this new option.
Quoted 1 line
My english is not a good and I don't understand it, sorry.
guess_remote_head() compares the object ids from remote_head and all of the remote's refs to guess which is the right one.
Let's say that the repo has:
refs/heads/master: object1
refs/heads/foo: object2
refs/heads/bar: object1
If you do "git clone -b foo ...", then remote_head->old_sha1 will be "object2". guess_remote_head() compares that to all the remote heads. In this case, it will find refs/heads/foo (as expected).
But when you do "git clone -b bar", then remote_head->old_sha1 will be "object1". And guess_remote_head() will then take refs/heads/master, as it prefers that one.
doener@atjola:h $ mkdir a; cd a; git init Initialized empty Git repository in /home/doener/h/a/.git/ doener@atjola:a (master) $ git commit --allow-empty -m init [master (root-commit) a7a0b54] init
doener@atjola:a (master) $ git branch bar doener@atjola:a (master) $ git checkout -b foo Switched to a new branch 'foo'
doener@atjola:a (foo) $ git commit --allow-empty -m on_foo [foo 375047e] on_foo
doener@atjola:a (foo) $ cd ..
doener@atjola:h $ (git clone -b foo a foo; cd foo; git branch) Initialized empty Git repository in /home/doener/h/foo/.git/
* foo
doener@atjola:h $ (git clone -b bar a bar; cd bar; git branch) Initialized empty Git repository in /home/doener/h/bar/.git/
* master
That said, I actually wonder why you don't simple set HEAD in the original repo so that you get whichever branch you want by default anyway.
Björn
PATCH 1/1 Add option -b/--branch to clone for select a new HEAD
Sometimes (especially on production systems) we need to use only one remote branch for building software. It's really annoying to clone origin and then switch branch by hand everytime. So this patch provides functionality to clone a remote branch with one command without using checkout after clone.
--- Documentation/git-clone.txt | 5 ++++ builtin-clone.c | 25 ++++++++++++++++++--- t/t5706-clone-branch.sh | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100755 t/t5706-clone-branch.sh
Documentation/git-clone.txt +5 -0
@@ -127,6 +127,11 @@ objects from the source repository into a pack in the cloned repository. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>. +--branch <name>:: +-b <name>:: + Create a local branch head for <name> instead of the branch + referenced by the remote repos HEAD. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +21 -4
@@ -41,6 +41,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;
@@ -65,6 +66,8 @@ static struct option builtin_clone_options[] = { "reference repository"),OPT_STRING ('o' ,"origin" , &option_origin,"branch" ,"use <branch> instead of 'origin' to track upstream" ), +OPT_STRING ('b' ,"branch" , &option_branch,"branch" , +"use <branch> from upstream as HEAD" ),OPT_STRING ('u' ,"upload-pack" , &option_upload_pack,"path" ,"path to git-upload-pack on the remote" ), OPT_STRING(0, "depth", &option_depth, "depth",
@@ -347,8 +350,8 @@ 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; -struct strbuf key =STRBUF_INIT , value =STRBUF_INIT ; +const struct ref *refs, *head_points_at, *remote_head =NULL , *mapped_refs; +struct strbuf key =STRBUF_INIT , value =STRBUF_INIT , branch_head =STRBUF_INIT ;struct strbuf branch_top =STRBUF_INIT , reflog_msg =STRBUF_INIT ;struct transport *transport =NULL ;char *src_ref_prefix ="refs/heads/" ;
@@ -518,8 +521,22 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, refspec, reflog_msg.buf); - remote_head =find_ref_by_name (refs,"HEAD" ); - head_points_at =guess_remote_head (remote_head, mapped_refs,0 ); +if (option_branch) { +strbuf_addf (&branch_head,"%s%s" , src_ref_prefix, option_branch); + + remote_head =find_ref_by_name (refs, branch_head.buf); + } + +if (!remote_head) { +if (option_branch) +warning ("Remote branch %s not found in upstream %s" +", using HEAD instead" , + option_branch, option_origin); + + remote_head =find_ref_by_name (refs,"HEAD" ); + } + + head_points_at =guess_remote_head (remote_head, mapped_refs,1 ); }else {warning ("You appear to have cloned an empty repository." );
t/t5706-clone-branch.sh new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
1.6.2
Quoted 12 lines · Kirill A. Korinskiy
builtin-clone.c +6
-2@@ -518,8 +521,22 @@ int cmd_clone(int argc, const char **argv, const char *prefix) mapped_refs =write_remote_refs (refs, refspec, reflog_msg.buf); - remote_head =find_ref_by_name (refs,"HEAD" ); - head_points_at =guess_remote_head (remote_head, mapped_refs,0 ); +if (option_branch) { +strbuf_addf (&branch_head,"%s%s" , src_ref_prefix, option_branch); + + remote_head =find_ref_by_name (refs, branch_head.buf); + } +
builtin-clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 539
Quoted 10 lines
builtin-clone.c +10
-0+if (!remote_head) { +if (option_branch) +warning ("Remote branch %s not found in upstream %s" +", using HEAD instead" , + option_branch, option_origin); + + remote_head =find_ref_by_name (refs,"HEAD" ); + } + + head_points_at =guess_remote_head (remote_head, mapped_refs,1 );
Just setting "all" to 1 there is wrong. With "all" set to 1, guess_remote_head() returns a linked list of _all_ matching refs. The first entry in that list depends on the order of mapped_refs.
doener@atjola:h $ mkdir a; cd a; git init Initialized empty Git repository in /home/doener/h/a/.git/ doener@atjola:a (master) $ git commit --allow-empty -m init [master (root-commit) aa39247] init doener@atjola:a (master) $ git branch foo doener@atjola:a (master) $ cd ..
doener@atjola:h $ (git clone -b foo a foo; cd foo; git branch) Initialized empty Git repository in /home/doener/h/foo/.git/
* foo
doener@atjola:h $ (git clone -b master a master; cd master; git branch) Initialized empty Git repository in /home/doener/h/master/.git/
* foo
Here, "foo" was first in mapped_refs, and so "-b master" used that, too.
Using guess_remote_head() seems pretty wrong. With -b given, you don't want to guess anymore, you _know_ which one you want. Unfortunately, I don't see a straight-forward way to handle that (but I'm totally clueless about the code, so don't let me scare you ;-)).
Quoted 41 lines
t/t5706-clone-branch.sh new file +35
-0@@ -0,0 +1,49 @@ +#!/bin/sh + +test_description='branch clone options' +. ./test-lib.sh + +test_expect_success 'setup' ' + + mkdir parent && + (cd parent && git init && + echo one >file && git add file && + git commit -m one && git branch foo && + git checkout -b two && + echo two >f && git add f && git commit -m two && + git checkout master) + +' + +test_expect_success 'clone' ' + + git clone parent clone && + (cd clone && + test $(git rev-parse --verify HEAD) = \ + $(git rev-parse --verify refs/remotes/origin/master) && + test $(git rev-parse --verify HEAD) != \ + $(git rev-parse --verify refs/remotes/origin/two)) + + +' + +test_expect_success 'clone -b two' ' + + git clone -b two parent clone-b && + (cd clone-b && + test $(git rev-parse --verify HEAD) = \ + $(git rev-parse --verify refs/remotes/origin/two) &&
t/t5706-clone-branch.sh line 45
Quoted 10 lines
t/t5706-clone-branch.sh new file +10
-0+ test $(git rev-parse --verify HEAD) != \ + $(git rev-parse --verify refs/remotes/origin/master)) + +' + +test_expect_success 'clone -b foo' ' + + git clone -b foo parent clone-b-foo && + (cd clone-b-foo && + test $(git branch | grep \* | sed -e s:\*\ ::) = foo)
This should probably do "git symbolic-ref HEAD" instead of the branch + grep + sed. And it should also verify that rev-parse foo == rev-parse origin/foo.
And to catch the above bug, you need a second test like that, but for "master" instead of "foo".
HTH
Björn
Quoted 4 lines · Björn Steinbrink
Using
guess_remote_head()seems pretty wrong. With -b given, you don't want to guess anymore, you _know_ which one you want. Unfortunately, I don't see a straight-forward way to handle that (but I'm totally clueless about the code, so don't let me scare you ;-)).
Thanks for pointing this out, Björn (I really should have noticed it on first review, but I guess many eyes, shallow bugs, etc. :) ).
This code is a little bit confusing, so let me explain:
- we look up the remote HEAD, getting its commit sha1. If the protocol supports it, we also get its symref information.
- we then pass the result to guess_remote_head. _If_ we have symref information, then we can quit immediately, as the symref contains what we want. If it doesn't, then we proceed with trying to match up the commit sha1 with one of the other refs.
So if you want to create a "remote_head" object via "-b" which acts as if it was the remote HEAD, you would need to actually create a new ref object and set the "symref" field appropriately.
But I don't think there is any need to do that here. We simply want to avoid calling guess_remote_head at all, since we know there is nothing to guess at. We do still need to know whether remote_head is non-NULL later, though.
So I think the code should probably look like this (totally untested):
remote_head = find_ref_by_name(refs, "HEAD");
if (option_branch) {
strbuf_addf(&branch_head, "%s%s", src_ref_prefix, option_branch);
head_points_at = find_ref_by_name(refs, branch_head.buf);
if (!head_points_at)
warning("remote branch not found, etc");
}
if (!head_points_at)
head_points_at = guess_remote_head(remote_head, mapped_refs, 0);and then initialize head_points_at to NULL instead of remote_head.
-Peff
Quoted 11 lines · Jeff King
So I think the code should probably look like this (totally untested):
remote_head = find_ref_by_name(refs, "HEAD"); if (option_branch) { strbuf_addf(&branch_head, "%s%s", src_ref_prefix, option_branch); head_points_at = find_ref_by_name(refs, branch_head.buf); if (!head_points_at) warning("remote branch not found, etc"); } if (!head_points_at) head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
Hm, why "refs" for find_ref_by_name(), but "mapped_ref" for guess_remote_head()?
Björn
Quoted 13 lines · Björn Steinbrink
Quoted 11 lines · Jeff King
So I think the code should probably look like this (totally untested):
remote_head = find_ref_by_name(refs, "HEAD"); if (option_branch) { strbuf_addf(&branch_head, "%s%s", src_ref_prefix, option_branch); head_points_at = find_ref_by_name(refs, branch_head.buf); if (!head_points_at) warning("remote branch not found, etc"); } if (!head_points_at) head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
Quoted 2 lines
Hm, why "refs" for
find_ref_by_name(), but "mapped_ref" forguess_remote_head()?
Blind copying of the current code? :)
Good question, though. AFAICT, the difference between mapped_refs and refs is that the former contains only the refs we are actually fetching, and its peer_ref member is filled in as appropriate.
Later in the code, we look at head_points_at->peer_ref, which means it _must_ come from mapped_refs. And which means the code I posted is bogus, as the ref we look up in "refs" will not have that member filled in. So I think we do need:
head_points_at = find_ref_by_name(mapped_refs, branch_head.buf);
Good catch.
-Peff
Quoted 6 lines · Jeff King
Later in the code, we look at
head_points_at->peer_ref, which means it _must_ come frommapped_refs. And which means the code I posted is bogus, as the ref we look up in "refs" will not have that member filled in. So I think we do need:head_points_at = find_ref_by_name(mapped_refs, branch_head.buf);
Actually, it is much more complicated than that. We want to do several things with the remote HEAD:
1. set up our HEAD; this uses head_points_at now
2. set up a pointer in refs/remotes/$origin/HEAD; this uses head_points_at now
3. check out the actual contents; this uses remote_head (and it can't just blindly use head_points_at, because it may be a detached HEAD).
So you can see by (1) and (2) that we actually need to distinguish between the remote's HEAD and where we want our HEAD to be. And we need to checkout _our_ HEAD, if available, falling back to the remote's head.
I really wish clone was implemented simply as "init && remote add && fetch && checkout", which would have made this a lot easier. We would simply be munging the HEAD file in the middle.
Anyway, here is a patch which I think does the right thing, and tests each of the desired behaviors in the test script. If there are further "should it do X or Y" questions, please phrase them in the form of a patch to the test script. ;)
I also tried to roll in comments on the documentation to make it a bit clearer.
-- >8 --
Subject: [PATCH] clone: add --branch option to select a different HEAD
We currently point the HEAD of a newly cloned repo to the same ref as the parent repo's HEAD. While a user can then "git checkout -b foo origin/foo" whichever branch they choose, it is more convenient and more efficient to tell clone which branch you want in the first place.
Based on a patch by Kirill A. Korinskiy.
--- Documentation/git-clone.txt | 7 ++++ builtin-clone.c | 75 +++++++++++++++++++++++++++---------------- t/t5706-clone-branch.sh | 68 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 28 deletions(-) create mode 100755 t/t5706-clone-branch.sh
Documentation/git-clone.txt +7 -0
@@ -127,6 +127,13 @@ objects from the source repository into a pack in the cloned repository. Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>. +--branch <name>:: +-b <name>:: + Instead of pointing the newly created HEAD to the branch pointed + to by the cloned repositoroy's HEAD, point to <name> branch + instead. In a non-bare repository, this is the branch that will + be checked out. + --upload-pack <upload-pack>:: -u <upload-pack>:: When given, and the repository to clone from is accessed
builtin-clone.c +47 -28
125 lines not on this page · raw message
t/t5706-clone-branch.sh new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
1.6.4.1.340.ge9f66.dirty
Quoted 5 lines · Kirill A. Korinskiy
Sometimes (especially on production systems) we need to use only one remote branch for building software. It really annoying to clone origin and then swith branch by hand everytime. So this patch provide functionality to clone remote branch with one command without using checkout after clone.
Glad to see this topic being revised. Here's the original discussion for reference:
http://thread.gmane.org/gmane.comp.version-control.git/114919/
Tor Arne
Quoted 2 lines · Tor Arne Vestbø
Glad to see this topic being revised. Here's the original discussion for reference:
ISTR that early git supported this -- maybe just on the HTTP implementation? I sure remember writing
git clone http://host/foo.git#branchname
and getting a cloned repo where 'branchname' was tracking 'origin/branchname', and was checked out as HEAD.
It would have been dropped by the switch to C builtins :-/
cheers,
m
Signature · 5 lines
[email protected] [email protected] -- School Server Architect - ask interesting questions - don't get distracted with shiny stuff - working code first - http://wiki.laptop.org/go/User:Martinlanghoff
Quoted 11 lines · Martin Langhoff
Quoted 2 lines · Tor Arne Vestb?
Glad to see this topic being revised. Here's the original discussion for reference:
ISTR that early git supported this -- maybe just on the HTTP implementation? I sure remember writing
git clone http://host/foo.git#branchname
and getting a cloned repo where 'branchname' was tracking 'origin/branchname', and was checked out as HEAD.
Did you use cogito perhaps? That had #branch syntax IIRC ...
Quoted 8 lines
It would have been dropped by the switch to C builtins :-/
cheers,
m
Signature · 5 lines
Julian
---
"What duck?"
-- (Terry Pratchett, Soul Music)