Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(git): stop reading a remote URL's port as part of the repository path (#16723)

MarMar Labs4a8cabc5fdc8
diff --git a/__tests__/utils/parse-git-remote-url.test.ts b/__tests__/utils/parse-git-remote-url.test.ts
index df0a352e3b..8ab265cf28 100644
--- a/__tests__/utils/parse-git-remote-url.test.ts
+++ b/__tests__/utils/parse-git-remote-url.test.ts
@@ -50,6 +50,50 @@ describe("parseGitRemoteUrl", () => {
expect(result?.provider).toBe("gitlab");
});
+ it("parses ssh:// URLs that carry an explicit port", () => {
+ const result = parseGitRemoteUrl(
+ "ssh://[email protected]:2222/owner/repo.git",
+ );
+ expect(result?.host).toBe("git.example.com");
+ expect(result?.repository).toBe("owner/repo");
+ });
+
+ // The shorthand match would read `github.com:owner` as host and port; only
+ // a colon segment that is a port belongs to the host.
+ it("parses ssh:// URLs whose colon segment is a path, not a port", () => {
+ const result = parseGitRemoteUrl("ssh://[email protected]:owner/repo.git");
+ expect(result).toEqual({
+ url: "ssh://[email protected]:owner/repo.git",
+ host: "github.com",
+ repository: "owner/repo",
+ provider: "github",
+ });
+ });
+
+ it("parses http URLs that carry credentials and a port", () => {
+ const result = parseGitRemoteUrl(
+ "http://[email protected]:8080/owner/repo.git",
+ );
+ expect(result).toEqual({
+ url: "http://[email protected]:8080/owner/repo.git",
+ host: "git.example.com",
+ repository: "owner/repo",
+ provider: null,
+ });
+ });
+
+ it("parses https URLs that carry a user:password pair and a port", () => {
+ const result = parseGitRemoteUrl(
+ "https://user:[email protected]:8443/owner/repo.git",
+ );
+ expect(result).toEqual({
+ url: "https://user:[email protected]:8443/owner/repo.git",
+ host: "git.example.com",
+ repository: "owner/repo",
+ provider: null,
+ });
+ });
+
it("parses Bitbucket Cloud URLs", () => {
const result = parseGitRemoteUrl(
"https://bitbucket.org/owner/repo.git",
diff --git a/src/components/features/automations/git-sync/git-sync-overview-section.test.tsx b/src/components/features/automations/git-sync/git-sync-overview-section.test.tsx
index 8418f5c97d..7c5c59653b 100644
--- a/src/components/features/automations/git-sync/git-sync-overview-section.test.tsx
+++ b/src/components/features/automations/git-sync/git-sync-overview-section.test.tsx
@@ -60,6 +60,17 @@ describe("GitSyncOverviewSection", () => {
);
});
+ // The ssh port says nothing about where the web UI listens, so the rebuilt
+ // link drops it -- unlike the https case above, which keeps its own port.
+ it("drops the ssh port when rebuilding the browse link over https", () => {
+ renderWith("ssh://[email protected]:2222/org/repo.git");
+
+ expect(screen.getByTestId("git-sync-repo-link")).toHaveAttribute(
+ "href",
+ "https://git.example.com/org/repo",
+ );
+ });
+
// A remote configured with credentials in it must not put them in an href,
// where they would ride along in the outgoing navigation.
it("strips credentials from the link", () => {
diff --git a/src/utils/parse-git-remote-url.ts b/src/utils/parse-git-remote-url.ts
index c5b5263599..84fa41a856 100644
--- a/src/utils/parse-git-remote-url.ts
+++ b/src/utils/parse-git-remote-url.ts
@@ -70,6 +70,20 @@ export function parseGitRemoteUrl(
const url = remoteUrl?.trim();
if (!url) return null;
+ // ssh://, https://, http://, git://. Tried before the shorthand match
+ // below, whose `[^@\s]+` would otherwise swallow `ssh://git` and read the
+ // port in `ssh://git@host:2222/owner/repo` as the first path segment.
+ try {
+ const parsed = new URL(url);
+ // A bare Windows path (`C:/src/repo`) parses as a `c:` scheme with an
+ // empty host, so it is left to the shorthand match below.
+ if (parsed.hostname) {
+ return buildParsedGitRemoteUrl(url, parsed.hostname, parsed.pathname);
+ }
+ } catch {
+ // Not a URL the WHATWG parser accepts, so try the shorthand form.
+ }
+
// git@host:owner/repo(.git)
const scpMatch = url.match(/^[^@\s]+@([^:\s]+):(.+)$/);
if (scpMatch) {
@@ -77,15 +91,5 @@ export function parseGitRemoteUrl(
return buildParsedGitRemoteUrl(url, host, scpMatch[2]);
}
- // ssh://, https://, http://, git://
- try {
- const parsed = new URL(url);
- return buildParsedGitRemoteUrl(
- url,
- parsed.hostname || null,
- parsed.pathname,
- );
- } catch {
- return null;
- }
+ return null;
}