Skip to content

Commit

Permalink
feat(config): make the changelog section optional (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Jan 16, 2022
1 parent 8202e37 commit e02ae0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
7 changes: 5 additions & 2 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ use regex::Regex;
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct Config {
/// Configuration values about changelog generation.
#[serde(default)]
pub changelog: ChangelogConfig,
/// Configuration values about git.
#[serde(default)]
pub git: GitConfig,
}

/// Changelog configuration.
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
#[derive(
Debug, Default, Clone, serde_derive::Serialize, serde_derive::Deserialize,
)]
pub struct ChangelogConfig {
/// Changelog header.
pub header: Option<String>,
/// Changelog body, template.
pub body: String,
pub body: Option<String>,
/// Changelog footer.
pub footer: Option<String>,
/// Trim the template.
Expand Down
35 changes: 23 additions & 12 deletions git-cliff/src/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use git_cliff_core::commit::Commit;
use git_cliff_core::config::Config;
use git_cliff_core::error::Result;
use git_cliff_core::error::{
Error,
Result,
};
use git_cliff_core::release::Release;
use git_cliff_core::template::Template;
use std::io::Write;
Expand All @@ -16,19 +19,27 @@ pub struct Changelog<'a> {
impl<'a> Changelog<'a> {
/// Constructs a new instance.
pub fn new(releases: Vec<Release<'a>>, config: &'a Config) -> Result<Self> {
let mut template = config
.changelog
.body
.as_deref()
.unwrap_or_default()
.to_string();
if template.is_empty() {
return Err(Error::ChangelogError(String::from(
"changelog body cannot be empty",
)));
}
if config.changelog.trim.unwrap_or(true) {
template = template
.lines()
.map(|v| v.trim())
.collect::<Vec<&str>>()
.join("\n")
}
let mut changelog = Self {
releases,
template: Template::new({
let mut template = config.changelog.body.to_string();
if config.changelog.trim.unwrap_or(true) {
template = template
.lines()
.map(|v| v.trim())
.collect::<Vec<&str>>()
.join("\n")
}
template
})?,
template: Template::new(template)?,
config,
};
changelog.process_commits();
Expand Down
9 changes: 7 additions & 2 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ pub fn run(mut args: Opt) -> Result<()> {
}
}

// Load the default configuration if necessary.
let mut config = if fs::metadata(&path).is_ok() {
Config::parse(path)?
} else {
warn!("{:?} is not found, using the default configuration.", path);
EmbeddedConfig::parse()?
};
if config.changelog.body.is_none() {
warn!("Changelog body is not specified, using the default template.");
config.changelog.body = EmbeddedConfig::parse()?.changelog.body;
}

// Update the configuration based on command line arguments and vice versa.
match args.strip.as_deref() {
Expand All @@ -92,8 +97,8 @@ pub fn run(mut args: Opt) -> Result<()> {
)));
}
}
if let Some(template) = args.body {
config.changelog.body = template;
if args.body.is_some() {
config.changelog.body = args.body;
}
if args.sort == "oldest" {
if let Some(ref sort_commits) = config.git.sort_commits {
Expand Down

0 comments on commit e02ae0b

Please sign in to comment.