clone: move unborn head creation to update_head()

Prior to 4f37d45706 (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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 11, 2022 at 05:21 UTC daf7898abbadef81b120f455323066158514d61b
1 file changed +15 -12
builtin/clone.c
+15 -12
@@ -606,7 +606,7 @@ static void update_remote_refs(const struct ref *refs,
606 }
607
608 static void update_head(const struct ref *our, const struct ref *remote,
609 - const char *msg)
609 + const char *unborn, const char *msg)
610 {
611 const char *head;
612 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,
632 */
633 update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
634 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);
644 }
645 }
646
@@ -876,6 +885,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
885 const struct ref *refs, *remote_head;
886 struct ref *remote_head_points_at = NULL;
887 const struct ref *our_head_points_at;
888 + char *unborn_head = NULL;
889 struct ref *mapped_refs = NULL;
890 const struct ref *ref;
891 struct strbuf key = STRBUF_INIT;
@@ -1282,8 +1292,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1292 our_head_points_at = NULL;
1293 } else {
1294 const char *branch;
1285 - const char *ref;
1286 - char *ref_free = NULL;
1295
1296 if (!mapped_refs) {
1297 warning(_("You appear to have cloned an empty repository."));
@@ -1293,12 +1301,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1301 if (transport_ls_refs_options.unborn_head_target &&
1302 skip_prefix(transport_ls_refs_options.unborn_head_target,
1303 "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);
1305 } else {
1306 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);
1308 }
1309
1310 /*
@@ -1313,10 +1319,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1319 * a match.
1320 */
1321 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);
1322 }
1323
1324 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)
1338 branch_top.buf, reflog_msg.buf, transport,
1339 !is_local);
1340
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);
1342
1343 /*
1344 * 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)
1365 strbuf_release(&key);
1366 free_refs(mapped_refs);
1367 free_refs(remote_head_points_at);
1368 + free(unborn_head);
1369 free(dir);
1370 free(path);
1371 UNLEAK(repo);