From 478687a31e094e1afac9f574017f227abdb96525 Mon Sep 17 00:00:00 2001 From: Linus-Mussmaecher Date: Thu, 6 Jun 2024 15:55:18 +0200 Subject: [PATCH] Added gitignore to tracking --- src/config.rs | 38 ++++++++++++++++++++++++++++++++------ src/data/notefile.rs | 1 - 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 661906e..69947cc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -67,6 +67,8 @@ pub struct Config { css_path: Option, /// Pre-calculated object containing allowed file types types: ignore::types::Types, + /// Pre-fetched gitignore file in the vault path, if found. + gitignore: Option, } impl Default for Config { @@ -80,6 +82,7 @@ impl Default for Config { .select("markdown") .build() .expect("Markdown is a valid file type configured in the DEFAULTs."), + gitignore: None, } } } @@ -90,7 +93,7 @@ impl Config { config_file: ConfigFile, uistyles: ui::UiStyles, ) -> Result { - // === Resolve css path === + // Resolve css path let mut css_path = None; if let Some(css) = &config_file.css { @@ -105,18 +108,27 @@ impl Config { css_path = Some(css); } - // === Pre-calculate allowed file types === + // Pre-calculate allowed file types let mut types_builder = ignore::types::TypesBuilder::new(); types_builder.add_defaults(); for name in config_file.file_types.iter() { types_builder.select(name); } + // Search and fetch gitignore + let gitignore_builder = ignore::gitignore::GitignoreBuilder::new( + config_file + .vault_path + .as_ref() + .unwrap_or(&path::PathBuf::from(".")), + ); + Ok(Self { config_file, uistyles, css_path, types: types_builder.build()?, + gitignore: gitignore_builder.build().ok(), }) } @@ -218,8 +230,8 @@ impl Config { ignore::WalkBuilder::new( self.config_file .vault_path - .clone() - .unwrap_or(path::PathBuf::from(".")), + .as_ref() + .unwrap_or(&path::PathBuf::from(".")), ) .types(self.types.clone()) .build() @@ -228,11 +240,25 @@ impl Config { /// Wether the given path is supposed to be tracked by rucola or not. /// Checks for file endings and (TODO) gitignore pub fn is_tracked(&self, path: &path::PathBuf) -> bool { - if let ignore::Match::Whitelist(_) = self.types.matched(path, false) { + let file_ending = if let ignore::Match::Whitelist(_) = self.types.matched(path, false) { true } else { false - } + }; + + let gitignore = self + .gitignore + .as_ref() + .map(|gi| { + if let ignore::Match::Ignore(_) = gi.matched(path, false) { + false + } else { + true + } + }) + .unwrap_or(true); + + return file_ending && gitignore; } /// Takes in a PathBuf and, if the current file extension is not set, append the default one. diff --git a/src/data/notefile.rs b/src/data/notefile.rs index 2c2232e..4ee19ec 100644 --- a/src/data/notefile.rs +++ b/src/data/notefile.rs @@ -13,7 +13,6 @@ pub fn rename_note_file( index: &mut super::NoteIndexContainer, id: &str, new_name: Option, - // TODO : remove this? config: &config::Config, ) -> Result<(), error::RucolaError> { let index_b = index.borrow_mut();