From 32abae1ec8c5d2ff7f89cf3a88acab09f60b6a46 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 23 Apr 2024 20:26:50 +0200 Subject: [PATCH 1/3] fix(cli): description empty parse panic Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/git.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/git.rs b/src/git.rs index 01ad22f..4fa3c95 100644 --- a/src/git.rs +++ b/src/git.rs @@ -119,9 +119,10 @@ pub fn parse_commit_message( /// does not have any rules for it. /// See: https://commitlint.js.org/#/reference-rules pub fn parse_subject(subject: &str) -> (Option, Option, Option) { - let re = - regex::Regex::new(r"^(?P\w+)(?:\((?P[^\)]+)\))?(!)?\:\s(?P.+)$") - .unwrap(); + let re = regex::Regex::new( + r"^(?P\w+)(?:\((?P[^\)]+)\))?(?:!)?\:\s?(?P.*)$", + ) + .unwrap(); if let Some(captures) = re.captures(subject) { let r#type = captures.name("type").map(|m| m.as_str().to_string()); let scope = captures.name("scope").map(|m| m.as_str().to_string()); @@ -232,7 +233,6 @@ Name: Keke"; ) ); } - #[test] fn test_parse_subject_with_emphasized_type_without_scope() { @@ -246,11 +246,26 @@ Name: Keke"; ) ); } + + #[test] + fn test_parse_subject_with_empty_description() { + let input = "feat(cli): "; + assert_eq!( + parse_subject(input), + ( + Some("feat".to_string()), + Some("cli".to_string()), + Some("".to_string()) + ) + ); + } + #[test] fn test_parse_subject_without_message() { let input = ""; assert_eq!(parse_subject(input), (None, None, Some("".to_string()))); } + #[test] fn test_parse_subject_with_error_message() { let input = "test"; From fe0f4fa4ccc14ba054d2ddd25d914e0bbc1d3797 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 23 Apr 2024 20:29:30 +0200 Subject: [PATCH 2/3] fix(cli): scope empty parse error Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/git.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/git.rs b/src/git.rs index 4fa3c95..0c0a9ae 100644 --- a/src/git.rs +++ b/src/git.rs @@ -260,6 +260,19 @@ Name: Keke"; ); } + #[test] + fn test_parse_subject_with_empty_scope() { + let input = "feat: add dummy commit"; + assert_eq!( + parse_subject(input), + ( + Some("feat".to_string()), + None, + Some("add dummy commit".to_string()) + ) + ); + } + #[test] fn test_parse_subject_without_message() { let input = ""; From 0fa566388904a0a9b5ed30a07928fe17a54891a4 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 23 Apr 2024 20:31:56 +0200 Subject: [PATCH 3/3] chore(cli): error handle format field getter Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/rule/description_format.rs | 20 +++++++++++++++----- src/rule/scope_format.rs | 20 +++++++++++++++----- src/rule/type_format.rs | 20 +++++++++++++++----- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/rule/description_format.rs b/src/rule/description_format.rs index 1f948a1..070d315 100644 --- a/src/rule/description_format.rs +++ b/src/rule/description_format.rs @@ -40,11 +40,21 @@ impl Rule for DescriptionFormat { } }; - if !regex.is_match(&message.description.as_ref().unwrap()) { - return Some(Violation { - level: self.level.unwrap_or(Self::LEVEL), - message: self.message(message), - }); + match &message.description { + None => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: "found no description".to_string(), + }); + } + Some(description) => { + if !regex.is_match(description) { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: self.message(message), + }); + } + } } } diff --git a/src/rule/scope_format.rs b/src/rule/scope_format.rs index d446168..9889bed 100644 --- a/src/rule/scope_format.rs +++ b/src/rule/scope_format.rs @@ -40,11 +40,21 @@ impl Rule for ScopeFormat { } }; - if !regex.is_match(&message.scope.as_ref().unwrap()) { - return Some(Violation { - level: self.level.unwrap_or(Self::LEVEL), - message: self.message(message), - }); + match &message.scope { + None => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: "found no description".to_string(), + }); + } + Some(description) => { + if !regex.is_match(description) { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: self.message(message), + }); + } + } } } diff --git a/src/rule/type_format.rs b/src/rule/type_format.rs index 3d1a1e4..259a3ee 100644 --- a/src/rule/type_format.rs +++ b/src/rule/type_format.rs @@ -40,11 +40,21 @@ impl Rule for TypeFormat { } }; - if !regex.is_match(&message.r#type.as_ref().unwrap()) { - return Some(Violation { - level: self.level.unwrap_or(Self::LEVEL), - message: self.message(message), - }); + match &message.r#type { + None => { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: "found no type".to_string(), + }); + } + Some(description) => { + if !regex.is_match(description) { + return Some(Violation { + level: self.level.unwrap_or(Self::LEVEL), + message: self.message(message), + }); + } + } } }