From 08a0f9bb0b6d7f2a289b7ce23ac1a53be17e1d6d Mon Sep 17 00:00:00 2001 From: ofsahof Date: Tue, 14 Jun 2022 11:36:08 +0300 Subject: [PATCH] fix: issue #50 --- git-cliff-core/src/commit.rs | 17 +++++++++++------ git-cliff-core/src/config.rs | 4 +++- git-cliff-core/tests/integration_test.rs | 2 ++ git-cliff/src/changelog.rs | 4 ++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/git-cliff-core/src/commit.rs b/git-cliff-core/src/commit.rs index f2dcd41332..526b157bfe 100644 --- a/git-cliff-core/src/commit.rs +++ b/git-cliff-core/src/commit.rs @@ -30,8 +30,10 @@ pub struct Commit<'a> { pub conv: Option>, /// Commit group based on a commit parser or its conventional type. pub group: Option, - /// Commit scope based on conventional type or a commit parser. - pub scope: Option, + /// Default commit scope based on(inherited from) conventional type or a commit parser. + pub default_scope: Option, + /// commit scope for overriding default one + pub scope: Option, /// A list of links found in the commit pub links: Vec, } @@ -63,6 +65,7 @@ impl Commit<'_> { message, conv: None, group: None, + default_scope: None, scope: None, links: vec![], } @@ -159,7 +162,8 @@ 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.scope = parser.scope.as_ref().cloned(); + self.default_scope = parser.default_scope.as_ref().cloned(); return Ok(self); } else { return Err(AppError::GroupError(String::from( @@ -236,13 +240,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)?; @@ -282,13 +286,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] diff --git a/git-cliff-core/src/config.rs b/git-cliff-core/src/config.rs index 489d7ede9e..35287d20e6 100644 --- a/git-cliff-core/src/config.rs +++ b/git-cliff-core/src/config.rs @@ -75,8 +75,10 @@ pub struct CommitParser { pub body: Option, /// Group of the commit. pub group: Option, - /// Scope of the commit. + /// Default scope of the commit. pub default_scope: Option, + /// scope of the commit for overriding default one + pub scope: Option, /// Whether to skip this commit group. pub skip: Option, } diff --git a/git-cliff-core/tests/integration_test.rs b/git-cliff-core/tests/integration_test.rs index 493c2fe566..d2ba00a193 100644 --- a/git-cliff-core/tests/integration_test.rs +++ b/git-cliff-core/tests/integration_test.rs @@ -51,6 +51,7 @@ fn generate_changelog() -> Result<()> { body: None, group: Some(String::from("shiny features")), default_scope: None, + scope: None, skip: None, }, CommitParser { @@ -58,6 +59,7 @@ fn generate_changelog() -> Result<()> { body: None, group: Some(String::from("fix bugs")), default_scope: None, + scope: None, skip: None, }, ]), diff --git a/git-cliff/src/changelog.rs b/git-cliff/src/changelog.rs index dc871df438..66d785e4a9 100644 --- a/git-cliff/src/changelog.rs +++ b/git-cliff/src/changelog.rs @@ -204,6 +204,7 @@ mod test { body: None, group: Some(String::from("New features")), default_scope: Some(String::from("other")), + scope: None, skip: None, }, CommitParser { @@ -211,6 +212,7 @@ mod test { body: None, group: Some(String::from("Bug Fixes")), default_scope: None, + scope: None, skip: None, }, CommitParser { @@ -218,6 +220,7 @@ mod test { body: None, group: None, default_scope: None, + scope: None, skip: Some(true), }, CommitParser { @@ -225,6 +228,7 @@ mod test { body: None, group: Some(String::from("Other")), default_scope: Some(String::from("other")), + scope: None, skip: None, }, ]),