1use std::sync::Arc;
2
3use git::GitHostingProviderRegistry;
4use gpui::App;
5use settings::{GitHostingProviderConfig, GitHostingProviderKind, Settings, SettingsStore};
6use url::Url;
7use util::ResultExt as _;
8
9use crate::{Bitbucket, Github, Gitlab};
10
11pub(crate) fn init(cx: &mut App) {
12 GitHostingProviderSettings::register(cx);
13
14 init_git_hosting_provider_settings(cx);
15}
16
17fn init_git_hosting_provider_settings(cx: &mut App) {
18 update_git_hosting_providers_from_settings(cx);
19
20 cx.observe_global::<SettingsStore>(update_git_hosting_providers_from_settings)
21 .detach();
22}
23
24fn update_git_hosting_providers_from_settings(cx: &mut App) {
25 let settings_store = cx.global::<SettingsStore>();
26 let settings = GitHostingProviderSettings::get_global(cx);
27 let provider_registry = GitHostingProviderRegistry::global(cx);
28
29 let local_values: Vec<GitHostingProviderConfig> = settings_store
30 .get_all_locals::<GitHostingProviderSettings>()
31 .into_iter()
32 .flat_map(|(_, _, providers)| providers.git_hosting_providers.clone())
33 .collect();
34
35 let iter = settings
36 .git_hosting_providers
37 .clone()
38 .into_iter()
39 .chain(local_values)
40 .filter_map(|provider| {
41 let url = Url::parse(&provider.base_url).log_err()?;
42
43 Some(match provider.provider {
44 GitHostingProviderKind::Bitbucket => {
45 Arc::new(Bitbucket::new(&provider.name, url)) as _
46 }
47 GitHostingProviderKind::Github => Arc::new(Github::new(&provider.name, url)) as _,
48 GitHostingProviderKind::Gitlab => Arc::new(Gitlab::new(&provider.name, url)) as _,
49 })
50 });
51
52 provider_registry.set_setting_providers(iter);
53}
54
55#[derive(Debug, Clone)]
56pub struct GitHostingProviderSettings {
57 pub git_hosting_providers: Vec<GitHostingProviderConfig>,
58}
59
60impl Settings for GitHostingProviderSettings {
61 fn from_defaults(content: &settings::SettingsContent, _cx: &mut App) -> Self {
62 Self {
63 git_hosting_providers: content.project.git_hosting_providers.clone().unwrap(),
64 }
65 }
66
67 fn refine(&mut self, content: &settings::SettingsContent, _: &mut App) {
68 if let Some(more) = &content.project.git_hosting_providers {
69 self.git_hosting_providers.extend_from_slice(&more.clone());
70 }
71 }
72
73 fn import_from_vscode(_: &settings::VsCodeSettings, _: &mut settings::SettingsContent) {}
74}