Skip to content

Commit

Permalink
fix: issue orhun#50
Browse files Browse the repository at this point in the history
  • Loading branch information
ofsahof committed Jun 14, 2022
1 parent 7d0786c commit 3c286db
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
16 changes: 10 additions & 6 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ pub struct Commit<'a> {
pub conv: Option<ConventionalCommit<'a>>,
/// Commit group based on a commit parser or its conventional type.
pub group: Option<String>,
/// Commit scope based on conventional type or a commit parser.
pub scope: Option<String>,
/// Default commit scope based on(inherited from) conventional type or a commit parser.
pub default_scope: Option<String>,
/// commit scope for overriding default one
pub scope: Option<String>,
/// A list of links found in the commit
pub links: Vec<Link>,
}
Expand Down Expand Up @@ -63,6 +65,7 @@ impl Commit<'_> {
message,
conv: None,
group: None,
default_scope: None,
scope: None,
links: vec![],
}
Expand Down Expand Up @@ -159,7 +162,7 @@ impl Commit<'_> {
if regex.is_match(&text) {
if parser.skip != Some(true) {
self.group = parser.group.as_ref().cloned();
self.scope = parser.default_scope.as_ref().cloned();
self.default_scope = parser.default_scope.as_ref().cloned();
return Ok(self);
} else {
return Err(AppError::GroupError(String::from(
Expand Down Expand Up @@ -236,13 +239,13 @@ impl Serialize for Commit<'_> {
commit.serialize_field("breaking", &conv.breaking())?;
commit.serialize_field(
"scope",
&conv.scope().map(|v| v.as_str()).or(self.scope.as_deref()),
&self.scope.as_deref().or_else(|| conv.scope().map(|v| v.as_str())).or_else(|| self.default_scope.as_deref())
)?;
}
None => {
commit.serialize_field("message", &self.message)?;
commit.serialize_field("group", &self.group)?;
commit.serialize_field("scope", &self.scope)?;
commit.serialize_field("scope", &self.scope.as_deref().or_else(|| self.default_scope.as_deref()))?;
}
}
commit.serialize_field("links", &self.links)?;
Expand Down Expand Up @@ -282,13 +285,14 @@ mod test {
body: None,
group: Some(String::from("test_group")),
default_scope: Some(String::from("test_scope")),
scope: None,
skip: None,
}],
false,
)
.unwrap();
assert_eq!(Some(String::from("test_group")), commit.group);
assert_eq!(Some(String::from("test_scope")), commit.scope);
assert_eq!(Some(String::from("test_scope")), commit.default_scope);
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ pub struct CommitParser {
pub body: Option<Regex>,
/// Group of the commit.
pub group: Option<String>,
/// Scope of the commit.
/// Default scope of the commit.
pub default_scope: Option<String>,
/// scope of the commit for overriding default one
pub scope: Option<String>,
/// Whether to skip this commit group.
pub skip: Option<bool>,
}
Expand Down
2 changes: 2 additions & 0 deletions git-cliff-core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ fn generate_changelog() -> Result<()> {
body: None,
group: Some(String::from("shiny features")),
default_scope: None,
scope: None,
skip: None,
},
CommitParser {
message: Regex::new("^fix").ok(),
body: None,
group: Some(String::from("fix bugs")),
default_scope: None,
scope: None,
skip: None,
},
]),
Expand Down
4 changes: 4 additions & 0 deletions git-cliff/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,27 +204,31 @@ mod test {
body: None,
group: Some(String::from("New features")),
default_scope: Some(String::from("other")),
scope: None,
skip: None,
},
CommitParser {
message: Regex::new("fix*").ok(),
body: None,
group: Some(String::from("Bug Fixes")),
default_scope: None,
scope: None,
skip: None,
},
CommitParser {
message: Regex::new("merge*").ok(),
body: None,
group: None,
default_scope: None,
scope: None,
skip: Some(true),
},
CommitParser {
message: Regex::new(".*").ok(),
body: None,
group: Some(String::from("Other")),
default_scope: Some(String::from("other")),
scope: None,
skip: None,
},
]),
Expand Down

0 comments on commit 3c286db

Please sign in to comment.