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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
use indexmap::IndexMap;
use std::collections::BTreeMap;
use anyhow::Result;
use regex::Regex;
use crate::process::ProcessExecutor;
use crate::util::svn::SvnUtil;
use super::{DistReference, DriverConfig, SourceReference, VcsDriver};
/// SVN VCS driver.
///
/// Corresponds to Composer's `Repository\Vcs\SvnDriver`.
pub struct SvnDriver {
url: String,
base_url: String,
trunk_path: String,
branches_path: String,
tags_path: String,
root_identifier: Option<String>,
tags: Option<BTreeMap<String, String>>,
branches: Option<BTreeMap<String, String>>,
info_cache: IndexMap<String, Option<serde_json::Value>>,
svn_util: SvnUtil,
}
impl SvnDriver {
pub fn new(url: &str, _config: DriverConfig) -> Self {
let process = ProcessExecutor::new();
Self {
url: url.to_string(),
base_url: url.to_string(),
trunk_path: "trunk".to_string(),
branches_path: "branches".to_string(),
tags_path: "tags".to_string(),
root_identifier: None,
tags: None,
branches: None,
info_cache: IndexMap::new(),
svn_util: SvnUtil::new(process),
}
}
pub fn supports(url: &str) -> bool {
url.starts_with("svn://") || url.starts_with("svn+ssh://")
}
fn svn_info(&self, url: &str) -> Result<serde_json::Value> {
let output = self.svn_util.execute(&["info", "--xml", url], None)?;
// Parse minimal info from XML output
let stdout = &output.stdout;
let mut info = serde_json::Map::new();
if let Some(rev) = extract_xml_attr(stdout, "entry", "revision") {
info.insert("revision".to_string(), serde_json::Value::String(rev));
}
if let Some(url_val) = extract_xml_content(stdout, "url") {
info.insert("url".to_string(), serde_json::Value::String(url_val));
}
if let Some(date) = extract_xml_content(stdout, "date") {
info.insert("date".to_string(), serde_json::Value::String(date));
}
Ok(serde_json::Value::Object(info))
}
fn svn_ls(&self, url: &str) -> Result<Vec<String>> {
let output = self.svn_util.execute(&["ls", url], None)?;
Ok(ProcessExecutor::split_lines(&output.stdout)
.into_iter()
.map(|s| s.trim_end_matches('/').to_string())
.collect())
}
}
impl VcsDriver for SvnDriver {
async fn initialize(&mut self) -> Result<()> {
let info = self.svn_info(&self.url)?;
if let Some(url) = info["url"].as_str() {
self.base_url = url.to_string();
}
self.root_identifier = info["revision"].as_str().map(|s| s.to_string());
Ok(())
}
fn root_identifier(&self) -> &str {
self.root_identifier.as_deref().unwrap_or("HEAD")
}
async fn branches(&mut self) -> Result<&BTreeMap<String, String>> {
if self.branches.is_none() {
let mut branches = BTreeMap::new();
// Add trunk
let trunk_url = format!("{}/{}", self.base_url, self.trunk_path);
if let Ok(info) = self.svn_info(&trunk_url)
&& let Some(rev) = info["revision"].as_str()
{
branches.insert("trunk".to_string(), rev.to_string());
}
// List branches directory
let branches_url = format!("{}/{}", self.base_url, self.branches_path);
if let Ok(items) = self.svn_ls(&branches_url) {
for name in items {
let branch_url = format!("{}/{}", branches_url, name);
if let Ok(info) = self.svn_info(&branch_url)
&& let Some(rev) = info["revision"].as_str()
{
branches.insert(name, rev.to_string());
}
}
}
self.branches = Some(branches);
}
Ok(self.branches.as_ref().unwrap())
}
async fn tags(&mut self) -> Result<&BTreeMap<String, String>> {
if self.tags.is_none() {
let mut tags = BTreeMap::new();
let tags_url = format!("{}/{}", self.base_url, self.tags_path);
if let Ok(items) = self.svn_ls(&tags_url) {
for name in items {
let tag_url = format!("{}/{}", tags_url, name);
if let Ok(info) = self.svn_info(&tag_url)
&& let Some(rev) = info["revision"].as_str()
{
tags.insert(name, rev.to_string());
}
}
}
self.tags = Some(tags);
}
Ok(self.tags.as_ref().unwrap())
}
async fn composer_information(
&mut self,
identifier: &str,
) -> Result<Option<serde_json::Value>> {
if let Some(cached) = self.info_cache.get(identifier) {
return Ok(cached.clone());
}
let content = self.file_content("composer.json", identifier).await?;
let value = content.and_then(|c| serde_json::from_str(&c).ok());
self.info_cache
.insert(identifier.to_string(), value.clone());
Ok(value)
}
async fn file_content(&self, file: &str, identifier: &str) -> Result<Option<String>> {
// identifier is either a path (trunk, branches/x, tags/y) or a revision number
let url = if identifier.contains('/') || identifier == "trunk" {
format!("{}/{}/{}", self.base_url, identifier, file)
} else {
format!(
"{}/{}/{}@{}",
self.base_url, self.trunk_path, file, identifier
)
};
let output = self.svn_util.execute(&["cat", &url], None);
match output {
Ok(o) if !o.stdout.is_empty() => Ok(Some(o.stdout)),
_ => Ok(None),
}
}
async fn change_date(&self, identifier: &str) -> Result<Option<String>> {
let url = if identifier.contains('/') || identifier == "trunk" {
format!("{}/{}", self.base_url, identifier)
} else {
format!("{}@{}", self.base_url, identifier)
};
match self.svn_info(&url) {
Ok(info) => Ok(info["date"].as_str().map(|s| s.to_string())),
Err(_) => Ok(None),
}
}
async fn dist(&self, _identifier: &str) -> Result<Option<DistReference>> {
// SVN doesn't provide dist archives
Ok(None)
}
fn source(&self, identifier: &str) -> SourceReference {
SourceReference {
source_type: "svn".to_string(),
url: self.base_url.clone(),
reference: identifier.to_string(),
}
}
fn url(&self) -> &str {
&self.url
}
async fn cleanup(&mut self) -> Result<()> {
Ok(())
}
}
/// Extract an XML attribute value from a simple XML string.
fn extract_xml_attr(xml: &str, tag: &str, attr: &str) -> Option<String> {
let pattern = format!(r#"<{tag}\s[^>]*{attr}="([^"]*)"#);
let re = Regex::new(&pattern).ok()?;
re.captures(xml).map(|c| c[1].to_string())
}
/// Extract text content between XML tags.
fn extract_xml_content(xml: &str, tag: &str) -> Option<String> {
let pattern = format!(r"<{tag}>([^<]*)</{tag}>");
let re = Regex::new(&pattern).ok()?;
re.captures(xml).map(|c| c[1].to_string())
}
|