@@ -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 = 0 ;
1098
1119
1099
- rc = open_fetch_head (& fetch_head );
1100
- if (rc )
1101
- return -1 ;
1102
-
1103
1120
if (verbosity >= 0 )
1104
1121
summary_width = transport_summary_width (ref_map );
1105
1122
@@ -1118,14 +1135,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1118
1135
}
1119
1136
}
1120
1137
1121
- if (atomic_fetch ) {
1122
- transaction = ref_transaction_begin (& err );
1123
- if (!transaction ) {
1124
- error ("%s" , err .buf );
1125
- goto abort ;
1126
- }
1127
- }
1128
-
1129
1138
prepare_format_display (ref_map );
1130
1139
1131
1140
/*
@@ -1209,7 +1218,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1209
1218
strbuf_addf (& note , "'%s' of " , what );
1210
1219
}
1211
1220
1212
- append_fetch_head (& fetch_head , & rm -> old_oid ,
1221
+ append_fetch_head (fetch_head , & rm -> old_oid ,
1213
1222
rm -> fetch_head_status ,
1214
1223
note .buf , url , url_len );
1215
1224
@@ -1241,17 +1250,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1241
1250
}
1242
1251
}
1243
1252
1244
- if (!rc && transaction ) {
1245
- rc = ref_transaction_commit (transaction , & err );
1246
- if (rc ) {
1247
- error ("%s" , err .buf );
1248
- goto abort ;
1249
- }
1250
- }
1251
-
1252
- if (!rc )
1253
- commit_fetch_head (& fetch_head );
1254
-
1255
1253
if (rc & STORE_REF_ERROR_DF_CONFLICT )
1256
1254
error (_ ("some local refs could not be updated; try running\n"
1257
1255
" 'git remote prune %s' to remove any old, conflicting "
@@ -1269,9 +1267,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1269
1267
abort :
1270
1268
strbuf_release (& note );
1271
1269
strbuf_release (& err );
1272
- ref_transaction_free (transaction );
1273
1270
free (url );
1274
- close_fetch_head (& fetch_head );
1275
1271
return rc ;
1276
1272
}
1277
1273
@@ -1311,7 +1307,9 @@ static int check_exist_and_connected(struct ref *ref_map)
1311
1307
}
1312
1308
1313
1309
static int fetch_and_consume_refs (struct transport * transport ,
1310
+ struct ref_transaction * transaction ,
1314
1311
struct ref * ref_map ,
1312
+ struct fetch_head * fetch_head ,
1315
1313
struct worktree * * worktrees )
1316
1314
{
1317
1315
int connectivity_checked = 1 ;
@@ -1334,19 +1332,23 @@ static int fetch_and_consume_refs(struct transport *transport,
1334
1332
1335
1333
trace2_region_enter ("fetch" , "consume_refs" , the_repository );
1336
1334
ret = store_updated_refs (transport -> url , transport -> remote -> name ,
1337
- connectivity_checked , ref_map , worktrees );
1335
+ connectivity_checked , transaction , ref_map ,
1336
+ fetch_head , worktrees );
1338
1337
trace2_region_leave ("fetch" , "consume_refs" , the_repository );
1339
1338
1340
1339
out :
1341
1340
transport_unlock_pack (transport , 0 );
1342
1341
return ret ;
1343
1342
}
1344
1343
1345
- static int prune_refs (struct refspec * rs , struct ref * ref_map ,
1344
+ static int prune_refs (struct refspec * rs ,
1345
+ struct ref_transaction * transaction ,
1346
+ struct ref * ref_map ,
1346
1347
const char * raw_url )
1347
1348
{
1348
1349
int url_len , i , result = 0 ;
1349
1350
struct ref * ref , * stale_refs = get_stale_heads (rs , ref_map );
1351
+ struct strbuf err = STRBUF_INIT ;
1350
1352
char * url ;
1351
1353
const char * dangling_msg = dry_run
1352
1354
? _ (" (%s will become dangling)" )
@@ -1366,13 +1368,22 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
1366
1368
url_len = i - 3 ;
1367
1369
1368
1370
if (!dry_run ) {
1369
- struct string_list refnames = STRING_LIST_INIT_NODUP ;
1371
+ if (transaction ) {
1372
+ for (ref = stale_refs ; ref ; ref = ref -> next ) {
1373
+ result = ref_transaction_delete (transaction , ref -> name , NULL , 0 ,
1374
+ "fetch: prune" , & err );
1375
+ if (result )
1376
+ goto cleanup ;
1377
+ }
1378
+ } else {
1379
+ struct string_list refnames = STRING_LIST_INIT_NODUP ;
1370
1380
1371
- for (ref = stale_refs ; ref ; ref = ref -> next )
1372
- string_list_append (& refnames , ref -> name );
1381
+ for (ref = stale_refs ; ref ; ref = ref -> next )
1382
+ string_list_append (& refnames , ref -> name );
1373
1383
1374
- result = delete_refs ("fetch: prune" , & refnames , 0 );
1375
- string_list_clear (& refnames , 0 );
1384
+ result = delete_refs ("fetch: prune" , & refnames , 0 );
1385
+ string_list_clear (& refnames , 0 );
1386
+ }
1376
1387
}
1377
1388
1378
1389
if (verbosity >= 0 ) {
@@ -1393,6 +1404,8 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
1393
1404
}
1394
1405
}
1395
1406
1407
+ cleanup :
1408
+ strbuf_release (& err );
1396
1409
free (url );
1397
1410
free_refs (stale_refs );
1398
1411
return result ;
@@ -1507,10 +1520,13 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
1507
1520
return transport ;
1508
1521
}
1509
1522
1510
- static void backfill_tags (struct transport * transport , struct ref * ref_map ,
1511
- struct worktree * * worktrees )
1523
+ static int backfill_tags (struct transport * transport ,
1524
+ struct ref_transaction * transaction ,
1525
+ struct ref * ref_map ,
1526
+ struct fetch_head * fetch_head ,
1527
+ struct worktree * * worktrees )
1512
1528
{
1513
- int cannot_reuse ;
1529
+ int retcode , cannot_reuse ;
1514
1530
1515
1531
/*
1516
1532
* Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
@@ -1529,25 +1545,30 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map,
1529
1545
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , NULL );
1530
1546
transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
1531
1547
transport_set_option (transport , TRANS_OPT_DEEPEN_RELATIVE , NULL );
1532
- fetch_and_consume_refs (transport , ref_map , worktrees );
1548
+ retcode = fetch_and_consume_refs (transport , transaction , ref_map , fetch_head , worktrees );
1533
1549
1534
1550
if (gsecondary ) {
1535
1551
transport_disconnect (gsecondary );
1536
1552
gsecondary = NULL ;
1537
1553
}
1554
+
1555
+ return retcode ;
1538
1556
}
1539
1557
1540
1558
static int do_fetch (struct transport * transport ,
1541
1559
struct refspec * rs )
1542
1560
{
1543
- struct ref * ref_map ;
1561
+ struct ref_transaction * transaction = NULL ;
1562
+ struct ref * ref_map = NULL ;
1544
1563
int autotags = (transport -> remote -> fetch_tags == 1 );
1545
1564
int retcode = 0 ;
1546
1565
const struct ref * remote_refs ;
1547
1566
struct transport_ls_refs_options transport_ls_refs_options =
1548
1567
TRANSPORT_LS_REFS_OPTIONS_INIT ;
1549
1568
int must_list_refs = 1 ;
1550
1569
struct worktree * * worktrees = get_worktrees ();
1570
+ struct fetch_head fetch_head = { 0 };
1571
+ struct strbuf err = STRBUF_INIT ;
1551
1572
1552
1573
if (tags == TAGS_DEFAULT ) {
1553
1574
if (transport -> remote -> fetch_tags == 2 )
@@ -1605,6 +1626,18 @@ static int do_fetch(struct transport *transport,
1605
1626
if (!update_head_ok )
1606
1627
check_not_current_branch (ref_map , worktrees );
1607
1628
1629
+ retcode = open_fetch_head (& fetch_head );
1630
+ if (retcode )
1631
+ goto cleanup ;
1632
+
1633
+ if (atomic_fetch ) {
1634
+ transaction = ref_transaction_begin (& err );
1635
+ if (!transaction ) {
1636
+ retcode = error ("%s" , err .buf );
1637
+ goto cleanup ;
1638
+ }
1639
+ }
1640
+
1608
1641
if (tags == TAGS_DEFAULT && autotags )
1609
1642
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , "1" );
1610
1643
if (prune ) {
@@ -1614,21 +1647,61 @@ static int do_fetch(struct transport *transport,
1614
1647
* don't care whether --tags was specified.
1615
1648
*/
1616
1649
if (rs -> nr ) {
1617
- retcode = prune_refs (rs , ref_map , transport -> url );
1650
+ retcode = prune_refs (rs , transaction , ref_map , transport -> url );
1618
1651
} else {
1619
1652
retcode = prune_refs (& transport -> remote -> fetch ,
1620
- ref_map ,
1653
+ transaction , ref_map ,
1621
1654
transport -> url );
1622
1655
}
1623
1656
if (retcode != 0 )
1624
1657
retcode = 1 ;
1625
1658
}
1626
- if ( fetch_and_consume_refs ( transport , ref_map , worktrees )) {
1627
- free_refs ( ref_map );
1659
+
1660
+ if ( fetch_and_consume_refs ( transport , transaction , ref_map , & fetch_head , worktrees )) {
1628
1661
retcode = 1 ;
1629
1662
goto cleanup ;
1630
1663
}
1631
1664
1665
+ /*
1666
+ * If neither --no-tags nor --tags was specified, do automated tag
1667
+ * following.
1668
+ */
1669
+ if (tags == TAGS_DEFAULT && autotags ) {
1670
+ struct ref * tags_ref_map = NULL , * * tail = & tags_ref_map ;
1671
+
1672
+ find_non_local_tags (remote_refs , transaction , & tags_ref_map , & tail );
1673
+ if (tags_ref_map ) {
1674
+ /*
1675
+ * If backfilling of tags fails then we want to tell
1676
+ * the user so, but we have to continue regardless to
1677
+ * populate upstream information of the references we
1678
+ * have already fetched above. The exception though is
1679
+ * when `--atomic` is passed: in that case we'll abort
1680
+ * the transaction and don't commit anything.
1681
+ */
1682
+ if (backfill_tags (transport , transaction , tags_ref_map ,
1683
+ & fetch_head , worktrees ))
1684
+ retcode = 1 ;
1685
+ }
1686
+
1687
+ free_refs (tags_ref_map );
1688
+ }
1689
+
1690
+ if (transaction ) {
1691
+ if (retcode )
1692
+ goto cleanup ;
1693
+
1694
+ retcode = ref_transaction_commit (transaction , & err );
1695
+ if (retcode ) {
1696
+ error ("%s" , err .buf );
1697
+ ref_transaction_free (transaction );
1698
+ transaction = NULL ;
1699
+ goto cleanup ;
1700
+ }
1701
+ }
1702
+
1703
+ commit_fetch_head (& fetch_head );
1704
+
1632
1705
if (set_upstream ) {
1633
1706
struct branch * branch = branch_get ("HEAD" );
1634
1707
struct ref * rm ;
@@ -1648,7 +1721,7 @@ static int do_fetch(struct transport *transport,
1648
1721
if (!rm -> peer_ref ) {
1649
1722
if (source_ref ) {
1650
1723
warning (_ ("multiple branches detected, incompatible with --set-upstream" ));
1651
- goto skip ;
1724
+ goto cleanup ;
1652
1725
} else {
1653
1726
source_ref = rm ;
1654
1727
}
@@ -1662,7 +1735,7 @@ static int do_fetch(struct transport *transport,
1662
1735
warning (_ ("could not set upstream of HEAD to '%s' from '%s' when "
1663
1736
"it does not point to any branch." ),
1664
1737
shortname , transport -> remote -> name );
1665
- goto skip ;
1738
+ goto cleanup ;
1666
1739
}
1667
1740
1668
1741
if (!strcmp (source_ref -> name , "HEAD" ) ||
@@ -1682,21 +1755,16 @@ static int do_fetch(struct transport *transport,
1682
1755
"you need to specify exactly one branch with the --set-upstream option" ));
1683
1756
}
1684
1757
}
1685
- skip :
1686
- free_refs (ref_map );
1687
1758
1688
- /* if neither --no-tags nor --tags was specified, do automated tag
1689
- * following ... */
1690
- if (tags == TAGS_DEFAULT && autotags ) {
1691
- struct ref * * tail = & ref_map ;
1692
- ref_map = NULL ;
1693
- find_non_local_tags (remote_refs , & ref_map , & tail );
1694
- if (ref_map )
1695
- backfill_tags (transport , ref_map , worktrees );
1696
- free_refs (ref_map );
1759
+ cleanup :
1760
+ if (retcode && transaction ) {
1761
+ ref_transaction_abort (transaction , & err );
1762
+ error ("%s" , err .buf );
1697
1763
}
1698
1764
1699
- cleanup :
1765
+ close_fetch_head (& fetch_head );
1766
+ strbuf_release (& err );
1767
+ free_refs (ref_map );
1700
1768
free_worktrees (worktrees );
1701
1769
return retcode ;
1702
1770
}
0 commit comments