diff --git a/src/init.rs b/src/init.rs index cda14f939c49e2a83948ac23fe7c9abb0c61f2de..0c878ec892595632aa6b8a00b2f8e201159dad95 100644 --- a/src/init.rs +++ b/src/init.rs @@ -70,21 +70,24 @@ fn init_repository() { } } -/// Function that returns the path to the .gitignore file +/// Function that returns the path to the .gitignore and restuls/.gitignore file /// # Return -/// The path to the .gitignore file of the project -pub(crate) fn get_gitignore_file() -> PathBuf { - let (borg_folder, _) = commit::check_path(); +/// The path to the .gitignore file and results/.gitignore of the project +fn get_gitignore_file() -> (PathBuf, PathBuf) { + let (borg_folder, results) = commit::check_path(); let borg_folder = borg_folder.canonicalize().unwrap(); + let mut results = results.canonicalize().unwrap(); let mut gitignore = borg_folder.parent().unwrap().to_path_buf(); gitignore.push(".gitignore"); - gitignore + results.push(".gitignore"); + (gitignore, results) } /// This function update the .gitignore file with folder created by gblk /// # Arguments /// - ` gitignore_file` : Path to the gitignore file -pub(crate) fn update_gitignore(gitignore_file: &PathBuf) -> () { +/// - `ignore`: The string to add in gitignore file +fn update_gitignore(gitignore_file: &PathBuf, ignore: &str) -> () { if !gitignore_file.is_file() { std::fs::File::create(gitignore_file).expect(&format!( "{}: Unable to create {} file", @@ -100,12 +103,18 @@ pub(crate) fn update_gitignore(gitignore_file: &PathBuf) -> () { "error".red(), &gitignore_file.display() )); - giti.write_all("results/*\n!results/.gitignore\n.borg\n.mount\n.tmp\n".as_bytes()) - .expect(&format!( - "{}: Unable to write {} file", - "error".red(), - &gitignore_file.display() - )); + giti.write_all(ignore.as_bytes()).expect(&format!( + "{}: Unable to write {} file", + "error".red(), + &gitignore_file.display() + )); +} + +/// Update .gitignore and results/.gitignore +pub(crate) fn update_gitignores() { + let (gitignore, giti_res) = get_gitignore_file(); + update_gitignore(&gitignore, ".borg\n.mount\n.tmp\n"); + update_gitignore(&giti_res, "*\n!.gitignore"); } /// Creation of a borg repository and creation of hooks if needed inside `.git/hooks` @@ -117,8 +126,7 @@ pub(crate) fn update_gitignore(gitignore_file: &PathBuf) -> () { pub fn init_and_hook(hooks: bool, compression: &str, mode: &str) { init_repository(); configt::create_local_config(); - let gitignore = get_gitignore_file(); - update_gitignore(&gitignore); + update_gitignores(); if hooks { create_hooks::create_hooks(compression, mode); }