Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94e3458

Browse files
authored
Merge pull request #5651 from libgit2/ethomson/clone_branch
clone: update origin's HEAD
2 parents 6244791 + c1f1bca commit 94e3458

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/clone.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,64 @@ static int update_head_to_default(git_repository *repo)
162162
return error;
163163
}
164164

165+
static int update_remote_head(
166+
git_repository *repo,
167+
git_remote *remote,
168+
git_buf *target,
169+
const char *reflog_message)
170+
{
171+
git_refspec *refspec;
172+
git_reference *remote_head = NULL;
173+
git_buf remote_head_name = GIT_BUF_INIT;
174+
git_buf remote_branch_name = GIT_BUF_INIT;
175+
int error;
176+
177+
/* Determine the remote tracking ref name from the local branch */
178+
refspec = git_remote__matching_refspec(remote, git_buf_cstr(target));
179+
180+
if (refspec == NULL) {
181+
git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration");
182+
error = GIT_EINVALIDSPEC;
183+
goto cleanup;
184+
}
185+
186+
if ((error = git_refspec_transform(
187+
&remote_branch_name,
188+
refspec,
189+
git_buf_cstr(target))) < 0)
190+
goto cleanup;
191+
192+
if ((error = git_buf_printf(&remote_head_name,
193+
"%s%s/%s",
194+
GIT_REFS_REMOTES_DIR,
195+
git_remote_name(remote),
196+
GIT_HEAD_FILE)) < 0)
197+
goto cleanup;
198+
199+
error = git_reference_symbolic_create(
200+
&remote_head,
201+
repo,
202+
git_buf_cstr(&remote_head_name),
203+
git_buf_cstr(&remote_branch_name),
204+
true,
205+
reflog_message);
206+
207+
cleanup:
208+
git_reference_free(remote_head);
209+
git_buf_dispose(&remote_branch_name);
210+
git_buf_dispose(&remote_head_name);
211+
return error;
212+
}
213+
165214
static int update_head_to_remote(
166215
git_repository *repo,
167216
git_remote *remote,
168217
const char *reflog_message)
169218
{
170219
int error = 0;
171220
size_t refs_len;
172-
git_refspec *refspec;
173221
const git_remote_head *remote_head, **refs;
174222
const git_oid *remote_head_id;
175-
git_buf remote_branch_name = GIT_BUF_INIT;
176223
git_buf branch = GIT_BUF_INIT;
177224

178225
if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
@@ -195,19 +242,7 @@ static int update_head_to_remote(
195242
goto cleanup;
196243
}
197244

198-
refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch));
199-
200-
if (refspec == NULL) {
201-
git_error_set(GIT_ERROR_NET, "the remote's default branch does not fit the refspec configuration");
202-
error = GIT_EINVALIDSPEC;
203-
goto cleanup;
204-
}
205-
206-
/* Determine the remote tracking ref name from the local branch */
207-
if ((error = git_refspec_transform(
208-
&remote_branch_name,
209-
refspec,
210-
git_buf_cstr(&branch))) < 0)
245+
if ((error = update_remote_head(repo, remote, &branch, reflog_message)) < 0)
211246
goto cleanup;
212247

213248
error = update_head_to_new_branch(
@@ -217,7 +252,6 @@ static int update_head_to_remote(
217252
reflog_message);
218253

219254
cleanup:
220-
git_buf_dispose(&remote_branch_name);
221255
git_buf_dispose(&branch);
222256

223257
return error;

tests/online/clone.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static int fetch_progress(const git_indexer_progress *stats, void *payload)
176176
void test_online_clone__can_checkout_a_cloned_repo(void)
177177
{
178178
git_buf path = GIT_BUF_INIT;
179-
git_reference *head;
179+
git_reference *head, *remote_head;
180180
bool checkout_progress_cb_was_called = false,
181181
fetch_progress_cb_was_called = false;
182182

@@ -195,9 +195,14 @@ void test_online_clone__can_checkout_a_cloned_repo(void)
195195
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head));
196196
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
197197

198+
cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD"));
199+
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head));
200+
cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head));
201+
198202
cl_assert_equal_i(true, checkout_progress_cb_was_called);
199203
cl_assert_equal_i(true, fetch_progress_cb_was_called);
200204

205+
git_reference_free(remote_head);
201206
git_reference_free(head);
202207
git_buf_dispose(&path);
203208
}

0 commit comments

Comments
 (0)