diff --git a/fmt/src/document/factory.rs b/fmt/src/document/factory.rs index 9537bb4..0bea1fb 100644 --- a/fmt/src/document/factory.rs +++ b/fmt/src/document/factory.rs @@ -19,7 +19,7 @@ use std::path::Path; use std::path::PathBuf; use anyhow::Context; -use gix::date::time::format; +use gix::date::time::CustomFormat; use crate::config::Mapping; use crate::document::Document; @@ -79,15 +79,14 @@ impl DocumentFactory { properties.insert("hawkeye.core.filename".to_string(), filename); if let Some(attrs) = self.git_file_attrs.get(filepath) { + const YEAR_FORMAT: CustomFormat = CustomFormat::new("%Y"); properties.insert( "hawkeye.git.fileCreatedYear".to_string(), - // TODO(tisonkun): hack until git-date provide external constructor for - // CustomFormat. - attrs.created_time.format(format::SHORT)[0..4].to_string(), + attrs.created_time.format(YEAR_FORMAT), ); properties.insert( "hawkeye.git.fileModifiedYear".to_string(), - attrs.modified_time.format(format::SHORT)[0..4].to_string(), + attrs.modified_time.format(YEAR_FORMAT), ); } diff --git a/fmt/src/header/parser.rs b/fmt/src/header/parser.rs index b562cc7..eadd407 100644 --- a/fmt/src/header/parser.rs +++ b/fmt/src/header/parser.rs @@ -75,7 +75,19 @@ fn find_first_position( file_content: &mut FileContent, header_def: &HeaderDef, ) -> usize { + const UTF8_BOM: [u8; 3] = [0xEF, 0xBB, 0xBF]; + let mut begin_pos = 0; + + if let Some(l) = line.as_ref() { + // skip UTF-8 BOM if exists + if l.as_bytes().starts_with(&UTF8_BOM) { + log::debug!("Detected UTF-8 BOM for {}; skip", file_content); + begin_pos = 3; + file_content.reset_to(3); + } + } + if header_def.skip_line_pattern.is_some() { // the format expect to find lines to be skipped while line @@ -104,8 +116,17 @@ fn find_first_position( begin_pos = 0; file_content.reset(); *line = file_content.next_line(); + + // recheck for UTF-8 BOM + if let Some(l) = line.as_ref() { + if l.as_bytes().starts_with(&UTF8_BOM) { + begin_pos = 3; + file_content.reset_to(3); + } + } } } + begin_pos } diff --git a/licenserc.toml b/licenserc.toml index bf41637..d641698 100644 --- a/licenserc.toml +++ b/licenserc.toml @@ -22,6 +22,7 @@ excludes = [ "fmt/tests/content/**", "tests/load_header_path/**", "tests/regression_blank_line/**", + "tests/bom_issue/**", # Generated files ".github/workflows/release.yml", diff --git a/tests/bom_issue/headerless_bom.cs b/tests/bom_issue/headerless_bom.cs new file mode 100644 index 0000000..b55135b --- /dev/null +++ b/tests/bom_issue/headerless_bom.cs @@ -0,0 +1,11 @@ +using System; + +namespace Some.Fake.Namespace; + +public class SomeFakeClass +{ + public void SomeFakeMethod() + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/tests/bom_issue/headerless_bom.cs.expected b/tests/bom_issue/headerless_bom.cs.expected new file mode 100644 index 0000000..ccf4728 --- /dev/null +++ b/tests/bom_issue/headerless_bom.cs.expected @@ -0,0 +1,15 @@ +// ------------------------------------------------------- +// Static Copyright +// ------------------------------------------------------- + +using System; + +namespace Some.Fake.Namespace; + +public class SomeFakeClass +{ + public void SomeFakeMethod() + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/tests/bom_issue/license.txt b/tests/bom_issue/license.txt new file mode 100644 index 0000000..d0118b8 --- /dev/null +++ b/tests/bom_issue/license.txt @@ -0,0 +1 @@ +Static Copyright \ No newline at end of file diff --git a/tests/bom_issue/licenserc.toml b/tests/bom_issue/licenserc.toml new file mode 100644 index 0000000..767fc59 --- /dev/null +++ b/tests/bom_issue/licenserc.toml @@ -0,0 +1,11 @@ +baseDir = "." +headerPath = "license.txt" + +excludes = ["*.toml", "*.expected"] + +additionalHeaders = [ + "style.toml" +] + +[mapping.HASH_SOURCE_STYLE] +extensions = ["cs"] diff --git a/tests/bom_issue/style.toml b/tests/bom_issue/style.toml new file mode 100644 index 0000000..72d8013 --- /dev/null +++ b/tests/bom_issue/style.toml @@ -0,0 +1,9 @@ +[HASH_SOURCE_STYLE] +firstLine = "// -------------------------------------------------------" +endLine = "// -------------------------------------------------------\n" +skipLinePattern = "^#!.*$" +allowBlankLines = false +multipleLines = false +beforeEachLine = "// " +firstLineDetectionPattern = "//\\s+\\-+" +lastLineDetectionPattern = "//\\s+\\-+" diff --git a/tests/it.py b/tests/it.py index e33eba7..3ee5cfe 100755 --- a/tests/it.py +++ b/tests/it.py @@ -38,3 +38,6 @@ def diff_files(file1, file2): subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=(basedir / "regression_blank_line"), check=True) diff_files(basedir / "regression_blank_line" / "main.rs.expected", basedir / "regression_blank_line" / "main.rs.formatted") + +subprocess.run([hawkeye, "format", "--fail-if-unknown", "--fail-if-updated=false", "--dry-run"], cwd=(basedir / "bom_issue"), check=True) +diff_files(basedir / "bom_issue" / "headerless_bom.cs.expected", basedir / "bom_issue" / "headerless_bom.cs.formatted")