Skip to content
TypeScript

fix(shared): preserve IPv6 hosts when switching clone protocols (#30798)

Simone10581521d27d
diff --git a/packages/shared/src/cloneUrl.test.ts b/packages/shared/src/cloneUrl.test.ts
index 50ab17f9114..fc029e1b3b2 100644
--- a/packages/shared/src/cloneUrl.test.ts
+++ b/packages/shared/src/cloneUrl.test.ts
@@ -117,6 +117,18 @@ describe("cloneUrlForProtocol", () => {
expect(cloneUrlForProtocol("ssh://[email protected]/group/sub/repo.git", "https")).toBe(
"https://gitlab.com/group/sub/repo.git",
);
+ expect(cloneUrlForProtocol("git@[2001:db8::1]:org/repo.git", "https")).toBe(
+ "https://[2001:db8::1]/org/repo.git",
+ );
+ expect(cloneUrlForProtocol("[2001:db8::1]:org/repo.git", "https")).toBe(
+ "https://[2001:db8::1]/org/repo.git",
+ );
+ expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]/org/repo.git", "https")).toBe(
+ "https://[2001:db8::1]/org/repo.git",
+ );
+ expect(cloneUrlForProtocol("https://[2001:db8::1]/org/repo.git", "ssh")).toBe(
+ "git@[2001:db8::1]:org/repo.git",
+ );
});
it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
@@ -134,6 +146,17 @@ describe("cloneUrlForProtocol", () => {
expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
"https://host.example:8443/a/b.git",
);
+ expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "https")).toBeNull();
+ expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "ssh")).toBe(
+ "ssh://git@[2001:db8::1]:2222/a/b.git",
+ );
+ expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "ssh")).toBeNull();
+ expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "https")).toBe(
+ "https://[2001:db8::1]:8443/a/b.git",
+ );
+ expect(cloneUrlForProtocol("[email protected]:/srv/repo.git", "https")).toBeNull();
+ expect(cloneUrlForProtocol("git@[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
+ expect(cloneUrlForProtocol("[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
expect(cloneUrlForProtocol("/srv/git/repo.git", "https")).toBeNull();
expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
});
diff --git a/packages/shared/src/cloneUrl.ts b/packages/shared/src/cloneUrl.ts
index 597f21d1d1f..673a5991894 100644
--- a/packages/shared/src/cloneUrl.ts
+++ b/packages/shared/src/cloneUrl.ts
@@ -2,10 +2,10 @@ import { isWindowsDrivePath } from "./path.ts";
export type CloneProtocol = "ssh" | "https";
-const SCP_LIKE_URL = /^(?:[^@/\s]+@)?([^:/\s]+):(?!\/)(.+)$/;
+const SCP_LIKE_URL = /^(?:[^@/\s]+@)?(\[[^\]@/\s]+\]|[^:/\s[\]]+):(?!\/)(.+)$/;
// Userinfo may contain `@`; the last one before the first slash separates the host.
-const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;
-const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;
+const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
+const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?(\[[^\]@/\s]+\]|[^@:/\s]+)(:\d+)?\/(.+)$/;
function parseCloneUrl(
url: string,