Skip to content

Commit daf7898

Browse files
peffgitster
authored andcommitted
clone: move unborn head creation to update_head()
Prior to 4f37d45 (clone: respect remote unborn HEAD, 2021-02-05), creation of the local HEAD was always done in update_head(). That commit added code to handle an unborn head in an empty repository, and just did all symref creation and config setup there. This makes the code flow a little bit confusing, especially as new corner cases have been covered (like the previous commit to match our default branch name to a non-HEAD remote branch). Let's move the creation of the unborn symref into update_head(). This matches the other HEAD-creation cases, and now the logic is consistently separated: the main cmd_clone() function only examines the situation and sets variables based on what it finds, and update_head() actually performs the update. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc8fcd1 commit daf7898

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

builtin/clone.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static void update_remote_refs(const struct ref *refs,
606606
}
607607

608608
static void update_head(const struct ref *our, const struct ref *remote,
609-
const char *msg)
609+
const char *unborn, const char *msg)
610610
{
611611
const char *head;
612612
if (our && skip_prefix(our->name, "refs/heads/", &head)) {
@@ -632,6 +632,15 @@ static void update_head(const struct ref *our, const struct ref *remote,
632632
*/
633633
update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
634634
UPDATE_REFS_DIE_ON_ERR);
635+
} else if (unborn && skip_prefix(unborn, "refs/heads/", &head)) {
636+
/*
637+
* Unborn head from remote; same as "our" case above except
638+
* that we have no ref to update.
639+
*/
640+
if (create_symref("HEAD", unborn, NULL) < 0)
641+
die(_("unable to update HEAD"));
642+
if (!option_bare)
643+
install_branch_config(0, head, remote_name, unborn);
635644
}
636645
}
637646

@@ -876,6 +885,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
876885
const struct ref *refs, *remote_head;
877886
struct ref *remote_head_points_at = NULL;
878887
const struct ref *our_head_points_at;
888+
char *unborn_head = NULL;
879889
struct ref *mapped_refs = NULL;
880890
const struct ref *ref;
881891
struct strbuf key = STRBUF_INIT;
@@ -1282,8 +1292,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12821292
our_head_points_at = NULL;
12831293
} else {
12841294
const char *branch;
1285-
const char *ref;
1286-
char *ref_free = NULL;
12871295

12881296
if (!mapped_refs) {
12891297
warning(_("You appear to have cloned an empty repository."));
@@ -1293,12 +1301,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12931301
if (transport_ls_refs_options.unborn_head_target &&
12941302
skip_prefix(transport_ls_refs_options.unborn_head_target,
12951303
"refs/heads/", &branch)) {
1296-
ref = transport_ls_refs_options.unborn_head_target;
1297-
create_symref("HEAD", ref, reflog_msg.buf);
1304+
unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
12981305
} else {
12991306
branch = git_default_branch_name(0);
1300-
ref_free = xstrfmt("refs/heads/%s", branch);
1301-
ref = ref_free;
1307+
unborn_head = xstrfmt("refs/heads/%s", branch);
13021308
}
13031309

13041310
/*
@@ -1313,10 +1319,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13131319
* a match.
13141320
*/
13151321
our_head_points_at = find_remote_branch(mapped_refs, branch);
1316-
1317-
if (!option_bare && !our_head_points_at)
1318-
install_branch_config(0, branch, remote_name, ref);
1319-
free(ref_free);
13201322
}
13211323

13221324
write_refspec_config(src_ref_prefix, our_head_points_at,
@@ -1336,7 +1338,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13361338
branch_top.buf, reflog_msg.buf, transport,
13371339
!is_local);
13381340

1339-
update_head(our_head_points_at, remote_head, reflog_msg.buf);
1341+
update_head(our_head_points_at, remote_head, unborn_head, reflog_msg.buf);
13401342

13411343
/*
13421344
* We want to show progress for recursive submodule clones iff
@@ -1363,6 +1365,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13631365
strbuf_release(&key);
13641366
free_refs(mapped_refs);
13651367
free_refs(remote_head_points_at);
1368+
free(unborn_head);
13661369
free(dir);
13671370
free(path);
13681371
UNLEAK(repo);

0 commit comments

Comments
 (0)