@@ -349,7 +349,19 @@ static void clear_item(struct refname_hash_entry *item)
349
349
item -> ignore = 1 ;
350
350
}
351
351
352
+
353
+ static void add_already_queued_tags (const char * refname ,
354
+ const struct object_id * old_oid ,
355
+ const struct object_id * new_oid ,
356
+ void * cb_data )
357
+ {
358
+ struct hashmap * queued_tags = cb_data ;
359
+ if (starts_with (refname , "refs/tags/" ) && new_oid )
360
+ (void ) refname_hash_add (queued_tags , refname , new_oid );
361
+ }
362
+
352
363
static void find_non_local_tags (const struct ref * refs ,
364
+ struct ref_transaction * transaction ,
353
365
struct ref * * head ,
354
366
struct ref * * * tail )
355
367
{
@@ -367,6 +379,16 @@ static void find_non_local_tags(const struct ref *refs,
367
379
create_fetch_oidset (head , & fetch_oids );
368
380
369
381
for_each_ref (add_one_refname , & existing_refs );
382
+
383
+ /*
384
+ * If we already have a transaction, then we need to filter out all
385
+ * tags which have already been queued up.
386
+ */
387
+ if (transaction )
388
+ ref_transaction_for_each_queued_update (transaction ,
389
+ add_already_queued_tags ,
390
+ & existing_refs );
391
+
370
392
for (ref = refs ; ref ; ref = ref -> next ) {
371
393
if (!starts_with (ref -> name , "refs/tags/" ))
372
394
continue ;
@@ -600,7 +622,7 @@ static struct ref *get_ref_map(struct remote *remote,
600
622
/* also fetch all tags */
601
623
get_fetch_map (remote_refs , tag_refspec , & tail , 0 );
602
624
else if (tags == TAGS_DEFAULT && * autotags )
603
- find_non_local_tags (remote_refs , & ref_map , & tail );
625
+ find_non_local_tags (remote_refs , NULL , & ref_map , & tail );
604
626
605
627
/* Now append any refs to be updated opportunistically: */
606
628
* tail = orefs ;
@@ -1083,23 +1105,18 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
1083
1105
"to avoid this check\n" );
1084
1106
1085
1107
static int store_updated_refs (const char * raw_url , const char * remote_name ,
1086
- int connectivity_checked , struct ref * ref_map ,
1087
- struct worktree * * worktrees )
1108
+ int connectivity_checked ,
1109
+ struct ref_transaction * transaction , struct ref * ref_map ,
1110
+ struct fetch_head * fetch_head , struct worktree * * worktrees )
1088
1111
{
1089
- struct fetch_head fetch_head ;
1090
1112
int url_len , i , rc = 0 ;
1091
1113
struct strbuf note = STRBUF_INIT , err = STRBUF_INIT ;
1092
- struct ref_transaction * transaction = NULL ;
1093
1114
const char * what , * kind ;
1094
1115
struct ref * rm ;
1095
1116
char * url ;
1096
1117
int want_status ;
1097
1118
int summary_width = transport_summary_width (ref_map );
1098
1119
1099
- rc = open_fetch_head (& fetch_head );
1100
- if (rc )
1101
- return -1 ;
1102
-
1103
1120
if (raw_url )
1104
1121
url = transport_anonymize_url (raw_url );
1105
1122
else
@@ -1115,14 +1132,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1115
1132
}
1116
1133
}
1117
1134
1118
- if (atomic_fetch ) {
1119
- transaction = ref_transaction_begin (& err );
1120
- if (!transaction ) {
1121
- error ("%s" , err .buf );
1122
- goto abort ;
1123
- }
1124
- }
1125
-
1126
1135
prepare_format_display (ref_map );
1127
1136
1128
1137
/*
@@ -1206,7 +1215,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1206
1215
strbuf_addf (& note , "'%s' of " , what );
1207
1216
}
1208
1217
1209
- append_fetch_head (& fetch_head , & rm -> old_oid ,
1218
+ append_fetch_head (fetch_head , & rm -> old_oid ,
1210
1219
rm -> fetch_head_status ,
1211
1220
note .buf , url , url_len );
1212
1221
@@ -1238,17 +1247,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1238
1247
}
1239
1248
}
1240
1249
1241
- if (!rc && transaction ) {
1242
- rc = ref_transaction_commit (transaction , & err );
1243
- if (rc ) {
1244
- error ("%s" , err .buf );
1245
- goto abort ;
1246
- }
1247
- }
1248
-
1249
- if (!rc )
1250
- commit_fetch_head (& fetch_head );
1251
-
1252
1250
if (rc & STORE_REF_ERROR_DF_CONFLICT )
1253
1251
error (_ ("some local refs could not be updated; try running\n"
1254
1252
" 'git remote prune %s' to remove any old, conflicting "
@@ -1266,9 +1264,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1266
1264
abort :
1267
1265
strbuf_release (& note );
1268
1266
strbuf_release (& err );
1269
- ref_transaction_free (transaction );
1270
1267
free (url );
1271
- close_fetch_head (& fetch_head );
1272
1268
return rc ;
1273
1269
}
1274
1270
@@ -1308,7 +1304,9 @@ static int check_exist_and_connected(struct ref *ref_map)
1308
1304
}
1309
1305
1310
1306
static int fetch_and_consume_refs (struct transport * transport ,
1307
+ struct ref_transaction * transaction ,
1311
1308
struct ref * ref_map ,
1309
+ struct fetch_head * fetch_head ,
1312
1310
struct worktree * * worktrees )
1313
1311
{
1314
1312
int connectivity_checked = 1 ;
@@ -1331,19 +1329,23 @@ static int fetch_and_consume_refs(struct transport *transport,
1331
1329
1332
1330
trace2_region_enter ("fetch" , "consume_refs" , the_repository );
1333
1331
ret = store_updated_refs (transport -> url , transport -> remote -> name ,
1334
- connectivity_checked , ref_map , worktrees );
1332
+ connectivity_checked , transaction , ref_map ,
1333
+ fetch_head , worktrees );
1335
1334
trace2_region_leave ("fetch" , "consume_refs" , the_repository );
1336
1335
1337
1336
out :
1338
1337
transport_unlock_pack (transport , 0 );
1339
1338
return ret ;
1340
1339
}
1341
1340
1342
- static int prune_refs (struct refspec * rs , struct ref * ref_map ,
1341
+ static int prune_refs (struct refspec * rs ,
1342
+ struct ref_transaction * transaction ,
1343
+ struct ref * ref_map ,
1343
1344
const char * raw_url )
1344
1345
{
1345
1346
int url_len , i , result = 0 ;
1346
1347
struct ref * ref , * stale_refs = get_stale_heads (rs , ref_map );
1348
+ struct strbuf err = STRBUF_INIT ;
1347
1349
char * url ;
1348
1350
int summary_width = transport_summary_width (stale_refs );
1349
1351
const char * dangling_msg = dry_run
@@ -1364,13 +1366,22 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
1364
1366
url_len = i - 3 ;
1365
1367
1366
1368
if (!dry_run ) {
1367
- struct string_list refnames = STRING_LIST_INIT_NODUP ;
1369
+ if (transaction ) {
1370
+ for (ref = stale_refs ; ref ; ref = ref -> next ) {
1371
+ result = ref_transaction_delete (transaction , ref -> name , NULL , 0 ,
1372
+ "fetch: prune" , & err );
1373
+ if (result )
1374
+ goto cleanup ;
1375
+ }
1376
+ } else {
1377
+ struct string_list refnames = STRING_LIST_INIT_NODUP ;
1368
1378
1369
- for (ref = stale_refs ; ref ; ref = ref -> next )
1370
- string_list_append (& refnames , ref -> name );
1379
+ for (ref = stale_refs ; ref ; ref = ref -> next )
1380
+ string_list_append (& refnames , ref -> name );
1371
1381
1372
- result = delete_refs ("fetch: prune" , & refnames , 0 );
1373
- string_list_clear (& refnames , 0 );
1382
+ result = delete_refs ("fetch: prune" , & refnames , 0 );
1383
+ string_list_clear (& refnames , 0 );
1384
+ }
1374
1385
}
1375
1386
1376
1387
if (verbosity >= 0 ) {
@@ -1389,6 +1400,8 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
1389
1400
}
1390
1401
}
1391
1402
1403
+ cleanup :
1404
+ strbuf_release (& err );
1392
1405
free (url );
1393
1406
free_refs (stale_refs );
1394
1407
return result ;
@@ -1503,10 +1516,13 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
1503
1516
return transport ;
1504
1517
}
1505
1518
1506
- static void backfill_tags (struct transport * transport , struct ref * ref_map ,
1507
- struct worktree * * worktrees )
1519
+ static int backfill_tags (struct transport * transport ,
1520
+ struct ref_transaction * transaction ,
1521
+ struct ref * ref_map ,
1522
+ struct fetch_head * fetch_head ,
1523
+ struct worktree * * worktrees )
1508
1524
{
1509
- int cannot_reuse ;
1525
+ int retcode , cannot_reuse ;
1510
1526
1511
1527
/*
1512
1528
* Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
@@ -1525,25 +1541,30 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map,
1525
1541
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , NULL );
1526
1542
transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
1527
1543
transport_set_option (transport , TRANS_OPT_DEEPEN_RELATIVE , NULL );
1528
- fetch_and_consume_refs (transport , ref_map , worktrees );
1544
+ retcode = fetch_and_consume_refs (transport , transaction , ref_map , fetch_head , worktrees );
1529
1545
1530
1546
if (gsecondary ) {
1531
1547
transport_disconnect (gsecondary );
1532
1548
gsecondary = NULL ;
1533
1549
}
1550
+
1551
+ return retcode ;
1534
1552
}
1535
1553
1536
1554
static int do_fetch (struct transport * transport ,
1537
1555
struct refspec * rs )
1538
1556
{
1539
- struct ref * ref_map ;
1557
+ struct ref_transaction * transaction = NULL ;
1558
+ struct ref * ref_map = NULL ;
1540
1559
int autotags = (transport -> remote -> fetch_tags == 1 );
1541
1560
int retcode = 0 ;
1542
1561
const struct ref * remote_refs ;
1543
1562
struct transport_ls_refs_options transport_ls_refs_options =
1544
1563
TRANSPORT_LS_REFS_OPTIONS_INIT ;
1545
1564
int must_list_refs = 1 ;
1546
1565
struct worktree * * worktrees = get_worktrees ();
1566
+ struct fetch_head fetch_head = { 0 };
1567
+ struct strbuf err = STRBUF_INIT ;
1547
1568
1548
1569
if (tags == TAGS_DEFAULT ) {
1549
1570
if (transport -> remote -> fetch_tags == 2 )
@@ -1601,6 +1622,18 @@ static int do_fetch(struct transport *transport,
1601
1622
if (!update_head_ok )
1602
1623
check_not_current_branch (ref_map , worktrees );
1603
1624
1625
+ retcode = open_fetch_head (& fetch_head );
1626
+ if (retcode )
1627
+ goto cleanup ;
1628
+
1629
+ if (atomic_fetch ) {
1630
+ transaction = ref_transaction_begin (& err );
1631
+ if (!transaction ) {
1632
+ retcode = error ("%s" , err .buf );
1633
+ goto cleanup ;
1634
+ }
1635
+ }
1636
+
1604
1637
if (tags == TAGS_DEFAULT && autotags )
1605
1638
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , "1" );
1606
1639
if (prune ) {
@@ -1610,21 +1643,61 @@ static int do_fetch(struct transport *transport,
1610
1643
* don't care whether --tags was specified.
1611
1644
*/
1612
1645
if (rs -> nr ) {
1613
- retcode = prune_refs (rs , ref_map , transport -> url );
1646
+ retcode = prune_refs (rs , transaction , ref_map , transport -> url );
1614
1647
} else {
1615
1648
retcode = prune_refs (& transport -> remote -> fetch ,
1616
- ref_map ,
1649
+ transaction , ref_map ,
1617
1650
transport -> url );
1618
1651
}
1619
1652
if (retcode != 0 )
1620
1653
retcode = 1 ;
1621
1654
}
1622
- if ( fetch_and_consume_refs ( transport , ref_map , worktrees )) {
1623
- free_refs ( ref_map );
1655
+
1656
+ if ( fetch_and_consume_refs ( transport , transaction , ref_map , & fetch_head , worktrees )) {
1624
1657
retcode = 1 ;
1625
1658
goto cleanup ;
1626
1659
}
1627
1660
1661
+ /*
1662
+ * If neither --no-tags nor --tags was specified, do automated tag
1663
+ * following.
1664
+ */
1665
+ if (tags == TAGS_DEFAULT && autotags ) {
1666
+ struct ref * tags_ref_map = NULL , * * tail = & tags_ref_map ;
1667
+
1668
+ find_non_local_tags (remote_refs , transaction , & tags_ref_map , & tail );
1669
+ if (tags_ref_map ) {
1670
+ /*
1671
+ * If backfilling of tags fails then we want to tell
1672
+ * the user so, but we have to continue regardless to
1673
+ * populate upstream information of the references we
1674
+ * have already fetched above. The exception though is
1675
+ * when `--atomic` is passed: in that case we'll abort
1676
+ * the transaction and don't commit anything.
1677
+ */
1678
+ if (backfill_tags (transport , transaction , tags_ref_map ,
1679
+ & fetch_head , worktrees ))
1680
+ retcode = 1 ;
1681
+ }
1682
+
1683
+ free_refs (tags_ref_map );
1684
+ }
1685
+
1686
+ if (transaction ) {
1687
+ if (retcode )
1688
+ goto cleanup ;
1689
+
1690
+ retcode = ref_transaction_commit (transaction , & err );
1691
+ if (retcode ) {
1692
+ error ("%s" , err .buf );
1693
+ ref_transaction_free (transaction );
1694
+ transaction = NULL ;
1695
+ goto cleanup ;
1696
+ }
1697
+ }
1698
+
1699
+ commit_fetch_head (& fetch_head );
1700
+
1628
1701
if (set_upstream ) {
1629
1702
struct branch * branch = branch_get ("HEAD" );
1630
1703
struct ref * rm ;
@@ -1644,7 +1717,7 @@ static int do_fetch(struct transport *transport,
1644
1717
if (!rm -> peer_ref ) {
1645
1718
if (source_ref ) {
1646
1719
warning (_ ("multiple branches detected, incompatible with --set-upstream" ));
1647
- goto skip ;
1720
+ goto cleanup ;
1648
1721
} else {
1649
1722
source_ref = rm ;
1650
1723
}
@@ -1658,7 +1731,7 @@ static int do_fetch(struct transport *transport,
1658
1731
warning (_ ("could not set upstream of HEAD to '%s' from '%s' when "
1659
1732
"it does not point to any branch." ),
1660
1733
shortname , transport -> remote -> name );
1661
- goto skip ;
1734
+ goto cleanup ;
1662
1735
}
1663
1736
1664
1737
if (!strcmp (source_ref -> name , "HEAD" ) ||
@@ -1678,21 +1751,16 @@ static int do_fetch(struct transport *transport,
1678
1751
"you need to specify exactly one branch with the --set-upstream option" ));
1679
1752
}
1680
1753
}
1681
- skip :
1682
- free_refs (ref_map );
1683
1754
1684
- /* if neither --no-tags nor --tags was specified, do automated tag
1685
- * following ... */
1686
- if (tags == TAGS_DEFAULT && autotags ) {
1687
- struct ref * * tail = & ref_map ;
1688
- ref_map = NULL ;
1689
- find_non_local_tags (remote_refs , & ref_map , & tail );
1690
- if (ref_map )
1691
- backfill_tags (transport , ref_map , worktrees );
1692
- free_refs (ref_map );
1755
+ cleanup :
1756
+ if (retcode && transaction ) {
1757
+ ref_transaction_abort (transaction , & err );
1758
+ error ("%s" , err .buf );
1693
1759
}
1694
1760
1695
- cleanup :
1761
+ close_fetch_head (& fetch_head );
1762
+ strbuf_release (& err );
1763
+ free_refs (ref_map );
1696
1764
free_worktrees (worktrees );
1697
1765
return retcode ;
1698
1766
}
0 commit comments