From dc3859fe4d836c080f1b24a878fe18a80c85254d Mon Sep 17 00:00:00 2001
From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr>
Date: Tue, 10 May 2022 15:30:41 +0200
Subject: [PATCH] src/commit.rs: add a commit functionality

---
 src/commit.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 src/commit.rs

diff --git a/src/commit.rs b/src/commit.rs
new file mode 100644
index 0000000..9e2f08d
--- /dev/null
+++ b/src/commit.rs
@@ -0,0 +1,48 @@
+use crate::init;
+use std::process::{exit, Command};
+
+/// Get the current commit name of a git repository
+///
+/// # Return:
+/// * A string corresponding to the current commit id
+pub fn get_current_commit() -> String {
+    let output = Command::new("git")
+        .arg("rev-parse")
+        .arg("--verify")
+        .arg("HEAD")
+        .output()
+        .unwrap();
+    match output.status.code().unwrap() {
+        0 => (),
+        num => {
+            eprintln!("{}", String::from_utf8(output.stderr).unwrap());
+            exit(num);
+        }
+    }
+    let mut commit = String::from_utf8(output.stdout).unwrap();
+    commit.pop();
+    commit
+}
+
+/// Create a commit of the results folder named as the current git commit id
+pub fn commit(compression: String) {
+    let (borg_folder, results_folder) = init::get_borg_folder();
+    let commit = get_current_commit();
+    let output = Command::new("borg")
+        .arg("create")
+        .arg("--stats")
+        .arg("--progress")
+        .arg("--compression")
+        .arg(compression)
+        .arg(format!("{}::{}", borg_folder.to_str().unwrap(), commit))
+        .arg(results_folder.to_str().unwrap())
+        .output()
+        .unwrap();
+    match output.status.code().unwrap() {
+        0 => (),
+        num => {
+            eprintln!("{}", String::from_utf8(output.stderr).unwrap());
+            exit(num);
+        }
+    }
+}
-- 
GitLab