Skip to content

Commit 6bb3587

Browse files
Alexander OvchinnikovAlexander Ovchinnikov
authored andcommitted
clone: set refs/remotes/origin/HEAD to default branch when branch is specified, attempt 2
1 parent 8643b52 commit 6bb3587

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/clone.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,38 @@ static int update_head_to_remote(
259259

260260
static int update_head_to_branch(
261261
git_repository *repo,
262-
const char *remote_name,
262+
git_remote *remote,
263263
const char *branch,
264264
const char *reflog_message)
265265
{
266266
int retcode;
267267
git_buf remote_branch_name = GIT_BUF_INIT;
268268
git_reference* remote_ref = NULL;
269+
git_buf default_branch = GIT_BUF_INIT;
269270

270-
GIT_ASSERT_ARG(remote_name);
271+
GIT_ASSERT_ARG(remote);
271272
GIT_ASSERT_ARG(branch);
272273

273274
if ((retcode = git_buf_printf(&remote_branch_name, GIT_REFS_REMOTES_DIR "%s/%s",
274-
remote_name, branch)) < 0 )
275+
git_remote_name(remote), branch)) < 0 )
275276
goto cleanup;
276277

277278
if ((retcode = git_reference_lookup(&remote_ref, repo, git_buf_cstr(&remote_branch_name))) < 0)
278279
goto cleanup;
279280

280-
retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
281-
reflog_message);
281+
if ((retcode = update_head_to_new_branch(repo, git_reference_target(remote_ref), branch,
282+
reflog_message)) < 0)
283+
goto cleanup;
284+
285+
if ((retcode = git_remote_default_branch(&default_branch, remote)) < 0)
286+
goto cleanup;
287+
288+
retcode = update_remote_head(repo, remote, &default_branch, reflog_message);
282289

283290
cleanup:
284291
git_reference_free(remote_ref);
285292
git_buf_dispose(&remote_branch_name);
293+
git_buf_dispose(&default_branch);
286294
return retcode;
287295
}
288296

@@ -367,8 +375,7 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c
367375
int error;
368376

369377
if (branch)
370-
error = update_head_to_branch(repo, git_remote_name(remote), branch,
371-
reflog_message);
378+
error = update_head_to_branch(repo, remote, branch, reflog_message);
372379
/* Point HEAD to the same ref as the remote's head */
373380
else
374381
error = update_head_to_remote(repo, remote, reflog_message);

tests/clone/nonetwork.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
158158

159159
void test_clone_nonetwork__can_checkout_given_branch(void)
160160
{
161+
git_reference *remote_head;
162+
161163
g_options.checkout_branch = "test";
162164
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
163165

@@ -167,6 +169,12 @@ void test_clone_nonetwork__can_checkout_given_branch(void)
167169
cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
168170

169171
cl_assert(git_path_exists("foo/readme.txt"));
172+
173+
cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD"));
174+
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head));
175+
cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head));
176+
177+
git_reference_free(remote_head);
170178
}
171179

172180
static int clone_cancel_fetch_transfer_progress_cb(

0 commit comments

Comments
 (0)