aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-vcs/src/driver/hg.rs
blob: 7bfb07e7dc9ad5c3e95c5399e5dbc6e940fd0671 (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
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;

use anyhow::Result;

use crate::process::ProcessExecutor;
use crate::util::hg::HgUtil;

use super::{DistReference, DriverConfig, SourceReference, VcsDriver};

/// Mercurial VCS driver.
///
/// Corresponds to Composer's `Repository\Vcs\HgDriver`.
pub struct HgDriver {
    url: String,
    repo_dir: Option<PathBuf>,
    root_identifier: Option<String>,
    tags: Option<BTreeMap<String, String>>,
    branches: Option<BTreeMap<String, String>>,
    info_cache: HashMap<String, Option<serde_json::Value>>,
    hg_util: HgUtil,
    config: DriverConfig,
}

impl HgDriver {
    pub fn new(url: &str, config: DriverConfig) -> Self {
        let process = ProcessExecutor::new();
        Self {
            url: url.to_string(),
            repo_dir: None,
            root_identifier: None,
            tags: None,
            branches: None,
            info_cache: HashMap::new(),
            hg_util: HgUtil::new(process),
            config,
        }
    }

    pub fn supports(url: &str) -> bool {
        url.starts_with("hg://") || url.contains("hg.") || url.ends_with(".hg")
    }

    fn get_repo_dir(&self) -> Result<&PathBuf> {
        self.repo_dir
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("HgDriver not initialized"))
    }
}

impl VcsDriver for HgDriver {
    fn initialize(&mut self) -> Result<()> {
        let cache_dir = self.config.cache_dir.join("hg");
        std::fs::create_dir_all(&cache_dir)?;
        let repo_dir = cache_dir.join(crate::util::git::GitUtil::sanitize_url(&self.url));

        if repo_dir.join(".hg").is_dir() {
            // Update existing clone
            self.hg_util.execute(&["pull"], Some(&repo_dir))?;
        } else {
            // Clone without checkout
            let dir_str = repo_dir.to_string_lossy().to_string();
            self.hg_util
                .execute(&["clone", "--noupdate", &self.url, &dir_str], None)?;
        }

        self.repo_dir = Some(repo_dir.clone());

        // Get default branch
        let output = self.hg_util.execute(
            &["log", "-r", "default", "--template", "{node|short}"],
            Some(&repo_dir),
        );
        self.root_identifier = match output {
            Ok(o) if !o.stdout.trim().is_empty() => Some("default".to_string()),
            _ => Some("tip".to_string()),
        };

        Ok(())
    }

    fn root_identifier(&self) -> &str {
        self.root_identifier.as_deref().unwrap_or("default")
    }

    fn branches(&mut self) -> Result<&BTreeMap<String, String>> {
        if self.branches.is_none() {
            let repo_dir = self.get_repo_dir()?.clone();
            let mut branches = BTreeMap::new();

            // Named branches
            let output = self.hg_util.execute(&["branches", "-q"], Some(&repo_dir))?;
            for name in ProcessExecutor::split_lines(&output.stdout) {
                let name = name.trim();
                let rev_output = self.hg_util.execute(
                    &["log", "-r", name, "--template", "{node}"],
                    Some(&repo_dir),
                )?;
                branches.insert(name.to_string(), rev_output.stdout.trim().to_string());
            }

            // Bookmarks
            let output = self
                .hg_util
                .execute_unchecked(&["bookmarks", "-q"], Some(&repo_dir))?;
            if output.status == 0 {
                for name in ProcessExecutor::split_lines(&output.stdout) {
                    let name = name.trim();
                    if !branches.contains_key(name) {
                        let rev_output = self.hg_util.execute(
                            &["log", "-r", name, "--template", "{node}"],
                            Some(&repo_dir),
                        )?;
                        branches.insert(name.to_string(), rev_output.stdout.trim().to_string());
                    }
                }
            }

            self.branches = Some(branches);
        }
        Ok(self.branches.as_ref().unwrap())
    }

    fn tags(&mut self) -> Result<&BTreeMap<String, String>> {
        if self.tags.is_none() {
            let repo_dir = self.get_repo_dir()?.clone();
            let output = self.hg_util.execute(&["tags", "-q"], Some(&repo_dir))?;
            let mut tags = BTreeMap::new();
            for name in ProcessExecutor::split_lines(&output.stdout) {
                let name = name.trim();
                if name == "tip" {
                    continue; // Skip the "tip" pseudo-tag
                }
                let rev_output = self.hg_util.execute(
                    &["log", "-r", name, "--template", "{node}"],
                    Some(&repo_dir),
                )?;
                tags.insert(name.to_string(), rev_output.stdout.trim().to_string());
            }
            self.tags = Some(tags);
        }
        Ok(self.tags.as_ref().unwrap())
    }

    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)?;
        let value = content.and_then(|c| serde_json::from_str(&c).ok());
        self.info_cache
            .insert(identifier.to_string(), value.clone());
        Ok(value)
    }

    fn file_content(&self, file: &str, identifier: &str) -> Result<Option<String>> {
        let repo_dir = self.get_repo_dir()?;
        let output = self
            .hg_util
            .execute_unchecked(&["cat", "-r", identifier, "--", file], Some(repo_dir))?;
        if output.status == 0 {
            Ok(Some(output.stdout))
        } else {
            Ok(None)
        }
    }

    fn change_date(&self, identifier: &str) -> Result<Option<String>> {
        let repo_dir = self.get_repo_dir()?;
        let output = self.hg_util.execute(
            &["log", "-r", identifier, "--template", "{date|isodatesec}"],
            Some(repo_dir),
        )?;
        let date = output.stdout.trim().to_string();
        if date.is_empty() {
            Ok(None)
        } else {
            Ok(Some(date))
        }
    }

    fn dist(&self, _identifier: &str) -> Result<Option<DistReference>> {
        Ok(None)
    }

    fn source(&self, identifier: &str) -> SourceReference {
        SourceReference {
            source_type: "hg".to_string(),
            url: self.url.clone(),
            reference: identifier.to_string(),
        }
    }

    fn url(&self) -> &str {
        &self.url
    }

    fn cleanup(&mut self) -> Result<()> {
        Ok(())
    }
}