Skip to content

Commit 997af71

Browse files
KarthikNayakgitster
authored andcommitted
fetch: fix failed batched updates skipping operations
Fix a regression introduced with batched updates in 0e358de (fetch: use batched reference updates, 2025-05-19) when fetching references. In the `do_fetch()` function, we jump to cleanup if committing the transaction fails, regardless of whether using batched or atomic updates. This skips three subsequent operations: - Update 'FETCH_HEAD' as part of `commit_fetch_head()`. - Add upstream tracking information via `set_upstream()`. - Setting remote 'HEAD' values when `do_set_head` is true. For atomic updates, this is expected behavior. For batched updates, we want to continue with these operations even if some refs fail to update. Skipping `commit_fetch_head()` isn't actually a regression because 'FETCH_HEAD' is already updated via `append_fetch_head()` when not using '--atomic'. However, we add a test to validate this behavior. Skipping the other two operations (upstream tracking and remote HEAD) is a regression. Fix this by only jumping to cleanup when using '--atomic', allowing batched updates to continue with post-fetch operations. Add tests to prevent future regressions. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 24daae7 commit 997af71

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

builtin/fetch.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,11 @@ static int do_fetch(struct transport *transport,
18901890

18911891
retcode = commit_ref_transaction(&transaction, atomic_fetch,
18921892
transport->remote->name, &err);
1893-
if (retcode)
1893+
/*
1894+
* With '--atomic', bail out if the transaction fails. Without '--atomic',
1895+
* continue to fetch head and perform other post-fetch operations.
1896+
*/
1897+
if (retcode && atomic_fetch)
18941898
goto cleanup;
18951899

18961900
commit_fetch_head(&fetch_head);

t/t5510-fetch.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,93 @@ test_expect_success "backfill tags when providing a refspec" '
16391639
test_cmp expect actual
16401640
'
16411641

1642+
test_expect_success REFFILES "FETCH_HEAD is updated even if ref updates fail" '
1643+
test_when_finished rm -rf base repo &&
1644+
1645+
git init base &&
1646+
(
1647+
cd base &&
1648+
test_commit "updated" &&
1649+
1650+
git update-ref refs/heads/foo @ &&
1651+
git update-ref refs/heads/branch @
1652+
) &&
1653+
1654+
git init --bare repo &&
1655+
(
1656+
cd repo &&
1657+
! test -f FETCH_HEAD &&
1658+
git remote add origin ../base &&
1659+
touch refs/heads/foo.lock &&
1660+
test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1661+
test_grep "error: fetching ref refs/heads/foo failed: reference already exists" err &&
1662+
test -f FETCH_HEAD
1663+
)
1664+
'
1665+
1666+
test_expect_success REFFILES "upstream tracking info is added with --set-upstream" '
1667+
test_when_finished rm -rf base repo &&
1668+
1669+
git init --initial-branch=main base &&
1670+
test_commit -C base "updated" &&
1671+
1672+
git init --bare --initial-branch=main repo &&
1673+
(
1674+
cd repo &&
1675+
git remote add origin ../base &&
1676+
git fetch origin --set-upstream main &&
1677+
git config get branch.main.remote >actual &&
1678+
echo "origin" >expect &&
1679+
test_cmp expect actual
1680+
)
1681+
'
1682+
1683+
test_expect_success REFFILES "upstream tracking info is added even with conflicts" '
1684+
test_when_finished rm -rf base repo &&
1685+
1686+
git init --initial-branch=main base &&
1687+
test_commit -C base "updated" &&
1688+
1689+
git init --bare --initial-branch=main repo &&
1690+
(
1691+
cd repo &&
1692+
git remote add origin ../base &&
1693+
test_must_fail git config get branch.main.remote &&
1694+
1695+
mkdir -p refs/remotes/origin &&
1696+
touch refs/remotes/origin/main.lock &&
1697+
test_must_fail git fetch origin --set-upstream main &&
1698+
git config get branch.main.remote >actual &&
1699+
echo "origin" >expect &&
1700+
test_cmp expect actual
1701+
)
1702+
'
1703+
1704+
test_expect_success REFFILES "HEAD is updated even with conflicts" '
1705+
test_when_finished rm -rf base repo &&
1706+
1707+
git init base &&
1708+
(
1709+
cd base &&
1710+
test_commit "updated" &&
1711+
1712+
git update-ref refs/heads/foo @ &&
1713+
git update-ref refs/heads/branch @
1714+
) &&
1715+
1716+
git init --bare repo &&
1717+
(
1718+
cd repo &&
1719+
git remote add origin ../base &&
1720+
1721+
! test -f refs/remotes/origin/HEAD &&
1722+
mkdir -p refs/remotes/origin &&
1723+
touch refs/remotes/origin/branch.lock &&
1724+
test_must_fail git fetch origin &&
1725+
test -f refs/remotes/origin/HEAD
1726+
)
1727+
'
1728+
16421729
. "$TEST_DIRECTORY"/lib-httpd.sh
16431730
start_httpd
16441731

0 commit comments

Comments
 (0)