Skip to content

Commit 336a8c1

Browse files
fix(server): fork PR worktrees no longer fail when a local branch owns the t3code/pr-<n> prefix (pingdotgg#35768)
1 parent 7bc3684 commit 336a8c1

2 files changed

Lines changed: 103 additions & 58 deletions

File tree

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

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5353,66 +5353,79 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53535353
}),
53545354
);
53555355

5356-
it.effect("preserves fork upstream tracking when preparing a worktree PR thread", () =>
5357-
Effect.gen(function* () {
5358-
const repoDir = yield* makeTempDir("t3code-git-manager-");
5359-
yield* initRepo(repoDir);
5360-
const originDir = yield* createBareRemote();
5361-
const forkDir = yield* createBareRemote();
5362-
yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
5363-
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
5364-
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
5365-
yield* runGit(repoDir, ["checkout", "-b", "feature/pr-fork"]);
5366-
NodeFS.writeFileSync(NodePath.join(repoDir, "fork.txt"), "fork\n");
5367-
yield* runGit(repoDir, ["add", "fork.txt"]);
5368-
yield* runGit(repoDir, ["commit", "-m", "Fork PR branch"]);
5369-
yield* runGit(repoDir, ["push", "-u", "fork-seed", "feature/pr-fork"]);
5370-
yield* runGit(repoDir, ["checkout", "main"]);
5356+
it.effect.each([{ blocker: null }, { blocker: "t3code" }, { blocker: "t3code/pr-81" }])(
5357+
"preserves fork upstream tracking when preparing a worktree PR thread (local branch $blocker)",
5358+
({ blocker }) =>
5359+
Effect.gen(function* () {
5360+
const repoDir = yield* makeTempDir("t3code-git-manager-");
5361+
yield* initRepo(repoDir);
5362+
const originDir = yield* createBareRemote();
5363+
const forkDir = yield* createBareRemote();
5364+
yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
5365+
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
5366+
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
5367+
yield* runGit(repoDir, ["checkout", "-b", "feature/pr-fork"]);
5368+
NodeFS.writeFileSync(NodePath.join(repoDir, "fork.txt"), "fork\n");
5369+
yield* runGit(repoDir, ["add", "fork.txt"]);
5370+
yield* runGit(repoDir, ["commit", "-m", "Fork PR branch"]);
5371+
yield* runGit(repoDir, ["push", "-u", "fork-seed", "feature/pr-fork"]);
5372+
yield* runGit(repoDir, ["checkout", "main"]);
5373+
const mainHead = (yield* runGit(repoDir, ["rev-parse", "main"])).stdout.trim();
5374+
// A local branch owning a parent of the generated `t3code/pr-<n>/<head>` ref must not block checkout.
5375+
if (blocker !== null) yield* runGit(repoDir, ["branch", blocker]);
53715376

5372-
const { manager } = yield* makeManager({
5373-
providerScenario: {
5374-
pullRequest: {
5375-
number: 81,
5376-
title: "Fork PR",
5377-
url: "https://github.com/pingdotgg/codething-mvp/pull/81",
5378-
baseRefName: "main",
5379-
headRefName: "feature/pr-fork",
5380-
state: "open",
5381-
isCrossRepository: true,
5382-
headRepositoryNameWithOwner: "octocat/codething-mvp",
5383-
headRepositoryOwnerLogin: "octocat",
5384-
},
5385-
repositoryCloneUrls: {
5386-
"octocat/codething-mvp": {
5387-
url: forkDir,
5388-
sshUrl: forkDir,
5377+
const { manager } = yield* makeManager({
5378+
providerScenario: {
5379+
pullRequest: {
5380+
number: 81,
5381+
title: "Fork PR",
5382+
url: "https://github.com/pingdotgg/codething-mvp/pull/81",
5383+
baseRefName: "main",
5384+
headRefName: "feature/pr-fork",
5385+
state: "open",
5386+
isCrossRepository: true,
5387+
headRepositoryNameWithOwner: "octocat/codething-mvp",
5388+
headRepositoryOwnerLogin: "octocat",
5389+
},
5390+
repositoryCloneUrls: {
5391+
"octocat/codething-mvp": {
5392+
url: forkDir,
5393+
sshUrl: forkDir,
5394+
},
53895395
},
53905396
},
5391-
},
5392-
});
5397+
});
53935398

5394-
const result = yield* preparePullRequestThread(manager, {
5395-
cwd: repoDir,
5396-
reference: "81",
5397-
mode: "worktree",
5398-
});
5399+
const result = yield* preparePullRequestThread(manager, {
5400+
cwd: repoDir,
5401+
reference: "81",
5402+
mode: "worktree",
5403+
});
53995404

5400-
expect(result.worktreePath).not.toBeNull();
5401-
const upstreamRef = (yield* runGit(result.worktreePath as string, [
5402-
"rev-parse",
5403-
"--abbrev-ref",
5404-
"@{upstream}",
5405-
])).stdout.trim();
5406-
expect(upstreamRef).toBe("fork-seed/feature/pr-fork");
5407-
expect(upstreamRef.startsWith("origin/")).toBe(false);
5408-
expect(
5409-
(yield* runGit(result.worktreePath as string, [
5410-
"config",
5411-
"--get",
5412-
"remote.fork-seed.url",
5413-
])).stdout.trim(),
5414-
).toBe(forkDir);
5415-
}),
5405+
expect(result.worktreePath).not.toBeNull();
5406+
const upstreamRef = (yield* runGit(result.worktreePath as string, [
5407+
"rev-parse",
5408+
"--abbrev-ref",
5409+
"@{upstream}",
5410+
])).stdout.trim();
5411+
expect(upstreamRef).toBe("fork-seed/feature/pr-fork");
5412+
expect(upstreamRef.startsWith("origin/")).toBe(false);
5413+
expect(
5414+
(yield* runGit(result.worktreePath as string, [
5415+
"config",
5416+
"--get",
5417+
"remote.fork-seed.url",
5418+
])).stdout.trim(),
5419+
).toBe(forkDir);
5420+
expect(
5421+
(yield* runGit(result.worktreePath as string, ["rev-parse", "HEAD"])).stdout.trim(),
5422+
).toBe((yield* runGit(repoDir, ["rev-parse", "fork-seed/feature/pr-fork"])).stdout.trim());
5423+
if (blocker !== null) {
5424+
expect(
5425+
(yield* runGit(repoDir, ["rev-parse", `refs/heads/${blocker}`])).stdout.trim(),
5426+
).toBe(mainHead);
5427+
}
5428+
}),
54165429
);
54175430

54185431
it.effect.each([

‎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)