Detect wider variety of usernames for SSH-based remotes (#21508)

Nick Breaton created

Closes #21507

Release Notes:

- Fixed detection of git remotes when using SSH and username is not
"git".

Change summary

Cargo.lock               |  1 +
crates/git/Cargo.toml    |  1 +
crates/git/src/remote.rs | 16 ++++++++++++++--
3 files changed, 16 insertions(+), 2 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -5131,6 +5131,7 @@ dependencies = [
  "log",
  "parking_lot",
  "pretty_assertions",
+ "regex",
  "rope",
  "serde",
  "serde_json",

crates/git/Cargo.toml 🔗

@@ -21,6 +21,7 @@ gpui.workspace = true
 http_client.workspace = true
 log.workspace = true
 parking_lot.workspace = true
+regex.workspace = true
 rope.workspace = true
 serde.workspace = true
 smol.workspace = true

crates/git/src/remote.rs 🔗

@@ -1,17 +1,23 @@
+use std::sync::LazyLock;
+
 use derive_more::Deref;
+use regex::Regex;
 use url::Url;
 
 /// The URL to a Git remote.
 #[derive(Debug, PartialEq, Eq, Clone, Deref)]
 pub struct RemoteUrl(Url);
 
+static USERNAME_REGEX: LazyLock<Regex> =
+    LazyLock::new(|| Regex::new(r"^[0-9a-zA-Z\-_]+@").expect("Failed to create USERNAME_REGEX"));
+
 impl std::str::FromStr for RemoteUrl {
     type Err = url::ParseError;
 
     fn from_str(input: &str) -> Result<Self, Self::Err> {
-        if input.starts_with("git@") {
+        if USERNAME_REGEX.is_match(input) {
             // Rewrite remote URLs like `git@github.com:user/repo.git` to `ssh://git@github.com/user/repo.git`
-            let ssh_url = input.replacen(':', "/", 1).replace("git@", "ssh://git@");
+            let ssh_url = format!("ssh://{}", input.replacen(':', "/", 1));
             Ok(RemoteUrl(Url::parse(&ssh_url)?))
         } else {
             Ok(RemoteUrl(Url::parse(input)?))
@@ -40,6 +46,12 @@ mod tests {
                 "github.com",
                 "/octocat/zed.git",
             ),
+            (
+                "org-000000@github.com:octocat/zed.git",
+                "ssh",
+                "github.com",
+                "/octocat/zed.git",
+            ),
             (
                 "ssh://git@github.com/octocat/zed.git",
                 "ssh",