fetch: fix failed batched updates skipping operations

Fix a regression introduced with batched updates in 0e358de64a (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>

Karthik Nayak committed Nov 21, 2025 at 12:13 UTC b7b17ec8a6b1cb176206ad69c194b84eb3490b99
2 files changed +93 -1
builtin/fetch.c
+5 -1
@@ -1890,7 +1890,11 @@ static int do_fetch(struct transport *transport,
1890
1891 retcode = commit_ref_transaction(&transaction, atomic_fetch,
1892 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)
1898 goto cleanup;
1899
1900 commit_fetch_head(&fetch_head);
t/t5510-fetch.sh
+88
@@ -1639,6 +1639,94 @@ test_expect_success "backfill tags when providing a refspec" '
1639 test_cmp expect actual
1640 '
1641
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 + rm -f FETCH_HEAD &&
1658 + git remote add origin ../base &&
1659 + >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_grep "branch ${SQ}branch${SQ} of ../base" FETCH_HEAD &&
1663 + test_grep "branch ${SQ}foo${SQ} of ../base" FETCH_HEAD
1664 + )
1665 +'
1666 +
1667 +test_expect_success "upstream tracking info is added with --set-upstream" '
1668 + test_when_finished rm -rf base repo &&
1669 +
1670 + git init --initial-branch=main base &&
1671 + test_commit -C base "updated" &&
1672 +
1673 + git init --bare --initial-branch=main repo &&
1674 + (
1675 + cd repo &&
1676 + git remote add origin ../base &&
1677 + git fetch origin --set-upstream main &&
1678 + git config get branch.main.remote >actual &&
1679 + echo "origin" >expect &&
1680 + test_cmp expect actual
1681 + )
1682 +'
1683 +
1684 +test_expect_success REFFILES "upstream tracking info is added even with conflicts" '
1685 + test_when_finished rm -rf base repo &&
1686 +
1687 + git init --initial-branch=main base &&
1688 + test_commit -C base "updated" &&
1689 +
1690 + git init --bare --initial-branch=main repo &&
1691 + (
1692 + cd repo &&
1693 + git remote add origin ../base &&
1694 + test_must_fail git config get branch.main.remote &&
1695 +
1696 + mkdir -p refs/remotes/origin &&
1697 + >refs/remotes/origin/main.lock &&
1698 + test_must_fail git fetch origin --set-upstream main &&
1699 + git config get branch.main.remote >actual &&
1700 + echo "origin" >expect &&
1701 + test_cmp expect actual
1702 + )
1703 +'
1704 +
1705 +test_expect_success REFFILES "HEAD is updated even with conflicts" '
1706 + test_when_finished rm -rf base repo &&
1707 +
1708 + git init base &&
1709 + (
1710 + cd base &&
1711 + test_commit "updated" &&
1712 +
1713 + git update-ref refs/heads/foo @ &&
1714 + git update-ref refs/heads/branch @
1715 + ) &&
1716 +
1717 + git init --bare repo &&
1718 + (
1719 + cd repo &&
1720 + git remote add origin ../base &&
1721 +
1722 + test_path_is_missing refs/remotes/origin/HEAD &&
1723 + mkdir -p refs/remotes/origin &&
1724 + >refs/remotes/origin/branch.lock &&
1725 + test_must_fail git fetch origin &&
1726 + test -f refs/remotes/origin/HEAD
1727 + )
1728 +'
1729 +
1730 . "$TEST_DIRECTORY"/lib-httpd.sh
1731 start_httpd
1732