diff --git a/src/commit.rs b/src/commit.rs index 212d18198669af9c827dca236fd86cf4215efc95..71ae66d891967c93aab406960587c00e681534ce 100644 --- a/src/commit.rs +++ b/src/commit.rs @@ -1,4 +1,4 @@ -use crate::init; +use crate::{init, checkout}; use std::{ path::PathBuf, process::{exit, Command, Stdio}, @@ -68,6 +68,35 @@ pub fn check_if_borgignore_exists(borg_folder: &PathBuf) -> (bool, PathBuf) { (project_folder.is_file(), project_folder) } + +/// Function that checks if the archive with the name of the current commit id +/// exits in borg repository +pub fn is_a_commit(borg_path: &PathBuf, commit_id: &str) -> bool{ + // 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 => true, + _ => false + } +} + + +/// Revert the result folder back to the current commit archive +pub fn revert_commit() { + let commit_id = get_current_commit(); + let (borg_path, _) = check_path(); + if is_a_commit(&borg_path, &commit_id) { + checkout::checkout("hard"); + } else { + eprintln!("Cannot revert the content of the results folder previously saved for that commit: No archive found for this commit !"); + exit(17); + } +} + /// Create a commit of the results folder named as the current git commit id pub fn commit(compression: String, mut commit_id: String, update: bool) { let (borg_folder, results_folder) = check_path();