Unverified Commit 3f8bf8be by Edward Thomson Committed by GitHub

Merge pull request #6010 from A-Ovchinnikov-mx/a-ovchin/remote-head-branch-clone

Set refs/remotes/origin/HEAD to default branch when branch is specified
parents fabacb7c cb078d51
......@@ -162,37 +162,6 @@ done:
return error;
}
static int update_remote_head_byname(
git_repository *repo,
const char *remote_name,
const char *tracking_branch_name,
const char *reflog_message)
{
git_buf tracking_head_name = GIT_BUF_INIT;
git_reference *remote_head = NULL;
int error;
if ((error = git_buf_printf(&tracking_head_name,
"%s%s/%s",
GIT_REFS_REMOTES_DIR,
remote_name,
GIT_HEAD_FILE)) < 0)
goto cleanup;
error = git_reference_symbolic_create(
&remote_head,
repo,
git_buf_cstr(&tracking_head_name),
tracking_branch_name,
true,
reflog_message);
cleanup:
git_reference_free(remote_head);
git_buf_dispose(&tracking_head_name);
return error;
}
static int update_remote_head(
git_repository *repo,
git_remote *remote,
......@@ -200,7 +169,9 @@ static int update_remote_head(
const char *reflog_message)
{
git_refspec *refspec;
git_buf tracking_branch_name = GIT_BUF_INIT;
git_reference *remote_head = NULL;
git_buf remote_head_name = GIT_BUF_INIT;
git_buf remote_branch_name = GIT_BUF_INIT;
int error;
/* Determine the remote tracking ref name from the local branch */
......@@ -213,19 +184,30 @@ static int update_remote_head(
}
if ((error = git_refspec_transform(
&tracking_branch_name,
&remote_branch_name,
refspec,
git_buf_cstr(target))) < 0)
goto cleanup;
error = update_remote_head_byname(
repo,
if ((error = git_buf_printf(&remote_head_name,
"%s%s/%s",
GIT_REFS_REMOTES_DIR,
git_remote_name(remote),
git_buf_cstr(&tracking_branch_name),
GIT_HEAD_FILE)) < 0)
goto cleanup;
error = git_reference_symbolic_create(
&remote_head,
repo,
git_buf_cstr(&remote_head_name),
git_buf_cstr(&remote_branch_name),
true,
reflog_message);
cleanup:
git_buf_dispose(&tracking_branch_name);
git_reference_free(remote_head);
git_buf_dispose(&remote_branch_name);
git_buf_dispose(&remote_head_name);
return error;
}
......@@ -277,19 +259,20 @@ cleanup:
static int update_head_to_branch(
git_repository *repo,
const char *remote_name,
git_remote *remote,
const char *branch,
const char *reflog_message)
{
int retcode;
git_buf remote_branch_name = GIT_BUF_INIT;
git_reference* remote_ref = NULL;
git_buf default_branch = GIT_BUF_INIT;
GIT_ASSERT_ARG(remote_name);
GIT_ASSERT_ARG(remote);
GIT_ASSERT_ARG(branch);
if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
remote_name, branch)) < 0 )
git_remote_name(remote), branch)) < 0 )
goto cleanup;
if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
......@@ -299,11 +282,18 @@ static int update_head_to_branch(
reflog_message)) < 0)
goto cleanup;
retcode = update_remote_head_byname(repo, remote_name, remote_branch_name.ptr, reflog_message);
if ((retcode = git_remote_default_branch(&default_branch, remote)) < 0)
goto cleanup;
if (!git_remote__matching_refspec(remote, git_buf_cstr(&default_branch)))
goto cleanup;
retcode = update_remote_head(repo, remote, &default_branch, reflog_message);
cleanup:
git_reference_free(remote_ref);
git_buf_dispose(&remote_branch_name);
git_buf_dispose(&default_branch);
return retcode;
}
......@@ -388,8 +378,7 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c
int error;
if (branch)
error = update_head_to_branch(repo, git_remote_name(remote), branch,
reflog_message);
error = update_head_to_branch(repo, remote, branch, reflog_message);
/* Point HEAD to the same ref as the remote's head */
else
error = update_head_to_remote(repo, remote, reflog_message);
......
......@@ -172,7 +172,7 @@ void test_clone_nonetwork__can_checkout_given_branch(void)
cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD"));
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head));
cl_assert_equal_s("refs/remotes/origin/test", git_reference_symbolic_target(remote_head));
cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head));
git_reference_free(remote_head);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment