aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-vcs/src/downloader/hg.rs
blob: 926cfa8cd6709bf99e026d231d6e6654aa4b9882 (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
use std::path::Path;

use anyhow::Result;

use crate::util::hg::HgUtil;

use super::VcsDownloader;

/// Mercurial downloader using clone/pull/update.
pub struct HgDownloader {
    hg_util: HgUtil,
}

impl HgDownloader {
    pub fn new(hg_util: HgUtil) -> Self {
        Self { hg_util }
    }
}

impl VcsDownloader for HgDownloader {
    fn download(&self, _url: &str, _reference: &str, _target: &Path) -> Result<()> {
        Ok(())
    }

    fn install(&self, url: &str, reference: &str, target: &Path) -> Result<()> {
        let target_str = target.to_string_lossy().to_string();
        self.hg_util
            .execute(&["clone", "--", url, &target_str], None)?;
        self.hg_util
            .execute(&["update", "-r", reference], Some(target))?;
        Ok(())
    }

    fn update(&self, url: &str, _old_ref: &str, new_ref: &str, target: &Path) -> Result<()> {
        self.hg_util.execute(&["pull", url], Some(target))?;
        self.hg_util
            .execute(&["update", "-r", new_ref], Some(target))?;
        Ok(())
    }

    fn remove(&self, target: &Path) -> Result<()> {
        if target.exists() {
            std::fs::remove_dir_all(target)?;
        }
        Ok(())
    }

    fn local_changes(&self, target: &Path) -> Result<Option<String>> {
        if !target.join(".hg").is_dir() {
            return Ok(None);
        }
        let output = self.hg_util.execute(&["st"], Some(target))?;
        let trimmed = output.stdout.trim();
        if trimmed.is_empty() {
            Ok(None)
        } else {
            Ok(Some(trimmed.to_string()))
        }
    }

    fn commit_logs(&self, from: &str, to: &str, target: &Path) -> Result<String> {
        let range = format!("{from}:{to}");
        let output = self.hg_util.execute(
            &[
                "log",
                "-r",
                &range,
                "--template",
                "{rev}:{node|short} {desc|firstline}\\n",
            ],
            Some(target),
        )?;
        Ok(output.stdout)
    }
}