The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 09:30 UTC
fix(vcs): look up pull request clone URLs on the pull request's host (#15000)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
Alex Southwell committed
Nov 1, 2026 at 02:03 UTC
28724828b3cd5267815cf7dd6c6e14b67b693b17
6 files changed
+278
-5
apps/server/src/git/GitManager.test.ts
+163
-1
@@ -490,7 +490,12 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
490
repository: input.repository,
491
}).pipe(
492
Effect.flatMap(() => {
493
- const cloneUrls = scenario.repositoryCloneUrls?.[input.repository];
493
+ const repository = input.host ? `${input.host}/${input.repository}` : input.repository;
494
+ const cloneUrls =
495
+ scenario.repositoryCloneUrls?.[repository] ??
496
+ (input.host === "github.com"
497
+ ? scenario.repositoryCloneUrls?.[input.repository]
498
+ : undefined);
499
return cloneUrls
500
? Effect.succeed({ nameWithOwner: input.repository, ...cloneUrls })
501
: Effect.fail(
@@ -3906,6 +3911,163 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
3911
}),
3912
);
3913
3914
+ it.effect.each([
3915
+ { mode: "local", repositoryType: "same repo" },
3916
+ { mode: "local", repositoryType: "fork" },
3917
+ { mode: "worktree", repositoryType: "same repo" },
3918
+ { mode: "worktree", repositoryType: "fork" },
3919
+ ] as const)(
3920
+ "checks out same-named repositories on the pull request host ($mode, $repositoryType)",
3921
+ ({ mode, repositoryType }) =>
3922
+ Effect.gen(function* () {
3923
+ const isCrossRepository = repositoryType === "fork";
3924
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
3925
+ yield* initRepo(repoDir);
3926
+ // The checkout is on github.com, which also has a same-named repository,
3927
+ // and the pull request is on another host. Clone lookups that fall back
3928
+ // to the checkout's host find the github.com repository.
3929
+ const enterpriseRemote = yield* createBareRemote();
3930
+ const defaultHostRemote = yield* createBareRemote();
3931
+ const host = "github.enterprise.test";
3932
+ const repository = isCrossRepository ? "fork/repo" : "owner/base";
3933
+ const cloneUrl = `https://${host}/${repository}.git`;
3934
+ yield* runGit(repoDir, ["remote", "add", "origin", defaultHostRemote]);
3935
+ yield* runGit(repoDir, ["push", "origin", "main", "main:feature/host"]);
3936
+ yield* runGit(repoDir, ["push", enterpriseRemote, "main"]);
3937
+ yield* runGit(repoDir, ["checkout", "-b", "feature/host"]);
3938
+ NodeFS.writeFileSync(NodePath.join(repoDir, "host.txt"), "enterprise head\n");
3939
+ yield* runGit(repoDir, ["add", "host.txt"]);
3940
+ yield* runGit(repoDir, ["commit", "-m", "Enterprise head"]);
3941
+ const expectedHead = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim();
3942
+ yield* runGit(repoDir, ["push", enterpriseRemote, "feature/host"]);
3943
+ yield* runGit(repoDir, ["checkout", "main"]);
3944
+ if (mode === "worktree") {
3945
+ yield* runGit(repoDir, ["branch", "-D", "feature/host"]);
3946
+ }
3947
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
3948
+ repoDir,
3949
+ "origin",
3950
+ "https://github.com/owner/base.git",
3951
+ defaultHostRemote,
3952
+ );
3953
+ yield* runGit(repoDir, ["config", `url.${enterpriseRemote}.insteadOf`, cloneUrl]);
3954
+ const defaultCloneUrl = `https://github.com/${repository}.git`;
3955
+ yield* runGit(repoDir, [
3956
+ "config",
3957
+ "--add",
3958
+ `url.${defaultHostRemote}.insteadOf`,
3959
+ defaultCloneUrl,
3960
+ ]);
3961
+
3962
+ const { manager } = yield* makeManager({
3963
+ providerScenario: {
3964
+ pullRequest: {
3965
+ number: 42,
3966
+ title: "Enterprise head",
3967
+ url: `https://${host}/owner/base/pull/42`,
3968
+ baseRefName: "main",
3969
+ headRefName: "feature/host",
3970
+ state: "open",
3971
+ isCrossRepository,
3972
+ headRepositoryNameWithOwner: repository,
3973
+ headRepositoryOwnerLogin: isCrossRepository ? "fork" : "owner",
3974
+ },
3975
+ repositoryCloneUrls: {
3976
+ [repository]: { url: defaultCloneUrl, sshUrl: defaultCloneUrl },
3977
+ [`${host}/${repository}`]: { url: cloneUrl, sshUrl: cloneUrl },
3978
+ },
3979
+ },
3980
+ });
3981
+ const result = yield* preparePullRequestThread(manager, {
3982
+ cwd: repoDir,
3983
+ reference: "42",
3984
+ mode,
3985
+ });
3986
+ const checkoutDir = result.worktreePath ?? repoDir;
3987
+ const upstreamRemote = (yield* runGit(checkoutDir, [
3988
+ "config",
3989
+ `branch.${result.branch}.remote`,
3990
+ ])).stdout.trim();
3991
+ const upstreamUrl = (yield* runGit(checkoutDir, [
3992
+ "config",
3993
+ `remote.${upstreamRemote}.url`,
3994
+ ])).stdout.trim();
3995
+ const head = (yield* runGit(checkoutDir, ["rev-parse", "HEAD"])).stdout.trim();
3996
+ expect({ upstreamUrl, head }).toEqual({ upstreamUrl: cloneUrl, head: expectedHead });
3997
+ expect(NodeFS.readFileSync(NodePath.join(checkoutDir, "host.txt"), "utf8")).toBe(
3998
+ "enterprise head\n",
3999
+ );
4000
+ }),
4001
+ );
4002
+
4003
+ it.effect.each([
4004
+ { mode: "local", url: "malformed pull request URL" },
4005
+ { mode: "worktree", url: "malformed pull request URL" },
4006
+ { mode: "local", url: "file:///owner/base/pull/42" },
4007
+ { mode: "worktree", url: "file:///owner/base/pull/42" },
4008
+ ] as const)(
4009
+ "checks out pull requests with hostless URLs using the default host ($mode, $url)",
4010
+ ({ mode, url }) =>
4011
+ Effect.gen(function* () {
4012
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4013
+ yield* initRepo(repoDir);
4014
+ const originDir = yield* createBareRemote();
4015
+ const forkDir = yield* createBareRemote();
4016
+ const cloneUrl = "https://github.com/fork/repo.git";
4017
+ yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
4018
+ yield* runGit(repoDir, ["push", "origin", "main"]);
4019
+ yield* runGit(repoDir, ["checkout", "-b", "feature/malformed-url"]);
4020
+ NodeFS.writeFileSync(NodePath.join(repoDir, "fork.txt"), "fork head\n");
4021
+ yield* runGit(repoDir, ["add", "fork.txt"]);
4022
+ yield* runGit(repoDir, ["commit", "-m", "Fork head"]);
4023
+ const expectedHead = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout.trim();
4024
+ yield* runGit(repoDir, ["push", forkDir, "feature/malformed-url"]);
4025
+ yield* runGit(repoDir, ["checkout", "main"]);
4026
+ if (mode === "worktree") {
4027
+ yield* runGit(repoDir, ["branch", "-D", "feature/malformed-url"]);
4028
+ }
4029
+ yield* runGit(repoDir, ["config", `url.${forkDir}.insteadOf`, cloneUrl]);
4030
+
4031
+ const { manager } = yield* makeManager({
4032
+ providerScenario: {
4033
+ pullRequest: {
4034
+ number: 42,
4035
+ title: "Fork head",
4036
+ url,
4037
+ baseRefName: "main",
4038
+ headRefName: "feature/malformed-url",
4039
+ state: "open",
4040
+ isCrossRepository: true,
4041
+ headRepositoryNameWithOwner: "fork/repo",
4042
+ headRepositoryOwnerLogin: "fork",
4043
+ },
4044
+ repositoryCloneUrls: {
4045
+ "fork/repo": { url: cloneUrl, sshUrl: cloneUrl },
4046
+ },
4047
+ },
4048
+ });
4049
+ const result = yield* preparePullRequestThread(manager, {
4050
+ cwd: repoDir,
4051
+ reference: "42",
4052
+ mode,
4053
+ });
4054
+ const checkoutDir = result.worktreePath ?? repoDir;
4055
+ const upstreamRemote = (yield* runGit(checkoutDir, [
4056
+ "config",
4057
+ `branch.${result.branch}.remote`,
4058
+ ])).stdout.trim();
4059
+ const upstreamUrl = (yield* runGit(checkoutDir, [
4060
+ "config",
4061
+ `remote.${upstreamRemote}.url`,
4062
+ ])).stdout.trim();
4063
+ const head = (yield* runGit(checkoutDir, ["rev-parse", "HEAD"])).stdout.trim();
4064
+ expect({ upstreamUrl, head }).toEqual({ upstreamUrl: cloneUrl, head: expectedHead });
4065
+ expect(NodeFS.readFileSync(NodePath.join(checkoutDir, "fork.txt"), "utf8")).toBe(
4066
+ "fork head\n",
4067
+ );
4068
+ }),
4069
+ );
4070
+
4071
const setUpLocalCheckoutPullRequest = Effect.fnUntraced(function* (
4072
reportedHead: (pullHead: string) => string | undefined = (pullHead) => pullHead,
4073
syncCheckoutWithPullHead = true,
apps/server/src/git/GitManager.ts
+7
@@ -893,6 +893,11 @@ export const make = Effect.gen(function* () {
893
}),
894
);
895
896
+ const pullRequestHostOptions = (url: string) => {
897
+ const host = URL.parse(url)?.host;
898
+ return host ? { host } : {};
899
+ };
900
+
901
const configurePullRequestHeadUpstreamBase = Effect.fn("configurePullRequestHeadUpstream")(
902
function* (
903
cwd: string,
@@ -923,6 +928,7 @@ export const make = Effect.gen(function* () {
928
const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
929
cwd,
930
repository: repositoryNameWithOwner,
931
+ ...pullRequestHostOptions(pullRequest.url),
932
});
933
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
934
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
@@ -1036,6 +1042,7 @@ export const make = Effect.gen(function* () {
1042
const cloneUrls = yield* (yield* sourceControlProvider(cwd)).getRepositoryCloneUrls({
1043
cwd,
1044
repository: repositoryNameWithOwner,
1045
+ ...pullRequestHostOptions(pullRequest.url),
1046
});
1047
const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url");
1048
const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url;
packages/source-control-core/src/server/SourceControlProvider.ts
+2
@@ -195,6 +195,8 @@ export class SourceControlProvider extends Context.Service<
195
readonly cwd: string;
196
readonly context?: SourceControlProviderContext;
197
readonly repository: string;
198
+ /** The host the repository is on. Without it, the checkout's host. */
199
+ readonly host?: string;
200
}) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
201
readonly createRepository: (input: {
202
readonly cwd: string;
packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
+67
@@ -33,6 +33,73 @@ const requestJson = (request: HttpClientRequest.HttpClientRequest): unknown => {
33
};
34
const encodeJsonEffect = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
35
36
+it.effect("reads clone URLs on the given host rather than the checkout's", () => {
37
+ const urls: string[] = [];
38
+ const client = HttpClient.make((request) => {
39
+ urls.push(request.url);
40
+ return Effect.succeed(
41
+ HttpClientResponse.fromWeb(
42
+ request,
43
+ new Response(
44
+ encodeJson({
45
+ full_name: "maria/project",
46
+ clone_url: "https://forgejo.test/maria/project.git",
47
+ ssh_url: "git@forgejo.test:maria/project.git",
48
+ }),
49
+ ),
50
+ ),
51
+ );
52
+ });
53
+ return Effect.gen(function* () {
54
+ const cli = yield* ForgejoCli.make;
55
+ const provider = yield* ForgejoSourceControlProvider.make.pipe(
56
+ Effect.provideService(ForgejoCli.ForgejoCli, cli),
57
+ );
58
+ const cloneUrls = yield* provider.getRepositoryCloneUrls({
59
+ cwd: "/repo",
60
+ repository: "maria/project",
61
+ host: "forgejo.test",
62
+ });
63
+ assert.strictEqual(cloneUrls.url, "https://forgejo.test/maria/project.git");
64
+ assert.deepStrictEqual(urls, ["https://forgejo.test/api/v1/repos/maria/project"]);
65
+ }).pipe(
66
+ Effect.provideService(HttpClient.HttpClient, client),
67
+ Effect.provideService(
68
+ FileSystem.FileSystem,
69
+ FileSystem.makeNoop({
70
+ exists: () => Effect.succeed(true),
71
+ readFileString: () =>
72
+ Effect.succeed(
73
+ encodeJson({
74
+ hosts: {
75
+ "forgejo.test": { type: "Application", token: "test-token" },
76
+ "code.example": { type: "Application", token: "other-token" },
77
+ },
78
+ }),
79
+ ),
80
+ }),
81
+ ),
82
+ Effect.provide(
83
+ TestSourceControlHost.layer({
84
+ process: {
85
+ run: (input) => {
86
+ if (input.command === "git") {
87
+ assert.deepStrictEqual(input.args, ["remote", "-v"]);
88
+ // The checkout is on another Forgejo server with a same-named repository.
89
+ return Effect.succeed(
90
+ processOutput("origin\thttps://code.example/maria/project.git (fetch)"),
91
+ );
92
+ }
93
+ assert.strictEqual(input.command, "fj");
94
+ assert.deepStrictEqual(input.args, ["--host", "https://forgejo.test", "whoami"]);
95
+ return Effect.succeed(processOutput(""));
96
+ },
97
+ },
98
+ }),
99
+ ),
100
+ );
101
+});
102
+
103
const requiredChecksPullRequest = {
104
number: 7,
105
title: "Required checks",
packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts
+32
@@ -289,6 +289,38 @@ describe("GitHubSourceControlProvider repository resolution", () => {
289
}).pipe(Effect.provide(layer));
290
});
291
292
+ it.effect("reads clone URLs on the given host before the checkout's", () => {
293
+ const paths: string[] = [];
294
+ const { layer } = harness({
295
+ remotes: remotesOutput(["origin", "https://github.com/acme/web.git"]),
296
+ api: {
297
+ rest: (input) =>
298
+ Effect.sync(() => {
299
+ paths.push(`${input.host} ${input.path}`);
300
+ return restResponse({
301
+ full_name: "acme/web",
302
+ html_url: `https://${input.host}/acme/web`,
303
+ ssh_url: `git@${input.host}:acme/web.git`,
304
+ });
305
+ }),
306
+ },
307
+ });
308
+ return Effect.gen(function* () {
309
+ const gh = yield* GitHubSourceControlProvider.make;
310
+ const cloneUrls = yield* gh.getRepositoryCloneUrls({
311
+ cwd: "/repo",
312
+ repository: "acme/web",
313
+ host: "GitHub.Enterprise.test",
314
+ });
315
+ yield* gh.getRepositoryCloneUrls({ cwd: "/repo", repository: "acme/web" });
316
+ assert.strictEqual(cloneUrls.url, "https://github.enterprise.test/acme/web");
317
+ assert.deepStrictEqual(paths, [
318
+ "github.enterprise.test repos/acme/web",
319
+ "github.com repos/acme/web",
320
+ ]);
321
+ }).pipe(Effect.provide(layer));
322
+ });
323
+
324
it.effect.each([
325
["github", "git@github:acme/web.git"],
326
["github.com-scale", "git@github.com-scale:acme/web.git"],
packages/source-control-github/src/server/GitHubSourceControlProvider.ts
+7
-4
@@ -996,10 +996,13 @@ export const make = Effect.gen(function* () {
996
),
997
getRepositoryCloneUrls: Effect.fn("GitHubSourceControlProvider.getRepositoryCloneUrls")(
998
function* (input) {
999
- const fallbackHost = (yield* resolveRepository(input).pipe(
1000
- Effect.map((locator) => locator.host),
1001
- Effect.orElseSucceed(() => environment.GH_HOST ?? "github.com"),
1002
- )).toLowerCase();
999
+ const fallbackHost = (
1000
+ input.host ??
1001
+ (yield* resolveRepository(input).pipe(
1002
+ Effect.map((locator) => locator.host),
1003
+ Effect.orElseSucceed(() => environment.GH_HOST ?? "github.com"),
1004
+ ))
1005
+ ).toLowerCase();
1006
const bareName = input.repository.trim().replace(/\.git$/i, "");
1007
const locator =
1008
parseGitHubRepositorySelector(input.repository, fallbackHost) ??