Skip to content
Open
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
28 changes: 27 additions & 1 deletion git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,15 @@ impl<'a> Changelog<'a> {
}

for release in &self.releases {
let mut release_for_changelog = release.clone();
release_for_changelog
.commits
.retain(|commit| commit.should_include_in_changelog());
let write_result = write!(
out,
"{}",
self.body_template.render(
&release,
&release_for_changelog,
Some(&self.additional_context),
&postprocessors
)?
Expand Down Expand Up @@ -837,6 +841,8 @@ mod test {
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -849,6 +855,8 @@ mod test {
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -861,6 +869,8 @@ mod test {
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -873,6 +883,8 @@ mod test {
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -885,6 +897,8 @@ mod test {
default_scope: Some(String::from("other")),
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -897,6 +911,8 @@ mod test {
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -909,6 +925,8 @@ mod test {
default_scope: None,
scope: Some(String::from("documentation")),
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -921,6 +939,8 @@ mod test {
default_scope: None,
scope: Some(String::from("documentation")),
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -933,6 +953,8 @@ mod test {
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -945,6 +967,8 @@ mod test {
default_scope: None,
scope: Some(String::from("footer")),
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -957,6 +981,8 @@ mod test {
default_scope: Some(String::from("other")),
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand Down
90 changes: 90 additions & 0 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ pub struct Commit<'a> {
/// In fact, it is pre-processed by [`Commit::preprocess`], and only be
/// generated when serializing into `context` the first time.
pub raw_message: Option<String>,

/// Whether this commit should contribute to version bumping.
///
/// `None` means include (default). Set by matching [`CommitParser::bump`].
#[serde(default)]
pub include_in_bump: Option<bool>,

/// Whether this commit should appear in the changelog.
///
/// `None` means include (default). Set by matching
/// [`CommitParser::include_in_changelog`].
#[serde(default)]
pub include_in_changelog: Option<bool>,
}

impl From<String> for Commit<'_> {
Expand Down Expand Up @@ -306,6 +319,26 @@ impl Commit<'_> {
!(self.conv.as_ref().is_some_and(ConventionalCommit::breaking) && protect_breaking)
}

/// Apply bump / changelog inclusion flags from a matching parser.
fn apply_parser_inclusion(&mut self, parser: &CommitParser) {
if parser.bump.is_some() {
self.include_in_bump = parser.bump;
}
if parser.include_in_changelog.is_some() {
self.include_in_changelog = parser.include_in_changelog;
}
}

/// Whether this commit should contribute to version bumping.
pub fn should_include_in_bump(&self) -> bool {
self.include_in_bump.unwrap_or(true)
}

/// Whether this commit should appear in the changelog.
pub fn should_include_in_changelog(&self) -> bool {
self.include_in_changelog.unwrap_or(true)
}

/// Parses the commit using [`CommitParser`]s.
///
/// Sets the [`group`] and [`scope`] of the commit.
Expand Down Expand Up @@ -400,6 +433,7 @@ impl Commit<'_> {
self.group = parser.group.clone().or(self.group);
self.scope = parser.scope.clone().or(self.scope);
self.default_scope = parser.default_scope.clone().or(self.default_scope);
self.apply_parser_inclusion(parser);
return Ok(self);
}
}
Expand All @@ -417,6 +451,7 @@ impl Commit<'_> {
self.group = parser.group.clone().map(regex_replace);
self.scope = parser.scope.clone().map(regex_replace);
self.default_scope.clone_from(&parser.default_scope);
self.apply_parser_inclusion(parser);
return Ok(self);
}
}
Expand Down Expand Up @@ -604,6 +639,8 @@ mod test {
default_scope: Some(String::from("test_scope")),
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
}],
Expand Down Expand Up @@ -761,6 +798,37 @@ mod test {
);
}

#[test]
fn parse_commit_inclusion_flags() -> Result<()> {
let docs = Commit::new(String::from("abc"), String::from("docs: update readme")).parse(
&[CommitParser {
message: Regex::new("^docs").ok(),
include_in_changelog: Some(false),
..Default::default()
}],
false,
true,
)?;
assert!(docs.should_include_in_bump());
assert!(!docs.should_include_in_changelog());

let chore = Commit::new(String::from("def"), String::from("chore: tidy")).parse(
&[CommitParser {
message: Regex::new("^chore").ok(),
bump: Some(false),
group: Some(String::from("Other")),
..Default::default()
}],
false,
true,
)?;
assert!(!chore.should_include_in_bump());
assert!(chore.should_include_in_changelog());
assert_eq!(chore.group.as_deref(), Some("Other"));

Ok(())
}

#[test]
fn parse_body() -> Result<()> {
let mut commit = Commit::new(
Expand Down Expand Up @@ -812,6 +880,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
}],
Expand Down Expand Up @@ -874,6 +944,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("author.name")),
pattern: Regex::new("John Doe").ok(),
}],
Expand All @@ -892,6 +964,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("remote.pr_title")),
pattern: Regex::new("feat: do something").ok(),
}],
Expand All @@ -910,6 +984,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("body")),
pattern: Regex::new("something great").ok(),
}],
Expand All @@ -928,6 +1004,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("remote.pr_labels")),
pattern: Regex::new("feature|deprecation").ok(),
}],
Expand All @@ -946,6 +1024,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("links")),
pattern: Regex::new(".*").ok(),
}],
Expand All @@ -964,6 +1044,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("remote")),
pattern: Regex::new(".*").ok(),
}],
Expand Down Expand Up @@ -995,6 +1077,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
}],
Expand Down Expand Up @@ -1037,6 +1121,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("author.name")),
pattern: Regex::new("^John Doe$").ok(),
}],
Expand All @@ -1055,6 +1141,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("remote.pr_title")),
pattern: Regex::new("^feat(\\([^)]+\\))?").ok(),
}],
Expand All @@ -1073,6 +1161,8 @@ Refs: #123
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: Some(String::from("author.name")),
pattern: Regex::new("Something else").ok(),
}],
Expand Down
11 changes: 11 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,17 @@ pub struct CommitParser {
pub scope: Option<String>,
/// Whether to skip this commit group.
pub skip: Option<bool>,
/// Whether this matched commit should contribute to version bumping.
///
/// Defaults to `true` when unset. Set to `false` to keep the commit in the
/// changelog while excluding it from `--bump` / `--bumped-version`.
pub bump: Option<bool>,
/// Whether this matched commit should appear in the changelog.
///
/// Defaults to `true` when unset. Set to `false` to keep the commit for
/// version bumping while omitting it from changelog output. Matched
/// commits still satisfy `filter_commits`.
pub include_in_changelog: Option<bool>,
/// Field name of the commit to match the regex against.
pub field: Option<String>,
/// Regex for matching the field value.
Expand Down
8 changes: 8 additions & 0 deletions git-cliff-core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ mod test {
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -297,6 +299,8 @@ mod test {
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand Down Expand Up @@ -337,6 +341,8 @@ mod test {
default_scope: None,
scope: None,
skip: Some(true),
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand All @@ -349,6 +355,8 @@ mod test {
default_scope: None,
scope: None,
skip: None,
bump: None,
include_in_changelog: None,
field: None,
pattern: None,
},
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl Release<'_> {
&old_semver,
self.commits
.iter()
.filter(|commit| commit.should_include_in_bump())
.map(|commit| commit.raw_message().trim_end().to_string())
.collect::<Vec<String>>(),
);
Expand Down
Loading
Loading