@@ -486,6 +486,29 @@ static void write_followtags(const struct ref *refs, const char *msg)
486486 }
487487}
488488
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+
489512static int checkout (void )
490513{
491514 unsigned char sha1 [20 ];
@@ -807,23 +830,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
807830 reflog_msg .buf );
808831 }
809832
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 );
827834
828835 if (transport ) {
829836 transport_unlock_pack (transport );
0 commit comments