From 2c050d31a79c86516d24cbfc195aaee11d94f736 Mon Sep 17 00:00:00 2001
From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr>
Date: Fri, 10 Jun 2022 14:32:21 +0200
Subject: [PATCH] src/commit.rs: add handle for .borgignore file

---
 src/commit.rs | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/src/commit.rs b/src/commit.rs
index f6afdbb..d7c229a 100644
--- a/src/commit.rs
+++ b/src/commit.rs
@@ -53,23 +53,48 @@ pub fn delete_commit(commit: &str, borg_path: &PathBuf) {
     }
 }
 
+/// Checks if .borgignore file exists in the project folder
+///
+/// # Arguments:
+/// * borg_folder: Path to .borg folder
+///
+/// # Return:
+/// A tuple containing:
+/// * A boolean indicating if .borgignore file exits
+/// * The path to this borgignore file
+fn check_if_borgignore_exists(borg_folder: &PathBuf) -> (bool, PathBuf) {
+    let mut project_folder = borg_folder.parent().unwrap().to_path_buf();
+    project_folder.push(".borgignore");
+    (project_folder.is_file(), project_folder)
+}
+
 /// 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();
+    let (has_ignore, borgignore) = check_if_borgignore_exists(&borg_folder);
     if commit_id == String::from("") {
         commit_id = get_current_commit();
     }
     if update {
         delete_commit(&commit_id, &borg_folder);
     }
+
+    let mut margs: Vec<&str> = vec![
+        "create",
+        "--stats",
+        "--progress",
+        "--compression",
+        &compression,
+    ];
+    if has_ignore {
+        margs.push("--exclude-from");
+        margs.push(borgignore.to_str().unwrap());
+    }
+    let cmd_part = format!("{}::{}", borg_folder.to_str().unwrap(), commit_id);
+    margs.push(&cmd_part);
+    margs.push(results_folder.to_str().unwrap());
     let mut output = Command::new("borg")
-        .arg("create")
-        .arg("--stats")
-        .arg("--progress")
-        .arg("--compression")
-        .arg(compression)
-        .arg(format!("{}::{}", borg_folder.to_str().unwrap(), commit_id))
-        .arg(results_folder.to_str().unwrap())
+        .args(margs)
         .stdout(Stdio::piped())
         .spawn()
         .unwrap_or_else(|e| {
@@ -77,7 +102,7 @@ pub fn commit(compression: String, mut commit_id: String, update: bool) {
             exit(8);
         });
     let ecode = output.wait().expect("error");
-    match ecode.code().unwrap() { 
+    match ecode.code().unwrap() {
         0 => (),
         num => {
             eprintln!("{}", ecode.to_string());
-- 
GitLab