| ... | ... | @@ -272,6 +272,11 @@ function initRepo( |
| 272 | 272 | yield* runGit(cwd, ["init", "--initial-branch=main"]); |
| 273 | 273 | yield* runGit(cwd, ["config", "user.email", "test@example.com"]); |
| 274 | 274 | yield* runGit(cwd, ["config", "user.name", "Test User"]); |
| 275 | // Hermetic line endings: machine-global `core.autocrlf=true` on Windows |
| 276 | // makes Git check out CRLF, which breaks exact-content assertions that |
| 277 | // write LF with Node. Local config overrides the global so the fixture |
| 278 | // (and its linked worktrees, which share config) always checks out LF. |
| 279 | yield* runGit(cwd, ["config", "core.autocrlf", "false"]); |
| 275 | 280 | yield* fs.writeFileString(NodePath.join(cwd, "README.md"), "hello\n"); |
| 276 | 281 | yield* runGit(cwd, ["add", "README.md"]); |
| 277 | 282 | yield* runGit(cwd, ["commit", "-m", "Initial commit"]); |
| ... | ... | @@ -1630,6 +1635,316 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { |
| 1630 | 1635 | }), |
| 1631 | 1636 | ); |
| 1632 | 1637 | |
| 1638 | it.effect("finds a fork PR for a branch pushed without an upstream", () => |
| 1639 | Effect.gen(function* () { |
| 1640 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| 1641 | yield* initRepo(repoDir); |
| 1642 | const originDir = yield* createBareRemote(); |
| 1643 | const forkDir = yield* createBareRemote(); |
| 1644 | yield* runGit(repoDir, ["remote", "add", "origin", originDir]); |
| 1645 | yield* runGit(repoDir, ["push", "-u", "origin", "main"]); |
| 1646 | yield* runGit(repoDir, ["remote", "add", "fork", forkDir]); |
| 1647 | yield* runGit(repoDir, ["checkout", "-b", "feature/no-upstream-fork"]); |
| 1648 | yield* runGit(repoDir, ["push", "fork", "feature/no-upstream-fork"]); |
| 1649 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1650 | repoDir, |
| 1651 | "origin", |
| 1652 | "git@github.com:pingdotgg/codething-mvp.git", |
| 1653 | originDir, |
| 1654 | ); |
| 1655 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1656 | repoDir, |
| 1657 | "fork", |
| 1658 | "git@github.com:contributor/codething-mvp.git", |
| 1659 | forkDir, |
| 1660 | ); |
| 1661 | |
| 1662 | const ghScenario = { |
| 1663 | prListByHeadSelector: { |
| 1664 | "feature/no-upstream-fork": JSON.stringify([ |
| 1665 | { |
| 1666 | number: 40, |
| 1667 | title: "Another fork's PR", |
| 1668 | url: "https://github.com/pingdotgg/codething-mvp/pull/40", |
| 1669 | baseRefName: "main", |
| 1670 | headRefName: "feature/no-upstream-fork", |
| 1671 | state: "OPEN", |
| 1672 | isCrossRepository: true, |
| 1673 | headRepository: { nameWithOwner: "someone-else/codething-mvp" }, |
| 1674 | headRepositoryOwner: { login: "someone-else" }, |
| 1675 | }, |
| 1676 | { |
| 1677 | number: 41, |
| 1678 | title: "Our fork's PR", |
| 1679 | url: "https://github.com/pingdotgg/codething-mvp/pull/41", |
| 1680 | baseRefName: "main", |
| 1681 | headRefName: "feature/no-upstream-fork", |
| 1682 | state: "OPEN", |
| 1683 | isCrossRepository: true, |
| 1684 | headRepository: { nameWithOwner: "contributor/codething-mvp" }, |
| 1685 | headRepositoryOwner: { login: "contributor" }, |
| 1686 | }, |
| 1687 | ]), |
| 1688 | }, |
| 1689 | }; |
| 1690 | const { manager } = yield* makeManager({ ghScenario }); |
| 1691 | |
| 1692 | const pullRequest = yield* manager.branchPullRequest({ |
| 1693 | cwd: repoDir, |
| 1694 | branch: "feature/no-upstream-fork", |
| 1695 | }); |
| 1696 | expect(pullRequest?.number).toBe(41); |
| 1697 | expect((yield* manager.status({ cwd: repoDir })).pr?.number).toBe(41); |
| 1698 | |
| 1699 | yield* runGit(repoDir, ["commit", "--allow-empty", "-m", "Continue feature"]); |
| 1700 | const localHead = yield* runGit(repoDir, ["rev-parse", "HEAD"]); |
| 1701 | const pushedHead = yield* runGit(repoDir, [ |
| 1702 | "rev-parse", |
| 1703 | "refs/remotes/fork/feature/no-upstream-fork", |
| 1704 | ]); |
| 1705 | expect(localHead.stdout.trim()).not.toBe(pushedHead.stdout.trim()); |
| 1706 | const { manager: aheadManager } = yield* makeManager({ ghScenario }); |
| 1707 | expect( |
| 1708 | (yield* aheadManager.branchPullRequest({ |
| 1709 | cwd: repoDir, |
| 1710 | branch: "feature/no-upstream-fork", |
| 1711 | }))?.number, |
| 1712 | ).toBe(41); |
| 1713 | expect((yield* aheadManager.status({ cwd: repoDir })).pr?.number).toBe(41); |
| 1714 | |
| 1715 | yield* runGit(repoDir, ["config", "branch.feature/no-upstream-fork.remote", "origin"]); |
| 1716 | const upstream = yield* runGit(repoDir, [ |
| 1717 | "for-each-ref", |
| 1718 | "--format=%(upstream:short)", |
| 1719 | "refs/heads/feature/no-upstream-fork", |
| 1720 | ]); |
| 1721 | expect(upstream.stdout.trim()).toBe(""); |
| 1722 | const { manager: staleRemoteManager } = yield* makeManager({ ghScenario }); |
| 1723 | expect( |
| 1724 | (yield* staleRemoteManager.branchPullRequest({ |
| 1725 | cwd: repoDir, |
| 1726 | branch: "feature/no-upstream-fork", |
| 1727 | }))?.number, |
| 1728 | ).toBe(41); |
| 1729 | |
| 1730 | yield* runGit(repoDir, ["config", "--unset", "branch.feature/no-upstream-fork.remote"]); |
| 1731 | yield* runGit(repoDir, ["checkout", "-b", "diverged-push", "main"]); |
| 1732 | yield* runGit(repoDir, ["commit", "--allow-empty", "-m", "Rewrite fork head"]); |
| 1733 | yield* runGit(repoDir, [ |
| 1734 | "push", |
| 1735 | "--force", |
| 1736 | "fork", |
| 1737 | "HEAD:refs/heads/feature/no-upstream-fork", |
| 1738 | ]); |
| 1739 | yield* runGit(repoDir, ["checkout", "feature/no-upstream-fork"]); |
| 1740 | const { manager: divergedManager } = yield* makeManager({ ghScenario }); |
| 1741 | expect( |
| 1742 | yield* divergedManager.branchPullRequest({ |
| 1743 | cwd: repoDir, |
| 1744 | branch: "feature/no-upstream-fork", |
| 1745 | }), |
| 1746 | ).toBeNull(); |
| 1747 | }), |
| 1748 | ); |
| 1749 | |
| 1750 | it.effect("prefers an exact fork ref but does not guess between equal refs", () => |
| 1751 | Effect.gen(function* () { |
| 1752 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| 1753 | yield* initRepo(repoDir); |
| 1754 | const originDir = yield* createBareRemote(); |
| 1755 | const firstForkDir = yield* createBareRemote(); |
| 1756 | const secondForkDir = yield* createBareRemote(); |
| 1757 | yield* runGit(repoDir, ["remote", "add", "origin", originDir]); |
| 1758 | yield* runGit(repoDir, ["push", "-u", "origin", "main"]); |
| 1759 | yield* runGit(repoDir, ["remote", "add", "first-fork", firstForkDir]); |
| 1760 | yield* runGit(repoDir, ["remote", "add", "second-fork", secondForkDir]); |
| 1761 | yield* runGit(repoDir, ["checkout", "-b", "feature/ambiguous-fork"]); |
| 1762 | yield* runGit(repoDir, ["push", "first-fork", "feature/ambiguous-fork"]); |
| 1763 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1764 | repoDir, |
| 1765 | "origin", |
| 1766 | "git@github.com:pingdotgg/codething-mvp.git", |
| 1767 | originDir, |
| 1768 | ); |
| 1769 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1770 | repoDir, |
| 1771 | "first-fork", |
| 1772 | "git@github.com:contributor/codething-mvp.git", |
| 1773 | firstForkDir, |
| 1774 | ); |
| 1775 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1776 | repoDir, |
| 1777 | "second-fork", |
| 1778 | "git@github.com:someone-else/codething-mvp.git", |
| 1779 | secondForkDir, |
| 1780 | ); |
| 1781 | |
| 1782 | const ghScenario = { |
| 1783 | prListByHeadSelector: { |
| 1784 | "feature/ambiguous-fork": JSON.stringify([ |
| 1785 | { |
| 1786 | number: 41, |
| 1787 | title: "One fork's PR", |
| 1788 | url: "https://github.com/pingdotgg/codething-mvp/pull/41", |
| 1789 | baseRefName: "main", |
| 1790 | headRefName: "feature/ambiguous-fork", |
| 1791 | state: "OPEN", |
| 1792 | isCrossRepository: true, |
| 1793 | headRepository: { nameWithOwner: "contributor/codething-mvp" }, |
| 1794 | headRepositoryOwner: { login: "contributor" }, |
| 1795 | }, |
| 1796 | ]), |
| 1797 | }, |
| 1798 | }; |
| 1799 | const { manager: singleRemoteManager } = yield* makeManager({ ghScenario }); |
| 1800 | expect( |
| 1801 | (yield* singleRemoteManager.branchPullRequest({ |
| 1802 | cwd: repoDir, |
| 1803 | branch: "feature/ambiguous-fork", |
| 1804 | }))?.number, |
| 1805 | ).toBe(41); |
| 1806 | |
| 1807 | yield* runGit(repoDir, ["push", "second-fork", "feature/ambiguous-fork"]); |
| 1808 | const { manager } = yield* makeManager({ ghScenario }); |
| 1809 | expect( |
| 1810 | yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature/ambiguous-fork" }), |
| 1811 | ).toBeNull(); |
| 1812 | |
| 1813 | yield* runGit(repoDir, ["commit", "--allow-empty", "-m", "Continue feature"]); |
| 1814 | yield* runGit(repoDir, ["push", "first-fork", "feature/ambiguous-fork"]); |
| 1815 | const { manager: exactManager } = yield* makeManager({ ghScenario }); |
| 1816 | expect( |
| 1817 | (yield* exactManager.branchPullRequest({ |
| 1818 | cwd: repoDir, |
| 1819 | branch: "feature/ambiguous-fork", |
| 1820 | }))?.number, |
| 1821 | ).toBe(41); |
| 1822 | }), |
| 1823 | ); |
| 1824 | |
| 1825 | it.effect("does not guess a fork when an ancestry probe fails", () => |
| 1826 | Effect.gen(function* () { |
| 1827 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| 1828 | yield* initRepo(repoDir); |
| 1829 | const originDir = yield* createBareRemote(); |
| 1830 | const forkDir = yield* createBareRemote(); |
| 1831 | yield* runGit(repoDir, ["remote", "add", "origin", originDir]); |
| 1832 | yield* runGit(repoDir, ["push", "-u", "origin", "main"]); |
| 1833 | yield* runGit(repoDir, ["remote", "add", "fork", forkDir]); |
| 1834 | yield* runGit(repoDir, ["checkout", "-b", "feature/failed-probe"]); |
| 1835 | yield* runGit(repoDir, ["push", "fork", "feature/failed-probe"]); |
| 1836 | yield* runGit(repoDir, ["push", "origin", "feature/failed-probe"]); |
| 1837 | // Local work past both tips, so no exact ref matches and the ancestor |
| 1838 | // tier decides. |
| 1839 | yield* runGit(repoDir, ["commit", "--allow-empty", "-m", "Local work"]); |
| 1840 | // Simulate a failed ancestry probe: point origin's tracking ref at a |
| 1841 | // non-commit object so `merge-base --is-ancestor` exits 128 — neither |
| 1842 | // a match (0) nor a negative result (1). |
| 1843 | const treeOid = (yield* runGit(repoDir, ["rev-parse", "HEAD^{tree}"])).stdout.trim(); |
| 1844 | yield* runGit(repoDir, ["update-ref", "refs/remotes/origin/feature/failed-probe", treeOid]); |
| 1845 | const forkProbe = yield* runGit( |
| 1846 | repoDir, |
| 1847 | ["merge-base", "--is-ancestor", "refs/remotes/fork/feature/failed-probe", "HEAD"], |
| 1848 | true, |
| 1849 | ); |
| 1850 | expect(forkProbe.exitCode).toBe(0); |
| 1851 | const originProbe = yield* runGit( |
| 1852 | repoDir, |
| 1853 | ["merge-base", "--is-ancestor", "refs/remotes/origin/feature/failed-probe", "HEAD"], |
| 1854 | true, |
| 1855 | ); |
| 1856 | expect(originProbe.exitCode).not.toBe(0); |
| 1857 | expect(originProbe.exitCode).not.toBe(1); |
| 1858 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1859 | repoDir, |
| 1860 | "origin", |
| 1861 | "git@github.com:pingdotgg/codething-mvp.git", |
| 1862 | originDir, |
| 1863 | ); |
| 1864 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1865 | repoDir, |
| 1866 | "fork", |
| 1867 | "git@github.com:contributor/codething-mvp.git", |
| 1868 | forkDir, |
| 1869 | ); |
| 1870 | |
| 1871 | const ghScenario = { |
| 1872 | prListByHeadSelector: { |
| 1873 | "feature/failed-probe": JSON.stringify([ |
| 1874 | { |
| 1875 | number: 43, |
| 1876 | title: "Failed probe PR", |
| 1877 | url: "https://github.com/pingdotgg/codething-mvp/pull/43", |
| 1878 | baseRefName: "main", |
| 1879 | headRefName: "feature/failed-probe", |
| 1880 | state: "OPEN", |
| 1881 | isCrossRepository: true, |
| 1882 | headRepository: { nameWithOwner: "contributor/codething-mvp" }, |
| 1883 | headRepositoryOwner: { login: "contributor" }, |
| 1884 | }, |
| 1885 | ]), |
| 1886 | }, |
| 1887 | }; |
| 1888 | const { manager } = yield* makeManager({ ghScenario }); |
| 1889 | // The fork probe matches, but the failed origin probe rejects the |
| 1890 | // whole inference: unknown, invisible rather than wrong. |
| 1891 | expect( |
| 1892 | yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature/failed-probe" }), |
| 1893 | ).toBeNull(); |
| 1894 | }), |
| 1895 | ); |
| 1896 | |
| 1897 | it.effect("finds a fork PR when the remote name contains a slash", () => |
| 1898 | Effect.gen(function* () { |
| 1899 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| 1900 | yield* initRepo(repoDir); |
| 1901 | const originDir = yield* createBareRemote(); |
| 1902 | const forkDir = yield* createBareRemote(); |
| 1903 | yield* runGit(repoDir, ["remote", "add", "origin", originDir]); |
| 1904 | yield* runGit(repoDir, ["push", "-u", "origin", "main"]); |
| 1905 | yield* runGit(repoDir, ["remote", "add", "team/fork", forkDir]); |
| 1906 | yield* runGit(repoDir, ["checkout", "-b", "feature/slash-remote"]); |
| 1907 | yield* runGit(repoDir, ["push", "team/fork", "feature/slash-remote"]); |
| 1908 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1909 | repoDir, |
| 1910 | "origin", |
| 1911 | "git@github.com:pingdotgg/codething-mvp.git", |
| 1912 | originDir, |
| 1913 | ); |
| 1914 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 1915 | repoDir, |
| 1916 | "team/fork", |
| 1917 | "git@github.com:contributor/codething-mvp.git", |
| 1918 | forkDir, |
| 1919 | ); |
| 1920 | |
| 1921 | const { manager } = yield* makeManager({ |
| 1922 | ghScenario: { |
| 1923 | prListByHeadSelector: { |
| 1924 | "feature/slash-remote": JSON.stringify([ |
| 1925 | { |
| 1926 | number: 42, |
| 1927 | title: "Slash remote PR", |
| 1928 | url: "https://github.com/pingdotgg/codething-mvp/pull/42", |
| 1929 | baseRefName: "main", |
| 1930 | headRefName: "feature/slash-remote", |
| 1931 | state: "OPEN", |
| 1932 | isCrossRepository: true, |
| 1933 | headRepository: { nameWithOwner: "contributor/codething-mvp" }, |
| 1934 | headRepositoryOwner: { login: "contributor" }, |
| 1935 | }, |
| 1936 | ]), |
| 1937 | }, |
| 1938 | }, |
| 1939 | }); |
| 1940 | |
| 1941 | expect( |
| 1942 | (yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature/slash-remote" })) |
| 1943 | ?.number, |
| 1944 | ).toBe(42); |
| 1945 | }), |
| 1946 | ); |
| 1947 | |
| 1633 | 1948 | it.effect("branch PR lookup does not reuse a cached PR after the remote is repointed", () => |
| 1634 | 1949 | Effect.gen(function* () { |
| 1635 | 1950 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| ... | ... | @@ -2096,9 +2411,11 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { |
| 2096 | 2411 | ); |
| 2097 | 2412 | |
| 2098 | 2413 | it.effect.each([ |
| 2099 | | "git@gitlab.com:Group/Subgroup/Fork.git", |
| 2100 | | "https://gitlab.com/Group/Subgroup/Fork.git", |
| 2101 | | ])("matches nested GitLab forks through the adapter for %s", (remoteUrl) => |
| 2414 | { remoteUrl: "git@gitlab.com:Group/Subgroup/Fork.git", setUpstream: true }, |
| 2415 | { remoteUrl: "git@gitlab.com:Group/Subgroup/Fork.git", setUpstream: false }, |
| 2416 | { remoteUrl: "https://gitlab.com/Group/Subgroup/Fork.git", setUpstream: true }, |
| 2417 | { remoteUrl: "https://gitlab.com/Group/Subgroup/Fork.git", setUpstream: false }, |
| 2418 | ] as const)("matches GitLab forks for $remoteUrl/$setUpstream", ({ remoteUrl, setUpstream }) => |
| 2102 | 2419 | Effect.gen(function* () { |
| 2103 | 2420 | const repoDir = yield* makeTempDir("t3code-git-manager-"); |
| 2104 | 2421 | yield* initRepo(repoDir); |
| ... | ... | @@ -2109,7 +2426,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { |
| 2109 | 2426 | yield* runGit(repoDir, ["push", "-u", "origin", "main"]); |
| 2110 | 2427 | yield* runGit(repoDir, ["remote", "add", "fork", forkDir]); |
| 2111 | 2428 | yield* runGit(repoDir, ["checkout", "-b", branch]); |
| 2112 | | yield* runGit(repoDir, ["push", "-u", "fork", branch]); |
| 2429 | yield* runGit(repoDir, ["push", ...(setUpstream ? ["-u"] : []), "fork", branch]); |
| 2430 | const upstream = yield* runGit(repoDir, [ |
| 2431 | "for-each-ref", |
| 2432 | "--format=%(upstream:short)", |
| 2433 | `refs/heads/${branch}`, |
| 2434 | ]); |
| 2435 | expect(upstream.stdout.trim()).toBe(setUpstream ? `fork/${branch}` : ""); |
| 2113 | 2436 | yield* configureVisibleRemoteUrlWithLocalRewrite( |
| 2114 | 2437 | repoDir, |
| 2115 | 2438 | "origin", |
| ... | ... | @@ -5639,9 +5962,12 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { |
| 5639 | 5962 | expect((yield* runGit(worktreePath, ["rev-parse", "HEAD"])).stdout.trim()).toBe( |
| 5640 | 5963 | rewrittenHead, |
| 5641 | 5964 | ); |
| 5642 | | expect(NodeFS.readFileSync(NodePath.join(worktreePath, "force-pushed.txt"), "utf8")).toBe( |
| 5643 | | "rewritten\n", |
| 5644 | | ); |
| 5965 | expect( |
| 5966 | NodeFS.readFileSync(NodePath.join(worktreePath, "force-pushed.txt"), "utf8").replace( |
| 5967 | /\r\n/g, |
| 5968 | "\n", |
| 5969 | ), |
| 5970 | ).toBe("rewritten\n"); |
| 5645 | 5971 | }), |
| 5646 | 5972 | ); |
| 5647 | 5973 | |