diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-10 20:01:21 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-10 20:01:21 +0900 |
| commit | 24bb31c109332ae982b7091ffcd5183442ce6f6f (patch) | |
| tree | ce590a34053bd48506310c0a0c26395605973d45 /crates/mozart-core | |
| parent | dd13f29a3535bf15bb2494da4c67b5e2c61bbda5 (diff) | |
| download | php-mozart-24bb31c109332ae982b7091ffcd5183442ce6f6f.tar.gz php-mozart-24bb31c109332ae982b7091ffcd5183442ce6f6f.tar.zst php-mozart-24bb31c109332ae982b7091ffcd5183442ce6f6f.zip | |
refactor(vcs): move VCS drivers under repository module
Mirror Composer's structure where VCS drivers live under
Repository/Vcs/. Rename VcsDriver trait to VcsDriverInterface and
BitbucketDriver to GitBitbucketDriver to match Composer naming, and
add stubs for FossilDriver, PerforceDriver, and the VcsDriver base.
Diffstat (limited to 'crates/mozart-core')
17 files changed, 138 insertions, 121 deletions
diff --git a/crates/mozart-core/src/repository.rs b/crates/mozart-core/src/repository.rs index ba96729..ce6de85 100644 --- a/crates/mozart-core/src/repository.rs +++ b/crates/mozart-core/src/repository.rs @@ -14,6 +14,7 @@ pub mod path_repository; pub mod repository; pub mod repository_filter; pub mod resolver; +pub mod vcs; pub mod vcs_bridge; pub mod version; pub mod version_selector; diff --git a/crates/mozart-core/src/vcs/driver/mod.rs b/crates/mozart-core/src/repository/vcs.rs index cfaf11e..88ed702 100644 --- a/crates/mozart-core/src/vcs/driver/mod.rs +++ b/crates/mozart-core/src/repository/vcs.rs @@ -1,10 +1,26 @@ -pub mod bitbucket; -pub mod forgejo; -pub mod git; -pub mod github; -pub mod gitlab; -pub mod hg; -pub mod svn; +mod forgejo_driver; +mod fossil_driver; +mod git_bitbucket_driver; +mod git_driver; +mod github_driver; +mod gitlab_driver; +mod hg_driver; +mod perforce_driver; +mod svn_driver; +mod vcs_driver; +mod vcs_driver_interface; + +pub use forgejo_driver::*; +pub use fossil_driver::*; +pub use git_bitbucket_driver::*; +pub use git_driver::*; +pub use github_driver::*; +pub use gitlab_driver::*; +pub use hg_driver::*; +pub use perforce_driver::*; +pub use svn_driver::*; +pub use vcs_driver::*; +pub use vcs_driver_interface::*; use std::collections::BTreeMap; use std::path::PathBuf; @@ -97,56 +113,17 @@ pub enum DriverType { Hg, } -/// The VCS driver interface. -/// -/// Corresponds to Composer's `VcsDriverInterface`. -trait VcsDriver { - /// Initialize the driver (e.g., clone mirror, fetch API metadata). - async fn initialize(&mut self) -> Result<()>; - - /// The root identifier (default branch/trunk). - fn root_identifier(&self) -> &str; - - /// All branches as `name -> commit_hash`. - async fn branches(&mut self) -> Result<&BTreeMap<String, String>>; - - /// All tags as `name -> commit_hash`. - async fn tags(&mut self) -> Result<&BTreeMap<String, String>>; - - /// Get composer.json content parsed as JSON for a given identifier. - async fn composer_information(&mut self, identifier: &str) - -> Result<Option<serde_json::Value>>; - - /// Get raw file content at a given path and identifier. - async fn file_content(&self, file: &str, identifier: &str) -> Result<Option<String>>; - - /// Get the change date for a given identifier (ISO 8601). - async fn change_date(&self, identifier: &str) -> Result<Option<String>>; - - /// Get the dist reference for a given identifier. - async fn dist(&self, identifier: &str) -> Result<Option<DistReference>>; - - /// Get the source reference for a given identifier. - fn source(&self, identifier: &str) -> SourceReference; - - /// The canonical URL of this repository. - fn url(&self) -> &str; - - /// Clean up resources (temp dirs, etc.). - async fn cleanup(&mut self) -> Result<()>; -} - /// Enum-dispatched VCS driver. /// /// Wraps all concrete driver types to allow static dispatch with async trait methods. pub enum AnyVcsDriver { - GitHub(github::GitHubDriver), - GitLab(gitlab::GitLabDriver), - Bitbucket(bitbucket::BitbucketDriver), - Forgejo(forgejo::ForgejoDriver), - Git(git::GitDriver), - Svn(svn::SvnDriver), - Hg(hg::HgDriver), + GitHub(GitHubDriver), + GitLab(GitLabDriver), + Bitbucket(GitBitbucketDriver), + Forgejo(ForgejoDriver), + Git(GitDriver), + Svn(SvnDriver), + Hg(HgDriver), } macro_rules! dispatch { @@ -251,37 +228,37 @@ pub fn detect_driver( let url_lower = url.to_lowercase(); // GitHub - if github::GitHubDriver::supports(url) { + if GitHubDriver::supports(url) { return Some(DriverType::GitHub); } // GitLab - if gitlab::GitLabDriver::supports(url, &config.gitlab_domains) { + if GitLabDriver::supports(url, &config.gitlab_domains) { return Some(DriverType::GitLab); } // Bitbucket - if bitbucket::BitbucketDriver::supports(url) { + if GitBitbucketDriver::supports(url) { return Some(DriverType::Bitbucket); } // Forgejo - if forgejo::ForgejoDriver::supports(url, &config.forgejo_domains) { + if ForgejoDriver::supports(url, &config.forgejo_domains) { return Some(DriverType::Forgejo); } // Git - if git::GitDriver::supports(url) { + if GitDriver::supports(url) { return Some(DriverType::Git); } // Hg - if hg::HgDriver::supports(url) { + if HgDriver::supports(url) { return Some(DriverType::Hg); } // SVN - if url_lower.contains("svn") || svn::SvnDriver::supports(url) { + if url_lower.contains("svn") || SvnDriver::supports(url) { return Some(DriverType::Svn); } @@ -296,14 +273,12 @@ pub fn detect_driver( /// Create a driver instance for the given URL and type. pub fn create_driver(url: &str, driver_type: DriverType, config: DriverConfig) -> AnyVcsDriver { match driver_type { - DriverType::GitHub => AnyVcsDriver::GitHub(github::GitHubDriver::new(url, config)), - DriverType::GitLab => AnyVcsDriver::GitLab(gitlab::GitLabDriver::new(url, config)), - DriverType::Bitbucket => { - AnyVcsDriver::Bitbucket(bitbucket::BitbucketDriver::new(url, config)) - } - DriverType::Forgejo => AnyVcsDriver::Forgejo(forgejo::ForgejoDriver::new(url, config)), - DriverType::Git => AnyVcsDriver::Git(git::GitDriver::new(url, config)), - DriverType::Svn => AnyVcsDriver::Svn(svn::SvnDriver::new(url, config)), - DriverType::Hg => AnyVcsDriver::Hg(hg::HgDriver::new(url, config)), + DriverType::GitHub => AnyVcsDriver::GitHub(GitHubDriver::new(url, config)), + DriverType::GitLab => AnyVcsDriver::GitLab(GitLabDriver::new(url, config)), + DriverType::Bitbucket => AnyVcsDriver::Bitbucket(GitBitbucketDriver::new(url, config)), + DriverType::Forgejo => AnyVcsDriver::Forgejo(ForgejoDriver::new(url, config)), + DriverType::Git => AnyVcsDriver::Git(GitDriver::new(url, config)), + DriverType::Svn => AnyVcsDriver::Svn(SvnDriver::new(url, config)), + DriverType::Hg => AnyVcsDriver::Hg(HgDriver::new(url, config)), } } diff --git a/crates/mozart-core/src/vcs/driver/forgejo.rs b/crates/mozart-core/src/repository/vcs/forgejo_driver.rs index 8a290c0..699959b 100644 --- a/crates/mozart-core/src/vcs/driver/forgejo.rs +++ b/crates/mozart-core/src/repository/vcs/forgejo_driver.rs @@ -1,13 +1,13 @@ -use indexmap::IndexMap; -use std::collections::BTreeMap; - +use crate::repository::vcs::{ + DistReference, DriverConfig, GitDriver, SourceReference, VcsDriverInterface, + base64_decode_content, +}; use anyhow::{Result, bail}; +use indexmap::IndexMap; use regex::Regex; use reqwest::Client; use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT}; - -use super::git::GitDriver; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use std::collections::BTreeMap; /// Forgejo/Gitea VCS driver using the REST API v1. /// @@ -138,7 +138,7 @@ impl ForgejoDriver { } } -impl VcsDriver for ForgejoDriver { +impl VcsDriverInterface for ForgejoDriver { async fn initialize(&mut self) -> Result<()> { match self.api_get("").await { Ok(data) => { @@ -229,7 +229,7 @@ impl VcsDriver for ForgejoDriver { Ok(data) => { if let Some(content) = data["content"].as_str() { // Forgejo returns base64-encoded content - let decoded = super::github::base64_decode_content(content)?; + let decoded = base64_decode_content(content)?; Ok(Some(decoded)) } else { Ok(None) diff --git a/crates/mozart-core/src/repository/vcs/fossil_driver.rs b/crates/mozart-core/src/repository/vcs/fossil_driver.rs new file mode 100644 index 0000000..8d45a43 --- /dev/null +++ b/crates/mozart-core/src/repository/vcs/fossil_driver.rs @@ -0,0 +1 @@ +pub struct FossilDriver; diff --git a/crates/mozart-core/src/vcs/driver/bitbucket.rs b/crates/mozart-core/src/repository/vcs/git_bitbucket_driver.rs index 2235e10..0ec1dfd 100644 --- a/crates/mozart-core/src/vcs/driver/bitbucket.rs +++ b/crates/mozart-core/src/repository/vcs/git_bitbucket_driver.rs @@ -1,16 +1,15 @@ -use indexmap::IndexMap; -use std::collections::BTreeMap; - +use crate::repository::vcs::{ + DistReference, DriverConfig, GitDriver, SourceReference, VcsDriverInterface, +}; use anyhow::{Result, bail}; +use indexmap::IndexMap; use regex::Regex; use reqwest::Client; use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT}; - -use super::git::GitDriver; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use std::collections::BTreeMap; /// Bitbucket VCS driver using the REST API 2.0. -pub struct BitbucketDriver { +pub struct GitBitbucketDriver { owner: String, repo: String, url: String, @@ -25,7 +24,7 @@ pub struct BitbucketDriver { vcs_type: String, // "git" or "hg" } -impl BitbucketDriver { +impl GitBitbucketDriver { pub fn new(url: &str, config: DriverConfig) -> Self { let (owner, repo) = Self::parse_url(url).unwrap_or_default(); Self { @@ -133,7 +132,7 @@ impl BitbucketDriver { } } -impl VcsDriver for BitbucketDriver { +impl VcsDriverInterface for GitBitbucketDriver { async fn initialize(&mut self) -> Result<()> { match self.api_get("").await { Ok(data) => { diff --git a/crates/mozart-core/src/vcs/driver/git.rs b/crates/mozart-core/src/repository/vcs/git_driver.rs index 7d6643f..d2f9c04 100644 --- a/crates/mozart-core/src/vcs/driver/git.rs +++ b/crates/mozart-core/src/repository/vcs/git_driver.rs @@ -1,6 +1,6 @@ -use super::super::process::ProcessExecutor; -use super::super::util::git::GitUtil; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use crate::repository::vcs::{DistReference, DriverConfig, SourceReference, VcsDriverInterface}; +use crate::vcs::process::ProcessExecutor; +use crate::vcs::util::git::GitUtil; use anyhow::Result; use indexmap::IndexMap; use std::collections::BTreeMap; @@ -128,7 +128,7 @@ impl GitDriver { } } -impl VcsDriver for GitDriver { +impl VcsDriverInterface for GitDriver { async fn initialize(&mut self) -> Result<()> { if self.is_local { // Local repo: use directly (or its .git subdir) diff --git a/crates/mozart-core/src/vcs/driver/github.rs b/crates/mozart-core/src/repository/vcs/github_driver.rs index 4df2c1c..7a8bc98 100644 --- a/crates/mozart-core/src/vcs/driver/github.rs +++ b/crates/mozart-core/src/repository/vcs/github_driver.rs @@ -1,13 +1,12 @@ -use indexmap::IndexMap; -use std::collections::BTreeMap; - +use crate::repository::vcs::{ + DistReference, DriverConfig, GitDriver, SourceReference, VcsDriverInterface, +}; use anyhow::{Result, bail}; +use indexmap::IndexMap; use regex::Regex; use reqwest::Client; use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT}; - -use super::git::GitDriver; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use std::collections::BTreeMap; /// GitHub VCS driver using the REST API v3. /// @@ -141,7 +140,7 @@ impl GitHubDriver { } } -impl VcsDriver for GitHubDriver { +impl VcsDriverInterface for GitHubDriver { async fn initialize(&mut self) -> Result<()> { // Try to fetch repo data from API match self.api_get("").await { diff --git a/crates/mozart-core/src/vcs/driver/gitlab.rs b/crates/mozart-core/src/repository/vcs/gitlab_driver.rs index f181e63..6b5e7af 100644 --- a/crates/mozart-core/src/vcs/driver/gitlab.rs +++ b/crates/mozart-core/src/repository/vcs/gitlab_driver.rs @@ -1,13 +1,12 @@ -use indexmap::IndexMap; -use std::collections::BTreeMap; - +use crate::repository::vcs::{ + DistReference, DriverConfig, GitDriver, SourceReference, VcsDriverInterface, +}; use anyhow::{Result, bail}; +use indexmap::IndexMap; use regex::Regex; use reqwest::Client; use reqwest::header::{ACCEPT, USER_AGENT}; - -use super::git::GitDriver; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use std::collections::BTreeMap; /// GitLab VCS driver using the REST API v4. /// @@ -144,7 +143,7 @@ impl GitLabDriver { } } -impl VcsDriver for GitLabDriver { +impl VcsDriverInterface for GitLabDriver { async fn initialize(&mut self) -> Result<()> { match self.api_get("").await { Ok(data) => { diff --git a/crates/mozart-core/src/vcs/driver/hg.rs b/crates/mozart-core/src/repository/vcs/hg_driver.rs index e2c3fcd..525b64f 100644 --- a/crates/mozart-core/src/vcs/driver/hg.rs +++ b/crates/mozart-core/src/repository/vcs/hg_driver.rs @@ -1,6 +1,7 @@ -use super::super::process::ProcessExecutor; -use super::super::util::hg::HgUtil; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use crate::repository::vcs::{DistReference, DriverConfig, SourceReference, VcsDriverInterface}; +use crate::vcs::process::ProcessExecutor; +use crate::vcs::util::git::GitUtil; +use crate::vcs::util::hg::HgUtil; use anyhow::Result; use indexmap::IndexMap; use std::collections::BTreeMap; @@ -46,11 +47,11 @@ impl HgDriver { } } -impl VcsDriver for HgDriver { +impl VcsDriverInterface for HgDriver { async fn initialize(&mut self) -> Result<()> { let cache_dir = &self.config.cache_vcs_dir; std::fs::create_dir_all(cache_dir)?; - let repo_dir = cache_dir.join(super::super::util::git::GitUtil::sanitize_url(&self.url)); + let repo_dir = cache_dir.join(GitUtil::sanitize_url(&self.url)); if repo_dir.join(".hg").is_dir() { // Update existing clone diff --git a/crates/mozart-core/src/repository/vcs/perforce_driver.rs b/crates/mozart-core/src/repository/vcs/perforce_driver.rs new file mode 100644 index 0000000..0f9a237 --- /dev/null +++ b/crates/mozart-core/src/repository/vcs/perforce_driver.rs @@ -0,0 +1 @@ +pub struct PerforceDriver; diff --git a/crates/mozart-core/src/vcs/driver/svn.rs b/crates/mozart-core/src/repository/vcs/svn_driver.rs index 7ba9e86..cfd6703 100644 --- a/crates/mozart-core/src/vcs/driver/svn.rs +++ b/crates/mozart-core/src/repository/vcs/svn_driver.rs @@ -1,6 +1,6 @@ -use super::super::process::ProcessExecutor; -use super::super::util::svn::SvnUtil; -use super::{DistReference, DriverConfig, SourceReference, VcsDriver}; +use crate::repository::vcs::{DistReference, DriverConfig, SourceReference, VcsDriverInterface}; +use crate::vcs::process::ProcessExecutor; +use crate::vcs::util::svn::SvnUtil; use anyhow::Result; use indexmap::IndexMap; use regex::Regex; @@ -71,7 +71,7 @@ impl SvnDriver { } } -impl VcsDriver for SvnDriver { +impl VcsDriverInterface for SvnDriver { async fn initialize(&mut self) -> Result<()> { let info = self.svn_info(&self.url)?; if let Some(url) = info["url"].as_str() { diff --git a/crates/mozart-core/src/repository/vcs/vcs_driver.rs b/crates/mozart-core/src/repository/vcs/vcs_driver.rs new file mode 100644 index 0000000..abecc0e --- /dev/null +++ b/crates/mozart-core/src/repository/vcs/vcs_driver.rs @@ -0,0 +1 @@ +pub struct VcsDriver; diff --git a/crates/mozart-core/src/repository/vcs/vcs_driver_interface.rs b/crates/mozart-core/src/repository/vcs/vcs_driver_interface.rs new file mode 100644 index 0000000..49cbd98 --- /dev/null +++ b/crates/mozart-core/src/repository/vcs/vcs_driver_interface.rs @@ -0,0 +1,43 @@ +use crate::repository::vcs::{DistReference, SourceReference}; + +/// The VCS driver interface. +/// +/// Corresponds to Composer's `VcsDriverInterface`. +#[allow(async_fn_in_trait)] +pub trait VcsDriverInterface { + /// Initialize the driver (e.g., clone mirror, fetch API metadata). + async fn initialize(&mut self) -> anyhow::Result<()>; + + /// The root identifier (default branch/trunk). + fn root_identifier(&self) -> &str; + + /// All branches as `name -> commit_hash`. + async fn branches(&mut self) -> anyhow::Result<&std::collections::BTreeMap<String, String>>; + + /// All tags as `name -> commit_hash`. + async fn tags(&mut self) -> anyhow::Result<&std::collections::BTreeMap<String, String>>; + + /// Get composer.json content parsed as JSON for a given identifier. + async fn composer_information( + &mut self, + identifier: &str, + ) -> anyhow::Result<Option<serde_json::Value>>; + + /// Get raw file content at a given path and identifier. + async fn file_content(&self, file: &str, identifier: &str) -> anyhow::Result<Option<String>>; + + /// Get the change date for a given identifier (ISO 8601). + async fn change_date(&self, identifier: &str) -> anyhow::Result<Option<String>>; + + /// Get the dist reference for a given identifier. + async fn dist(&self, identifier: &str) -> anyhow::Result<Option<DistReference>>; + + /// Get the source reference for a given identifier. + fn source(&self, identifier: &str) -> SourceReference; + + /// The canonical URL of this repository. + fn url(&self) -> &str; + + /// Clean up resources (temp dirs, etc.). + async fn cleanup(&mut self) -> anyhow::Result<()>; +} diff --git a/crates/mozart-core/src/repository/vcs_bridge.rs b/crates/mozart-core/src/repository/vcs_bridge.rs index 37d066b..18a8420 100644 --- a/crates/mozart-core/src/repository/vcs_bridge.rs +++ b/crates/mozart-core/src/repository/vcs_bridge.rs @@ -3,11 +3,11 @@ //! Scans VCS repositories defined in composer.json and converts //! discovered package versions into pool inputs for the SAT resolver. -use super::packagist::PackagistVersion; -use super::resolver::{parse_normalized, version_stability}; use crate::dependency_resolver::{PoolPackageInput, make_pool_links}; use crate::package::{RawRepository, Stability}; -use crate::vcs::driver::DriverConfig; +use crate::repository::packagist::PackagistVersion; +use crate::repository::resolver::{parse_normalized, version_stability}; +use crate::repository::vcs::DriverConfig; use crate::vcs::repository::{VcsPackageVersion, VcsRepository}; use indexmap::IndexMap; use std::collections::BTreeMap; diff --git a/crates/mozart-core/src/vcs.rs b/crates/mozart-core/src/vcs.rs index 11db58d..137cc5b 100644 --- a/crates/mozart-core/src/vcs.rs +++ b/crates/mozart-core/src/vcs.rs @@ -1,5 +1,4 @@ pub mod downloader; -pub mod driver; pub mod process; pub mod repository; pub mod util; diff --git a/crates/mozart-core/src/vcs/repository.rs b/crates/mozart-core/src/vcs/repository.rs index 55f98f9..8da847a 100644 --- a/crates/mozart-core/src/vcs/repository.rs +++ b/crates/mozart-core/src/vcs/repository.rs @@ -1,4 +1,4 @@ -use super::driver::{ +use crate::repository::vcs::{ DistReference, DriverConfig, DriverType, SourceReference, create_driver, detect_driver, }; use anyhow::{Result, bail}; diff --git a/crates/mozart-core/tests/git_driver_test.rs b/crates/mozart-core/tests/git_driver_test.rs index c0dd4af..0c1c344 100644 --- a/crates/mozart-core/tests/git_driver_test.rs +++ b/crates/mozart-core/tests/git_driver_test.rs @@ -1,6 +1,6 @@ +use mozart_core::repository::vcs::{DriverConfig, DriverType, create_driver, detect_driver}; use mozart_core::vcs::downloader::VcsDownloader; use mozart_core::vcs::downloader::git::GitDownloader; -use mozart_core::vcs::driver::{DriverConfig, DriverType, create_driver}; use mozart_core::vcs::process::ProcessExecutor; use mozart_core::vcs::repository::VcsRepository; use mozart_core::vcs::util::git::GitUtil; @@ -250,8 +250,6 @@ fn test_git_downloader_unpushed_changes() { #[test] fn test_detect_driver() { - use mozart_core::vcs::driver::{DriverType, detect_driver}; - let config = DriverConfig::default(); assert_eq!( |
