Skip to content
Snippets Groups Projects
Commit d3a8d2da authored by nfontrod's avatar nfontrod
Browse files

src/checkout.rs: add pre-checkout fonctionality

parent 19231485
No related branches found
No related tags found
No related merge requests found
use crate::commit;
use std::{
path::PathBuf,
process::{exit, Command},
};
/// Check if two commits are differents
pub fn is_diff(commit1: &str, commit2: &str, borg_folder: &PathBuf) -> bool {
let archive1 = format!("{}::{}", borg_folder.to_str().unwrap(), commit1);
let cmd = format!("borg diff {} {} | wc -l", archive1, commit2);
let output = Command::new("sh").arg("-c").arg(cmd).output().unwrap();
match output.status.code().unwrap() {
0 => (),
num => {
eprintln!("{}", String::from_utf8(output.stderr).unwrap());
exit(num);
}
}
let mut strcode = String::from_utf8(output.stdout).unwrap();
if strcode.ends_with('\n') {
strcode.pop();
}
let res: i32 = strcode.parse().unwrap();
res > 0
}
/// Function that checks if the archive with the name of the current commit id
/// exits in borg repository
pub fn check_if_current_commit_exits(borg_path: &PathBuf, commit_id: &str) {
// Trying to create an archive with the current git commit id
let output = Command::new("borg")
.arg("list")
.arg(format!("{}::{}", borg_path.to_str().unwrap(), commit_id))
.output()
.unwrap();
match output.status.code().unwrap() {
0 => (),
num => {
let msg = format!("Archive {} does not exist", commit_id);
let mut output_msg = String::from_utf8(output.stderr).unwrap();
if output_msg.ends_with("\n") {
output_msg.pop();
}
if output_msg == msg {
eprintln!(
"No archive with the current commit id found !\n You should commit it first !"
);
exit(3);
} else {
eprintln!("{}", output_msg);
exit(num);
}
}
}
}
/// Function that prepare checkout. **This function should be used before a
/// git checkout !**
///
/// # Description:
/// This function will check if the archive named as the current commit exits.
/// If it doesn't exists, it will create one. If it exists, it will create a
/// temporary archive of the current result respository and compare it with
/// the archive with the current git commit id.
/// If it is the same as the temporary achive then you can make a
/// checkout without losing data. If not, then you might.
pub fn prepare_checkout() {
let (borg_path, _) = commit::check_path();
let commit_id = commit::get_current_commit();
check_if_current_commit_exits(&borg_path, &commit_id);
// Create an archive with the content of the current commit
let tmp_name = format!("{}-tmp", commit_id);
commit::commit(String::from("none"), tmp_name.clone());
let res = is_diff(&commit_id, &tmp_name, &borg_path);
let output = Command::new("borg")
.arg("delete")
.arg(format!("{}::{}", borg_path.to_str().unwrap(), tmp_name))
.output()
.unwrap();
match output.status.code().unwrap() {
0 => (),
num => {
eprintln!("{}", String::from_utf8(output.stderr).unwrap());
exit(num);
}
}
if res {
eprintln!("Your results folder contains unsaved changes!\nPlease update your current commit with: gbl commit --update");
exit(4);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment