Skip to content

Commit 73a8153

Browse files
MarcoDotIOclaude
andcommitted
fix(source-control): remotes like github.com:owner/repo reach their host
Git reads `host:path` as SSH and takes the user from ssh config, but only `user@host:path` was read as naming a host. A checkout on such a remote got no provider, so pull request sync skipped it and every provider call failed as an unknown host. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d833184 commit 73a8153

12 files changed

Lines changed: 109 additions & 9 deletions

File tree

‎apps/server/src/project/RepositoryIdentityResolver.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
5151
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
5252
);
5353

54+
it.effect("identifies the host of an SSH remote that leaves its user to ssh config", () =>
55+
Effect.gen(function* () {
56+
const fileSystem = yield* FileSystem.FileSystem;
57+
const cwd = yield* fileSystem.makeTempDirectoryScoped({
58+
prefix: "t3-repository-identity-userless-remote-test-",
59+
});
60+
61+
yield* git(cwd, ["init"]);
62+
yield* git(cwd, ["remote", "add", "origin", "github.com:T3Tools/t3code.git"]);
63+
64+
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
65+
const identity = yield* resolver.resolve(cwd);
66+
67+
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
68+
expect(identity?.provider).toBe("github");
69+
expect(identity?.displayName).toBe("T3Tools/t3code");
70+
}).pipe(Effect.provide(RepositoryIdentityResolver.layer)),
71+
);
72+
5473
it.effect("resolves the fetch remote of a partial clone", () =>
5574
Effect.gen(function* () {
5675
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ it.effect("leaves a remote no integration claims unknown, with its server's addr
199199
}),
200200
);
201201

202+
it.effect.each([
203+
["github.com:team/project.git", "github"],
204+
["gitlab.com:team/project.git", "gitlab"],
205+
["bitbucket.org:team/project.git", "bitbucket"],
206+
["code.example.test:team/project.git", "unknown"],
207+
] as const)(
208+
"reads the host of an SSH remote that leaves its user to ssh config: %s",
209+
([url, expected]) =>
210+
Effect.gen(function* () {
211+
const registry = yield* makeRegistry({ remotes: [{ name: "origin", url }] });
212+
213+
const handle = yield* registry.resolveHandle({ cwd: "/repo" });
214+
215+
assert.strictEqual(handle.provider.kind, kind(expected));
216+
assert.strictEqual(handle.context?.remoteUrl, url);
217+
}),
218+
);
219+
202220
it.effect("refines the caller-selected remote instead of choosing another configured remote", () =>
203221
Effect.gen(function* () {
204222
const registry = yield* makeRegistry({

‎apps/server/src/sourceControl/sourceControlDefinitions.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ it("runs a driver for every host the server and clients read, in their order", (
1010
sourceControlDefinitions.definitions,
1111
);
1212
});
13+
14+
it.each([
15+
"C:/repos/project",
16+
"C:\\repos\\project",
17+
"/srv/git/repo.git",
18+
"./team:group/repo",
19+
"file:///srv/git/repo.git",
20+
"hg::team/repo",
21+
"codecommit::us-east-1://repo",
22+
])("finds no host in %s, which git does not read as SSH", (remoteUrl) => {
23+
expect(sourceControlDefinitions.recognizeRemote(remoteUrl)).toBeNull();
24+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { isSshRemoteUrl } from "./sourceControl.ts";
4+
5+
describe("isSshRemoteUrl", () => {
6+
it.each([
7+
"[email protected]:owner/repo.git",
8+
"ssh://[email protected]/owner/repo.git",
9+
"github.com:owner/repo.git",
10+
"github-work:owner/repo.git",
11+
])("reads %s as SSH", (remoteUrl) => {
12+
expect(isSshRemoteUrl(remoteUrl)).toBe(true);
13+
});
14+
15+
it.each([
16+
"https://github.com/owner/repo.git",
17+
"C:/repos/repo",
18+
"/srv/git/repo.git",
19+
"./team:group/repo",
20+
"file:///srv/git/repo.git",
21+
"hg::owner/repo",
22+
])("does not read %s as SSH", (remoteUrl) => {
23+
expect(isSshRemoteUrl(remoteUrl)).toBe(false);
24+
});
25+
});
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
const SCP_SSH_REMOTE_PATTERN = /^[a-zA-Z0-9._-]+@([^:/]+):/;
2+
// Git reads `host:path` as SSH too, leaving the user to ssh config. A single letter is a Windows
3+
// drive and `::` names a remote helper.
4+
const USERLESS_SCP_SSH_REMOTE_PATTERN = /^([^@:/\\\s]{2,}):(?!:)\/?[^/\s]+(?:\/[^/\s]+)+\/?$/u;
5+
6+
/** Null unless the remote is `host:path`, with no user. */
7+
export function userlessScpRemoteHost(remoteUrl: string): string | null {
8+
return USERLESS_SCP_SSH_REMOTE_PATTERN.exec(remoteUrl.trim())?.[1] ?? null;
9+
}
210

311
export function isSshRemoteUrl(remoteUrl: string): boolean {
412
const trimmed = remoteUrl.trim();
5-
return SCP_SSH_REMOTE_PATTERN.test(trimmed) || trimmed.toLowerCase().startsWith("ssh://");
13+
return (
14+
SCP_SSH_REMOTE_PATTERN.test(trimmed) ||
15+
trimmed.toLowerCase().startsWith("ssh://") ||
16+
userlessScpRemoteHost(trimmed) !== null
17+
);
618
}

‎packages/source-control-core/src/server/SourceControlProvider.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { assert, it } from "@effect/vitest";
22

3-
import { transportSafeSourceControlErrorValue } from "./SourceControlProvider.ts";
3+
import {
4+
repositoryPathFromRemoteUrl,
5+
transportSafeSourceControlErrorValue,
6+
} from "./SourceControlProvider.ts";
47

58
it("removes URL credentials, query parameters, and fragments from error transport values", () => {
69
assert.strictEqual(
@@ -10,3 +13,13 @@ it("removes URL credentials, query parameters, and fragments from error transpor
1013
"https://example.test/org/repo/pull/42",
1114
);
1215
});
16+
17+
it.each([
18+
["[email protected]:T3Tools/t3code.git", "T3Tools/t3code"],
19+
["github.com:T3Tools/t3code.git", "T3Tools/t3code"],
20+
["gitlab.com:T3Tools/platform/t3code", "T3Tools/platform/t3code"],
21+
["https://github.com/T3Tools/t3code.git", "T3Tools/t3code"],
22+
["C:/repos/t3code", null],
23+
])("reads the repository path of %s", (remoteUrl, expected) => {
24+
assert.strictEqual(repositoryPathFromRemoteUrl(remoteUrl), expected);
25+
});

‎packages/source-control-core/src/server/SourceControlProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function repositoryPathFromRemoteUrl(url: string | null): string | null {
106106
}
107107

108108
const match =
109-
/^(?:[^@/\s]+@[^:/\s]+:|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
109+
/^(?:[^@/\s]+@[^:/\s]+:|[^@:/\\\s]{2,}:(?!:)|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
110110
trimmed,
111111
);
112112
const path = match?.[1]?.trim() ?? "";

‎packages/source-control-core/src/shared/definition.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TrimmedString,
1010
} from "@t3tools/contracts";
1111
import { normalizeGitRemoteUrl, parseGitRemoteRepository } from "@t3tools/shared/git";
12+
import { userlessScpRemoteHost } from "@t3tools/shared/sourceControl";
1213
import * as Effect from "effect/Effect";
1314
import * as Schema from "effect/Schema";
1415
import * as SchemaTransformation from "effect/SchemaTransformation";
@@ -497,7 +498,7 @@ export function parseGitRemoteHost(remoteUrl: string): GitRemoteHost | null {
497498
const trimmed = remoteUrl.trim();
498499
if (trimmed.length === 0) return null;
499500
let host: string;
500-
const scpHost = SCP_REMOTE.exec(trimmed)?.[1];
501+
const scpHost = SCP_REMOTE.exec(trimmed)?.[1] ?? userlessScpRemoteHost(trimmed);
501502
if (scpHost) {
502503
host = scpHost.toLowerCase();
503504
} else {

‎packages/source-control-gitcafe/src/server/GitCafeSourceControlProvider.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";
2121
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
2222
import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
23+
import { isSshRemoteUrl } from "@t3tools/shared/sourceControl";
2324
import { parsePullRequestUrl } from "../shared/definition.ts";
2425

2526
import * as GitCafeApi from "./GitCafeApi.ts";
@@ -165,7 +166,7 @@ const cloneUrls = (target: Target) => ({
165166
});
166167

167168
const isSshRemote = (remoteUrl: string | undefined) =>
168-
remoteUrl !== undefined && (/^ssh:\/\//iu.test(remoteUrl) || /^[^/:]+@[^/:]+:/u.test(remoteUrl));
169+
remoteUrl !== undefined && isSshRemoteUrl(remoteUrl);
169170

170171
export const make = Effect.gen(function* () {
171172
const api = yield* GitCafeApi.GitCafeApi;

‎packages/source-control-gitcafe/src/server/gitCafeHosts.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe("gitCafeHosts", () => {
1616
["https://staging.git.cafe/team/project.git/", "staging.git.cafe"],
1717
["[email protected]:team/project.git", "git.cafe"],
1818
["ssh://[email protected]/team/project.git", "git.cafe"],
19+
["git.cafe:team/project.git", "git.cafe"],
1920
])("reads the host and repository of %s", (url, host) => {
2021
assert.deepStrictEqual(GitCafeHosts.parseGitCafeRemote(url), {
2122
host,

0 commit comments

Comments
 (0)