Skip to content

Commit fd5016a

Browse files
committed
fix(server): keep a gh-resolved base distinct from the sole fork remote
When the only remote is a fork and `gh repo set-default` points it at the parent (remote.<name>.gh-resolved = OWNER/REPO), the fork was taken as the target repository. The head then looked same-repository and gh pr create got a bare branch instead of owner:branch. Read the target remote's gh-resolved value and use the named repository as the target when it is set; `base` keeps the remote itself.
1 parent c1a439d commit fd5016a

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,38 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
38953895
}),
38963896
);
38973897

3898+
it.effect(
3899+
"keeps fork PR selectors when gh set-default points the only remote at its parent",
3900+
() =>
3901+
Effect.gen(function* () {
3902+
const repoDir = yield* makeTempDir("t3code-git-manager-");
3903+
yield* initRepo(repoDir);
3904+
yield* runGit(repoDir, ["checkout", "-b", "dev"]);
3905+
const forkDir = yield* createBareRemote();
3906+
yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
3907+
yield* runGit(repoDir, ["push", "fork", "main"]);
3908+
yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
3909+
yield* configureVisibleRemoteUrlWithLocalRewrite(
3910+
repoDir,
3911+
"fork",
3912+
"https://github.com/contributor/demo.git",
3913+
forkDir,
3914+
);
3915+
yield* runGit(repoDir, ["config", "remote.fork.gh-resolved", "acme/demo"]);
3916+
NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
3917+
3918+
const { manager, ghCalls } = yield* makeManager();
3919+
const created = yield* runStackedAction(manager, {
3920+
cwd: repoDir,
3921+
action: "commit_push_pr",
3922+
});
3923+
expect(created.pr.status).toBe("created");
3924+
expect(
3925+
ghCalls.some((call) => call.startsWith("pr create --base main --head contributor:dev ")),
3926+
).toBe(true);
3927+
}),
3928+
);
3929+
38983930
it.effect(
38993931
"returns the correct existing PR when a slash remote checks out to a synthetic local alias",
39003932
() =>

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,25 @@ export const make = Effect.gen(function* () {
14011401
Effect.orElseSucceed(() => "origin"),
14021402
);
14031403

1404+
// The target remote's repository, or the OWNER/REPO that `gh repo set-default`
1405+
// recorded in its gh-resolved key (`base` means the remote itself).
1406+
const resolveTargetRepositoryContext = Effect.fn("resolveTargetRepositoryContext")(function* (
1407+
cwd: string,
1408+
remoteName: string | null,
1409+
) {
1410+
const context = yield* resolveRemoteRepositoryContext(cwd, remoteName);
1411+
if (!remoteName) return context;
1412+
const resolved = yield* readConfigValueNullable(cwd, `remote.${remoteName}.gh-resolved`);
1413+
const [owner, name, ...rest] = resolved?.trim().split("/") ?? [];
1414+
if (!owner || !name || rest.length > 0) return context;
1415+
const host = context.remoteUrlKey?.split("/")[0];
1416+
return {
1417+
remoteUrlKey: host ? `${host}/${owner}/${name}`.toLowerCase() : context.remoteUrlKey,
1418+
repositoryNameWithOwner: `${owner}/${name}`,
1419+
ownerLogin: owner,
1420+
};
1421+
});
1422+
14041423
const resolvePrLookupRepositoryIdentity = Effect.fn("resolvePrLookupRepositoryIdentity")(
14051424
function* (cwd: string, branch: string, remoteNameOverride?: string) {
14061425
const remoteName =
@@ -1409,7 +1428,7 @@ export const make = Effect.gen(function* () {
14091428
const [headRemote, targetRemote] = yield* Effect.all(
14101429
[
14111430
resolveRemoteRepositoryContext(cwd, remoteName),
1412-
resolveRemoteRepositoryContext(cwd, targetRemoteName),
1431+
resolveTargetRepositoryContext(cwd, targetRemoteName),
14131432
],
14141433
{ concurrency: "unbounded" },
14151434
);
@@ -1440,7 +1459,7 @@ export const make = Effect.gen(function* () {
14401459
const [remoteRepository, targetRepository] = yield* Effect.all(
14411460
[
14421461
resolveRemoteRepositoryContext(cwd, remoteName),
1443-
resolveRemoteRepositoryContext(cwd, targetRemoteName),
1462+
resolveTargetRepositoryContext(cwd, targetRemoteName),
14441463
],
14451464
{ concurrency: "unbounded" },
14461465
);

0 commit comments

Comments
 (0)