From 4b2d46a94100fd04be966ede5e036745da8ac8e0 Mon Sep 17 00:00:00 2001 From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr> Date: Tue, 31 Jan 2023 17:14:00 +0100 Subject: [PATCH] src/init.rs: add functions to update gitignore --- src/init.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/init.rs b/src/init.rs index 157a1bf..dda3347 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,4 +1,7 @@ -use crate::create_hooks; +use colored::Colorize; + +use crate::{commit, create_hooks}; +use std::io::Write; use std::path::PathBuf; use std::process::{exit, Command}; @@ -67,6 +70,44 @@ fn init_repository() { } } +/// Function that returns the path to the .gitignore file +/// # Return +/// The path to the .gitignore file of the project +fn get_gitignore_file() -> PathBuf { + let (borg_folder, _) = commit::check_path(); + let borg_folder = borg_folder.canonicalize().unwrap(); + let mut gitignore = borg_folder.parent().unwrap().to_path_buf(); + gitignore.push(".gitignore"); + gitignore +} + +/// This function update the .gitignore file with folder created by gblk +/// # Arguments +/// - ` gitignore_file` : Path to the gitignore file +fn update_gitignore(gitignore_file: &PathBuf) -> () { + if !gitignore_file.is_file() { + std::fs::File::create(gitignore_file).expect(&format!( + "{}: Unable to create {} file", + "error".red(), + &gitignore_file.display() + )); + } + let mut giti = std::fs::OpenOptions::new() + .append(true) + .open(&gitignore_file) + .expect(&format!( + "{}: Unable to open {} file", + "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() + )); +} + /// Creation of a borg repository and creation of hooks if needed inside `.git/hooks` /// folder /// # Arguments: @@ -75,6 +116,8 @@ fn init_repository() { /// * `mode`: The checkout mode used by gblk automatically after a git checkout: soft or hard. pub fn init_and_hook(hooks: bool, compression: &str, mode: &str) { init_repository(); + let gitignore = get_gitignore_file(); + update_gitignore(&gitignore); if hooks { create_hooks::create_hooks(compression, mode); } -- GitLab