diff --git a/fmt/src/document/factory.rs b/fmt/src/document/factory.rs index 42b0c4a..d577250 100644 --- a/fmt/src/document/factory.rs +++ b/fmt/src/document/factory.rs @@ -12,16 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -use snafu::ResultExt; -use std::borrow::Cow; use std::{ + borrow::Cow, collections::{HashMap, HashSet}, path::Path, }; -use crate::error::CreateDocumentSnafu; -use crate::Result; -use crate::{config::Mapping, document::Document, header::model::HeaderDef}; +use snafu::ResultExt; + +use crate::{ + config::Mapping, document::Document, error::CreateDocumentSnafu, git::GitContext, + header::model::HeaderDef, Result, +}; pub struct DocumentFactory { mapping: HashSet, @@ -29,6 +31,7 @@ pub struct DocumentFactory { properties: HashMap, keywords: Vec, + git_context: GitContext, } impl DocumentFactory { @@ -37,12 +40,14 @@ impl DocumentFactory { definitions: HashMap, properties: HashMap, keywords: Vec, + git_context: GitContext, ) -> Self { Self { mapping, definitions, properties, keywords, + git_context, } } @@ -72,6 +77,7 @@ impl DocumentFactory { .unwrap_or_else(|| Cow::Borrowed("")) .to_string(); properties.insert("hawkeye.core.filename".to_string(), filename); + properties.extend(resolve_git_file_attrs(&self.git_context, filepath)?); Document::new( filepath.to_path_buf(), @@ -81,3 +87,15 @@ impl DocumentFactory { ) } } + +fn resolve_git_file_attrs( + _git_context: &GitContext, + _path: &Path, +) -> Result> { + let properties = HashMap::new(); + + // TODO - implement these: + // properties.insert("hawkeye.git.file_created".to_string(), ...); + // properties.insert("hawkeye.git.file_modified".to_string(), ...); + Ok(properties) +} diff --git a/fmt/src/document/mod.rs b/fmt/src/document/mod.rs index b92c45e..c1cba0a 100644 --- a/fmt/src/document/mod.rs +++ b/fmt/src/document/mod.rs @@ -17,11 +17,13 @@ use std::{collections::HashMap, fs, fs::File, io::BufRead, path::PathBuf}; use snafu::ResultExt; use tracing::debug; -use crate::error::CreateDocumentSnafu; -use crate::header::parser::{parse_header, FileContent}; use crate::{ - error::SaveDocumentSnafu, - header::{matcher::HeaderMatcher, model::HeaderDef, parser::HeaderParser}, + error::{CreateDocumentSnafu, SaveDocumentSnafu}, + header::{ + matcher::HeaderMatcher, + model::HeaderDef, + parser::{parse_header, FileContent, HeaderParser}, + }, Result, }; diff --git a/fmt/src/git.rs b/fmt/src/git.rs index 9848f67..fd83ad5 100644 --- a/fmt/src/git.rs +++ b/fmt/src/git.rs @@ -18,9 +18,9 @@ use gix::Repository; use snafu::IntoError; use tracing::info; -use crate::config::FeatureGate; use crate::{ config, + config::FeatureGate, error::{GixDiscoverOpSnafu, InvalidConfigSnafu}, Result, }; diff --git a/fmt/src/processor.rs b/fmt/src/processor.rs index 295c1cd..b4c361b 100644 --- a/fmt/src/processor.rs +++ b/fmt/src/processor.rs @@ -68,6 +68,7 @@ pub fn check_license_header(run_config: PathBuf, callback: &mut C) ); let git_context = git::discover(&basedir, config.git)?; + let selected_files = { let selection = Selection::new( basedir, @@ -75,7 +76,7 @@ pub fn check_license_header(run_config: PathBuf, callback: &mut C) &config.includes, &config.excludes, config.use_default_excludes, - git_context, + git_context.clone(), ); selection.select()? }; @@ -107,8 +108,13 @@ pub fn check_license_header(run_config: PathBuf, callback: &mut C) HeaderMatcher::new(header_source.content) }; - let document_factory = - DocumentFactory::new(mapping, definitions, config.properties, config.keywords); + let document_factory = DocumentFactory::new( + mapping, + definitions, + config.properties, + config.keywords, + git_context, + ); for file in selected_files { let document = match document_factory.create_document(&file)? { diff --git a/fmt/src/selection.rs b/fmt/src/selection.rs index 327adb2..eb11d3c 100644 --- a/fmt/src/selection.rs +++ b/fmt/src/selection.rs @@ -19,12 +19,12 @@ use snafu::{ensure, ResultExt}; use tracing::debug; use walkdir::WalkDir; -use crate::git::GitContext; use crate::{ error::{ GixCheckExcludeOpSnafu, GixExcludeOpSnafu, ResolveAbsolutePathSnafu, SelectFilesSnafu, SelectWithIgnoreSnafu, TraverseDirSnafu, }, + git::GitContext, Result, }; diff --git a/fmt/tests/tests.rs b/fmt/tests/tests.rs index 2097e3d..1b08fd3 100644 --- a/fmt/tests/tests.rs +++ b/fmt/tests/tests.rs @@ -14,8 +14,10 @@ use std::path::Path; -use hawkeye_fmt::header::parser::FileContent; -use hawkeye_fmt::header::{model::default_headers, parser::parse_header}; +use hawkeye_fmt::header::{ + model::default_headers, + parser::{parse_header, FileContent}, +}; #[test] fn test_remove_file_only_header() {