1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
pub mod bitbucket;
pub mod forgejo;
pub mod git;
pub mod github;
pub mod gitlab;
pub mod hg;
pub mod svn;
use std::collections::BTreeMap;
use std::path::PathBuf;
use anyhow::Result;
use serde::{Deserialize, Serialize};
/// Reference to a source distribution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceReference {
#[serde(rename = "type")]
pub source_type: String,
pub url: String,
pub reference: String,
}
/// Reference to a dist (archive) distribution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistReference {
#[serde(rename = "type")]
pub dist_type: String,
pub url: String,
pub reference: String,
pub shasum: Option<String>,
}
/// Configuration passed to VCS drivers.
#[derive(Debug, Clone)]
pub struct DriverConfig {
/// Path for caching VCS mirrors.
pub cache_dir: PathBuf,
/// GitHub OAuth token (from `GITHUB_TOKEN` or config).
pub github_token: Option<String>,
/// GitLab OAuth token.
pub gitlab_token: Option<String>,
/// Bitbucket OAuth consumer key/secret.
pub bitbucket_oauth: Option<(String, String)>,
/// Forgejo token.
pub forgejo_token: Option<String>,
/// Custom GitLab domains (for self-hosted).
pub gitlab_domains: Vec<String>,
/// Custom Forgejo domains (for self-hosted).
pub forgejo_domains: Vec<String>,
}
impl Default for DriverConfig {
fn default() -> Self {
Self {
cache_dir: PathBuf::from(".cache/mozart/vcs"),
github_token: None,
gitlab_token: None,
bitbucket_oauth: None,
forgejo_token: None,
gitlab_domains: vec!["gitlab.com".to_string()],
forgejo_domains: vec!["codeberg.org".to_string()],
}
}
}
/// Type of VCS driver.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DriverType {
GitHub,
GitLab,
Bitbucket,
Forgejo,
Git,
Svn,
Hg,
}
/// The VCS driver interface.
///
/// Corresponds to Composer's `VcsDriverInterface`.
pub trait VcsDriver {
/// Initialize the driver (e.g., clone mirror, fetch API metadata).
fn initialize(&mut self) -> Result<()>;
/// The root identifier (default branch/trunk).
fn root_identifier(&self) -> &str;
/// All branches as `name -> commit_hash`.
fn branches(&mut self) -> Result<&BTreeMap<String, String>>;
/// All tags as `name -> commit_hash`.
fn tags(&mut self) -> Result<&BTreeMap<String, String>>;
/// Get composer.json content parsed as JSON for a given identifier.
fn composer_information(&mut self, identifier: &str) -> Result<Option<serde_json::Value>>;
/// Get raw file content at a given path and identifier.
fn file_content(&self, file: &str, identifier: &str) -> Result<Option<String>>;
/// Get the change date for a given identifier (ISO 8601).
fn change_date(&self, identifier: &str) -> Result<Option<String>>;
/// Get the dist reference for a given identifier.
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.).
fn cleanup(&mut self) -> Result<()>;
}
/// Detect which driver type should handle a given URL.
///
/// Priority order matches Composer:
/// 1. GitHub → 2. GitLab → 3. Bitbucket → 4. Forgejo → 5. Git → 6. Hg → 7. SVN
pub fn detect_driver(
url: &str,
forced_type: Option<&str>,
config: &DriverConfig,
) -> Option<DriverType> {
if let Some(t) = forced_type {
return match t {
"github" => Some(DriverType::GitHub),
"gitlab" => Some(DriverType::GitLab),
"bitbucket" => Some(DriverType::Bitbucket),
"forgejo" => Some(DriverType::Forgejo),
"git" => Some(DriverType::Git),
"svn" => Some(DriverType::Svn),
"hg" | "mercurial" => Some(DriverType::Hg),
_ => None,
};
}
let url_lower = url.to_lowercase();
// GitHub
if github::GitHubDriver::supports(url) {
return Some(DriverType::GitHub);
}
// GitLab
if gitlab::GitLabDriver::supports(url, &config.gitlab_domains) {
return Some(DriverType::GitLab);
}
// Bitbucket
if bitbucket::BitbucketDriver::supports(url) {
return Some(DriverType::Bitbucket);
}
// Forgejo
if forgejo::ForgejoDriver::supports(url, &config.forgejo_domains) {
return Some(DriverType::Forgejo);
}
// Git
if git::GitDriver::supports(url) {
return Some(DriverType::Git);
}
// Hg
if hg::HgDriver::supports(url) {
return Some(DriverType::Hg);
}
// SVN
if url_lower.contains("svn") || svn::SvnDriver::supports(url) {
return Some(DriverType::Svn);
}
// Default to git for generic URLs
if url.starts_with("http://") || url.starts_with("https://") {
return Some(DriverType::Git);
}
None
}
/// Create a driver instance for the given URL and type.
pub fn create_driver(
url: &str,
driver_type: DriverType,
config: DriverConfig,
) -> Box<dyn VcsDriver> {
match driver_type {
DriverType::GitHub => Box::new(github::GitHubDriver::new(url, config)),
DriverType::GitLab => Box::new(gitlab::GitLabDriver::new(url, config)),
DriverType::Bitbucket => Box::new(bitbucket::BitbucketDriver::new(url, config)),
DriverType::Forgejo => Box::new(forgejo::ForgejoDriver::new(url, config)),
DriverType::Git => Box::new(git::GitDriver::new(url, config)),
DriverType::Svn => Box::new(svn::SvnDriver::new(url, config)),
DriverType::Hg => Box::new(hg::HgDriver::new(url, config)),
}
}
|