aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/downloader/fossil_downloader.rs
blob: 47ad9730f0c9a20334834274a76db58a1f84f8c8 (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
//! ref: composer/src/Composer/Downloader/FossilDownloader.php

use anyhow::Result;
use shirabe_external_packages::composer::pcre::preg::Preg;
use shirabe_external_packages::react::promise::promise_interface::PromiseInterface;
use shirabe_php_shim::RuntimeException;
use crate::downloader::vcs_downloader::VcsDownloader;
use crate::package::package_interface::PackageInterface;

#[derive(Debug)]
pub struct FossilDownloader {
    inner: VcsDownloader,
}

impl FossilDownloader {
    pub(crate) fn do_download(
        &self,
        _package: &dyn PackageInterface,
        _path: String,
        _url: String,
        _prev_package: Option<&dyn PackageInterface>,
    ) -> Result<Box<dyn PromiseInterface>> {
        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub(crate) fn do_install(
        &self,
        package: &dyn PackageInterface,
        path: String,
        url: String,
    ) -> Result<Box<dyn PromiseInterface>> {
        self.inner.config.prohibit_url_by_config(&url, &self.inner.io)?;

        let repo_file = format!("{}.fossil", path);
        let real_path = shirabe_php_shim::realpath(&path);

        self.inner.io.write_error(&format!("Cloning {}", package.get_source_reference().unwrap_or_default()));

        let mut output = String::new();
        self.execute(
            vec!["fossil".to_string(), "clone".to_string(), "--".to_string(), url, repo_file.clone()],
            None,
            &mut output,
        )?;
        self.execute(
            vec!["fossil".to_string(), "open".to_string(), "--nested".to_string(), "--".to_string(), repo_file],
            real_path.clone(),
            &mut output,
        )?;
        self.execute(
            vec!["fossil".to_string(), "update".to_string(), "--".to_string(), package.get_source_reference().unwrap_or_default()],
            real_path,
            &mut output,
        )?;

        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub(crate) fn do_update(
        &self,
        _initial: &dyn PackageInterface,
        target: &dyn PackageInterface,
        path: String,
        url: String,
    ) -> Result<Box<dyn PromiseInterface>> {
        self.inner.config.prohibit_url_by_config(&url, &self.inner.io)?;

        self.inner.io.write_error(&format!(" Updating to {}", target.get_source_reference().unwrap_or_default()));

        if !self.has_metadata_repository(&path) {
            return Err(RuntimeException {
                message: format!(
                    "The .fslckout file is missing from {}, see https://getcomposer.org/commit-deps for more information",
                    path
                ),
                code: 0,
            }.into());
        }

        let real_path = shirabe_php_shim::realpath(&path);
        let mut output = String::new();
        self.execute(
            vec!["fossil".to_string(), "pull".to_string()],
            real_path.clone(),
            &mut output,
        )?;
        self.execute(
            vec!["fossil".to_string(), "up".to_string(), "--".to_string(), target.get_source_reference().unwrap_or_default()],
            real_path,
            &mut output,
        )?;

        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub fn get_local_changes(&self, _package: &dyn PackageInterface, path: String) -> Option<String> {
        if !self.has_metadata_repository(&path) {
            return None;
        }

        let mut output = String::new();
        self.inner.process.execute(
            &["fossil".to_string(), "changes".to_string()],
            &mut output,
            shirabe_php_shim::realpath(&path),
        );

        let output = output.trim().to_string();

        if output.len() > 0 { Some(output) } else { None }
    }

    pub(crate) fn get_commit_logs(
        &self,
        _from_reference: String,
        to_reference: String,
        path: String,
    ) -> Result<String> {
        let mut output = String::new();
        self.execute(
            vec![
                "fossil".to_string(), "timeline".to_string(),
                "-t".to_string(), "ci".to_string(),
                "-W".to_string(), "0".to_string(),
                "-n".to_string(), "0".to_string(),
                "before".to_string(), to_reference.clone(),
            ],
            shirabe_php_shim::realpath(&path),
            &mut output,
        )?;

        let mut log = String::new();
        let match_pattern = format!("/\\d\\d:\\d\\d:\\d\\d\\s+\\[{}\\]/", to_reference);

        let trimmed = output.trim().to_string();
        let lines: Vec<String> = if trimmed.is_empty() {
            vec![]
        } else {
            Preg::split(r"{\r?\n}", &trimmed)?
        };

        for line in lines {
            if Preg::is_match(&match_pattern, &line)? {
                break;
            }
            log.push_str(&line);
        }

        Ok(log)
    }

    fn execute(&self, command: Vec<String>, cwd: Option<String>, output: &mut String) -> Result<()> {
        if self.inner.process.execute(&command, output, cwd) != 0 {
            return Err(RuntimeException {
                message: format!(
                    "Failed to execute {}\n\n{}",
                    command.join(" "),
                    self.inner.process.get_error_output()
                ),
                code: 0,
            }.into());
        }
        Ok(())
    }

    pub(crate) fn has_metadata_repository(&self, path: &str) -> bool {
        std::path::Path::new(&format!("{}/.fslckout", path)).is_file()
            || std::path::Path::new(&format!("{}/_FOSSIL_", path)).is_file()
    }
}