Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, Option<String>, Option<String>) {
let re =
regex::Regex::new(r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(!)?\:\s(?P<description>.+)$")
.unwrap();
let re = regex::Regex::new(
r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(?:!)?\:\s?(?P<description>.*)$",
)
.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());
Expand Down Expand Up @@ -232,7 +233,6 @@ Name: Keke";
)
);
}


#[test]
fn test_parse_subject_with_emphasized_type_without_scope() {
Expand All @@ -246,11 +246,39 @@ 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_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 = "";
assert_eq!(parse_subject(input), (None, None, Some("".to_string())));
}

#[test]
fn test_parse_subject_with_error_message() {
let input = "test";
Expand Down
20 changes: 15 additions & 5 deletions src/rule/description_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}
}
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/rule/scope_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}
}
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/rule/type_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}
}
}
}

Expand Down