1mod providers;
2mod settings;
3
4use std::sync::Arc;
5
6use anyhow::Context as _;
7use anyhow::Result;
8use git::GitHostingProviderRegistry;
9use git::repository::GitRepository;
10use gpui::App;
11use url::Url;
12use util::maybe;
13
14pub use crate::providers::*;
15pub use crate::settings::*;
16
17/// Initializes the Git hosting providers.
18pub fn init(cx: &mut App) {
19 crate::settings::init(cx);
20
21 let provider_registry = GitHostingProviderRegistry::global(cx);
22 provider_registry.register_hosting_provider(Arc::new(Bitbucket::public_instance()));
23 provider_registry.register_hosting_provider(Arc::new(Chromium));
24 provider_registry.register_hosting_provider(Arc::new(Forgejo::public_instance()));
25 provider_registry.register_hosting_provider(Arc::new(Gitea::public_instance()));
26 provider_registry.register_hosting_provider(Arc::new(Gitee));
27 provider_registry.register_hosting_provider(Arc::new(Github::public_instance()));
28 provider_registry.register_hosting_provider(Arc::new(Gitlab::public_instance()));
29 provider_registry.register_hosting_provider(Arc::new(Sourcehut));
30}
31
32/// Registers additional Git hosting providers.
33///
34/// These require information from the Git repository to construct, so their
35/// registration is deferred until we have a Git repository initialized.
36pub fn register_additional_providers(
37 provider_registry: Arc<GitHostingProviderRegistry>,
38 repository: Arc<dyn GitRepository>,
39) {
40 let Some(origin_url) = repository.remote_url("origin") else {
41 return;
42 };
43
44 if let Ok(gitlab_self_hosted) = Gitlab::from_remote_url(&origin_url) {
45 provider_registry.register_hosting_provider(Arc::new(gitlab_self_hosted));
46 } else if let Ok(github_self_hosted) = Github::from_remote_url(&origin_url) {
47 provider_registry.register_hosting_provider(Arc::new(github_self_hosted));
48 } else if let Ok(forgejo_self_hosted) = Forgejo::from_remote_url(&origin_url) {
49 provider_registry.register_hosting_provider(Arc::new(forgejo_self_hosted));
50 } else if let Ok(gitea_self_hosted) = Gitea::from_remote_url(&origin_url) {
51 provider_registry.register_hosting_provider(Arc::new(gitea_self_hosted));
52 }
53}
54
55pub fn get_host_from_git_remote_url(remote_url: &str) -> Result<String> {
56 maybe!({
57 if let Some(remote_url) = remote_url.strip_prefix("git@")
58 && let Some((host, _)) = remote_url.trim_start_matches("git@").split_once(':')
59 {
60 return Some(host.to_string());
61 }
62
63 Url::parse(remote_url)
64 .ok()
65 .and_then(|remote_url| remote_url.host_str().map(|host| host.to_string()))
66 })
67 .context("URL has no host")
68}
69
70#[cfg(test)]
71mod tests {
72 use super::get_host_from_git_remote_url;
73 use pretty_assertions::assert_eq;
74
75 #[test]
76 fn test_get_host_from_git_remote_url() {
77 let tests = [
78 (
79 "https://jlannister@github.com/some-org/some-repo.git",
80 Some("github.com".to_string()),
81 ),
82 (
83 "git@github.com:zed-industries/zed.git",
84 Some("github.com".to_string()),
85 ),
86 (
87 "git@my.super.long.subdomain.com:zed-industries/zed.git",
88 Some("my.super.long.subdomain.com".to_string()),
89 ),
90 ];
91
92 for (remote_url, expected_host) in tests {
93 let host = get_host_from_git_remote_url(remote_url).ok();
94 assert_eq!(host, expected_host);
95 }
96 }
97}