Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_hosting_providers: Add support for self-hosted Gerrit repositories (#53832)
Filip Czaplickid9afb21688e0
diff --git a/crates/git_hosting_providers/src/providers.rs b/crates/git_hosting_providers/src/providers.rsindex 4c9929afdf2..7c310bbebd4 100644--- a/crates/git_hosting_providers/src/providers.rs+++ b/crates/git_hosting_providers/src/providers.rs@@ -2,6 +2,7 @@ mod azure;mod bitbucket;mod chromium;mod forgejo;+mod gerrit;mod gitea;mod gitee;mod github;@@ -13,6 +14,7 @@ pub use azure::*;pub use bitbucket::*;pub use chromium::*;pub use forgejo::*;+pub use gerrit::*;pub use gitea::*;pub use gitee::*;pub use github::*;diff --git a/crates/git_hosting_providers/src/providers/gerrit.rs b/crates/git_hosting_providers/src/providers/gerrit.rsnew file mode 100644index 00000000000..7eca8b0d98c--- /dev/null+++ b/crates/git_hosting_providers/src/providers/gerrit.rs@@ -0,0 +1,223 @@+use std::str::FromStr;+use std::sync::Arc;++use url::Url;++use git::{+ BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,+ RemoteUrl,+};++pub struct Gerrit {+ name: String,+ base_url: Url,+}++impl Gerrit {+ pub fn new(name: impl Into<String>, base_url: Url) -> Self {+ Self {+ name: name.into(),+ base_url,+ }+ }+}++#[async_trait::async_trait]+impl GitHostingProvider for Gerrit {+ fn name(&self) -> String {+ self.name.clone()+ }++ fn base_url(&self) -> Url {+ self.base_url.clone()+ }++ fn supports_avatars(&self) -> bool {+ false+ }++ fn format_line_number(&self, line: u32) -> String {+ format!("{line}")+ }++ fn format_line_numbers(&self, start_line: u32, _end_line: u32) -> String {+ format!("{start_line}")+ }++ fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {+ let url = RemoteUrl::from_str(url).ok()?;++ let host = url.host_str()?;+ if host != self.base_url.host_str()? {+ return None;+ }++ let path_segments = url.path_segments()?.collect::<Vec<_>>();+ let joined_path = path_segments.join("/");+ let repo = joined_path+ .trim_start_matches("a/")+ .trim_end_matches(".git");++ Some(ParsedGitRemote {+ owner: Arc::from(""),+ repo: repo.into(),+ })+ }++ fn build_commit_permalink(+ &self,+ remote: &ParsedGitRemote,+ params: BuildCommitPermalinkParams,+ ) -> Url {+ let BuildCommitPermalinkParams { sha } = params;+ let ParsedGitRemote { owner: _, repo } = remote;++ self.base_url().join(&format!("{repo}/+/{sha}")).unwrap()+ }++ fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {+ let ParsedGitRemote { owner: _, repo } = remote;+ let BuildPermalinkParams {+ sha,+ path,+ selection,+ } = params;++ let mut permalink = self+ .base_url()+ .join(&format!("{repo}/+/{sha}/{path}"))+ .unwrap();+ permalink.set_fragment(+ selection+ .map(|selection| self.line_fragment(&selection))+ .as_deref(),+ );+ permalink+ }+}++#[cfg(test)]+mod tests {+ use std::sync::Arc;++ use git::repository::repo_path;+ use pretty_assertions::assert_eq;++ use super::*;++ fn gerrit_instance() -> Gerrit {+ Gerrit::new("Gerrit", Url::parse("https://gerrit.example.com").unwrap())+ }++ #[test]+ fn test_parse_remote_url_given_https_url() {+ let parsed_remote = gerrit_instance()+ .parse_remote_url("https://gerrit.example.com/my-project")+ .unwrap();++ assert_eq!(+ parsed_remote,+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ }+ );+ }++ #[test]+ fn test_parse_remote_url_strips_a_prefix() {+ let parsed_remote = gerrit_instance()+ .parse_remote_url("https://gerrit.example.com/a/my-project.git")+ .unwrap();++ assert_eq!(+ parsed_remote,+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ }+ );+ }++ #[test]+ fn test_parse_remote_url_given_ssh_url() {+ let parsed_remote = gerrit_instance()+ .parse_remote_url("[email protected]:my-project.git")+ .unwrap();++ assert_eq!(+ parsed_remote,+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ }+ );+ }++ #[test]+ fn test_build_commit_permalink() {+ let permalink = gerrit_instance().build_commit_permalink(+ &ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ },+ BuildCommitPermalinkParams {+ sha: "abc123def456",+ },+ );++ assert_eq!(+ permalink.to_string(),+ "https://gerrit.example.com/my-project/+/abc123def456"+ );+ }++ #[test]+ fn test_build_permalink() {+ let permalink = gerrit_instance().build_permalink(+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ },+ BuildPermalinkParams::new("abc123def456", &repo_path("src/main.rs"), None),+ );++ assert_eq!(+ permalink.to_string(),+ "https://gerrit.example.com/my-project/+/abc123def456/src/main.rs"+ );+ }++ #[test]+ fn test_build_permalink_with_single_line_selection() {+ let permalink = gerrit_instance().build_permalink(+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ },+ BuildPermalinkParams::new("abc123def456", &repo_path("src/main.rs"), Some(41..41)),+ );++ assert_eq!(+ permalink.to_string(),+ "https://gerrit.example.com/my-project/+/abc123def456/src/main.rs#42"+ );+ }++ #[test]+ fn test_build_permalink_with_multi_line_selection() {+ let permalink = gerrit_instance().build_permalink(+ ParsedGitRemote {+ owner: Arc::from(""),+ repo: "my-project".into(),+ },+ BuildPermalinkParams::new("abc123def456", &repo_path("src/main.rs"), Some(41..55)),+ );++ // Gitiles only supports single line anchors+ assert_eq!(+ permalink.to_string(),+ "https://gerrit.example.com/my-project/+/abc123def456/src/main.rs#42"+ );+ }+}diff --git a/crates/git_hosting_providers/src/settings.rs b/crates/git_hosting_providers/src/settings.rsindex 3431f7ebbdd..5b5218b436b 100644--- a/crates/git_hosting_providers/src/settings.rs+++ b/crates/git_hosting_providers/src/settings.rs@@ -8,7 +8,7 @@ use settings::{use url::Url;use util::ResultExt as _;-use crate::{Bitbucket, Forgejo, Gitea, Github, Gitlab, SourceHut, Tangled};+use crate::{Bitbucket, Forgejo, Gerrit, Gitea, Github, Gitlab, SourceHut, Tangled};pub(crate) fn init(cx: &mut App) {init_git_hosting_provider_settings(cx);@@ -48,6 +48,7 @@ fn update_git_hosting_providers_from_settings(cx: &mut App) {GitHostingProviderKind::Gitlab => Arc::new(Gitlab::new(&provider.name, url)) as _,GitHostingProviderKind::Gitea => Arc::new(Gitea::new(&provider.name, url)) as _,GitHostingProviderKind::Forgejo => Arc::new(Forgejo::new(&provider.name, url)) as _,+ GitHostingProviderKind::Gerrit => Arc::new(Gerrit::new(&provider.name, url)) as _,GitHostingProviderKind::SourceHut => {Arc::new(SourceHut::new(&provider.name, url)) as _}diff --git a/crates/settings_content/src/project.rs b/crates/settings_content/src/project.rsindex 49bd242d5fe..fab846d1f02 100644--- a/crates/settings_content/src/project.rs+++ b/crates/settings_content/src/project.rs@@ -1011,7 +1011,7 @@ pub enum DiagnosticSeverityContent {pub struct GitHostingProviderConfig {/// The type of the provider.///- /// Must be one of `github`, `gitlab`, `bitbucket`, `gitea`, `forgejo`, `sourcehut`, or `tangled`.+ /// Must be one of `github`, `gitlab`, `bitbucket`, `gitea`, `forgejo`, `gerrit`, `sourcehut`, or `tangled`.pub provider: GitHostingProviderKind,/// The base URL for the provider (e.g., "https://code.corp.big.com").@@ -1029,6 +1029,7 @@ pub enum GitHostingProviderKind {Bitbucket,Gitea,Forgejo,+ Gerrit,SourceHut,Tangled,}