@@ -606,7 +606,7 @@ static void update_remote_refs(const struct ref *refs,
606
606
}
607
607
608
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
610
{
611
611
const char * head ;
612
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
632
*/
633
633
update_ref (msg , "HEAD" , & remote -> old_oid , NULL , REF_NO_DEREF ,
634
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 );
635
644
}
636
645
}
637
646
@@ -672,7 +681,7 @@ static int checkout(int submodule_progress, int filter_submodules)
672
681
head = resolve_refdup ("HEAD" , RESOLVE_REF_READING , & oid , NULL );
673
682
if (!head ) {
674
683
warning (_ ("remote HEAD refers to nonexistent ref, "
675
- "unable to checkout.\n " ));
684
+ "unable to checkout" ));
676
685
return 0 ;
677
686
}
678
687
if (!strcmp (head , "HEAD" )) {
@@ -876,6 +885,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
876
885
const struct ref * refs , * remote_head ;
877
886
struct ref * remote_head_points_at = NULL ;
878
887
const struct ref * our_head_points_at ;
888
+ char * unborn_head = NULL ;
879
889
struct ref * mapped_refs = NULL ;
880
890
const struct ref * ref ;
881
891
struct strbuf key = STRBUF_INIT ;
@@ -1266,51 +1276,49 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1266
1276
if (transport_fetch_refs (transport , mapped_refs ))
1267
1277
die (_ ("remote transport reported error" ));
1268
1278
}
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 ;
1284
1279
}
1285
- else {
1286
- const char * branch ;
1287
- const char * ref ;
1288
- char * ref_free = NULL ;
1289
1280
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 );
1293
1283
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 ) {
1295
1292
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
+ }
1299
1300
1300
1301
if (transport_ls_refs_options .unborn_head_target &&
1301
1302
skip_prefix (transport_ls_refs_options .unborn_head_target ,
1302
1303
"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 );
1305
1305
} else {
1306
1306
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 );
1309
1308
}
1310
1309
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 );
1314
1322
}
1315
1323
1316
1324
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)
1330
1338
branch_top .buf , reflog_msg .buf , transport ,
1331
1339
!is_local );
1332
1340
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 );
1334
1342
1335
1343
/*
1336
1344
* 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)
1357
1365
strbuf_release (& key );
1358
1366
free_refs (mapped_refs );
1359
1367
free_refs (remote_head_points_at );
1368
+ free (unborn_head );
1360
1369
free (dir );
1361
1370
free (path );
1362
1371
UNLEAK (repo );
0 commit comments