Skip to content

Leading comments in commit message are not ignored with --commit-file #454

@deckstose

Description

@deckstose

This happened to use while using commit message templates with the commit-msg hook.

To reproduce, create a file with a leading comment and lint it with --commit-file.

cat <<EOF > message
# I like ducks
chore: Add commit message template
EOF
cat <<EOF > committed.toml
style = "conventional"
EOF
git init .
committed --commit-file message
# msg: error Commit is not in Conventional format: Missing type in the commit summary, expected `type: description`

I'd expect comments to be ignored.

The below patch fixes it

diff --git a/crates/committed/src/checks.rs b/crates/committed/src/checks.rs
index f6b7ebf4b3..59d1ff7ac1 100644
--- a/crates/committed/src/checks.rs
+++ b/crates/committed/src/checks.rs
@@ -3,12 +3,18 @@
 
 pub(crate) fn check_message(
     source: report::Source<'_>,
-    mut message: &str,
+    message: &str,
     config: &crate::config::Config,
     report: report::Report,
 ) -> Result<bool, anyhow::Error> {
     let mut failed = false;
 
+    let message = message
+        .split('\n')
+        .filter(|line| !line.trim_start().starts_with('#'))
+        .collect::<String>();
+    let mut message = message.as_str();
+
     failed |= check_has_message(source, message, report)?;
     if failed {
         return Ok(failed);
diff --git a/crates/committed/tests/cmd.rs b/crates/committed/tests/cmd.rs
index 7d91fb5139..70fe2d8864 100644
--- a/crates/committed/tests/cmd.rs
+++ b/crates/committed/tests/cmd.rs
@@ -77,6 +77,21 @@
         .stderr_eq(str![]);
 }
 
+#[test]
+fn leading_comments_ignored() {
+    run_committed(
+        r#"
+# This is a nice comment
+
+
+
+chore: Add a test
+        "#,
+        r#"style = "conventional""#,
+    )
+    .success();
+}
+
 #[track_caller]
 fn run_committed(message: &str, config: &str) -> snapbox::cmd::OutputAssert {
     let root = snapbox::dir::DirRoot::mutable_temp().unwrap();
diff --git a/patch b/patch
new file mode 100644
index 0000000000..e69de29bb2

Tests pass, however I'm not 100% sure if this changes the meaning of the code.

Let me know what you think and I'm happy to open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions