git_hosting_providers: Support GitHub remote URLs that start with a slash (#34134)

Zachary Hamm and Peter Tripp created

Remote URLs like `git@github.com:/zed-industries/zed` are valid git
remotes, but currently Zed does not parse them correctly. The result is
invalid "permalink" generation, and presumably other bugs anywhere that
git remote urls are required for the current repository. This ensures
Zed will parse those URLs correctly.

Release Notes:

- Improved support for GitHub remote urls where the
username/organization is preceded by an extra `/` to match the behavior
of `git`, `gh` and `github.com`.

Co-authored-by: Peter Tripp <peter@zed.dev>

Change summary

crates/git_hosting_providers/src/providers/github.rs | 22 +++++++++++++
1 file changed, 21 insertions(+), 1 deletion(-)

Detailed changes

crates/git_hosting_providers/src/providers/github.rs 🔗

@@ -159,7 +159,11 @@ impl GitHostingProvider for Github {
         }
 
         let mut path_segments = url.path_segments()?;
-        let owner = path_segments.next()?;
+        let mut owner = path_segments.next()?;
+        if owner.is_empty() {
+            owner = path_segments.next()?;
+        }
+
         let repo = path_segments.next()?.trim_end_matches(".git");
 
         Some(ParsedGitRemote {
@@ -244,6 +248,22 @@ mod tests {
 
     use super::*;
 
+    #[test]
+    fn test_remote_url_with_root_slash() {
+        let remote_url = "git@github.com:/zed-industries/zed";
+        let parsed_remote = Github::public_instance()
+            .parse_remote_url(remote_url)
+            .unwrap();
+
+        assert_eq!(
+            parsed_remote,
+            ParsedGitRemote {
+                owner: "zed-industries".into(),
+                repo: "zed".into(),
+            }
+        );
+    }
+
     #[test]
     fn test_invalid_self_hosted_remote_url() {
         let remote_url = "git@github.com:zed-industries/zed.git";