cargo_mobile2/util/git/
repo.rs

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
use crate::util::{self, Git};
use std::{
    ffi::OsStr,
    fmt::{self, Display},
    io,
    path::{Path, PathBuf},
};

#[derive(Debug)]
pub enum Error {
    NoHomeDir(util::NoHomeDir),
    FetchFailed(std::io::Error),
    RevParseLocalFailed(std::io::Error),
    RevParseRemoteFailed(std::io::Error),
    LogFailed(std::io::Error),
    ParentDirCreationFailed { path: PathBuf, cause: io::Error },
    CloneFailed(std::io::Error),
    ResetFailed(std::io::Error),
    CleanFailed(std::io::Error),
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoHomeDir(err) => write!(f, "{}", err),
            Self::FetchFailed(err) => write!(f, "Failed to fetch repo: {}", err),
            Self::RevParseLocalFailed(err) => write!(f, "Failed to get checkout revision: {}", err),
            Self::RevParseRemoteFailed(err) => {
                write!(f, "Failed to get upstream revision: {}", err)
            }
            Self::LogFailed(err) => write!(f, "Failed to get commit log: {}", err),
            Self::ParentDirCreationFailed { path, cause } => {
                write!(f, "Failed to create parent directory {:?}: {}", path, cause)
            }
            Self::CloneFailed(err) => write!(f, "Failed to clone repo: {}", err),
            Self::ResetFailed(err) => write!(f, "Failed to reset repo: {}", err),
            Self::CleanFailed(err) => write!(f, "Failed to clean repo: {}", err),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Status {
    Stale,
    Fresh,
}

impl Status {
    pub fn stale(self) -> bool {
        matches!(self, Self::Stale)
    }

    pub fn fresh(self) -> bool {
        matches!(self, Self::Fresh)
    }
}

#[derive(Clone, Debug)]
pub struct Repo {
    path: PathBuf,
}

impl Repo {
    pub fn from_path(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    pub fn checkouts_dir(checkout: impl AsRef<Path>) -> Result<Self, util::NoHomeDir> {
        util::checkouts_dir()
            .map(|dir| dir.join(checkout))
            .map(Self::from_path)
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn git(&self) -> Git<'_> {
        Git::new(self.path())
    }

    pub fn status(&self) -> Result<Status, Error> {
        let status = if !self.path().is_dir() {
            Status::Stale
        } else {
            let git = self.git();
            git.command_parse("fetch origin")
                .run()
                .map_err(Error::FetchFailed)?;
            let local = git
                .command_parse("rev-parse HEAD")
                .stderr_capture()
                .stdout_capture()
                .run()
                .map_err(Error::RevParseLocalFailed)?;
            let remote = git
                .command_parse("rev-parse @{u}")
                .stderr_capture()
                .stdout_capture()
                .run()
                .map_err(Error::RevParseRemoteFailed)?;
            if local.stdout != remote.stdout {
                Status::Stale
            } else {
                Status::Fresh
            }
        };
        Ok(status)
    }

    pub fn latest_subject(&self) -> Result<String, Error> {
        self.git()
            .command_parse("log -1 --pretty=%s")
            .read()
            .map(|s| s.trim().to_owned())
            .map_err(Error::LogFailed)
    }

    pub fn latest_hash(&self) -> Result<String, Error> {
        self.git()
            .command_parse("log -1 --pretty=%H")
            .read()
            .map(|s| s.trim().to_owned())
            .map_err(Error::LogFailed)
    }

    pub fn update(&self, url: impl AsRef<OsStr>, branch: &str) -> Result<(), Error> {
        let path = self.path();
        if !path.is_dir() {
            let parent = self
                .path()
                .parent()
                .expect("developer error: `Repo` path was at root");
            if !parent.is_dir() {
                std::fs::create_dir_all(parent).map_err(|cause| {
                    Error::ParentDirCreationFailed {
                        path: parent.to_owned(),
                        cause,
                    }
                })?;
            }
            Git::new(parent)
                .command_parse(format!(
                    "clone --depth 1 --single-branch {} {}",
                    url.as_ref().to_string_lossy(),
                    path.to_string_lossy()
                ))
                .run()
                .map_err(Error::CloneFailed)?;
        } else {
            println!(
                "Updating `{}` repo...",
                Path::new(
                    self.path()
                        .file_name()
                        .expect("developer error: `Repo` path had no file name")
                )
                .display()
            );
            self.git()
                .command_parse("fetch --depth 1")
                .run()
                .map_err(Error::FetchFailed)?;
            self.git()
                .command_parse(format!("reset --hard origin/{branch}"))
                .run()
                .map_err(Error::ResetFailed)?;
            self.git()
                .command_parse("clean -dfx --exclude /target")
                .run()
                .map_err(Error::CleanFailed)?;
        }
        Ok(())
    }
}