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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
use indexmap::IndexMap;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use anyhow::Result;
use crate::process::ProcessExecutor;
use crate::util::git::GitUtil;
use super::{DistReference, DriverConfig, SourceReference, VcsDriver};
/// Git VCS driver.
///
/// Corresponds to Composer's `Repository\Vcs\GitDriver`.
pub struct GitDriver {
url: String,
repo_dir: Option<PathBuf>,
root_identifier: Option<String>,
tags: Option<BTreeMap<String, String>>,
branches: Option<BTreeMap<String, String>>,
info_cache: IndexMap<String, Option<serde_json::Value>>,
git_util: GitUtil,
is_local: bool,
}
impl GitDriver {
pub fn new(url: &str, config: DriverConfig) -> Self {
let is_local = Self::is_local_path(url);
let process = ProcessExecutor::new();
let git_util = GitUtil::new(process, config.cache_vcs_dir.clone());
Self {
url: url.to_string(),
repo_dir: if is_local {
Some(PathBuf::from(url))
} else {
None
},
root_identifier: None,
tags: None,
branches: None,
info_cache: IndexMap::new(),
git_util,
is_local,
}
}
/// Check if a URL is supported by the Git driver.
pub fn supports(url: &str) -> bool {
if Self::is_local_path(url) {
return Path::new(url).join(".git").is_dir() || url.ends_with(".git");
}
url.starts_with("git://")
|| url.starts_with("git@")
|| url.ends_with(".git")
|| url.contains("git.")
}
fn is_local_path(url: &str) -> bool {
!url.contains("://") && !url.starts_with("git@") && Path::new(url).exists()
}
fn get_repo_dir(&self) -> Result<&Path> {
self.repo_dir
.as_deref()
.ok_or_else(|| anyhow::anyhow!("GitDriver not initialized"))
}
fn parse_branches(output: &str) -> BTreeMap<String, String> {
let mut branches = BTreeMap::new();
for line in output.lines() {
let line = line.trim();
if line.is_empty() || line.contains("HEAD detached") || line.contains("->") {
continue;
}
// Remove leading "* " for current branch
let line = line.strip_prefix("* ").unwrap_or(line);
// Format: "branch_name commit_hash ..."
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
branches.insert(parts[0].to_string(), parts[1].to_string());
}
}
branches
}
fn parse_tags(output: &str) -> BTreeMap<String, String> {
let mut tags = BTreeMap::new();
// First pass: collect dereferenced tags (^{})
let mut dereferenced = IndexMap::new();
for line in output.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
// Format: "commit_hash refs/tags/tag_name" or "commit_hash refs/tags/tag_name^{}"
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
let hash = parts[0];
let refname = parts[1];
if let Some(tag_name) = refname.strip_prefix("refs/tags/")
&& let Some(tag_name) = tag_name.strip_suffix("^{}")
{
// Dereferenced tag - this is the actual commit
dereferenced.insert(tag_name.to_string(), hash.to_string());
}
}
}
// Second pass: collect all tags, preferring dereferenced values
for line in output.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
let hash = parts[0];
let refname = parts[1];
if let Some(tag_name) = refname.strip_prefix("refs/tags/") {
if tag_name.ends_with("^{}") {
continue; // Skip dereferenced entries themselves
}
let resolved = dereferenced
.get(tag_name)
.cloned()
.unwrap_or_else(|| hash.to_string());
tags.insert(tag_name.to_string(), resolved);
}
}
}
tags
}
}
impl VcsDriver for GitDriver {
async fn initialize(&mut self) -> Result<()> {
if self.is_local {
// Local repo: use directly (or its .git subdir)
let path = Path::new(&self.url);
if path.join(".git").is_dir() {
self.repo_dir = Some(path.join(".git"));
} else {
self.repo_dir = Some(path.to_path_buf());
}
} else {
// Remote repo: sync mirror
let mirror_dir = self.git_util.sync_mirror(&self.url)?;
self.repo_dir = Some(mirror_dir);
}
// Determine root identifier (default branch)
let repo_dir = self.repo_dir.clone().unwrap();
if let Ok(Some(branch)) = self.git_util.get_default_branch(&repo_dir) {
self.root_identifier = Some(branch);
} else {
// Fallback: try common branch names
let process = ProcessExecutor::new();
for name in &["main", "master"] {
let output =
process.execute(&["git", "rev-parse", "--verify", name], Some(&repo_dir))?;
if output.status == 0 {
self.root_identifier = Some(name.to_string());
break;
}
}
}
if self.root_identifier.is_none() {
self.root_identifier = Some("master".to_string());
}
Ok(())
}
fn root_identifier(&self) -> &str {
self.root_identifier.as_deref().unwrap_or("master")
}
async fn branches(&mut self) -> Result<&BTreeMap<String, String>> {
if self.branches.is_none() {
let repo_dir = self.get_repo_dir()?.to_path_buf();
let process = ProcessExecutor::new();
let output = process.execute_checked(
&["git", "branch", "--no-color", "--no-abbrev", "-v"],
Some(&repo_dir),
)?;
self.branches = Some(Self::parse_branches(&output.stdout));
}
Ok(self.branches.as_ref().unwrap())
}
async fn tags(&mut self) -> Result<&BTreeMap<String, String>> {
if self.tags.is_none() {
let repo_dir = self.get_repo_dir()?.to_path_buf();
let process = ProcessExecutor::new();
let output = process.execute(
&["git", "show-ref", "--tags", "--dereference"],
Some(&repo_dir),
)?;
self.tags = Some(if output.status == 0 {
Self::parse_tags(&output.stdout)
} else {
BTreeMap::new()
});
}
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 = match content {
Some(c) => serde_json::from_str(&c).ok(),
None => None,
};
self.info_cache
.insert(identifier.to_string(), value.clone());
Ok(value)
}
async fn file_content(&self, file: &str, identifier: &str) -> Result<Option<String>> {
let repo_dir = self.get_repo_dir()?;
let process = ProcessExecutor::new();
let resource = format!("{identifier}:{file}");
let output = process.execute(&["git", "show", &resource], Some(repo_dir))?;
if output.status == 0 {
Ok(Some(output.stdout))
} else {
Ok(None)
}
}
async fn change_date(&self, identifier: &str) -> Result<Option<String>> {
let repo_dir = self.get_repo_dir()?;
let process = ProcessExecutor::new();
let output = process.execute(
&["git", "log", "-1", "--format=%aI", identifier],
Some(repo_dir),
)?;
if output.status == 0 {
let date = output.stdout.trim().to_string();
if date.is_empty() {
Ok(None)
} else {
Ok(Some(date))
}
} else {
Ok(None)
}
}
async fn dist(&self, _identifier: &str) -> Result<Option<DistReference>> {
// Plain git repos don't provide dist archives
Ok(None)
}
fn source(&self, identifier: &str) -> SourceReference {
SourceReference {
source_type: "git".to_string(),
url: self.url.clone(),
reference: identifier.to_string(),
}
}
fn url(&self) -> &str {
&self.url
}
async fn cleanup(&mut self) -> Result<()> {
Ok(())
}
}
|