Check if additional git provider is not the original git provider (#26533)

Nils Koch and Marshall Bowers created

Release Notes:

- N/A

Yesterday I worked on https://github.com/zed-industries/zed/pull/26482
and noticed afterwards that we have duplicated hosting providers if the
git remote host is "gitlab.com" and after the PR also for "github.com".
This is not a big problem, since the original providers are registered
first and therefore we first find a match with the original providers,
but I think we should address this nevertheless.

We initialize every hosting provider with the defaults here:

https://github.com/zed-industries/zed/blob/b008b2863ee015a9dc6ecdcd6dedbc708983f8b3/crates/git_hosting_providers/src/git_hosting_providers.rs#L15-L24

After that, we also register additional hosting providers:

https://github.com/zed-industries/zed/blob/b008b2863ee015a9dc6ecdcd6dedbc708983f8b3/crates/git_hosting_providers/src/git_hosting_providers.rs#L30-L43

If we do not check if the additional provider is not the original
provider, we will register the same provider twice.

---------

Co-authored-by: Marshall Bowers <git@maxdeviant.com>

Change summary

crates/git_hosting_providers/src/providers/github.rs | 10 ++++++++++
crates/git_hosting_providers/src/providers/gitlab.rs | 10 ++++++++++
2 files changed, 20 insertions(+)

Detailed changes

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

@@ -60,6 +60,9 @@ impl Github {
 
     pub fn from_remote_url(remote_url: &str) -> Result<Self> {
         let host = get_host_from_git_remote_url(remote_url)?;
+        if host == "github.com" {
+            bail!("the GitHub instance is not self-hosted");
+        }
 
         // TODO: detecting self hosted instances by checking whether "github" is in the url or not
         // is not very reliable. See https://github.com/zed-industries/zed/issues/26393 for more
@@ -236,6 +239,13 @@ mod tests {
 
     use super::*;
 
+    #[test]
+    fn test_invalid_self_hosted_remote_url() {
+        let remote_url = "git@github.com:zed-industries/zed.git";
+        let github = Github::from_remote_url(remote_url);
+        assert!(github.is_err());
+    }
+
     #[test]
     fn test_from_remote_url_ssh() {
         let remote_url = "git@github.my-enterprise.com:zed-industries/zed.git";

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

@@ -26,6 +26,9 @@ impl Gitlab {
 
     pub fn from_remote_url(remote_url: &str) -> Result<Self> {
         let host = get_host_from_git_remote_url(remote_url)?;
+        if host == "gitlab.com" {
+            bail!("the GitLab instance is not self-hosted");
+        }
 
         // TODO: detecting self hosted instances by checking whether "gitlab" is in the url or not
         // is not very reliable. See https://github.com/zed-industries/zed/issues/26393 for more
@@ -123,6 +126,13 @@ mod tests {
 
     use super::*;
 
+    #[test]
+    fn test_invalid_self_hosted_remote_url() {
+        let remote_url = "https://gitlab.com/zed-industries/zed.git";
+        let github = Gitlab::from_remote_url(remote_url);
+        assert!(github.is_err());
+    }
+
     #[test]
     fn test_parse_remote_url_given_ssh_url() {
         let parsed_remote = Gitlab::new()