This PR does not close an issue, but it is an issue and and fix in one. I hope this is ok, but please let me know if you prefer me to open an issue before. Release Notes: - Add "copy permalink" action for self-hosted GitHub enterprise instances # Issue ### Related issues: * https://github.com/zed-industries/zed/issues/26393 * https://github.com/zed-industries/zed/issues/11043 When you try to copy a permalink from a self-hosted GitHub enterprise instance, you get the following error: <img width="383" alt="permalink" src="https://github.com/user-attachments/assets/b32338a7-a2d7-48fc-86bf-ade1d32ed1f7" /> You also cannot open a PR or commit when you hover over a git blame: https://github.com/user-attachments/assets/a5491ce7-270b-412f-b9ac-027ec020b028 ### Reproduce If you do not have access to a self-hosted GitHub instance, you can change the remote url of any git repo: ``` git remote set-url origin git@github.mycorp.com:nilskch/zed.git ``` With the fix, permalinks still won't bring you to a valid website, but you can verify that they are correctly created. # Solution Currently, we only support detecting self-hosted GitLab instances, but not self-hosted GitHub instances. We detect GitLab instances by checking if "gitlab" is part of the git URL. This PR adds the same logic to detect self-hosted GitHub enterprise instances (by checking if "github" is in the URL). This solution is not ideal, since self-hosted GitHub or GitLab instances might not contain the word "github" or "gitlab". #26393 proposes adding a setting that would allow users to map specific domains to their corresponding git provider types. This mapping would help Zed correctly identify the appropriate git instance, even if "gitlab" or "github" are not part of the URL. This PR does not implement the offered solution, but I added a TODO where the fix for #26393 has to make changes.
88 lines
2.9 KiB
Rust
88 lines
2.9 KiB
Rust
mod providers;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use git::repository::GitRepository;
|
|
use git::GitHostingProviderRegistry;
|
|
use gpui::App;
|
|
use url::Url;
|
|
use util::maybe;
|
|
|
|
pub use crate::providers::*;
|
|
|
|
/// Initializes the Git hosting providers.
|
|
pub fn init(cx: &App) {
|
|
let provider_registry = GitHostingProviderRegistry::global(cx);
|
|
provider_registry.register_hosting_provider(Arc::new(Bitbucket));
|
|
provider_registry.register_hosting_provider(Arc::new(Chromium));
|
|
provider_registry.register_hosting_provider(Arc::new(Codeberg));
|
|
provider_registry.register_hosting_provider(Arc::new(Gitee));
|
|
provider_registry.register_hosting_provider(Arc::new(Github::new()));
|
|
provider_registry.register_hosting_provider(Arc::new(Gitlab::new()));
|
|
provider_registry.register_hosting_provider(Arc::new(Sourcehut));
|
|
}
|
|
|
|
/// Registers additional Git hosting providers.
|
|
///
|
|
/// These require information from the Git repository to construct, so their
|
|
/// registration is deferred until we have a Git repository initialized.
|
|
pub fn register_additional_providers(
|
|
provider_registry: Arc<GitHostingProviderRegistry>,
|
|
repository: Arc<dyn GitRepository>,
|
|
) {
|
|
let Some(origin_url) = repository.remote_url("origin") else {
|
|
return;
|
|
};
|
|
|
|
if let Ok(gitlab_self_hosted) = Gitlab::from_remote_url(&origin_url) {
|
|
provider_registry.register_hosting_provider(Arc::new(gitlab_self_hosted));
|
|
} else if let Ok(github_self_hosted) = Github::from_remote_url(&origin_url) {
|
|
provider_registry.register_hosting_provider(Arc::new(github_self_hosted));
|
|
}
|
|
}
|
|
|
|
pub fn get_host_from_git_remote_url(remote_url: &str) -> Result<String> {
|
|
maybe!({
|
|
if let Some(remote_url) = remote_url.strip_prefix("git@") {
|
|
if let Some((host, _)) = remote_url.trim_start_matches("git@").split_once(':') {
|
|
return Some(host.to_string());
|
|
}
|
|
}
|
|
|
|
Url::parse(&remote_url)
|
|
.ok()
|
|
.and_then(|remote_url| remote_url.host_str().map(|host| host.to_string()))
|
|
})
|
|
.ok_or_else(|| anyhow!("URL has no host"))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::get_host_from_git_remote_url;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[test]
|
|
fn test_get_host_from_git_remote_url() {
|
|
let tests = [
|
|
(
|
|
"https://jlannister@github.com/some-org/some-repo.git",
|
|
Some("github.com".to_string()),
|
|
),
|
|
(
|
|
"git@github.com:zed-industries/zed.git",
|
|
Some("github.com".to_string()),
|
|
),
|
|
(
|
|
"git@my.super.long.subdomain.com:zed-industries/zed.git",
|
|
Some("my.super.long.subdomain.com".to_string()),
|
|
),
|
|
];
|
|
|
|
for (remote_url, expected_host) in tests {
|
|
let host = get_host_from_git_remote_url(remote_url).ok();
|
|
assert_eq!(host, expected_host);
|
|
}
|
|
}
|
|
}
|