Skip to content

Commit 91c868e

Browse files
fix(server): fork PR worktrees no longer fail when a local branch owns the t3code/pr-<n> prefix
Fixes #35743 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0179451 commit 91c868e

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,32 @@ function resolveHeadRepositoryNameWithOwner(pullRequest: PullRequestHeadRemoteIn
274274

275275
function resolvePullRequestWorktreeLocalBranchName(
276276
pullRequest: ResolvedPullRequest & PullRequestHeadRemoteInfo,
277+
localBranches: ReadonlySet<string>,
277278
): string {
278279
if (!pullRequest.isCrossRepository) {
279280
return pullRequest.headBranch;
280281
}
281282

282283
const sanitizedHeadBranch = sanitizeBranchFragment(pullRequest.headBranch).trim();
283284
const suffix = sanitizedHeadBranch.length > 0 ? sanitizedHeadBranch : "head";
284-
return `t3code/pr-${pullRequest.number}/${suffix}`;
285+
// Git cannot create a ref whose path is a parent or child of an existing one (`t3code` blocks `t3code/...`).
286+
const isCreatable = (name: string) =>
287+
!name
288+
.split("/")
289+
.slice(0, -1)
290+
.some((_, index, segments) => localBranches.has(segments.slice(0, index + 1).join("/"))) &&
291+
![...localBranches].some((branch) => branch.startsWith(`${name}/`));
292+
const preferred = `t3code/pr-${pullRequest.number}/${suffix}`;
293+
if (isCreatable(preferred)) {
294+
return preferred;
295+
}
296+
const flat = `t3code-pr-${pullRequest.number}-${suffix.replaceAll("/", "-")}`;
297+
for (let attempt = 1; ; attempt++) {
298+
const candidate = attempt === 1 ? flat : `${flat}-${attempt}`;
299+
if (isCreatable(candidate)) {
300+
return candidate;
301+
}
302+
}
285303
}
286304

287305
export function parseRepositoryNameWithOwnerFromRemoteUrl(
@@ -2988,8 +3006,22 @@ export const make = Effect.gen(function* () {
29883006
...pullRequest,
29893007
...toPullRequestHeadRemoteInfo(pullRequestSummary),
29903008
} as const;
2991-
const localPullRequestBranch =
2992-
resolvePullRequestWorktreeLocalBranchName(pullRequestWithRemoteInfo);
3009+
const localBranches = pullRequestWithRemoteInfo.isCrossRepository
3010+
? new Set(
3011+
(yield* gitCore.execute({
3012+
operation: "GitManager.preparePullRequestThread.localBranches",
3013+
cwd: input.cwd,
3014+
args: ["for-each-ref", "--format=%(refname:lstrip=2)", "refs/heads"],
3015+
timeoutMs: 5_000,
3016+
})).stdout
3017+
.split("\n")
3018+
.filter((name) => name.length > 0),
3019+
)
3020+
: new Set<string>();
3021+
const localPullRequestBranch = resolvePullRequestWorktreeLocalBranchName(
3022+
pullRequestWithRemoteInfo,
3023+
localBranches,
3024+
);
29933025

29943026
// Git refuses to move checked-out branches; advance reused checkouts from inside their worktree.
29953027
const reuseExistingWorktree = Effect.fn("reuseExistingWorktree")(function* (

0 commit comments

Comments
 (0)