aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-core/src/vcs/repository.rs
blob: 8da847a27a2506ab87b05a823ca518ccd1eecd8c (plain)
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
use crate::repository::vcs::{
    DistReference, DriverConfig, DriverType, SourceReference, create_driver, detect_driver,
};
use anyhow::{Result, bail};

/// A single package version discovered from a VCS repository.
#[derive(Debug, Clone)]
pub struct VcsPackageVersion {
    /// Package name (from composer.json).
    pub name: String,
    /// Version string (e.g., "1.2.3" for tags, "dev-main" for branches).
    pub version: String,
    /// Normalized version for comparison.
    pub version_normalized: String,
    /// Full composer.json data as JSON.
    pub composer_json: serde_json::Value,
    /// Source reference (VCS checkout info).
    pub source: SourceReference,
    /// Dist reference (archive download, if available).
    pub dist: Option<DistReference>,
    /// Whether this is the default branch version.
    pub is_default_branch: bool,
    /// Release date (ISO 8601).
    pub time: Option<String>,
}

/// Repository that scans a VCS URL for package versions.
///
/// Corresponds to Composer's `Repository\VcsRepository`.
pub struct VcsRepository {
    url: String,
    driver_type: Option<DriverType>,
    config: DriverConfig,
}

impl VcsRepository {
    pub fn new(url: String, repo_type: Option<&str>, config: DriverConfig) -> Self {
        let driver_type = detect_driver(&url, repo_type, &config);
        Self {
            url,
            driver_type,
            config,
        }
    }

    /// Scan the VCS repository for all package versions.
    ///
    /// 1. Detects the driver type and initializes it
    /// 2. Reads composer.json from the root to get the package name
    /// 3. Scans tags → version releases
    /// 4. Scans branches → dev versions
    pub async fn scan(&self) -> Result<Vec<VcsPackageVersion>> {
        let driver_type = self
            .driver_type
            .ok_or_else(|| anyhow::anyhow!("No suitable VCS driver found for URL: {}", self.url))?;

        let mut driver = create_driver(&self.url, driver_type, self.config.clone());
        driver.initialize().await?;

        // Get package name from root composer.json
        let root_id = driver.root_identifier().to_string();
        let root_info = driver.composer_information(&root_id).await?;
        let package_name = match &root_info {
            Some(info) => info["name"]
                .as_str()
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "composer.json at root of {} does not contain a 'name' field",
                        self.url,
                    )
                })?
                .to_string(),
            None => bail!(
                "No composer.json found at root of {} (ref: {})",
                self.url,
                root_id,
            ),
        };

        let mut versions = Vec::new();

        // Scan tags
        let tags = driver.tags().await?.clone();
        for (tag_name, tag_hash) in &tags {
            if let Some(version) = self.tag_to_version(tag_name) {
                match driver.composer_information(tag_hash).await {
                    Ok(Some(info)) => {
                        let time = driver.change_date(tag_hash).await.unwrap_or(None);
                        let source = driver.source(tag_hash);
                        let dist = driver.dist(tag_hash).await.unwrap_or(None);

                        // Ensure name matches root package
                        if info["name"].as_str() != Some(&package_name) {
                            continue;
                        }

                        let normalized = self.normalize_version(&version);

                        versions.push(VcsPackageVersion {
                            name: package_name.clone(),
                            version: version.clone(),
                            version_normalized: normalized,
                            composer_json: info,
                            source,
                            dist,
                            is_default_branch: false,
                            time,
                        });
                    }
                    Ok(None) | Err(_) => continue,
                }
            }
        }

        // Scan branches
        let branches = driver.branches().await?.clone();
        let default_branch = driver.root_identifier().to_string();
        for (branch_name, branch_hash) in &branches {
            match driver.composer_information(branch_hash).await {
                Ok(Some(info)) => {
                    if info["name"].as_str() != Some(&package_name) {
                        continue;
                    }

                    let time = driver.change_date(branch_hash).await.unwrap_or(None);
                    let source = driver.source(branch_hash);
                    let dist = driver.dist(branch_hash).await.unwrap_or(None);
                    let is_default = branch_name == &default_branch;

                    let version = self.branch_to_version(branch_name);
                    let normalized = self.normalize_version(&version);

                    // Check for branch-alias
                    let aliased_version = info
                        .get("extra")
                        .and_then(|e| e.get("branch-alias"))
                        .and_then(|ba| ba.get(format!("dev-{branch_name}")))
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());

                    versions.push(VcsPackageVersion {
                        name: package_name.clone(),
                        version: aliased_version.unwrap_or(version),
                        version_normalized: normalized,
                        composer_json: info,
                        source,
                        dist,
                        is_default_branch: is_default,
                        time,
                    });
                }
                Ok(None) | Err(_) => continue,
            }
        }

        driver.cleanup().await?;
        Ok(versions)
    }

    /// Convert a tag name to a version string.
    /// Returns `None` if the tag doesn't look like a version.
    fn tag_to_version(&self, tag: &str) -> Option<String> {
        // Strip common prefixes
        let version = tag
            .strip_prefix('v')
            .or_else(|| tag.strip_prefix("V"))
            .or_else(|| tag.strip_prefix("release-"))
            .or_else(|| tag.strip_prefix("release/"))
            .unwrap_or(tag);

        // Basic semver-ish check
        if version.is_empty() {
            return None;
        }
        if version.chars().next()?.is_ascii_digit() {
            Some(version.to_string())
        } else {
            None
        }
    }

    /// Convert a branch name to a dev version string.
    fn branch_to_version(&self, branch: &str) -> String {
        // Numeric branches like "1.x", "2.0" become "1.x-dev", "2.0.x-dev"
        if branch.chars().next().is_some_and(|c| c.is_ascii_digit()) {
            let version = if branch.ends_with(".x") || branch.ends_with(".*") {
                branch.to_string()
            } else {
                format!("{branch}.x")
            };
            format!("{version}-dev")
        } else {
            format!("dev-{branch}")
        }
    }

    /// Normalize a version string.
    fn normalize_version(&self, version: &str) -> String {
        // Use mozart-semver for proper normalization if available,
        // otherwise do a simple normalization
        mozart_semver::Version::parse(version)
            .map(|v| v.to_string())
            .unwrap_or_else(|_| version.to_string())
    }
}