The best way to control Claude Code, Codex and more from any computer or phone
fix(server): branch PRs stay matched when a remote has a second push URL (#29625)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Alex Southwell committed
Nov 15, 2026 at 15:38 UTC
6152fe3ae821b662f75dfebb5a5a213734be6388
6 files changed
+185
-11
apps/server/src/git/GitManager.test.ts
+48
@@ -926,6 +926,54 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
926
}),
927
);
928
929
+ it.effect("status reads a remote's first URL when it has a second push URL", () =>
930
+ Effect.gen(function* () {
931
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
932
+ yield* initRepo(repoDir);
933
+ yield* runGit(repoDir, ["checkout", "-b", "feature/mirrored"]);
934
+ const remoteDir = yield* createBareRemote();
935
+ yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
936
+ yield* runGit(repoDir, ["push", "-u", "origin", "feature/mirrored"]);
937
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
938
+ repoDir,
939
+ "origin",
940
+ "git@github.com:pingdotgg/codething-mvp.git",
941
+ remoteDir,
942
+ );
943
+ // `git remote set-url --add`: Git fetches from the first URL and also pushes to this one.
944
+ yield* runGit(repoDir, [
945
+ "config",
946
+ "--add",
947
+ "remote.origin.url",
948
+ "git@gitlab.com:mirror-owner/codething-mvp.git",
949
+ ]);
950
+
951
+ const { manager } = yield* makeManager({
952
+ providerScenario: {
953
+ prListSequence: [
954
+ [
955
+ {
956
+ number: 16,
957
+ title: "Mirrored branch",
958
+ url: "https://github.com/pingdotgg/codething-mvp/pull/16",
959
+ baseRefName: "main",
960
+ headRefName: "feature/mirrored",
961
+ isCrossRepository: false,
962
+ headRepositoryNameWithOwner: "pingdotgg/codething-mvp",
963
+ headRepositoryOwnerLogin: "pingdotgg",
964
+ },
965
+ ],
966
+ ],
967
+ },
968
+ });
969
+
970
+ const status = yield* manager.status({ cwd: repoDir });
971
+
972
+ expect(status.sourceControlProvider?.kind).toBe("github");
973
+ expect(status.pr?.number).toBe(16);
974
+ }),
975
+ );
976
+
977
it.effect("status reports the latest of a branch's closed and merged PRs", () =>
978
Effect.gen(function* () {
979
const repoDir = yield* makeTempDir("t3code-git-manager-");
apps/server/src/git/GitManager.ts
+26
-8
@@ -722,6 +722,16 @@ export const make = Effect.gen(function* () {
722
const path = yield* Path.Path;
723
724
const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd });
725
+ // Git fetches from a remote's first URL; `config --get` returns the last of several.
726
+ const readRemoteUrl = (cwd: string, remoteName: string) =>
727
+ gitCore
728
+ .execute({
729
+ operation: "GitManager.readRemoteUrl",
730
+ cwd,
731
+ args: ["config", "--get-all", `remote.${remoteName}.url`],
732
+ allowNonZeroExit: true,
733
+ })
734
+ .pipe(Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null));
735
const changeRequestTermsFor = Effect.fn("GitManager.changeRequestTermsFor")(function* (
736
cwd: string,
737
) {
@@ -1004,7 +1014,7 @@ export const make = Effect.gen(function* () {
1014
repository: repositoryNameWithOwner,
1015
...pullRequestHostOptions(pullRequest.url),
1016
});
1007
- const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
1017
+ const originRemoteUrl = yield* readRemoteUrl(cwd, "origin");
1018
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
1019
const preferredRemoteName =
1020
pullRequest.headRepositoryOwnerLogin?.trim() ||
@@ -1089,7 +1099,7 @@ export const make = Effect.gen(function* () {
1099
? Effect.fail(cause)
1100
: Effect.gen(function* () {
1101
const remoteName = yield* gitCore.resolvePrimaryRemoteName(cwd);
1092
- const remoteUrl = yield* gitCore.readConfigValue(cwd, `remote.${remoteName}.url`);
1102
+ const remoteUrl = yield* readRemoteUrl(cwd, remoteName);
1103
const pullRequestKey = pullRequestRepositoryKey(pullRequest.url);
1104
if (
1105
remoteUrl === null ||
@@ -1118,7 +1128,7 @@ export const make = Effect.gen(function* () {
1128
repository: repositoryNameWithOwner,
1129
...pullRequestHostOptions(pullRequest.url),
1130
});
1121
- const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
1131
+ const originRemoteUrl = yield* readRemoteUrl(cwd, "origin");
1132
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
1133
const preferredRemoteName =
1134
pullRequest.headRepositoryOwnerLogin?.trim() ||
@@ -1528,6 +1538,8 @@ export const make = Effect.gen(function* () {
1538
1539
const readConfigValueNullable = (cwd: string, key: string) =>
1540
gitCore.readConfigValue(cwd, key).pipe(Effect.orElseSucceed(() => null));
1541
+ const readRemoteUrlNullable = (cwd: string, remoteName: string) =>
1542
+ readRemoteUrl(cwd, remoteName).pipe(Effect.orElseSucceed(() => null));
1543
1544
// Whether the remote's host creates change requests in this remote's repository. A host's tools
1545
// can let configuration, such as gh's default repository, name another one.
@@ -1561,8 +1573,8 @@ export const make = Effect.gen(function* () {
1573
? "origin"
1574
: ((yield* readConfigValueNullable(cwd, `branch.${branch}.remote`)) ?? "origin");
1575
const remoteUrl =
1564
- (yield* readConfigValueNullable(cwd, `remote.${preferredRemoteName}.url`)) ??
1565
- (yield* readConfigValueNullable(cwd, "remote.origin.url"));
1576
+ (yield* readRemoteUrlNullable(cwd, preferredRemoteName)) ??
1577
+ (yield* readRemoteUrlNullable(cwd, "origin"));
1578
1579
const provider = remoteUrl ? sourceControlDefinitions.recognizeRemote(remoteUrl) : null;
1580
if (!remoteUrl || provider?.kind !== "unknown") return provider;
@@ -1588,7 +1600,7 @@ export const make = Effect.gen(function* () {
1600
};
1601
}
1602
1591
- const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`);
1603
+ const remoteUrl = yield* readRemoteUrlNullable(cwd, remoteName);
1604
let repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl);
1605
if (
1606
remoteUrl !== null &&
@@ -1844,7 +1856,7 @@ export const make = Effect.gen(function* () {
1856
remoteName = matches[0];
1857
}
1858
if (remoteName === undefined) return null;
1847
- const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`);
1859
+ const remoteUrl = yield* readRemoteUrlNullable(cwd, remoteName);
1860
return remoteUrl === null ? null : remoteName;
1861
});
1862
@@ -2631,7 +2643,13 @@ export const make = Effect.gen(function* () {
2643
const readRemoteUrlKey = (remoteName: string | null) =>
2644
remoteName === null
2645
? Effect.succeed(null)
2634
- : readConfig(`remote.${remoteName}.url`).pipe(
2646
+ : rememberPrLookupGitState(
2647
+ cwd,
2648
+ `remoteUrl\u0000${remoteName}`,
2649
+ fresh,
2650
+ readRemoteUrl(cwd, remoteName),
2651
+ ).pipe(
2652
+ Effect.orElseSucceed(() => null),
2653
Effect.map((remoteUrl) =>
2654
remoteUrl ? sourceControlDefinitions.repositoryKeyFromRemote(remoteUrl) : null,
2655
),
packages/source-control-bitbucket/src/server/BitbucketApi.test.ts
+50
@@ -9,6 +9,7 @@ import * as Fiber from "effect/Fiber";
9
import * as FileSystem from "effect/FileSystem";
10
import * as Layer from "effect/Layer";
11
import * as Option from "effect/Option";
12
+import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
13
import * as Queue from "effect/Queue";
14
import * as Result from "effect/Result";
15
import * as Schema from "effect/Schema";
@@ -63,6 +64,14 @@ const repositoryJson = {
64
mainbranch: { name: "main" },
65
};
66
67
+const gitOutput = (stdout: string) => ({
68
+ exitCode: ChildProcessSpawner.ExitCode(0),
69
+ stdout,
70
+ stderr: "",
71
+ stdoutTruncated: false,
72
+ stderrTruncated: false,
73
+});
74
+
75
function makeLayer(input: {
76
readonly response: (request: HttpClientRequest.HttpClientRequest) => Response;
77
/** Holds the answer until it resolves, in place of answering at once. */
@@ -85,6 +94,9 @@ function makeLayer(input: {
94
),
95
);
96
const gitMock = {
97
+ execute: vi.fn<HostGit["execute"]>(() =>
98
+ Effect.succeed(gitOutput("git@bitbucket.org:pingdotgg/t3code.git\n")),
99
+ ),
100
readConfigValue: vi.fn<HostGit["readConfigValue"]>(() =>
101
Effect.succeed<string | null>("git@bitbucket.org:pingdotgg/t3code.git"),
102
),
@@ -253,6 +265,44 @@ it.effect.each([false, true])("honors PR URL repositories, context=%s", (withCon
265
}).pipe(Effect.provide(layer));
266
});
267
268
+it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
269
+ // `git remote -v` shows origin's SSH `insteadOf` rewrite; config holds HTTPS, then an SSH push URL.
270
+ const { git, layer } = makeLayer({
271
+ git: {
272
+ execute: () =>
273
+ Effect.succeed(
274
+ gitOutput(
275
+ "https://bitbucket.org/pingdotgg/t3code.git\ngit@bitbucket.org:pingdotgg/t3code.git\n",
276
+ ),
277
+ ),
278
+ },
279
+ response: (request) =>
280
+ Response.json(
281
+ request.url.endsWith("/pullrequests/42")
282
+ ? bitbucketPullRequest
283
+ : {
284
+ ...repositoryJson,
285
+ full_name: "octocat/t3code",
286
+ links: {
287
+ clone: [
288
+ { name: "https", href: "https://bitbucket.org/octocat/t3code.git" },
289
+ { name: "ssh", href: "git@bitbucket.org:octocat/t3code.git" },
290
+ ],
291
+ },
292
+ },
293
+ ),
294
+ });
295
+ return Effect.gen(function* () {
296
+ const bitbucket = yield* BitbucketApi.BitbucketApi;
297
+ yield* bitbucket.checkoutPullRequest({ cwd: "/repo", reference: "#42" });
298
+ assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], {
299
+ cwd: "/repo",
300
+ preferredName: "octocat",
301
+ url: "https://bitbucket.org/octocat/t3code.git",
302
+ });
303
+ }).pipe(Effect.provide(layer));
304
+});
305
+
306
it.effect.each([false, true])(
307
"reuses matching checkout remotes for numeric references, context=%s",
308
(withContext) => {
packages/source-control-bitbucket/src/server/BitbucketApi.ts
+12
-1
@@ -918,7 +918,18 @@ export const make = Effect.gen(function* () {
918
repository: input.sourceRepositoryName,
919
...(input.context ? { context: input.context } : {}),
920
}).pipe(Effect.map(normalizeRepositoryCloneUrls));
921
- const originRemoteUrl = yield* readConfigValueNullable(input.cwd, "remote.origin.url");
921
+ // Git fetches from a remote's first URL; `config --get` returns the last of several, and
922
+ // `git remote -v` applies `insteadOf` rewrites.
923
+ const originRemoteUrl = yield* git
924
+ .execute({
925
+ operation: "BitbucketApi.readOriginRemoteUrl",
926
+ cwd: input.cwd,
927
+ args: ["config", "--get-all", "remote.origin.url"],
928
+ })
929
+ .pipe(
930
+ Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null),
931
+ Effect.orElseSucceed(() => null),
932
+ );
933
return yield* git.ensureRemote({
934
cwd: input.cwd,
935
preferredName: input.isCrossRepository
packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts
+39
-1
@@ -164,6 +164,7 @@ function harness(input: {
164
readonly localBranches?: ReadonlyArray<string>;
165
readonly resolved?: string;
166
readonly gitFailure?: unknown;
167
+ readonly originUrls?: string;
168
}) {
169
const git: Array<readonly [string, unknown]> = [];
170
const graphqlCalls: Array<GraphQlCall> = [];
@@ -178,7 +179,9 @@ function harness(input: {
179
execute: (args) =>
180
Effect.sync(() => {
181
git.push(["execute", args.args]);
181
- return processOutput("");
182
+ return processOutput(
183
+ args.args[0] === "config" ? (input.originUrls ?? "git@github.com:acme/web.git\n") : "",
184
+ );
185
}),
186
resolvePrimaryRemoteName: () => Effect.succeed("origin"),
187
readConfigValue: () => Effect.succeed("git@github.com:acme/web.git"),
@@ -682,6 +685,41 @@ describe("GitHubSourceControlProvider.checkoutChangeRequest", () => {
685
}).pipe(Effect.provide(layer));
686
});
687
688
+ it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
689
+ // `git remote -v` shows origin's SSH `insteadOf` rewrite; config holds HTTPS, then an SSH push URL.
690
+ const { layer, git } = harness({
691
+ remotes: [
692
+ "origin\tgit@github.com:acme/web.git (fetch)",
693
+ "origin\tgit@github.com:acme/web.git (push)",
694
+ "origin\tgit@github.com:acme/web.git (push)",
695
+ ].join("\n"),
696
+ originUrls: "https://github.com/acme/web.git\ngit@github.com:acme/web.git\n",
697
+ graphql: () => ({ pullRequest: node(7, "feature", "someone") }),
698
+ api: {
699
+ rest: (input) => {
700
+ const fullName = input.path.slice("repos/".length);
701
+ return Effect.succeed(
702
+ restResponse({
703
+ full_name: fullName,
704
+ html_url: `https://github.com/${fullName}`,
705
+ ssh_url: `git@github.com:${fullName}.git`,
706
+ default_branch: "main",
707
+ }),
708
+ );
709
+ },
710
+ },
711
+ });
712
+ return Effect.gen(function* () {
713
+ const gh = yield* GitHubSourceControlProvider.make;
714
+ yield* gh.checkoutChangeRequest({ cwd: "/repo", reference: "7" });
715
+ assert.deepStrictEqual(git.find(([name]) => name === "ensureRemote")?.[1], {
716
+ cwd: "/repo",
717
+ preferredName: "someone",
718
+ url: "https://github.com/someone/web",
719
+ });
720
+ }).pipe(Effect.provide(layer));
721
+ });
722
+
723
it.effect("keeps git's own output out of what a failed checkout reports", () => {
724
const { layer } = harness({
725
remotes: remotesOutput(["origin", "git@github.com:acme/web.git"]),
packages/source-control-github/src/server/GitHubSourceControlProvider.ts
+10
-1
@@ -822,7 +822,16 @@ export const make = Effect.gen(function* () {
822
if (known !== null) return known;
823
const [owner, name] = headNameWithOwner.split("/");
824
const fork = yield* readRepository({ host: base.host, owner: owner!, name: name! });
825
- const originUrl = yield* git.readConfigValue(input.cwd, "remote.origin.url");
825
+ // Git fetches from a remote's first URL; `config --get` returns the last of several, and
826
+ // `git remote -v` applies `insteadOf` rewrites.
827
+ const originUrl = yield* runGit(input.cwd, "readOriginUrl", [
828
+ "config",
829
+ "--get-all",
830
+ "remote.origin.url",
831
+ ]).pipe(
832
+ Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null),
833
+ Effect.orElseSucceed(() => null),
834
+ );
835
return yield* git.ensureRemote({
836
cwd: input.cwd,
837
preferredName: headOwner ?? "fork",