-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.rs
66 lines (55 loc) · 2.07 KB
/
utils.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
use log::debug;
use tokio::process::Command;
use coffee_lib::errors::CoffeeError;
use coffee_lib::macros::error;
use coffee_lib::url::URL;
use coffee_lib::{commit_id, get_repo_info, sh};
use coffee_lib::types::response::UpgradeStatus;
pub async fn clone_recursive_fix(repo: git2::Repository, url: &URL) -> Result<(), CoffeeError> {
let repository = repo.submodules().unwrap_or_default();
debug!("submodule count: {}", repository.len());
for (index, sub) in repository.iter().enumerate() {
debug!("url {}: {}", index + 1, sub.url().unwrap());
let path = format!("{}/{}", &url.path_string, sub.path().to_str().unwrap());
match git2::Repository::clone(sub.url().unwrap(), &path) {
// Fix error handling
Ok(_) => {
debug!("added {}", sub.url().unwrap());
debug!("at path {}", &path);
Ok(())
}
Err(err) => Err(error!("{}", err.message())),
}?;
}
Ok(())
}
pub async fn git_upgrade(
path: &str,
branch: &str,
verbose: bool,
) -> Result<UpgradeStatus, CoffeeError> {
let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?;
let (local_commit, _) = get_repo_info!(repo);
let mut cmd = format!("git fetch origin\n");
cmd += &format!("git reset --hard origin/{branch}");
sh!(path, cmd, verbose);
let (upstream_commit, date) = get_repo_info!(repo);
if local_commit == upstream_commit {
Ok(UpgradeStatus::UpToDate(upstream_commit, date))
} else {
Ok(UpgradeStatus::Updated(upstream_commit, date))
}
}
pub async fn git_checkout(
path: &str,
branch: &str,
verbose: bool,
) -> Result<(String, String), CoffeeError> {
let mut cmd = format!("git fetch origin\n");
cmd += &format!("git reset --hard\n");
cmd += &format!("git checkout origin/{branch}");
sh!(path, cmd, verbose);
let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?;
let (upstream_commit, date) = get_repo_info!(repo);
Ok((upstream_commit, date))
}