@@ -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,12 +1105,12 @@ 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 ,
1108
+ int connectivity_checked ,
1109
+ struct ref_transaction * transaction , struct ref * ref_map ,
1087
1110
struct fetch_head * fetch_head , struct worktree * * worktrees )
1088
1111
{
1089
1112
int url_len , i , rc = 0 ;
1090
1113
struct strbuf note = STRBUF_INIT , err = STRBUF_INIT ;
1091
- struct ref_transaction * transaction = NULL ;
1092
1114
const char * what , * kind ;
1093
1115
struct ref * rm ;
1094
1116
char * url ;
@@ -1110,14 +1132,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1110
1132
}
1111
1133
}
1112
1134
1113
- if (atomic_fetch ) {
1114
- transaction = ref_transaction_begin (& err );
1115
- if (!transaction ) {
1116
- error ("%s" , err .buf );
1117
- goto abort ;
1118
- }
1119
- }
1120
-
1121
1135
prepare_format_display (ref_map );
1122
1136
1123
1137
/*
@@ -1233,14 +1247,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1233
1247
}
1234
1248
}
1235
1249
1236
- if (!rc && transaction ) {
1237
- rc = ref_transaction_commit (transaction , & err );
1238
- if (rc ) {
1239
- error ("%s" , err .buf );
1240
- goto abort ;
1241
- }
1242
- }
1243
-
1244
1250
if (rc & STORE_REF_ERROR_DF_CONFLICT )
1245
1251
error (_ ("some local refs could not be updated; try running\n"
1246
1252
" 'git remote prune %s' to remove any old, conflicting "
@@ -1258,7 +1264,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1258
1264
abort :
1259
1265
strbuf_release (& note );
1260
1266
strbuf_release (& err );
1261
- ref_transaction_free (transaction );
1262
1267
free (url );
1263
1268
return rc ;
1264
1269
}
@@ -1299,6 +1304,7 @@ static int check_exist_and_connected(struct ref *ref_map)
1299
1304
}
1300
1305
1301
1306
static int fetch_and_consume_refs (struct transport * transport ,
1307
+ struct ref_transaction * transaction ,
1302
1308
struct ref * ref_map ,
1303
1309
struct fetch_head * fetch_head ,
1304
1310
struct worktree * * worktrees )
@@ -1323,7 +1329,8 @@ static int fetch_and_consume_refs(struct transport *transport,
1323
1329
1324
1330
trace2_region_enter ("fetch" , "consume_refs" , the_repository );
1325
1331
ret = store_updated_refs (transport -> url , transport -> remote -> name ,
1326
- connectivity_checked , ref_map , fetch_head , worktrees );
1332
+ connectivity_checked , transaction , ref_map ,
1333
+ fetch_head , worktrees );
1327
1334
trace2_region_leave ("fetch" , "consume_refs" , the_repository );
1328
1335
1329
1336
out :
@@ -1496,6 +1503,7 @@ static struct transport *prepare_transport(struct remote *remote, int deepen)
1496
1503
}
1497
1504
1498
1505
static int backfill_tags (struct transport * transport ,
1506
+ struct ref_transaction * transaction ,
1499
1507
struct ref * ref_map ,
1500
1508
struct fetch_head * fetch_head ,
1501
1509
struct worktree * * worktrees )
@@ -1519,7 +1527,7 @@ static int backfill_tags(struct transport *transport,
1519
1527
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , NULL );
1520
1528
transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
1521
1529
transport_set_option (transport , TRANS_OPT_DEEPEN_RELATIVE , NULL );
1522
- retcode = fetch_and_consume_refs (transport , ref_map , fetch_head , worktrees );
1530
+ retcode = fetch_and_consume_refs (transport , transaction , ref_map , fetch_head , worktrees );
1523
1531
1524
1532
if (gsecondary ) {
1525
1533
transport_disconnect (gsecondary );
@@ -1532,6 +1540,7 @@ static int backfill_tags(struct transport *transport,
1532
1540
static int do_fetch (struct transport * transport ,
1533
1541
struct refspec * rs )
1534
1542
{
1543
+ struct ref_transaction * transaction = NULL ;
1535
1544
struct ref * ref_map = NULL ;
1536
1545
int autotags = (transport -> remote -> fetch_tags == 1 );
1537
1546
int retcode = 0 ;
@@ -1541,6 +1550,7 @@ static int do_fetch(struct transport *transport,
1541
1550
int must_list_refs = 1 ;
1542
1551
struct worktree * * worktrees = get_worktrees ();
1543
1552
struct fetch_head fetch_head = { 0 };
1553
+ struct strbuf err = STRBUF_INIT ;
1544
1554
1545
1555
if (tags == TAGS_DEFAULT ) {
1546
1556
if (transport -> remote -> fetch_tags == 2 )
@@ -1602,6 +1612,14 @@ static int do_fetch(struct transport *transport,
1602
1612
if (retcode )
1603
1613
goto cleanup ;
1604
1614
1615
+ if (atomic_fetch ) {
1616
+ transaction = ref_transaction_begin (& err );
1617
+ if (!transaction ) {
1618
+ retcode = error ("%s" , err .buf );
1619
+ goto cleanup ;
1620
+ }
1621
+ }
1622
+
1605
1623
if (tags == TAGS_DEFAULT && autotags )
1606
1624
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , "1" );
1607
1625
if (prune ) {
@@ -1621,7 +1639,7 @@ static int do_fetch(struct transport *transport,
1621
1639
retcode = 1 ;
1622
1640
}
1623
1641
1624
- if (fetch_and_consume_refs (transport , ref_map , & fetch_head , worktrees )) {
1642
+ if (fetch_and_consume_refs (transport , transaction , ref_map , & fetch_head , worktrees )) {
1625
1643
retcode = 1 ;
1626
1644
goto cleanup ;
1627
1645
}
@@ -1633,21 +1651,37 @@ static int do_fetch(struct transport *transport,
1633
1651
if (tags == TAGS_DEFAULT && autotags ) {
1634
1652
struct ref * tags_ref_map = NULL , * * tail = & tags_ref_map ;
1635
1653
1636
- find_non_local_tags (remote_refs , & tags_ref_map , & tail );
1654
+ find_non_local_tags (remote_refs , transaction , & tags_ref_map , & tail );
1637
1655
if (tags_ref_map ) {
1638
1656
/*
1639
1657
* If backfilling of tags fails then we want to tell
1640
1658
* the user so, but we have to continue regardless to
1641
1659
* populate upstream information of the references we
1642
- * have already fetched above.
1660
+ * have already fetched above. The exception though is
1661
+ * when `--atomic` is passed: in that case we'll abort
1662
+ * the transaction and don't commit anything.
1643
1663
*/
1644
- if (backfill_tags (transport , tags_ref_map , & fetch_head , worktrees ))
1664
+ if (backfill_tags (transport , transaction , tags_ref_map ,
1665
+ & fetch_head , worktrees ))
1645
1666
retcode = 1 ;
1646
1667
}
1647
1668
1648
1669
free_refs (tags_ref_map );
1649
1670
}
1650
1671
1672
+ if (transaction ) {
1673
+ if (retcode )
1674
+ goto cleanup ;
1675
+
1676
+ retcode = ref_transaction_commit (transaction , & err );
1677
+ if (retcode ) {
1678
+ error ("%s" , err .buf );
1679
+ ref_transaction_free (transaction );
1680
+ transaction = NULL ;
1681
+ goto cleanup ;
1682
+ }
1683
+ }
1684
+
1651
1685
commit_fetch_head (& fetch_head );
1652
1686
1653
1687
if (set_upstream ) {
@@ -1705,7 +1739,13 @@ static int do_fetch(struct transport *transport,
1705
1739
}
1706
1740
1707
1741
cleanup :
1742
+ if (retcode && transaction ) {
1743
+ ref_transaction_abort (transaction , & err );
1744
+ error ("%s" , err .buf );
1745
+ }
1746
+
1708
1747
close_fetch_head (& fetch_head );
1748
+ strbuf_release (& err );
1709
1749
free_refs (ref_map );
1710
1750
free_worktrees (worktrees );
1711
1751
return retcode ;
0 commit comments