@devries-cameron20 / t3code-1 / commits / 3e9de4672c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): find existing PR worktrees beyond the first branch page (#18407)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com>

Alex Southwell committed Oct 19, 2026 at 07:06 UTC 3e9de4672ccf4fe0a3f78a7577224a892db2d42a
2 files changed +108 -20
apps/server/src/git/GitManager.test.ts
+80
@@ -6724,6 +6724,86 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
6724 }),
6725 );
6726
6727 + it.effect.each([
6728 + { isCrossRepository: false, padding: 0 },
6729 + { isCrossRepository: false, padding: 110 },
6730 + { isCrossRepository: true, padding: 0 },
6731 + { isCrossRepository: true, padding: 110 },
6732 + ])(
6733 + "reuses an existing PR worktree with $padding preceding matching branches (fork: $isCrossRepository)",
6734 + ({ isCrossRepository, padding }) =>
6735 + Effect.gen(function* () {
6736 + const repoDir = yield* makeTempDir("t3code-git-manager-");
6737 + yield* initRepo(repoDir);
6738 + const remoteDir = yield* createBareRemote();
6739 + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
6740 + yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
6741 + const forkDir = isCrossRepository ? yield* createBareRemote() : null;
6742 + if (forkDir) {
6743 + yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
6744 + yield* runGit(repoDir, ["push", "fork-seed", "main:pr-pagination"]);
6745 + }
6746 + const headBranch = "pr-pagination";
6747 + const localBranch = isCrossRepository ? "t3code/pr-78/pr-pagination" : headBranch;
6748 + yield* runGit(repoDir, ["branch", localBranch]);
6749 + yield* runGit(repoDir, ["push", "origin", `${localBranch}:refs/pull/78/head`]);
6750 + const worktreePath = NodePath.join(repoDir, "existing-pr");
6751 + yield* runGit(repoDir, ["worktree", "add", worktreePath, localBranch]);
6752 + for (let index = 0; index < padding; index++) {
6753 + yield* runGit(repoDir, [
6754 + "branch",
6755 + `a-padding-${String(index).padStart(3, "0")}/${localBranch}`,
6756 + ]);
6757 + }
6758 + const git = yield* GitVcsDriver.GitVcsDriver;
6759 + const page = yield* git.listRefs({
6760 + cwd: repoDir,
6761 + refKind: "local",
6762 + query: localBranch,
6763 + refresh: true,
6764 + });
6765 + expect(page.nextCursor !== null).toBe(padding > 0);
6766 + expect(page.refs.some((ref) => ref.name === localBranch)).toBe(padding === 0);
6767 + const { manager } = yield* makeManager({
6768 + ghScenario: {
6769 + repositoryCloneUrls: forkDir
6770 + ? { "contributor/codething-mvp": { url: forkDir, sshUrl: forkDir } }
6771 + : {},
6772 + pullRequest: {
6773 + number: 78,
6774 + title: "Paginated PR worktree",
6775 + url: "https://github.com/pingdotgg/codething-mvp/pull/78",
6776 + baseRefName: "main",
6777 + headRefName: headBranch,
6778 + state: "open",
6779 + isCrossRepository,
6780 + ...(isCrossRepository
6781 + ? {
6782 + headRepositoryNameWithOwner: "contributor/codething-mvp",
6783 + headRepositoryOwnerLogin: "contributor",
6784 + }
6785 + : {}),
6786 + },
6787 + },
6788 + });
6789 +
6790 + // Fetching directly into this checked-out branch would fail with git exit 128.
6791 + const result = yield* preparePullRequestThread(manager, {
6792 + cwd: repoDir,
6793 + reference: "78",
6794 + mode: "worktree",
6795 + });
6796 + expect(result.worktreePath && NodeFS.realpathSync.native(result.worktreePath)).toBe(
6797 + NodeFS.realpathSync.native(worktreePath),
6798 + );
6799 + expect(result.branch).toBe(localBranch);
6800 + expect(result.isOnPullRequestHead).toBe(true);
6801 + expect((yield* runGit(worktreePath, ["branch", "--show-current"])).stdout.trim()).toBe(
6802 + localBranch,
6803 + );
6804 + }),
6805 + );
6806 +
6807 it.effect("refreshes a reused PR worktree that has no upstream from the pull request ref", () =>
6808 Effect.gen(function* () {
6809 const repoDir = yield* makeTempDir("t3code-git-manager-");
apps/server/src/git/GitManager.ts
+28 -20
@@ -2813,26 +2813,34 @@ export const make = Effect.gen(function* () {
2813 });
2814
2815 const findLocalHeadBranch = Effect.fn("findLocalHeadBranch")(function* (cwd: string) {
2816 - const result = yield* gitCore.listRefs({ cwd, refresh: true });
2817 - const localBranch = result.refs.find(
2818 - (branch) => !branch.isRemote && branch.name === localPullRequestBranch,
2819 - );
2820 - if (localBranch) {
2821 - return localBranch;
2822 - }
2823 - if (localPullRequestBranch === pullRequest.headBranch) {
2824 - return null;
2825 - }
2826 -
2827 - for (const branch of result.refs) {
2828 - if (branch.isRemote || branch.name !== pullRequest.headBranch || !branch.worktreePath) {
2829 - continue;
2830 - }
2831 -
2832 - const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);
2833 - if (worktreePath !== rootWorktreePath) {
2834 - return branch;
2835 - }
2816 + for (const [index, name] of [
2817 + ...new Set([localPullRequestBranch, pullRequest.headBranch]),
2818 + ].entries()) {
2819 + let cursor: number | undefined;
2820 + do {
2821 + const result = yield* gitCore.listRefs({
2822 + cwd,
2823 + refKind: "local",
2824 + query: name,
2825 + cursor,
2826 + refresh: index === 0 && cursor === undefined,
2827 + });
2828 + for (const branch of result.refs) {
2829 + if (branch.name !== name) {
2830 + continue;
2831 + }
2832 + if (name === localPullRequestBranch) {
2833 + return branch;
2834 + }
2835 + if (branch.worktreePath) {
2836 + const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);
2837 + if (worktreePath !== rootWorktreePath) {
2838 + return branch;
2839 + }
2840 + }
2841 + }
2842 + cursor = result.nextCursor ?? undefined;
2843 + } while (cursor !== undefined);
2844 }
2845
2846 return null;