@@ -607,7 +607,7 @@ static void update_remote_refs(const struct ref *refs,
607
607
}
608
608
609
609
static void update_head (const struct ref * our , const struct ref * remote ,
610
- const char * msg )
610
+ const char * unborn , const char * msg )
611
611
{
612
612
const char * head ;
613
613
if (our && skip_prefix (our -> name , "refs/heads/" , & head )) {
@@ -633,6 +633,15 @@ static void update_head(const struct ref *our, const struct ref *remote,
633
633
*/
634
634
update_ref (msg , "HEAD" , & remote -> old_oid , NULL , REF_NO_DEREF ,
635
635
UPDATE_REFS_DIE_ON_ERR );
636
+ } else if (unborn && skip_prefix (unborn , "refs/heads/" , & head )) {
637
+ /*
638
+ * Unborn head from remote; same as "our" case above except
639
+ * that we have no ref to update.
640
+ */
641
+ if (create_symref ("HEAD" , unborn , NULL ) < 0 )
642
+ die (_ ("unable to update HEAD" ));
643
+ if (!option_bare )
644
+ install_branch_config (0 , head , remote_name , unborn );
636
645
}
637
646
}
638
647
@@ -673,7 +682,7 @@ static int checkout(int submodule_progress, int filter_submodules)
673
682
head = resolve_refdup ("HEAD" , RESOLVE_REF_READING , & oid , NULL );
674
683
if (!head ) {
675
684
warning (_ ("remote HEAD refers to nonexistent ref, "
676
- "unable to checkout.\n " ));
685
+ "unable to checkout" ));
677
686
return 0 ;
678
687
}
679
688
if (!strcmp (head , "HEAD" )) {
@@ -877,6 +886,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
877
886
const struct ref * refs , * remote_head ;
878
887
struct ref * remote_head_points_at = NULL ;
879
888
const struct ref * our_head_points_at ;
889
+ char * unborn_head = NULL ;
880
890
struct ref * mapped_refs = NULL ;
881
891
const struct ref * ref ;
882
892
struct strbuf key = STRBUF_INIT ;
@@ -1267,51 +1277,49 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1267
1277
if (transport_fetch_refs (transport , mapped_refs ))
1268
1278
die (_ ("remote transport reported error" ));
1269
1279
}
1270
-
1271
- remote_head = find_ref_by_name (refs , "HEAD" );
1272
- remote_head_points_at =
1273
- guess_remote_head (remote_head , mapped_refs , 0 );
1274
-
1275
- if (option_branch ) {
1276
- our_head_points_at =
1277
- find_remote_branch (mapped_refs , option_branch );
1278
-
1279
- if (!our_head_points_at )
1280
- die (_ ("Remote branch %s not found in upstream %s" ),
1281
- option_branch , remote_name );
1282
- }
1283
- else
1284
- our_head_points_at = remote_head_points_at ;
1285
1280
}
1286
- else {
1287
- const char * branch ;
1288
- const char * ref ;
1289
- char * ref_free = NULL ;
1290
1281
1291
- if (option_branch )
1292
- die (_ ("Remote branch %s not found in upstream %s" ),
1293
- option_branch , remote_name );
1282
+ remote_head = find_ref_by_name (refs , "HEAD" );
1283
+ remote_head_points_at = guess_remote_head (remote_head , mapped_refs , 0 );
1294
1284
1295
- warning (_ ("You appear to have cloned an empty repository." ));
1285
+ if (option_branch ) {
1286
+ our_head_points_at = find_remote_branch (mapped_refs , option_branch );
1287
+ if (!our_head_points_at )
1288
+ die (_ ("Remote branch %s not found in upstream %s" ),
1289
+ option_branch , remote_name );
1290
+ } else if (remote_head_points_at ) {
1291
+ our_head_points_at = remote_head_points_at ;
1292
+ } else if (remote_head ) {
1296
1293
our_head_points_at = NULL ;
1297
- remote_head_points_at = NULL ;
1298
- remote_head = NULL ;
1299
- option_no_checkout = 1 ;
1294
+ } else {
1295
+ const char * branch ;
1296
+
1297
+ if (!mapped_refs ) {
1298
+ warning (_ ("You appear to have cloned an empty repository." ));
1299
+ option_no_checkout = 1 ;
1300
+ }
1300
1301
1301
1302
if (transport_ls_refs_options .unborn_head_target &&
1302
1303
skip_prefix (transport_ls_refs_options .unborn_head_target ,
1303
1304
"refs/heads/" , & branch )) {
1304
- ref = transport_ls_refs_options .unborn_head_target ;
1305
- create_symref ("HEAD" , ref , reflog_msg .buf );
1305
+ unborn_head = xstrdup (transport_ls_refs_options .unborn_head_target );
1306
1306
} else {
1307
1307
branch = git_default_branch_name (0 );
1308
- ref_free = xstrfmt ("refs/heads/%s" , branch );
1309
- ref = ref_free ;
1308
+ unborn_head = xstrfmt ("refs/heads/%s" , branch );
1310
1309
}
1311
1310
1312
- if (!option_bare )
1313
- install_branch_config (0 , branch , remote_name , ref );
1314
- free (ref_free );
1311
+ /*
1312
+ * We may have selected a local default branch name "foo",
1313
+ * and even though the remote's HEAD does not point there,
1314
+ * it may still have a "foo" branch. If so, set it up so
1315
+ * that we can follow the usual checkout code later.
1316
+ *
1317
+ * Note that for an empty repo we'll already have set
1318
+ * option_no_checkout above, which would work against us here.
1319
+ * But for an empty repo, find_remote_branch() can never find
1320
+ * a match.
1321
+ */
1322
+ our_head_points_at = find_remote_branch (mapped_refs , branch );
1315
1323
}
1316
1324
1317
1325
write_refspec_config (src_ref_prefix , our_head_points_at ,
@@ -1331,7 +1339,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1331
1339
branch_top .buf , reflog_msg .buf , transport ,
1332
1340
!is_local );
1333
1341
1334
- update_head (our_head_points_at , remote_head , reflog_msg .buf );
1342
+ update_head (our_head_points_at , remote_head , unborn_head , reflog_msg .buf );
1335
1343
1336
1344
/*
1337
1345
* We want to show progress for recursive submodule clones iff
@@ -1358,6 +1366,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1358
1366
strbuf_release (& key );
1359
1367
free_refs (mapped_refs );
1360
1368
free_refs (remote_head_points_at );
1369
+ free (unborn_head );
1361
1370
free (dir );
1362
1371
free (path );
1363
1372
UNLEAK (repo );
0 commit comments