@@ -1399,11 +1399,40 @@ static int show(int argc, const char **argv, const char *prefix)
1399
1399
return result ;
1400
1400
}
1401
1401
1402
+ static void report_set_head_auto (const char * remote , const char * head_name ,
1403
+ struct strbuf * b_local_head , int was_detached ) {
1404
+ struct strbuf buf_prefix = STRBUF_INIT ;
1405
+ const char * prev_head = NULL ;
1406
+
1407
+ strbuf_addf (& buf_prefix , "refs/remotes/%s/" , remote );
1408
+ skip_prefix (b_local_head -> buf , buf_prefix .buf , & prev_head );
1409
+
1410
+ if (prev_head && !strcmp (prev_head , head_name ))
1411
+ printf (_ ("'%s/HEAD' is unchanged and points to '%s'\n" ),
1412
+ remote , head_name );
1413
+ else if (prev_head )
1414
+ printf (_ ("'%s/HEAD' has changed from '%s' and now points to '%s'\n" ),
1415
+ remote , prev_head , head_name );
1416
+ else if (!b_local_head -> len )
1417
+ printf (_ ("'%s/HEAD' is now created and points to '%s'\n" ),
1418
+ remote , head_name );
1419
+ else if (was_detached && b_local_head -> len )
1420
+ printf (_ ("'%s/HEAD' was detached at '%s' and now points to '%s'\n" ),
1421
+ remote , b_local_head -> buf , head_name );
1422
+ else
1423
+ printf (_ ("'%s/HEAD' used to point to '%s' "
1424
+ "(which is not a remote branch), but now points to '%s'\n" ),
1425
+ remote , b_local_head -> buf , head_name );
1426
+ strbuf_release (& buf_prefix );
1427
+ }
1428
+
1402
1429
static int set_head (int argc , const char * * argv , const char * prefix )
1403
1430
{
1404
- int i , opt_a = 0 , opt_d = 0 , result = 0 ;
1405
- struct strbuf buf = STRBUF_INIT , buf2 = STRBUF_INIT ;
1431
+ int i , opt_a = 0 , opt_d = 0 , result = 0 , was_detached ;
1432
+ struct strbuf b_head = STRBUF_INIT , b_remote_head = STRBUF_INIT ,
1433
+ b_local_head = STRBUF_INIT ;
1406
1434
char * head_name = NULL ;
1435
+ struct ref_store * refs = get_main_ref_store (the_repository );
1407
1436
1408
1437
struct option options [] = {
1409
1438
OPT_BOOL ('a' , "auto" , & opt_a ,
@@ -1415,7 +1444,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
1415
1444
argc = parse_options (argc , argv , prefix , options ,
1416
1445
builtin_remote_sethead_usage , 0 );
1417
1446
if (argc )
1418
- strbuf_addf (& buf , "refs/remotes/%s/HEAD" , argv [0 ]);
1447
+ strbuf_addf (& b_head , "refs/remotes/%s/HEAD" , argv [0 ]);
1419
1448
1420
1449
if (!opt_a && !opt_d && argc == 2 ) {
1421
1450
head_name = xstrdup (argv [1 ]);
@@ -1434,25 +1463,32 @@ static int set_head(int argc, const char **argv, const char *prefix)
1434
1463
head_name = xstrdup (states .heads .items [0 ].string );
1435
1464
free_remote_ref_states (& states );
1436
1465
} else if (opt_d && !opt_a && argc == 1 ) {
1437
- if (refs_delete_ref (get_main_ref_store ( the_repository ) , NULL , buf .buf , NULL , REF_NO_DEREF ))
1438
- result |= error (_ ("Could not delete %s" ), buf .buf );
1466
+ if (refs_delete_ref (refs , NULL , b_head .buf , NULL , REF_NO_DEREF ))
1467
+ result |= error (_ ("Could not delete %s" ), b_head .buf );
1439
1468
} else
1440
1469
usage_with_options (builtin_remote_sethead_usage , options );
1441
1470
1442
- if (head_name ) {
1443
- strbuf_addf (& buf2 , "refs/remotes/%s/%s" , argv [0 ], head_name );
1444
- /* make sure it's valid */
1445
- if (!refs_ref_exists (get_main_ref_store (the_repository ), buf2 .buf ))
1446
- result |= error (_ ("Not a valid ref: %s" ), buf2 .buf );
1447
- else if (refs_update_symref (get_main_ref_store (the_repository ), buf .buf , buf2 .buf , "remote set-head" ))
1448
- result |= error (_ ("Could not setup %s" ), buf .buf );
1449
- else if (opt_a )
1450
- printf ("%s/HEAD set to %s\n" , argv [0 ], head_name );
1451
- free (head_name );
1471
+ if (!head_name )
1472
+ goto cleanup ;
1473
+ strbuf_addf (& b_remote_head , "refs/remotes/%s/%s" , argv [0 ], head_name );
1474
+ if (!refs_ref_exists (refs , b_remote_head .buf )) {
1475
+ result |= error (_ ("Not a valid ref: %s" ), b_remote_head .buf );
1476
+ goto cleanup ;
1452
1477
}
1453
-
1454
- strbuf_release (& buf );
1455
- strbuf_release (& buf2 );
1478
+ was_detached = refs_update_symref_extended (refs , b_head .buf , b_remote_head .buf ,
1479
+ "remote set-head" , & b_local_head , 0 );
1480
+ if (was_detached == -1 ) {
1481
+ result |= error (_ ("Could not set up %s" ), b_head .buf );
1482
+ goto cleanup ;
1483
+ }
1484
+ if (opt_a )
1485
+ report_set_head_auto (argv [0 ], head_name , & b_local_head , was_detached );
1486
+
1487
+ cleanup :
1488
+ free (head_name );
1489
+ strbuf_release (& b_head );
1490
+ strbuf_release (& b_remote_head );
1491
+ strbuf_release (& b_local_head );
1456
1492
return result ;
1457
1493
}
1458
1494
0 commit comments