]> git.r.bdr.sh - rbdr/blog/blame_incremental - src/remote/git.rs
Add release
[rbdr/blog] / src / remote / git.rs
... / ...
CommitLineData
1use std::io::{Error, ErrorKind::Other, Result};
2use std::path::PathBuf;
3use std::process::{Command, Stdio};
4use std::time::{SystemTime, UNIX_EPOCH};
5
6pub struct Git;
7
8impl Git {
9 pub fn new() -> Self {
10 Git
11 }
12}
13
14impl super::Remote for Git {
15 fn can_handle(&self, _: &str) -> bool {
16 // If we ever implement another remote we will want a check strategy
17 true
18 }
19
20 fn sync_up(&self, remote: &str, directory: &PathBuf) -> Result<()> {
21 let timestamp = SystemTime::now().duration_since(UNIX_EPOCH)
22 .map_err(|_| Error::new(Other, "Invalid time"))?
23 .as_millis();
24
25 let commands = vec![
26 format!("cd {} && git init -b main", directory.display()),
27 format!("cd {} && git add .", directory.display()),
28 format!("cd {} && git commit --allow-empty -m blog-sync-up-{}", directory.display(), timestamp),
29 format!("cd {} && git push {} main --force", directory.display(), remote),
30 ];
31
32 for command in commands {
33 Command::new("sh")
34 .arg("-c")
35 .arg(&command)
36 .stdout(Stdio::null())
37 .stderr(Stdio::null())
38 .status()
39 .map_err(|_| Error::new(Other, "Failed while performing sync up with git"))?;
40 }
41
42 Ok(())
43 }
44
45 fn sync_down(&self, remote: &str, directory: &PathBuf) -> Result<()> {
46 let commands = vec![
47 format!("cd {} && git init -b main", directory.display()),
48 format!("cd {} && git checkout .", directory.display()),
49 format!("cd {} && git clean . -f", directory.display()),
50 format!("cd {} && git pull {} main", directory.display(), remote),
51 ];
52
53 for command in commands {
54 Command::new("sh")
55 .arg("-c")
56 .arg(&command)
57 .stdout(Stdio::null())
58 .stderr(Stdio::null())
59 .status()
60 .map_err(|_| Error::new(Other, "Failed while performing sync down with git"))?;
61 }
62 Ok(())
63 }
64}