@devries-cameron20 / t3code Mirror · read-only / commits / cf82fb8584
Fork 0

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 21, 18:30 UTC

fix(shared): preserve repository paths when switching clone protocols (#35322)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Simon Maggini committed Nov 12, 2026 at 21:00 UTC cf82fb858458be25daeccf811f213d6ed73979f1
2 files changed +40 -9
packages/shared/src/cloneUrl.test.ts
+12
@@ -129,6 +129,17 @@ describe("cloneUrlForProtocol", () => {
129 expect(cloneUrlForProtocol("https://[2001:db8::1]/org/repo.git", "ssh")).toBe(
130 "git@[2001:db8::1]:org/repo.git",
131 );
132 + for (const [literal, encoded] of [
133 + ["my repo.git", "my%20repo.git"],
134 + ["repo%20name.git", "repo%2520name.git"],
135 + ["repo#1?.git", "repo%231%3F.git"],
136 + ]) {
137 + const scp = `git@host.example:team/${literal}`;
138 + const https = `https://host.example/team/${encoded}`;
139 + expect(cloneUrlForProtocol(https, "ssh")).toBe(scp);
140 + expect(cloneUrlForProtocol(scp, "https")).toBe(https);
141 + expect(cloneUrlForProtocol(`ssh://git@host.example/team/${encoded}`, "https")).toBe(https);
142 + }
143 });
144
145 it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
@@ -141,6 +152,7 @@ describe("cloneUrlForProtocol", () => {
152 });
153
154 it("refuses rewrites that would guess wrong", () => {
155 + expect(cloneUrlForProtocol("https://host.example/team/repo%ZZ.git", "ssh")).toBeNull();
156 expect(cloneUrlForProtocol("ssh://git@host.example:2222/a/b.git", "https")).toBeNull();
157 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "ssh")).toBeNull();
158 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
packages/shared/src/cloneUrl.ts
+28 -9
@@ -7,16 +7,28 @@ const SCP_LIKE_URL = /^(?:[^@/\s]+@)?(\[[^\]@/\s]+\]|[^:/\s[\]]+):(?!\/)(.+)$/;
7 const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
8 const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
9
10 -function parseCloneUrl(
11 - url: string,
12 -): { protocol: CloneProtocol; host: string; path: string; customPort: boolean } | null {
10 +function parseCloneUrl(url: string): {
11 + protocol: CloneProtocol;
12 + host: string;
13 + path: string;
14 + customPort: boolean;
15 + scp: boolean;
16 +} | null {
17 const trimmed = url.trim();
18 const ssh = SSH_URL.exec(trimmed);
15 - if (ssh) return { protocol: "ssh", host: ssh[1]!, path: ssh[3]!, customPort: !!ssh[2] };
19 + if (ssh)
20 + return { protocol: "ssh", host: ssh[1]!, path: ssh[3]!, customPort: !!ssh[2], scp: false };
21 const https = HTTPS_URL.exec(trimmed);
17 - if (https) return { protocol: "https", host: https[1]!, path: https[3]!, customPort: !!https[2] };
22 + if (https)
23 + return {
24 + protocol: "https",
25 + host: https[1]!,
26 + path: https[3]!,
27 + customPort: !!https[2],
28 + scp: false,
29 + };
30 const scp = SCP_LIKE_URL.exec(trimmed);
19 - if (scp) return { protocol: "ssh", host: scp[1]!, path: scp[2]!, customPort: false };
31 + if (scp) return { protocol: "ssh", host: scp[1]!, path: scp[2]!, customPort: false, scp: true };
32 return null;
33 }
34
@@ -44,9 +56,16 @@ export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): strin
56 const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path);
57 return azure ? `git@ssh.dev.azure.com:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null;
58 }
47 - return protocol === "ssh"
48 - ? `git@${parsed.host}:${parsed.path}`
49 - : `https://${parsed.host}/${parsed.path}`;
59 + // SCP paths are literal; URI paths use percent escapes.
60 + try {
61 + if (protocol === "ssh") return `git@${parsed.host}:${decodeURIComponent(parsed.path)}`;
62 + const path = parsed.scp
63 + ? parsed.path.split("/").map(encodeURIComponent).join("/")
64 + : parsed.path;
65 + return `https://${parsed.host}/${path}`;
66 + } catch {
67 + return null;
68 + }
69 }
70
71 const UNSAFE_SEGMENT = /^\.{1,2}$|[\\:*?"<>|\p{Cc}]|[ .]$/u;