Skip to content

Commit d89b4e5

Browse files
saphidclaude
andcommitted
fix(source-control): fork protocol comes from origin's configured URL, not its insteadOf rewrite
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0f44341 commit d89b4e5

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

‎packages/source-control-bitbucket/src/server/BitbucketApi.test.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Fiber from "effect/Fiber";
99
import * as FileSystem from "effect/FileSystem";
1010
import * as Layer from "effect/Layer";
1111
import * as Option from "effect/Option";
12+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1213
import * as Queue from "effect/Queue";
1314
import * as Result from "effect/Result";
1415
import * as Schema from "effect/Schema";
@@ -63,6 +64,14 @@ const repositoryJson = {
6364
mainbranch: { name: "main" },
6465
};
6566

67+
const gitOutput = (stdout: string) => ({
68+
exitCode: ChildProcessSpawner.ExitCode(0),
69+
stdout,
70+
stderr: "",
71+
stdoutTruncated: false,
72+
stderrTruncated: false,
73+
});
74+
6675
function makeLayer(input: {
6776
readonly response: (request: HttpClientRequest.HttpClientRequest) => Response;
6877
/** Holds the answer until it resolves, in place of answering at once. */
@@ -85,6 +94,9 @@ function makeLayer(input: {
8594
),
8695
);
8796
const gitMock = {
97+
execute: vi.fn<HostGit["execute"]>(() =>
98+
Effect.succeed(gitOutput("[email protected]:pingdotgg/t3code.git\n")),
99+
),
88100
readConfigValue: vi.fn<HostGit["readConfigValue"]>(() =>
89101
Effect.succeed<string | null>("[email protected]:pingdotgg/t3code.git"),
90102
),
@@ -254,9 +266,16 @@ it.effect.each([false, true])("honors PR URL repositories, context=%s", (withCon
254266
});
255267

256268
it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
257-
// `readConfigValue` answers `remote.origin.url` with an SSH second URL.
269+
// `git remote -v` shows origin's SSH `insteadOf` rewrite; config holds HTTPS, then an SSH push URL.
258270
const { git, layer } = makeLayer({
259-
remoteUrl: "https://bitbucket.org/pingdotgg/t3code.git",
271+
git: {
272+
execute: () =>
273+
Effect.succeed(
274+
gitOutput(
275+
"https://bitbucket.org/pingdotgg/t3code.git\[email protected]:pingdotgg/t3code.git\n",
276+
),
277+
),
278+
},
260279
response: (request) =>
261280
Response.json(
262281
request.url.endsWith("/pullrequests/42")

‎packages/source-control-bitbucket/src/server/BitbucketApi.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,18 @@ export const make = Effect.gen(function* () {
915915
repository: input.sourceRepositoryName,
916916
...(input.context ? { context: input.context } : {}),
917917
}).pipe(Effect.map(normalizeRepositoryCloneUrls));
918-
// Git fetches from a remote's first URL; `config --get` returns the last of several.
919-
const originRemoteUrl = yield* git.remotes(input.cwd).pipe(
920-
Effect.flatten,
921-
Effect.map(({ remotes }) => remotes.find((remote) => remote.name === "origin")?.url ?? null),
922-
Effect.orElseSucceed(() => null),
923-
);
918+
// Git fetches from a remote's first URL; `config --get` returns the last of several, and
919+
// `git remote -v` applies `insteadOf` rewrites.
920+
const originRemoteUrl = yield* git
921+
.execute({
922+
operation: "BitbucketApi.readOriginRemoteUrl",
923+
cwd: input.cwd,
924+
args: ["config", "--get-all", "remote.origin.url"],
925+
})
926+
.pipe(
927+
Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null),
928+
Effect.orElseSucceed(() => null),
929+
);
924930
return yield* git.ensureRemote({
925931
cwd: input.cwd,
926932
preferredName: input.isCrossRepository

‎packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function harness(input: {
164164
readonly localBranches?: ReadonlyArray<string>;
165165
readonly resolved?: string;
166166
readonly gitFailure?: unknown;
167+
readonly originUrls?: string;
167168
}) {
168169
const git: Array<readonly [string, unknown]> = [];
169170
const graphqlCalls: Array<GraphQlCall> = [];
@@ -178,7 +179,9 @@ function harness(input: {
178179
execute: (args) =>
179180
Effect.sync(() => {
180181
git.push(["execute", args.args]);
181-
return processOutput("");
182+
return processOutput(
183+
args.args[0] === "config" ? (input.originUrls ?? "[email protected]:acme/web.git\n") : "",
184+
);
182185
}),
183186
resolvePrimaryRemoteName: () => Effect.succeed("origin"),
184187
readConfigValue: () => Effect.succeed("[email protected]:acme/web.git"),
@@ -652,13 +655,14 @@ describe("GitHubSourceControlProvider.checkoutChangeRequest", () => {
652655
});
653656

654657
it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => {
655-
// The harness's `remote.origin.url` config read answers with an SSH second URL.
658+
// `git remote -v` shows origin's SSH `insteadOf` rewrite; config holds HTTPS, then an SSH push URL.
656659
const { layer, git } = harness({
657660
remotes: [
658-
"origin\thttps://github.com/acme/web.git (fetch)",
659-
"origin\thttps://github.com/acme/web.git (push)",
661+
"origin\tgit@github.com:acme/web.git (fetch)",
662+
"origin\tgit@github.com:acme/web.git (push)",
660663
"origin\[email protected]:acme/web.git (push)",
661664
].join("\n"),
665+
originUrls: "https://github.com/acme/web.git\[email protected]:acme/web.git\n",
662666
graphql: () => ({ pullRequest: node(7, "feature", "someone") }),
663667
api: {
664668
rest: (input) => {

‎packages/source-control-github/src/server/GitHubSourceControlProvider.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,16 @@ export const make = Effect.gen(function* () {
724724
if (known !== null) return known;
725725
const [owner, name] = headNameWithOwner.split("/");
726726
const fork = yield* readRepository({ host: base.host, owner: owner!, name: name! });
727-
// Git fetches from a remote's first URL; `config --get` returns the last of several.
728-
const originUrl =
729-
parseFetchRemotes(remotes).find((remote) => remote.name === "origin")?.url ?? null;
727+
// Git fetches from a remote's first URL; `config --get` returns the last of several, and
728+
// `git remote -v` applies `insteadOf` rewrites.
729+
const originUrl = yield* runGit(input.cwd, "readOriginUrl", [
730+
"config",
731+
"--get-all",
732+
"remote.origin.url",
733+
]).pipe(
734+
Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null),
735+
Effect.orElseSucceed(() => null),
736+
);
730737
return yield* git.ensureRemote({
731738
cwd: input.cwd,
732739
preferredName: headOwner ?? "fork",

0 commit comments

Comments
 (0)