28
28
#include "promisor-remote.h"
29
29
#include "commit-graph.h"
30
30
#include "shallow.h"
31
+ #include "worktree.h"
31
32
32
33
#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
33
34
@@ -848,13 +849,12 @@ static void format_display(struct strbuf *display, char code,
848
849
849
850
static int update_local_ref (struct ref * ref ,
850
851
struct ref_transaction * transaction ,
851
- const char * remote ,
852
- const struct ref * remote_ref ,
853
- struct strbuf * display ,
854
- int summary_width )
852
+ const char * remote , const struct ref * remote_ref ,
853
+ struct strbuf * display , int summary_width ,
854
+ struct worktree * * worktrees )
855
855
{
856
856
struct commit * current = NULL , * updated ;
857
- struct branch * current_branch = branch_get ( NULL ) ;
857
+ const struct worktree * wt ;
858
858
const char * pretty_ref = prettify_refname (ref -> name );
859
859
int fast_forward = 0 ;
860
860
@@ -868,16 +868,17 @@ static int update_local_ref(struct ref *ref,
868
868
return 0 ;
869
869
}
870
870
871
- if (current_branch &&
872
- !strcmp (ref -> name , current_branch -> name ) &&
873
- !(update_head_ok || is_bare_repository ()) &&
874
- !is_null_oid (& ref -> old_oid )) {
871
+ if (!update_head_ok &&
872
+ (wt = find_shared_symref (worktrees , "HEAD" , ref -> name )) &&
873
+ !wt -> is_bare && !is_null_oid (& ref -> old_oid )) {
875
874
/*
876
875
* If this is the head, and it's not okay to update
877
876
* the head, and the old value of the head isn't empty...
878
877
*/
879
878
format_display (display , '!' , _ ("[rejected]" ),
880
- _ ("can't fetch in current branch" ),
879
+ wt -> is_current ?
880
+ _ ("can't fetch in current branch" ) :
881
+ _ ("checked out in another worktree" ),
881
882
remote , pretty_ref , summary_width );
882
883
return 1 ;
883
884
}
@@ -1076,7 +1077,8 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
1076
1077
"to avoid this check\n" );
1077
1078
1078
1079
static int store_updated_refs (const char * raw_url , const char * remote_name ,
1079
- int connectivity_checked , struct ref * ref_map )
1080
+ int connectivity_checked , struct ref * ref_map ,
1081
+ struct worktree * * worktrees )
1080
1082
{
1081
1083
struct fetch_head fetch_head ;
1082
1084
int url_len , i , rc = 0 ;
@@ -1205,7 +1207,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1205
1207
strbuf_reset (& note );
1206
1208
if (ref ) {
1207
1209
rc |= update_local_ref (ref , transaction , what ,
1208
- rm , & note , summary_width );
1210
+ rm , & note , summary_width ,
1211
+ worktrees );
1209
1212
free (ref );
1210
1213
} else if (write_fetch_head || dry_run ) {
1211
1214
/*
@@ -1298,7 +1301,9 @@ static int check_exist_and_connected(struct ref *ref_map)
1298
1301
return check_connected (iterate_ref_map , & rm , & opt );
1299
1302
}
1300
1303
1301
- static int fetch_and_consume_refs (struct transport * transport , struct ref * ref_map )
1304
+ static int fetch_and_consume_refs (struct transport * transport ,
1305
+ struct ref * ref_map ,
1306
+ struct worktree * * worktrees )
1302
1307
{
1303
1308
int connectivity_checked = 1 ;
1304
1309
int ret ;
@@ -1319,10 +1324,8 @@ static int fetch_and_consume_refs(struct transport *transport, struct ref *ref_m
1319
1324
}
1320
1325
1321
1326
trace2_region_enter ("fetch" , "consume_refs" , the_repository );
1322
- ret = store_updated_refs (transport -> url ,
1323
- transport -> remote -> name ,
1324
- connectivity_checked ,
1325
- ref_map );
1327
+ ret = store_updated_refs (transport -> url , transport -> remote -> name ,
1328
+ connectivity_checked , ref_map , worktrees );
1326
1329
trace2_region_leave ("fetch" , "consume_refs" , the_repository );
1327
1330
1328
1331
out :
@@ -1385,19 +1388,18 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
1385
1388
return result ;
1386
1389
}
1387
1390
1388
- static void check_not_current_branch (struct ref * ref_map )
1391
+ static void check_not_current_branch (struct ref * ref_map ,
1392
+ struct worktree * * worktrees )
1389
1393
{
1390
- struct branch * current_branch = branch_get (NULL );
1391
-
1392
- if (is_bare_repository () || !current_branch )
1393
- return ;
1394
-
1394
+ const struct worktree * wt ;
1395
1395
for (; ref_map ; ref_map = ref_map -> next )
1396
- if (ref_map -> peer_ref && !strcmp (current_branch -> refname ,
1397
- ref_map -> peer_ref -> name ))
1398
- die (_ ("refusing to fetch into current branch %s "
1399
- "of non-bare repository" ),
1400
- current_branch -> refname );
1396
+ if (ref_map -> peer_ref &&
1397
+ (wt = find_shared_symref (worktrees , "HEAD" ,
1398
+ ref_map -> peer_ref -> name )) &&
1399
+ !wt -> is_bare )
1400
+ die (_ ("refusing to fetch into branch '%s' "
1401
+ "checked out at '%s'" ),
1402
+ ref_map -> peer_ref -> name , wt -> path );
1401
1403
}
1402
1404
1403
1405
static int truncate_fetch_head (void )
@@ -1495,7 +1497,8 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
1495
1497
return transport ;
1496
1498
}
1497
1499
1498
- static void backfill_tags (struct transport * transport , struct ref * ref_map )
1500
+ static void backfill_tags (struct transport * transport , struct ref * ref_map ,
1501
+ struct worktree * * worktrees )
1499
1502
{
1500
1503
int cannot_reuse ;
1501
1504
@@ -1516,7 +1519,7 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
1516
1519
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , NULL );
1517
1520
transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
1518
1521
transport_set_option (transport , TRANS_OPT_DEEPEN_RELATIVE , NULL );
1519
- fetch_and_consume_refs (transport , ref_map );
1522
+ fetch_and_consume_refs (transport , ref_map , worktrees );
1520
1523
1521
1524
if (gsecondary ) {
1522
1525
transport_disconnect (gsecondary );
@@ -1534,6 +1537,7 @@ static int do_fetch(struct transport *transport,
1534
1537
struct transport_ls_refs_options transport_ls_refs_options =
1535
1538
TRANSPORT_LS_REFS_OPTIONS_INIT ;
1536
1539
int must_list_refs = 1 ;
1540
+ struct worktree * * worktrees = get_worktrees ();
1537
1541
1538
1542
if (tags == TAGS_DEFAULT ) {
1539
1543
if (transport -> remote -> fetch_tags == 2 )
@@ -1589,7 +1593,7 @@ static int do_fetch(struct transport *transport,
1589
1593
ref_map = get_ref_map (transport -> remote , remote_refs , rs ,
1590
1594
tags , & autotags );
1591
1595
if (!update_head_ok )
1592
- check_not_current_branch (ref_map );
1596
+ check_not_current_branch (ref_map , worktrees );
1593
1597
1594
1598
if (tags == TAGS_DEFAULT && autotags )
1595
1599
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , "1" );
@@ -1607,7 +1611,7 @@ static int do_fetch(struct transport *transport,
1607
1611
transport -> url );
1608
1612
}
1609
1613
}
1610
- if (fetch_and_consume_refs (transport , ref_map )) {
1614
+ if (fetch_and_consume_refs (transport , ref_map , worktrees )) {
1611
1615
free_refs (ref_map );
1612
1616
retcode = 1 ;
1613
1617
goto cleanup ;
@@ -1656,7 +1660,7 @@ static int do_fetch(struct transport *transport,
1656
1660
"you need to specify exactly one branch with the --set-upstream option" ));
1657
1661
}
1658
1662
}
1659
- skip :
1663
+ skip :
1660
1664
free_refs (ref_map );
1661
1665
1662
1666
/* if neither --no-tags nor --tags was specified, do automated tag
@@ -1666,11 +1670,12 @@ static int do_fetch(struct transport *transport,
1666
1670
ref_map = NULL ;
1667
1671
find_non_local_tags (remote_refs , & ref_map , & tail );
1668
1672
if (ref_map )
1669
- backfill_tags (transport , ref_map );
1673
+ backfill_tags (transport , ref_map , worktrees );
1670
1674
free_refs (ref_map );
1671
1675
}
1672
1676
1673
- cleanup :
1677
+ cleanup :
1678
+ free_worktrees (worktrees );
1674
1679
return retcode ;
1675
1680
}
1676
1681
0 commit comments