1
1
mirror of https://github.com/orhun/git-cliff.git synced 2024-11-29 14:36:25 +03:00

refactor(config): add section to configuration file

This commit is contained in:
orhun 2021-06-02 00:06:01 +03:00
parent 296717d2af
commit 0f35b07522
No known key found for this signature in database
GPG Key ID: B928720AEC532117
5 changed files with 49 additions and 40 deletions

View File

@ -8,7 +8,6 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
"""
# template for the changelog body
# https://tera.netlify.app/docs/#introduction
body = """
@ -24,12 +23,12 @@ body = """
{% endfor %}
{% endfor %}
"""
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""
[git]
# regex for parsing and grouping commits
commit_parsers = [
{ regex = "feat*", group = "Features"},
@ -37,12 +36,9 @@ commit_parsers = [
{ regex = "chore: Prepare*", skip = true},
{ regex = "chore*", group = "Miscellaneous Tasks"},
]
# filter out the commits that are not matched by commit parsers
filter_group = false
filter_commits = false
# glob pattern for matching git tags
git_tag_pattern = "v[0-9]*"
tag_pattern = "v[0-9]*"
# regex for skipping tags
skip_tags_regex = "v0.1.0"
skip_tags = "v0.1.0"

View File

@ -6,26 +6,33 @@ use regex::Regex;
pub struct Config {
/// Configuration values about changelog generation.
pub changelog: ChangelogConfig,
/// Configuration values about git.
pub git: GitConfig,
}
/// Changelog configuration.
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct ChangelogConfig {
/// Changelog header.
pub header: String,
pub header: String,
/// Changelog body, template.
pub body: String,
pub body: String,
/// Changelog footer.
pub footer: String,
pub footer: String,
}
/// Git configuration
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct GitConfig {
/// Git commit parsers.
pub commit_parsers: Vec<CommitParser>,
pub commit_parsers: Vec<CommitParser>,
/// Whether to filter out commits.
pub filter_group: bool,
pub filter_commits: bool,
/// Blob pattern for git tags.
pub git_tag_pattern: String,
pub tag_pattern: String,
#[serde(with = "serde_regex")]
/// Regex to skip matched tags.
pub skip_tags_regex: Regex,
pub skip_tags: Regex,
}
/// Parser for grouping commits.

View File

@ -2,6 +2,7 @@ use git_cliff_core::commit::Commit;
use git_cliff_core::config::{
ChangelogConfig,
CommitParser,
GitConfig,
};
use git_cliff_core::error::Result;
use git_cliff_core::release::*;
@ -12,9 +13,9 @@ use std::fmt::Write;
#[test]
fn generate_changelog() -> Result<()> {
let config = ChangelogConfig {
header: String::from("this is a changelog"),
body: String::from(
let changelog_config = ChangelogConfig {
header: String::from("this is a changelog"),
body: String::from(
r#"
## Release {{ version }}
{% for group, commits in commits | group_by(attribute="group") %}
@ -23,8 +24,10 @@ fn generate_changelog() -> Result<()> {
- {{ commit.message }}{% endfor %}
{% endfor %}"#,
),
footer: String::from("eoc - end of changelog"),
commit_parsers: vec![
footer: String::from("eoc - end of changelog"),
};
let git_config = GitConfig {
commit_parsers: vec![
CommitParser {
regex: Regex::new("feat*").unwrap(),
group: Some(String::from("shiny features")),
@ -36,9 +39,9 @@ fn generate_changelog() -> Result<()> {
skip: None,
},
],
filter_group: true,
git_tag_pattern: String::new(),
skip_tags_regex: Regex::new("v3*").unwrap(),
filter_commits: true,
tag_pattern: String::new(),
skip_tags: Regex::new("v3*").unwrap(),
};
let releases = vec![
@ -56,7 +59,8 @@ fn generate_changelog() -> Result<()> {
]
.iter()
.filter_map(|c| {
c.process(&config.commit_parsers, config.filter_group).ok()
c.process(&git_config.commit_parsers, git_config.filter_commits)
.ok()
})
.collect::<Vec<Commit>>(),
commit_id: None,
@ -88,15 +92,15 @@ fn generate_changelog() -> Result<()> {
];
let out = &mut String::new();
let template = Template::new(config.body)?;
if !config.header.is_empty() {
writeln!(out, "{}", config.header).unwrap();
let template = Template::new(changelog_config.body)?;
if !changelog_config.header.is_empty() {
writeln!(out, "{}", changelog_config.header).unwrap();
}
for release in releases {
write!(out, "{}", template.render(&release)?).unwrap();
}
if !config.footer.is_empty() {
writeln!(out, "{}", config.footer).unwrap();
if !changelog_config.footer.is_empty() {
writeln!(out, "{}", changelog_config.footer).unwrap();
}
assert_eq!(

View File

@ -1,5 +1,5 @@
use git_cliff_core::commit::Commit;
use git_cliff_core::config::ChangelogConfig as Config;
use git_cliff_core::config::Config;
use git_cliff_core::error::Result;
use git_cliff_core::release::Release;
use git_cliff_core::template::Template;
@ -18,7 +18,7 @@ impl<'a> Changelog<'a> {
pub fn new(releases: Vec<Release<'a>>, config: &'a Config) -> Result<Self> {
let mut changelog = Self {
releases,
template: Template::new(config.body.to_string())?,
template: Template::new(config.changelog.body.to_string())?,
config,
};
changelog.process_commits();
@ -35,8 +35,10 @@ impl<'a> Changelog<'a> {
.commits
.iter()
.filter_map(|commit| {
match commit.process(&config.commit_parsers, config.filter_group)
{
match commit.process(
&config.git.commit_parsers,
config.git.filter_commits,
) {
Ok(commit) => Some(commit),
Err(e) => {
debug!("Cannot process commit: {} ({})", commit.id, e);
@ -67,7 +69,7 @@ impl<'a> Changelog<'a> {
);
false
} else if let Some(version) = &release.version {
!self.config.skip_tags_regex.is_match(version)
!self.config.git.skip_tags.is_match(version)
} else {
true
}
@ -76,14 +78,14 @@ impl<'a> Changelog<'a> {
}
pub fn generate<W: Write>(&self, out: &mut W) -> Result<()> {
if !self.config.header.is_empty() {
writeln!(out, "{}", self.config.header)?;
if !self.config.changelog.header.is_empty() {
writeln!(out, "{}", self.config.changelog.header)?;
}
for release in &self.releases {
write!(out, "{}", self.template.render(release)?)?;
}
if !self.config.footer.is_empty() {
writeln!(out, "{}", self.config.footer)?;
if !self.config.changelog.footer.is_empty() {
writeln!(out, "{}", self.config.changelog.footer)?;
}
Ok(())
}

View File

@ -26,7 +26,7 @@ fn main() -> Result<()> {
let repository =
Repository::init(args.repository.unwrap_or(env::current_dir()?))?;
let mut tags = repository.tags(&config.changelog.git_tag_pattern)?;
let mut tags = repository.tags(&config.git.tag_pattern)?;
let commits = repository.commits()?;
if let Some(tag) = args.tag {
@ -56,5 +56,5 @@ fn main() -> Result<()> {
}
}
Changelog::new(releases, &config.changelog)?.generate(&mut io::stdout())
Changelog::new(releases, &config)?.generate(&mut io::stdout())
}