@@ -606,7 +606,7 @@ static void update_remote_refs(const struct ref *refs,
606606}
607607
608608static 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
@@ -672,7 +681,7 @@ static int checkout(int submodule_progress, int filter_submodules)
672681 head = resolve_refdup ("HEAD" , RESOLVE_REF_READING , & oid , NULL );
673682 if (!head ) {
674683 warning (_ ("remote HEAD refers to nonexistent ref, "
675- "unable to checkout.\n " ));
684+ "unable to checkout" ));
676685 return 0 ;
677686 }
678687 if (!strcmp (head , "HEAD" )) {
@@ -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 ;
@@ -1266,51 +1276,49 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12661276 if (transport_fetch_refs (transport , mapped_refs ))
12671277 die (_ ("remote transport reported error" ));
12681278 }
1269-
1270- remote_head = find_ref_by_name (refs , "HEAD" );
1271- remote_head_points_at =
1272- guess_remote_head (remote_head , mapped_refs , 0 );
1273-
1274- if (option_branch ) {
1275- our_head_points_at =
1276- find_remote_branch (mapped_refs , option_branch );
1277-
1278- if (!our_head_points_at )
1279- die (_ ("Remote branch %s not found in upstream %s" ),
1280- option_branch , remote_name );
1281- }
1282- else
1283- our_head_points_at = remote_head_points_at ;
12841279 }
1285- else {
1286- const char * branch ;
1287- const char * ref ;
1288- char * ref_free = NULL ;
12891280
1290- if (option_branch )
1291- die (_ ("Remote branch %s not found in upstream %s" ),
1292- option_branch , remote_name );
1281+ remote_head = find_ref_by_name (refs , "HEAD" );
1282+ remote_head_points_at = guess_remote_head (remote_head , mapped_refs , 0 );
12931283
1294- warning (_ ("You appear to have cloned an empty repository." ));
1284+ if (option_branch ) {
1285+ our_head_points_at = find_remote_branch (mapped_refs , option_branch );
1286+ if (!our_head_points_at )
1287+ die (_ ("Remote branch %s not found in upstream %s" ),
1288+ option_branch , remote_name );
1289+ } else if (remote_head_points_at ) {
1290+ our_head_points_at = remote_head_points_at ;
1291+ } else if (remote_head ) {
12951292 our_head_points_at = NULL ;
1296- remote_head_points_at = NULL ;
1297- remote_head = NULL ;
1298- option_no_checkout = 1 ;
1293+ } else {
1294+ const char * branch ;
1295+
1296+ if (!mapped_refs ) {
1297+ warning (_ ("You appear to have cloned an empty repository." ));
1298+ option_no_checkout = 1 ;
1299+ }
12991300
13001301 if (transport_ls_refs_options .unborn_head_target &&
13011302 skip_prefix (transport_ls_refs_options .unborn_head_target ,
13021303 "refs/heads/" , & branch )) {
1303- ref = transport_ls_refs_options .unborn_head_target ;
1304- create_symref ("HEAD" , ref , reflog_msg .buf );
1304+ unborn_head = xstrdup (transport_ls_refs_options .unborn_head_target );
13051305 } else {
13061306 branch = git_default_branch_name (0 );
1307- ref_free = xstrfmt ("refs/heads/%s" , branch );
1308- ref = ref_free ;
1307+ unborn_head = xstrfmt ("refs/heads/%s" , branch );
13091308 }
13101309
1311- if (!option_bare )
1312- install_branch_config (0 , branch , remote_name , ref );
1313- free (ref_free );
1310+ /*
1311+ * We may have selected a local default branch name "foo",
1312+ * and even though the remote's HEAD does not point there,
1313+ * it may still have a "foo" branch. If so, set it up so
1314+ * that we can follow the usual checkout code later.
1315+ *
1316+ * Note that for an empty repo we'll already have set
1317+ * option_no_checkout above, which would work against us here.
1318+ * But for an empty repo, find_remote_branch() can never find
1319+ * a match.
1320+ */
1321+ our_head_points_at = find_remote_branch (mapped_refs , branch );
13141322 }
13151323
13161324 write_refspec_config (src_ref_prefix , our_head_points_at ,
@@ -1330,7 +1338,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13301338 branch_top .buf , reflog_msg .buf , transport ,
13311339 !is_local );
13321340
1333- 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 );
13341342
13351343 /*
13361344 * We want to show progress for recursive submodule clones iff
@@ -1357,6 +1365,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13571365 strbuf_release (& key );
13581366 free_refs (mapped_refs );
13591367 free_refs (remote_head_points_at );
1368+ free (unborn_head );
13601369 free (dir );
13611370 free (path );
13621371 UNLEAK (repo );
0 commit comments