Skip to content

Commit c1f1bca

Browse files
committed
clone: update origin's HEAD
Update `refs/remotes/origin/HEAD` as a symbolic link to the remote's default branch.
1 parent c837bff commit c1f1bca

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
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;

0 commit comments

Comments
 (0)