Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: support SSH URLs with ports in remote matching (#1151)
Maximilian Roose7c520765c97
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rsindex c1aeac9aa..1aa61dcae 100644--- a/src/git/repository/remotes.rs+++ b/src/git/repository/remotes.rs@@ -171,17 +171,8 @@ impl Repository {if let Some(parsed) = GitRemoteUrl::parse(url.trim()) {return Ok(parsed.project_identifier());}- // Fallback for URLs that don't fit host/owner/repo model (e.g., with ports)+ // Fallback for URLs that don't fit host/owner/repo modellet url = url.strip_suffix(".git").unwrap_or(url.as_str());- // Handle ssh:// format with port: ssh://git@host:port/path -> host/port/path- if let Some(ssh_part) = url.strip_prefix("ssh://") {- let ssh_part = ssh_part.strip_prefix("git@").unwrap_or(ssh_part);- if let Some(colon_pos) = ssh_part.find(':') {- let (host, rest) = ssh_part.split_at(colon_pos);- return Ok(format!("{}{}", host, rest.replacen(':', "/", 1)));- }- return Ok(ssh_part.to_string());- }return Ok(url.to_string());}diff --git a/src/git/url.rs b/src/git/url.rsindex d7da3d70f..9f9b232e0 100644--- a/src/git/url.rs+++ b/src/git/url.rs@@ -12,7 +12,9 @@/// - `git://<host>/<namespace>/<repo>.git`/// - `git@<host>:<namespace>/<repo>.git`/// - `ssh://git@<host>/<namespace>/<repo>.git`+/// - `ssh://git@<host>:<port>/<namespace>/<repo>.git`/// - `ssh://<host>/<namespace>/<repo>.git`+/// - `ssh://<host>:<port>/<namespace>/<repo>.git`////// # Nested groups (GitLab subgroups)///@@ -89,15 +91,11 @@ impl GitRemoteUrl {(host, namespace, repo)} else if let Some(rest) = url.strip_prefix("ssh://") {// ssh://[email protected]/owner/repo.git or ssh://github.com/owner/repo.git- // Note: URLs with ports (ssh://host:2222/...) are not supported here- // as they don't fit the host/owner/repo model. They should be handled- // as raw strings (project_identifier fallback).+ // ssh://git@host:port/owner/repo.git (port is stripped — irrelevant to project identity)let without_user = rest.split('@').next_back()?;- let (host, path) = without_user.split_once('/')?;- // If host contains a colon (port), this URL doesn't fit our model- if host.contains(':') {- return None;- }+ let (host_with_port, path) = without_user.split_once('/')?;+ // Strip port from host (e.g., "gitlab.internal:2222" → "gitlab.internal")+ let host = host_with_port.split(':').next().unwrap_or(host_with_port);let (namespace, repo) = split_namespace_repo(path)?;(host, namespace, repo)} else if let Some(rest) = url.strip_prefix("git@") {@@ -250,6 +248,42 @@ mod tests {assert_eq!(url.owner(), "owner");}+ #[test]+ fn test_ssh_urls_with_ports() {+ // Standard SSH with port+ let url = GitRemoteUrl::parse("ssh://git@host:22/owner/repo.git").unwrap();+ assert_eq!(url.host(), "host");+ assert_eq!(url.owner(), "owner");+ assert_eq!(url.repo(), "repo");+ assert_eq!(url.project_identifier(), "host/owner/repo");++ // Without user+ let url = GitRemoteUrl::parse("ssh://host:2222/owner/repo.git").unwrap();+ assert_eq!(url.host(), "host");+ assert_eq!(url.owner(), "owner");+ assert_eq!(url.repo(), "repo");++ // Nested groups with port+ let url =+ GitRemoteUrl::parse("ssh://[email protected]:2222/group/subgroup/repo.git").unwrap();+ assert_eq!(url.host(), "gitlab.internal");+ assert_eq!(url.owner(), "group/subgroup");+ assert_eq!(url.repo(), "repo");+ assert_eq!(+ url.project_identifier(),+ "gitlab.internal/group/subgroup/repo"+ );++ // Port is stripped — same project identity as without port+ let with_port = GitRemoteUrl::parse("ssh://git@host:2222/owner/repo.git").unwrap();+ let without_port = GitRemoteUrl::parse("ssh://git@host/owner/repo.git").unwrap();+ assert_eq!(+ with_port.project_identifier(),+ without_port.project_identifier(),+ "Port is a transport detail — same project identity"+ );+ }+#[test]fn test_git_protocol_urls() {let url = GitRemoteUrl::parse("git://github.com/owner/repo.git").unwrap();diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rsindex c538882ff..11a5a06da 100644--- a/tests/integration_tests/repository.rs+++ b/tests/integration_tests/repository.rs@@ -282,8 +282,8 @@ fn test_project_identifier_ssh_protocol_with_port() {let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();let id = repository.project_identifier().unwrap();- // The port colon gets converted to /- assert_eq!(id, "gitlab.example.com/2222/team/project");+ // Port is stripped — irrelevant to project identity+ assert_eq!(id, "gitlab.example.com/team/project");}#[test]