summaryrefslogtreecommitdiff
path: root/ofborg/tickborg/src/clone.rs
blob: 0dcb71c2c5dba8722eb38f6efe3d19a676e506ba (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
use fs2::FileExt;

use std::ffi::OsStr;
use std::fs;
use std::io::Error;
use std::path::PathBuf;
use std::process::{Command, Stdio};

use tracing::{debug, info, warn};

pub struct Lock {
    lock: Option<fs::File>,
}

impl Lock {
    pub fn unlock(&mut self) {
        self.lock = None
    }
}

pub trait GitClonable {
    fn clone_from(&self) -> String;
    fn clone_to(&self) -> PathBuf;
    fn extra_clone_args(&self) -> Vec<&OsStr>;

    fn lock_path(&self) -> PathBuf;

    fn lock(&self) -> Result<Lock, Error> {
        debug!("Locking {:?}", self.lock_path());

        match fs::File::create(self.lock_path()) {
            Err(e) => {
                warn!("Failed to create lock file {:?}: {}", self.lock_path(), e);
                Err(e)
            }
            Ok(lock) => match lock.lock_exclusive() {
                Err(e) => {
                    warn!(
                        "Failed to get exclusive lock on file {:?}: {}",
                        self.lock_path(),
                        e
                    );
                    Err(e)
                }
                Ok(_) => {
                    debug!("Got lock on {:?}", self.lock_path());
                    Ok(Lock { lock: Some(lock) })
                }
            },
        }
    }

    fn clone_repo(&self) -> Result<(), Error> {
        let mut lock = self.lock()?;

        if self.clone_to().is_dir() {
            debug!("Found dir at {:?}, initial clone is done", self.clone_to());
            return Ok(());
        }

        info!(
            "Initial cloning of {} to {:?}",
            self.clone_from(),
            self.clone_to()
        );

        let result = Command::new("git")
            .arg("clone")
            .args(self.extra_clone_args())
            .arg(self.clone_from())
            .arg(self.clone_to())
            .stdout(Stdio::null())
            .status()?;

        lock.unlock();

        if result.success() {
            Ok(())
        } else {
            Err(Error::other(format!(
                "Failed to clone from {:?} to {:?}",
                self.clone_from(),
                self.clone_to()
            )))
        }
    }

    fn fetch_repo(&self) -> Result<(), Error> {
        let mut lock = self.lock()?;

        info!("Fetching from origin in {:?}", self.clone_to());
        let result = Command::new("git")
            .arg("fetch")
            .arg("origin")
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .status()?;

        lock.unlock();

        if result.success() {
            Ok(())
        } else {
            Err(Error::other("Failed to fetch"))
        }
    }

    fn clean(&self) -> Result<(), Error> {
        let mut lock = self.lock()?;

        debug!("git am --abort");
        Command::new("git")
            .arg("am")
            .arg("--abort")
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()?;

        debug!("git merge --abort");
        Command::new("git")
            .arg("merge")
            .arg("--abort")
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()?;

        debug!("git reset --hard");
        Command::new("git")
            .arg("reset")
            .arg("--hard")
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .status()?;

        debug!("git clean -x -d --force");
        Command::new("git")
            .arg("clean")
            .arg("-x")
            .arg("-d")
            .arg("--force")
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .status()?;

        lock.unlock();

        Ok(())
    }

    fn checkout(&self, git_ref: &OsStr) -> Result<(), Error> {
        let mut lock = self.lock()?;

        debug!("git checkout {:?}", git_ref);
        let result = Command::new("git")
            .arg("checkout")
            // we don't care if its dirty
            .arg("--force")
            .arg(git_ref)
            .current_dir(self.clone_to())
            .stdout(Stdio::null())
            .status()?;

        lock.unlock();

        if result.success() {
            Ok(())
        } else {
            Err(Error::other("Failed to checkout"))
        }
    }
}