TypeScript
fix(server): find existing PR worktrees beyond the first branch page (#18407)
Alex Southwell3e9de4672ccf
diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.tsindex bad1a80b371..303bc44f2a9 100644--- a/apps/server/src/git/GitManager.test.ts+++ b/apps/server/src/git/GitManager.test.ts@@ -6724,6 +6724,86 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {}),);+ it.effect.each([+ { isCrossRepository: false, padding: 0 },+ { isCrossRepository: false, padding: 110 },+ { isCrossRepository: true, padding: 0 },+ { isCrossRepository: true, padding: 110 },+ ])(+ "reuses an existing PR worktree with $padding preceding matching branches (fork: $isCrossRepository)",+ ({ isCrossRepository, padding }) =>+ Effect.gen(function* () {+ const repoDir = yield* makeTempDir("t3code-git-manager-");+ yield* initRepo(repoDir);+ const remoteDir = yield* createBareRemote();+ yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);+ yield* runGit(repoDir, ["push", "-u", "origin", "main"]);+ const forkDir = isCrossRepository ? yield* createBareRemote() : null;+ if (forkDir) {+ yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);+ yield* runGit(repoDir, ["push", "fork-seed", "main:pr-pagination"]);+ }+ const headBranch = "pr-pagination";+ const localBranch = isCrossRepository ? "t3code/pr-78/pr-pagination" : headBranch;+ yield* runGit(repoDir, ["branch", localBranch]);+ yield* runGit(repoDir, ["push", "origin", `${localBranch}:refs/pull/78/head`]);+ const worktreePath = NodePath.join(repoDir, "existing-pr");+ yield* runGit(repoDir, ["worktree", "add", worktreePath, localBranch]);+ for (let index = 0; index < padding; index++) {+ yield* runGit(repoDir, [+ "branch",+ `a-padding-${String(index).padStart(3, "0")}/${localBranch}`,+ ]);+ }+ const git = yield* GitVcsDriver.GitVcsDriver;+ const page = yield* git.listRefs({+ cwd: repoDir,+ refKind: "local",+ query: localBranch,+ refresh: true,+ });+ expect(page.nextCursor !== null).toBe(padding > 0);+ expect(page.refs.some((ref) => ref.name === localBranch)).toBe(padding === 0);+ const { manager } = yield* makeManager({+ ghScenario: {+ repositoryCloneUrls: forkDir+ ? { "contributor/codething-mvp": { url: forkDir, sshUrl: forkDir } }+ : {},+ pullRequest: {+ number: 78,+ title: "Paginated PR worktree",+ url: "https://github.com/pingdotgg/codething-mvp/pull/78",+ baseRefName: "main",+ headRefName: headBranch,+ state: "open",+ isCrossRepository,+ ...(isCrossRepository+ ? {+ headRepositoryNameWithOwner: "contributor/codething-mvp",+ headRepositoryOwnerLogin: "contributor",+ }+ : {}),+ },+ },+ });++ // Fetching directly into this checked-out branch would fail with git exit 128.+ const result = yield* preparePullRequestThread(manager, {+ cwd: repoDir,+ reference: "78",+ mode: "worktree",+ });+ expect(result.worktreePath && NodeFS.realpathSync.native(result.worktreePath)).toBe(+ NodeFS.realpathSync.native(worktreePath),+ );+ expect(result.branch).toBe(localBranch);+ expect(result.isOnPullRequestHead).toBe(true);+ expect((yield* runGit(worktreePath, ["branch", "--show-current"])).stdout.trim()).toBe(+ localBranch,+ );+ }),+ );+it.effect("refreshes a reused PR worktree that has no upstream from the pull request ref", () =>Effect.gen(function* () {const repoDir = yield* makeTempDir("t3code-git-manager-");diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.tsindex 6167ee6c1a8..097dd1e0408 100644--- a/apps/server/src/git/GitManager.ts+++ b/apps/server/src/git/GitManager.ts@@ -2813,26 +2813,34 @@ export const make = Effect.gen(function* () {});const findLocalHeadBranch = Effect.fn("findLocalHeadBranch")(function* (cwd: string) {- const result = yield* gitCore.listRefs({ cwd, refresh: true });- const localBranch = result.refs.find(- (branch) => !branch.isRemote && branch.name === localPullRequestBranch,- );- if (localBranch) {- return localBranch;- }- if (localPullRequestBranch === pullRequest.headBranch) {- return null;- }-- for (const branch of result.refs) {- if (branch.isRemote || branch.name !== pullRequest.headBranch || !branch.worktreePath) {- continue;- }-- const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);- if (worktreePath !== rootWorktreePath) {- return branch;- }+ for (const [index, name] of [+ ...new Set([localPullRequestBranch, pullRequest.headBranch]),+ ].entries()) {+ let cursor: number | undefined;+ do {+ const result = yield* gitCore.listRefs({+ cwd,+ refKind: "local",+ query: name,+ cursor,+ refresh: index === 0 && cursor === undefined,+ });+ for (const branch of result.refs) {+ if (branch.name !== name) {+ continue;+ }+ if (name === localPullRequestBranch) {+ return branch;+ }+ if (branch.worktreePath) {+ const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);+ if (worktreePath !== rootWorktreePath) {+ return branch;+ }+ }+ }+ cursor = result.nextCursor ?? undefined;+ } while (cursor !== undefined);}return null;