Skip to content

Commit 1f21017

Browse files
committed
fix(shared): resolve multi-account SSH alias hosts to the real provider host
A remote like [email protected]:org/repo.git, the documented ~/.ssh/config convention for multiple accounts, leaked the alias into every derived value: the provider base URL became https://github.com-work, the normalized identity key kept the alias so PR URLs never matched the project, and the owner/repo parser returned null. SSH remotes now canonicalize an aliased github.com, gitlab.com, or bitbucket.org host back to the real one; HTTPS remotes keep their literal host. Also drops GitManager's private copy of the owner/repo parser in favor of the shared one. Fixes #9458
1 parent 0372836 commit 1f21017

5 files changed

Lines changed: 88 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
detectSourceControlProviderFromGitRemoteUrl,
3737
mergeGitStatusParts,
3838
normalizeGitRemoteUrl,
39+
parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
3940
resolveAutoFeatureBranchName,
4041
sanitizeBranchFragment,
4142
sanitizeFeatureBranchName,
@@ -235,20 +236,6 @@ function resolvePullRequestWorktreeLocalBranchName(
235236
return `t3code/pr-${pullRequest.number}/${suffix}`;
236237
}
237238

238-
function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
239-
const trimmed = url?.trim() ?? "";
240-
if (trimmed.length === 0) {
241-
return null;
242-
}
243-
244-
const match =
245-
/^(?:git@github\.com:|ssh:\/\/git@github\.com\/|https:\/\/github\.com\/|git:\/\/github\.com\/)([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i.exec(
246-
trimmed,
247-
);
248-
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
249-
return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null;
250-
}
251-
252239
function parseRepositoryOwnerLogin(nameWithOwner: string | null): string | null {
253240
const trimmed = nameWithOwner?.trim() ?? "";
254241
if (trimmed.length === 0) {

‎packages/shared/src/git.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,35 @@ describe("applyGitStatusStreamEvent", () => {
172172
});
173173
});
174174
});
175+
176+
describe("SSH host aliases", () => {
177+
it("canonicalizes multi-account SSH alias hosts to the real provider host", () => {
178+
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/T3Code.git")).toBe(
179+
"github.com/t3tools/t3code",
180+
);
181+
expect(normalizeGitRemoteUrl("ssh://[email protected]/T3Tools/T3Code.git")).toBe(
182+
"github.com/t3tools/t3code",
183+
);
184+
expect(normalizeGitRemoteUrl("[email protected]:group/project.git")).toBe(
185+
"gitlab.com/group/project",
186+
);
187+
});
188+
189+
it("leaves non-SSH remotes and unrelated hosts alone", () => {
190+
expect(normalizeGitRemoteUrl("https://github.com-work/T3Tools/T3Code.git")).toBe(
191+
"github.com-work/t3tools/t3code",
192+
);
193+
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/T3Code.git")).toBe(
194+
"github.company.com/t3tools/t3code",
195+
);
196+
});
197+
198+
it("parses owner and repo through a GitHub SSH alias", () => {
199+
expect(
200+
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("[email protected]:owner/repo.git"),
201+
).toBe("owner/repo");
202+
expect(
203+
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://[email protected]/owner/repo"),
204+
).toBe("owner/repo");
205+
});
206+
});

‎packages/shared/src/git.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import type {
88
} from "@t3tools/contracts";
99
import * as Arr from "effect/Array";
1010
import * as Result from "effect/Result";
11-
import { detectSourceControlProviderFromRemoteUrl } from "./sourceControl.ts";
11+
import {
12+
canonicalizeSshRemoteHostname,
13+
detectSourceControlProviderFromRemoteUrl,
14+
} from "./sourceControl.ts";
1215

1316
export const WORKTREE_BRANCH_PREFIX = "t3code";
1417
// Canonical form is `t3code/<8 hex>`. Older mobile builds generated `t3code/<uuid>`
@@ -126,7 +129,9 @@ export function normalizeGitRemoteUrl(value: string): string {
126129
.filter((segment) => segment.length > 0)
127130
.join("/");
128131
if (url.hostname && repositoryPath.includes("/")) {
129-
return `${url.hostname}/${repositoryPath}`;
132+
const hostname =
133+
url.protocol === "ssh:" ? canonicalizeSshRemoteHostname(url.hostname) : url.hostname;
134+
return `${hostname}/${repositoryPath}`;
130135
}
131136
} catch {
132137
return normalized;
@@ -137,7 +142,7 @@ export function normalizeGitRemoteUrl(value: string): string {
137142
normalized,
138143
);
139144
if (scpStyleHostAndPath?.[1] && scpStyleHostAndPath[2]) {
140-
return `${scpStyleHostAndPath[1]}/${scpStyleHostAndPath[2]}`;
145+
return `${canonicalizeSshRemoteHostname(scpStyleHostAndPath[1])}/${scpStyleHostAndPath[2]}`;
141146
}
142147

143148
return normalized;
@@ -152,8 +157,9 @@ export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | nu
152157
return null;
153158
}
154159

160+
// SSH shapes tolerate a multi-account alias suffix such as `github.com-work`.
155161
const match =
156-
/^(?:git@github\.com:|ssh:\/\/git@github\.com\/|https:\/\/github\.com\/|git:\/\/github\.com\/)([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i.exec(
162+
/^(?:git@github\.com(?:-[^./:\s]+)?:|ssh:\/\/git@github\.com(?:-[^./\s]+)?\/|https:\/\/github\.com\/|git:\/\/github\.com\/)([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i.exec(
157163
trimmed,
158164
);
159165
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";

‎packages/shared/src/sourceControl.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,30 @@ describe("isSshRemoteUrl", () => {
160160
expect(isSshRemoteUrl("[email protected]/project/repo")).toBe(false);
161161
});
162162
});
163+
164+
describe("SSH host aliases", () => {
165+
it("resolves multi-account SSH alias hosts to the real provider", () => {
166+
expect(detectSourceControlProviderFromRemoteUrl("[email protected]:owner/repo.git")).toEqual({
167+
kind: "github",
168+
name: "GitHub",
169+
baseUrl: "https://github.com",
170+
});
171+
expect(
172+
detectSourceControlProviderFromRemoteUrl("ssh://[email protected]/group/repo.git"),
173+
).toEqual({
174+
kind: "gitlab",
175+
name: "GitLab",
176+
baseUrl: "https://gitlab.com",
177+
});
178+
});
179+
180+
it("keeps literal hosts for non-SSH remotes that merely resemble aliases", () => {
181+
expect(
182+
detectSourceControlProviderFromRemoteUrl("https://github.com-work/owner/repo.git"),
183+
).toEqual({
184+
kind: "github",
185+
name: "GitHub Self-Hosted",
186+
baseUrl: "https://github.com-work",
187+
});
188+
});
189+
});

‎packages/shared/src/sourceControl.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ function toBaseUrl(host: string): string {
170170
return `https://${host}`;
171171
}
172172

173+
// Multi-account ~/.ssh/config setups follow the documented convention of
174+
// suffixing the real host in the alias, e.g. `Host github.com-work`. The alias
175+
// only exists for SSH, so HTTPS remotes keep their literal host.
176+
const SSH_ALIAS_HOSTNAME_PATTERN = /^(github\.com|gitlab\.com|bitbucket\.org)-[^./]+$/;
177+
178+
export function canonicalizeSshRemoteHostname(hostname: string): string {
179+
return SSH_ALIAS_HOSTNAME_PATTERN.exec(hostname)?.[1] ?? hostname;
180+
}
181+
173182
function hasDnsLabel(host: string, label: string): boolean {
174183
return host.split(".").includes(label);
175184
}
@@ -201,11 +210,17 @@ function isBitbucketHost(host: string): boolean {
201210
export function detectSourceControlProviderFromRemoteUrl(
202211
remoteUrl: string,
203212
): SourceControlProviderInfo | null {
204-
const host = parseRemoteHost(remoteUrl);
205-
if (!host) {
213+
const parsedHost = parseRemoteHost(remoteUrl);
214+
if (!parsedHost) {
206215
return null;
207216
}
208-
const hostname = parseHostName(host);
217+
const parsedHostname = parseHostName(parsedHost);
218+
const hostname = isSshRemoteUrl(remoteUrl)
219+
? canonicalizeSshRemoteHostname(parsedHostname)
220+
: parsedHostname;
221+
// Stripping the alias suffix drops any port, but SSH ports never belong in
222+
// the https base URL anyway; unaliased hosts keep their port as before.
223+
const host = hostname === parsedHostname ? parsedHost : hostname;
209224

210225
if (isGitHubHost(hostname)) {
211226
return {

0 commit comments

Comments
 (0)