Skip to content

Commit 01dd6d2

Browse files
saphidcodex
andcommitted
fix(server): find existing PR worktrees beyond the first branch page
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 83fbbc1 commit 01dd6d2

2 files changed

Lines changed: 108 additions & 20 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6129,6 +6129,86 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
61296129
}),
61306130
);
61316131

6132+
it.effect.each([
6133+
{ isCrossRepository: false, padding: 0 },
6134+
{ isCrossRepository: false, padding: 110 },
6135+
{ isCrossRepository: true, padding: 0 },
6136+
{ isCrossRepository: true, padding: 110 },
6137+
])(
6138+
"reuses an existing PR worktree with $padding preceding matching branches (fork: $isCrossRepository)",
6139+
({ isCrossRepository, padding }) =>
6140+
Effect.gen(function* () {
6141+
const repoDir = yield* makeTempDir("t3code-git-manager-");
6142+
yield* initRepo(repoDir);
6143+
const remoteDir = yield* createBareRemote();
6144+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
6145+
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
6146+
const forkDir = isCrossRepository ? yield* createBareRemote() : null;
6147+
if (forkDir) {
6148+
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
6149+
yield* runGit(repoDir, ["push", "fork-seed", "main:pr-pagination"]);
6150+
}
6151+
const headBranch = "pr-pagination";
6152+
const localBranch = isCrossRepository ? "t3code/pr-78/pr-pagination" : headBranch;
6153+
yield* runGit(repoDir, ["branch", localBranch]);
6154+
yield* runGit(repoDir, ["push", "origin", `${localBranch}:refs/pull/78/head`]);
6155+
const worktreePath = NodePath.join(repoDir, "existing-pr");
6156+
yield* runGit(repoDir, ["worktree", "add", worktreePath, localBranch]);
6157+
for (let index = 0; index < padding; index++) {
6158+
yield* runGit(repoDir, [
6159+
"branch",
6160+
`a-padding-${String(index).padStart(3, "0")}/${localBranch}`,
6161+
]);
6162+
}
6163+
const git = yield* GitVcsDriver.GitVcsDriver;
6164+
const page = yield* git.listRefs({
6165+
cwd: repoDir,
6166+
refKind: "local",
6167+
query: localBranch,
6168+
refresh: true,
6169+
});
6170+
expect(page.nextCursor !== null).toBe(padding > 0);
6171+
expect(page.refs.some((ref) => ref.name === localBranch)).toBe(padding === 0);
6172+
const { manager } = yield* makeManager({
6173+
ghScenario: {
6174+
repositoryCloneUrls: forkDir
6175+
? { "contributor/codething-mvp": { url: forkDir, sshUrl: forkDir } }
6176+
: {},
6177+
pullRequest: {
6178+
number: 78,
6179+
title: "Paginated PR worktree",
6180+
url: "https://github.com/pingdotgg/codething-mvp/pull/78",
6181+
baseRefName: "main",
6182+
headRefName: headBranch,
6183+
state: "open",
6184+
isCrossRepository,
6185+
...(isCrossRepository
6186+
? {
6187+
headRepositoryNameWithOwner: "contributor/codething-mvp",
6188+
headRepositoryOwnerLogin: "contributor",
6189+
}
6190+
: {}),
6191+
},
6192+
},
6193+
});
6194+
6195+
// Fetching directly into this checked-out branch would fail with git exit 128.
6196+
const result = yield* preparePullRequestThread(manager, {
6197+
cwd: repoDir,
6198+
reference: "78",
6199+
mode: "worktree",
6200+
});
6201+
expect(result.worktreePath && NodeFS.realpathSync.native(result.worktreePath)).toBe(
6202+
NodeFS.realpathSync.native(worktreePath),
6203+
);
6204+
expect(result.branch).toBe(localBranch);
6205+
expect(result.isOnPullRequestHead).toBe(true);
6206+
expect((yield* runGit(worktreePath, ["branch", "--show-current"])).stdout.trim()).toBe(
6207+
localBranch,
6208+
);
6209+
}),
6210+
);
6211+
61326212
it.effect("refreshes a reused PR worktree that has no upstream from the pull request ref", () =>
61336213
Effect.gen(function* () {
61346214
const repoDir = yield* makeTempDir("t3code-git-manager-");

‎apps/server/src/git/GitManager.ts‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,26 +2714,34 @@ export const make = Effect.gen(function* () {
27142714
});
27152715

27162716
const findLocalHeadBranch = Effect.fn("findLocalHeadBranch")(function* (cwd: string) {
2717-
const result = yield* gitCore.listRefs({ cwd, refresh: true });
2718-
const localBranch = result.refs.find(
2719-
(branch) => !branch.isRemote && branch.name === localPullRequestBranch,
2720-
);
2721-
if (localBranch) {
2722-
return localBranch;
2723-
}
2724-
if (localPullRequestBranch === pullRequest.headBranch) {
2725-
return null;
2726-
}
2727-
2728-
for (const branch of result.refs) {
2729-
if (branch.isRemote || branch.name !== pullRequest.headBranch || !branch.worktreePath) {
2730-
continue;
2731-
}
2732-
2733-
const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);
2734-
if (worktreePath !== rootWorktreePath) {
2735-
return branch;
2736-
}
2717+
for (const [index, name] of [
2718+
...new Set([localPullRequestBranch, pullRequest.headBranch]),
2719+
].entries()) {
2720+
let cursor: number | undefined;
2721+
do {
2722+
const result = yield* gitCore.listRefs({
2723+
cwd,
2724+
refKind: "local",
2725+
query: name,
2726+
cursor,
2727+
refresh: index === 0 && cursor === undefined,
2728+
});
2729+
for (const branch of result.refs) {
2730+
if (branch.name !== name) {
2731+
continue;
2732+
}
2733+
if (name === localPullRequestBranch) {
2734+
return branch;
2735+
}
2736+
if (branch.worktreePath) {
2737+
const worktreePath = yield* canonicalizeExistingPath(branch.worktreePath);
2738+
if (worktreePath !== rootWorktreePath) {
2739+
return branch;
2740+
}
2741+
}
2742+
}
2743+
cursor = result.nextCursor ?? undefined;
2744+
} while (cursor !== undefined);
27372745
}
27382746

27392747
return null;

0 commit comments

Comments
 (0)