Skip to content

Commit

Permalink
fix(cli): fallback to description when no type is specified
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <[email protected]>
  • Loading branch information
KeisukeYamashita committed Feb 13, 2024
1 parent 9d74fa0 commit 30a5e69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
26 changes: 15 additions & 11 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn parse_commit_message(
/// Note that exclamation mark is not respected as the existing commitlint
/// does not have any rules for it.
/// See: https://commitlint.js.org/#/reference-rules
pub fn parse_subject(subject: &str) -> Option<(String, Option<String>, String)> {
pub fn parse_subject(subject: &str) -> (String, Option<String>, String) {
let re =
regex::Regex::new(r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(!)?\:\s(?P<description>.+)$")
.unwrap();
Expand All @@ -127,9 +127,10 @@ pub fn parse_subject(subject: &str) -> Option<(String, Option<String>, String)>
let scope = captures.name("scope").map(|m| m.as_str().to_string());
let description = captures.name("description").unwrap().as_str().to_string();

return Some((r#type, scope, description));
return (r#type, scope, description);
}
None
// Fall back to the description.
("".to_string(), None, subject.to_string())
}

#[cfg(test)]
Expand Down Expand Up @@ -198,11 +199,11 @@ Name: Keke";
let input = "feat(cli): add dummy option";
assert_eq!(
parse_subject(input),
Some((
(
"feat".to_string(),
Some("cli".to_string()),
"add dummy option".to_string()
))
)
);
}

Expand All @@ -211,11 +212,11 @@ Name: Keke";
let input = "feat(cli)!: add dummy option";
assert_eq!(
parse_subject(input),
Some((
(
"feat".to_string(),
Some("cli".to_string()),
"add dummy option".to_string()
))
)
);
}

Expand All @@ -224,7 +225,7 @@ Name: Keke";
let input = "feat: add dummy option";
assert_eq!(
parse_subject(input),
Some(("feat".to_string(), None, "add dummy option".to_string()))
("feat".to_string(), None, "add dummy option".to_string())
);
}

Expand All @@ -233,17 +234,20 @@ Name: Keke";
let input = "feat!: add dummy option";
assert_eq!(
parse_subject(input),
Some(("feat".to_string(), None, "add dummy option".to_string()))
("feat".to_string(), None, "add dummy option".to_string())
);
}
#[test]
fn test_parse_subject_without_message() {
let input = "";
assert_eq!(parse_subject(input), None);
assert_eq!(parse_subject(input), ("".to_string(), None, "".to_string()));
}
#[test]
fn test_parse_subject_with_error_message() {
let input = "test";
assert_eq!(parse_subject(input), None);
assert_eq!(
parse_subject(input),
("".to_string(), None, "test".to_string())
);
}
}
28 changes: 9 additions & 19 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,15 @@ impl Message {
/// Create a new Message.
pub fn new(raw: String) -> Self {
let (subject, body, footers) = parse_commit_message(&raw);
match parse_subject(&subject) {
Some((r#type, scope, description)) => Self {
body,
description: Some(description),
footers,
raw,
r#type: Some(r#type),
scope,
subject: Some(subject),
},
None => Self {
raw,
description: None,
r#type: None,
scope: None,
body,
footers,
subject: Some(subject),
},
let (r#type, scope, description) = parse_subject(&subject);
Self {
body,
description: Some(description),
footers,
raw,
r#type: Some(r#type),
scope,
subject: Some(subject),
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/rule/type_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Rule for TypeEmpty {
}

fn validate(&self, message: &Message) -> Option<Violation> {
if message.r#type.is_none() {
if message.r#type.is_none() || message.r#type.as_ref().unwrap().is_empty() {
return Some(Violation {
level: self.level.unwrap_or(Self::LEVEL),
message: self.message(message),
Expand Down Expand Up @@ -79,9 +79,6 @@ mod tests {
let violation = rule.validate(&message);
assert!(violation.is_some());
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(
violation.unwrap().message,
"type is empty".to_string()
);
assert_eq!(violation.unwrap().message, "type is empty".to_string());
}
}

0 comments on commit 30a5e69

Please sign in to comment.