Skip to content

Commit

Permalink
feat: resolve git file attrs
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Jun 10, 2024
1 parent b34adcc commit 307471d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
28 changes: 23 additions & 5 deletions fmt/src/document/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@
// 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<Mapping>,
definitions: HashMap<String, HeaderDef>,
properties: HashMap<String, String>,

keywords: Vec<String>,
git_context: GitContext,
}

impl DocumentFactory {
Expand All @@ -37,12 +40,14 @@ impl DocumentFactory {
definitions: HashMap<String, HeaderDef>,
properties: HashMap<String, String>,
keywords: Vec<String>,
git_context: GitContext,
) -> Self {
Self {
mapping,
definitions,
properties,
keywords,
git_context,
}
}

Expand Down Expand Up @@ -72,6 +77,7 @@ impl DocumentFactory {
.unwrap_or_else(|| Cow::Borrowed("<unknown>"))
.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(),
Expand All @@ -81,3 +87,15 @@ impl DocumentFactory {
)
}
}

fn resolve_git_file_attrs(
_git_context: &GitContext,
_path: &Path,
) -> Result<HashMap<String, String>> {
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)
}
10 changes: 6 additions & 4 deletions fmt/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
2 changes: 1 addition & 1 deletion fmt/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
12 changes: 9 additions & 3 deletions fmt/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ pub fn check_license_header<C: Callback>(run_config: PathBuf, callback: &mut C)
);

let git_context = git::discover(&basedir, config.git)?;

let selected_files = {
let selection = Selection::new(
basedir,
config.header_path.as_ref(),
&config.includes,
&config.excludes,
config.use_default_excludes,
git_context,
git_context.clone(),
);
selection.select()?
};
Expand Down Expand Up @@ -107,8 +108,13 @@ pub fn check_license_header<C: Callback>(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)? {
Expand Down
2 changes: 1 addition & 1 deletion fmt/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
6 changes: 4 additions & 2 deletions fmt/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 307471d

Please sign in to comment.