clone: factor out HEAD update code

While at it, update the comment at "if (remote_head)" Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 16, 2012 at 16:46 UTC f034d3549fa34e65ca09ee9ce721dcf43faefd1d
1 file changed +24 -17
builtin/clone.c
+24 -17
@@ -486,6 +486,29 @@ static void write_followtags(const struct ref *refs, const char *msg)
486 }
487 }
488
489 +static void update_head(const struct ref *our, const struct ref *remote,
490 + const char *msg)
491 +{
492 + if (our) {
493 + /* Local default branch link */
494 + create_symref("HEAD", our->name, NULL);
495 + if (!option_bare) {
496 + const char *head = skip_prefix(our->name, "refs/heads/");
497 + update_ref(msg, "HEAD", our->old_sha1, NULL, 0, DIE_ON_ERR);
498 + install_branch_config(0, head, option_origin, our->name);
499 + }
500 + } else if (remote) {
501 + /*
502 + * We know remote HEAD points to a non-branch, or
503 + * HEAD points to a branch but we don't know which one, or
504 + * we asked for a specific branch but it did not exist.
505 + * Detach HEAD in all these cases.
506 + */
507 + update_ref(msg, "HEAD", remote->old_sha1,
508 + NULL, REF_NODEREF, DIE_ON_ERR);
509 + }
510 +}
511 +
512 static int checkout(void)
513 {
514 unsigned char sha1[20];
@@ -807,23 +830,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
830 reflog_msg.buf);
831 }
832
810 - if (our_head_points_at) {
811 - /* Local default branch link */
812 - create_symref("HEAD", our_head_points_at->name, NULL);
813 - if (!option_bare) {
814 - const char *head = skip_prefix(our_head_points_at->name,
815 - "refs/heads/");
816 - update_ref(reflog_msg.buf, "HEAD",
817 - our_head_points_at->old_sha1,
818 - NULL, 0, DIE_ON_ERR);
819 - install_branch_config(0, head, option_origin,
820 - our_head_points_at->name);
821 - }
822 - } else if (remote_head) {
823 - /* Source had detached HEAD pointing somewhere. */
824 - update_ref(reflog_msg.buf, "HEAD", remote_head->old_sha1,
825 - NULL, REF_NODEREF, DIE_ON_ERR);
826 - }
833 + update_head(our_head_points_at, remote_head, reflog_msg.buf);
834
835 if (transport) {
836 transport_unlock_pack(transport);