diff options
| author | Sascha Cunz <sascha@babbelbox.org> | 2013-01-11 02:45:55 +0100 |
|---|---|---|
| committer | Sascha Cunz <sascha@babbelbox.org> | 2013-01-11 03:10:19 +0100 |
| commit | 88aef76635c012ac8dc770e0f97abc37980decf9 (patch) | |
| tree | 311690a7160deced5bd631f726201f6917453c74 /src/clone.c | |
| parent | 132c2db6a9f839d7697e8bff05861a92825a9c7c (diff) | |
| download | libgit2-88aef76635c012ac8dc770e0f97abc37980decf9.tar.gz | |
Implement analog for 'git checkout --branch xxx ...'
Diffstat (limited to 'src/clone.c')
| -rw-r--r-- | src/clone.c | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/clone.c b/src/clone.c index 32ac08bf2..e9686dce4 100644 --- a/src/clone.c +++ b/src/clone.c @@ -260,6 +260,31 @@ cleanup: return retcode; } +static int update_head_to_branch( + git_repository *repo, + const git_clone_options *options) +{ + int retcode; + git_buf remote_branch_name = GIT_BUF_INIT; + git_reference* remote_ref = NULL; + + assert(options->checkout_branch); + + if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s", + options->remote_name, options->checkout_branch)) < 0 ) + goto cleanup; + + if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0) + goto cleanup; + + retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), + options->checkout_branch); + +cleanup: + git_reference_free(remote_ref); + return retcode; +} + /* * submodules? */ @@ -331,8 +356,13 @@ static int setup_remotes_and_fetch( options->fetch_progress_payload)) { /* Create "origin/foo" branches for all remote branches */ if (!git_remote_update_tips(origin)) { + /* Point HEAD to the requested branch */ + if (options->checkout_branch) { + if (!update_head_to_branch(repo, options)) + retcode = 0; + } /* Point HEAD to the same ref as the remote's head */ - if (!update_head_to_remote(repo, origin)) { + else if (!update_head_to_remote(repo, origin)) { retcode = 0; } } |
