Skip to content

Commit 27896eb

Browse files
committed
refactor: default to embeded config
1 parent bf50336 commit 27896eb

File tree

6 files changed

+128
-138
lines changed

6 files changed

+128
-138
lines changed

config/cliff.toml

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ footer = """
3333
"""
3434
# Remove leading and trailing whitespaces from the changelog's body.
3535
trim = true
36+
# Render body even when there are no releases to process.
37+
render_always = true
3638
# An array of regex based postprocessors to modify the changelog.
3739
postprocessors = [
3840
# Replace the placeholder <REPO> with a URL.
@@ -62,6 +64,8 @@ commit_preprocessors = [
6264
# If the spelling is incorrect, it will be fixed automatically.
6365
#{ pattern = '.*', replace_command = 'typos --write-changes -' },
6466
]
67+
# Prevent commits that are breaking from being excluded by commit parsers.
68+
protect_breaking_commits = false
6569
# An array of regex based parsers for extracting data from the commit message.
6670
# Assigns commits to groups.
6771
# Optionally sets the commit's scope and can decide to exclude commits from further processing.
@@ -84,6 +88,10 @@ commit_parsers = [
8488
]
8589
# Exclude commits that are not matched by any commit parser.
8690
filter_commits = false
91+
# An array of link parsers for extracting external references, and turning them into URLs, using regex.
92+
link_parsers = []
93+
# Include only the tags that belong to the current branch.
94+
use_branch_tags = false
8795
# Order releases topologically instead of chronologically.
8896
topo_order = false
8997
# Order of commits in each group/release within the changelog.

git-cliff-core/src/changelog.rs

+39-49
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a> Changelog<'a> {
5454

5555
/// Builds a changelog from releases and config.
5656
fn build(releases: Vec<Release<'a>>, config: &'a Config) -> Result<Self> {
57-
let trim = config.changelog.trim.unwrap_or(true);
57+
let trim = config.changelog.trim;
5858
Ok(Self {
5959
releases,
6060
header_template: match &config.changelog.header {
@@ -127,7 +127,7 @@ impl<'a> Changelog<'a> {
127127
.cloned()
128128
.filter_map(|commit| Self::process_commit(&commit, &self.config.git))
129129
.flat_map(|commit| {
130-
if self.config.git.split_commits.unwrap_or(false) {
130+
if self.config.git.split_commits {
131131
commit
132132
.message
133133
.lines()
@@ -148,7 +148,7 @@ impl<'a> Changelog<'a> {
148148
.collect::<Vec<Commit>>();
149149
});
150150

151-
if self.config.git.require_conventional.unwrap_or(false) {
151+
if self.config.git.require_conventional {
152152
self.check_conventional_commits()?;
153153
}
154154

@@ -203,7 +203,7 @@ impl<'a> Changelog<'a> {
203203
}
204204
match &release.previous {
205205
Some(prev_release) if prev_release.commits.is_empty() => {
206-
self.config.changelog.render_always.unwrap_or(false)
206+
self.config.changelog.render_always
207207
}
208208
_ => false,
209209
}
@@ -561,12 +561,7 @@ impl<'a> Changelog<'a> {
561561
/// Generates the changelog and writes it to the given output.
562562
pub fn generate<W: Write + ?Sized>(&self, out: &mut W) -> Result<()> {
563563
debug!("Generating changelog...");
564-
let postprocessors = self
565-
.config
566-
.changelog
567-
.postprocessors
568-
.clone()
569-
.unwrap_or_default();
564+
let postprocessors = self.config.changelog.postprocessors.clone();
570565

571566
if let Some(header_template) = &self.header_template {
572567
let write_result = writeln!(
@@ -653,13 +648,7 @@ impl<'a> Changelog<'a> {
653648
}
654649

655650
fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
656-
let template_str = config
657-
.changelog
658-
.body
659-
.as_deref()
660-
.unwrap_or_default()
661-
.to_string();
662-
let template = Template::new("body", template_str, trim)?;
651+
let template = Template::new("body", config.changelog.body.clone(), trim)?;
663652
let deprecated_vars = [
664653
"commit.github",
665654
"commit.gitea",
@@ -694,7 +683,7 @@ mod test {
694683
let config = Config {
695684
changelog: ChangelogConfig {
696685
header: Some(String::from("# Changelog")),
697-
body: Some(String::from(
686+
body: String::from(
698687
r#"{% if version %}
699688
## Release [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }} - ({{ repository }})
700689
{% if commit_id %}({{ commit_id }}){% endif %}{% else %}
@@ -704,34 +693,34 @@ mod test {
704693
#### {{ group }}{% for commit in commits %}
705694
- {{ commit.message }}{% endfor %}
706695
{% endfor %}{% endfor %}"#,
707-
)),
696+
),
708697
footer: Some(String::from(
709698
r#"-- total releases: {{ releases | length }} --"#,
710699
)),
711-
trim: Some(true),
712-
postprocessors: Some(vec![TextProcessor {
700+
trim: true,
701+
postprocessors: vec![TextProcessor {
713702
pattern: Regex::new("boring")
714703
.expect("failed to compile regex"),
715704
replace: Some(String::from("exciting")),
716705
replace_command: None,
717-
}]),
718-
render_always: None,
706+
}],
707+
render_always: false,
719708
output: None,
720709
},
721710
git: GitConfig {
722-
conventional_commits: Some(true),
723-
require_conventional: Some(false),
724-
filter_unconventional: Some(false),
725-
split_commits: Some(false),
726-
commit_preprocessors: Some(vec![TextProcessor {
711+
conventional_commits: true,
712+
require_conventional: false,
713+
filter_unconventional: false,
714+
split_commits: false,
715+
commit_preprocessors: vec![TextProcessor {
727716
pattern: Regex::new("<preprocess>")
728717
.expect("failed to compile regex"),
729718
replace: Some(String::from(
730719
"this commit is preprocessed",
731720
)),
732721
replace_command: None,
733-
}]),
734-
commit_parsers: Some(vec![
722+
}],
723+
commit_parsers: vec![
735724
CommitParser {
736725
sha: Some(String::from("tea")),
737726
message: None,
@@ -864,17 +853,17 @@ mod test {
864853
field: None,
865854
pattern: None,
866855
},
867-
]),
868-
protect_breaking_commits: None,
869-
filter_commits: Some(false),
856+
],
857+
protect_breaking_commits: false,
858+
filter_commits: false,
870859
tag_pattern: None,
871860
skip_tags: Regex::new("v3.*").ok(),
872861
ignore_tags: None,
873862
count_tags: None,
874-
use_branch_tags: Some(false),
875-
topo_order: Some(false),
876-
sort_commits: Some(String::from("oldest")),
877-
link_parsers: None,
863+
use_branch_tags: false,
864+
topo_order: false,
865+
sort_commits: String::from("oldest"),
866+
link_parsers: [].to_vec(),
878867
limit_commits: None,
879868
},
880869
remote: RemoteConfig {
@@ -1150,14 +1139,17 @@ mod test {
11501139
#[test]
11511140
fn changelog_generator_split_commits() -> Result<()> {
11521141
let (mut config, mut releases) = get_test_data();
1153-
config.git.split_commits = Some(true);
1154-
config.git.filter_unconventional = Some(false);
1155-
config.git.protect_breaking_commits = Some(true);
1156-
1157-
if let Some(parsers) = config.git.commit_parsers.as_mut() {
1158-
for parser in parsers.iter_mut().filter(|p| p.footer.is_some()) {
1159-
parser.skip = Some(true);
1160-
}
1142+
config.git.split_commits = true;
1143+
config.git.filter_unconventional = false;
1144+
config.git.protect_breaking_commits = true;
1145+
1146+
for parser in config
1147+
.git
1148+
.commit_parsers
1149+
.iter_mut()
1150+
.filter(|p| p.footer.is_some())
1151+
{
1152+
parser.skip = Some(true);
11611153
}
11621154

11631155
releases[0].commits.push(Commit::new(
@@ -1280,8 +1272,7 @@ chore(deps): fix broken deps
12801272
fn changelog_adds_additional_context() -> Result<()> {
12811273
let (mut config, releases) = get_test_data();
12821274
// add `{{ custom_field }}` to the template
1283-
config.changelog.body = Some(
1284-
r#"{% if version %}
1275+
config.changelog.body = r#"{% if version %}
12851276
## {{ custom_field }} [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }}
12861277
{% if commit_id %}({{ commit_id }}){% endif %}{% else %}
12871278
## Unreleased{% endif %}
@@ -1290,8 +1281,7 @@ chore(deps): fix broken deps
12901281
#### {{ group }}{% for commit in commits %}
12911282
- {{ commit.message }}{% endfor %}
12921283
{% endfor %}{% endfor %}"#
1293-
.to_string(),
1294-
);
1284+
.to_string();
12951285
let mut changelog = Changelog::new(releases, &config)?;
12961286
changelog.add_context("custom_field", "Hello")?;
12971287
let mut out = Vec::new();

git-cliff-core/src/commit.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,26 @@ impl Commit<'_> {
212212
/// * extracts links and generates URLs
213213
pub fn process(&self, config: &GitConfig) -> Result<Self> {
214214
let mut commit = self.clone();
215-
if let Some(preprocessors) = &config.commit_preprocessors {
216-
commit = commit.preprocess(preprocessors)?;
217-
}
218-
if config.conventional_commits.unwrap_or(true) {
219-
if !config.require_conventional.unwrap_or(false) &&
220-
config.filter_unconventional.unwrap_or(true) &&
221-
!config.split_commits.unwrap_or(false)
215+
commit = commit.preprocess(&config.commit_preprocessors)?;
216+
if config.conventional_commits {
217+
if !config.require_conventional &&
218+
config.filter_unconventional &&
219+
!config.split_commits
222220
{
223221
commit = commit.into_conventional()?;
224222
} else if let Ok(conv_commit) = commit.clone().into_conventional() {
225223
commit = conv_commit;
226224
}
227225
}
228-
if let Some(parsers) = &config.commit_parsers {
229-
commit = commit.parse(
230-
parsers,
231-
config.protect_breaking_commits.unwrap_or(false),
232-
config.filter_commits.unwrap_or(false),
233-
)?;
234-
}
235-
if let Some(parsers) = &config.link_parsers {
236-
commit = commit.parse_links(parsers)?;
237-
}
226+
227+
commit = commit.parse(
228+
&config.commit_parsers,
229+
config.protect_breaking_commits,
230+
config.filter_commits,
231+
)?;
232+
233+
commit = commit.parse_links(&config.link_parsers)?;
234+
238235
Ok(commit)
239236
}
240237

@@ -555,7 +552,7 @@ mod test {
555552
#[test]
556553
fn conventional_footers() {
557554
let cfg = crate::config::GitConfig {
558-
conventional_commits: Some(true),
555+
conventional_commits: true,
559556
..Default::default()
560557
};
561558
let test_cases = vec![

git-cliff-core/src/config.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::command;
2+
use crate::embed::EmbeddedConfig;
23
use crate::error::Result;
34
use regex::{
45
Regex,
@@ -72,15 +73,15 @@ pub struct ChangelogConfig {
7273
/// Changelog header.
7374
pub header: Option<String>,
7475
/// Changelog body, template.
75-
pub body: Option<String>,
76+
pub body: String,
7677
/// Changelog footer.
7778
pub footer: Option<String>,
7879
/// Trim the template.
79-
pub trim: Option<bool>,
80+
pub trim: bool,
8081
/// Always render the body template.
81-
pub render_always: Option<bool>,
82+
pub render_always: bool,
8283
/// Changelog postprocessors.
83-
pub postprocessors: Option<Vec<TextProcessor>>,
84+
pub postprocessors: Vec<TextProcessor>,
8485
/// Output file path.
8586
pub output: Option<PathBuf>,
8687
}
@@ -89,30 +90,30 @@ pub struct ChangelogConfig {
8990
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
9091
pub struct GitConfig {
9192
/// Parse commits according to the conventional commits specification.
92-
pub conventional_commits: Option<bool>,
93+
pub conventional_commits: bool,
9394
/// Require all commits to be conventional.
9495
/// Takes precedence over filter_unconventional.
95-
pub require_conventional: Option<bool>,
96+
pub require_conventional: bool,
9697
/// Exclude commits that do not match the conventional commits specification
9798
/// from the changelog.
98-
pub filter_unconventional: Option<bool>,
99+
pub filter_unconventional: bool,
99100
/// Split commits on newlines, treating each line as an individual commit.
100-
pub split_commits: Option<bool>,
101+
pub split_commits: bool,
101102

102103
/// An array of regex based parsers to modify commit messages prior to
103104
/// further processing.
104-
pub commit_preprocessors: Option<Vec<TextProcessor>>,
105+
pub commit_preprocessors: Vec<TextProcessor>,
105106
/// An array of regex based parsers for extracting data from the commit
106107
/// message.
107-
pub commit_parsers: Option<Vec<CommitParser>>,
108+
pub commit_parsers: Vec<CommitParser>,
108109
/// Prevent commits having the `BREAKING CHANGE:` footer from being excluded
109110
/// by commit parsers.
110-
pub protect_breaking_commits: Option<bool>,
111+
pub protect_breaking_commits: bool,
111112
/// An array of regex based parsers to extract links from the commit message
112113
/// and add them to the commit's context.
113-
pub link_parsers: Option<Vec<LinkParser>>,
114+
pub link_parsers: Vec<LinkParser>,
114115
/// Exclude commits that are not matched by any commit parser.
115-
pub filter_commits: Option<bool>,
116+
pub filter_commits: bool,
116117
/// Regex to select git tags that represent releases.
117118
#[serde(with = "serde_regex", default)]
118119
pub tag_pattern: Option<Regex>,
@@ -126,11 +127,11 @@ pub struct GitConfig {
126127
#[serde(with = "serde_regex", default)]
127128
pub count_tags: Option<Regex>,
128129
/// Include only the tags that belong to the current branch.
129-
pub use_branch_tags: Option<bool>,
130+
pub use_branch_tags: bool,
130131
/// Order releases topologically instead of chronologically.
131-
pub topo_order: Option<bool>,
132+
pub topo_order: bool,
132133
/// How to order commits in each group/release within the changelog.
133-
pub sort_commits: Option<String>,
134+
pub sort_commits: String,
134135
/// Limit the total number of commits included in the changelog.
135136
pub limit_commits: Option<usize>,
136137
}
@@ -441,7 +442,13 @@ impl Config {
441442
}
442443
}
443444

445+
let default_config_str = EmbeddedConfig::get_config()?;
446+
444447
Ok(config::Config::builder()
448+
.add_source(config::File::from_str(
449+
&default_config_str,
450+
config::FileFormat::Toml,
451+
))
445452
.add_source(config::File::from(path))
446453
.add_source(
447454
config::Environment::with_prefix("GIT_CLIFF").separator("__"),

0 commit comments

Comments
 (0)