Skip to content

Commit

Permalink
refactor(changelog): support --bump for processed releases (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun authored Dec 26, 2023
1 parent bdce4b5 commit 89e4c72
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
30 changes: 27 additions & 3 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use crate::release::{
};
use crate::template::Template;
use std::io::Write;
use std::time::{
SystemTime,
UNIX_EPOCH,
};

/// Changelog generator.
#[derive(Debug)]
Expand Down Expand Up @@ -136,6 +140,23 @@ impl<'a> Changelog<'a> {
}
}

/// Increments the version for the unreleased changes based on semver.
pub fn bump_version(&mut self) -> Result<Option<String>> {
if let Some(ref mut last_release) = self.releases.iter_mut().next() {
if last_release.version.is_none() {
let next_version = last_release.calculate_next_version()?;
debug!("Bumping the version to {next_version}");
last_release.version = Some(next_version.to_string());
last_release.timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)?
.as_secs()
.try_into()?;
return Ok(Some(next_version));
}
}
Ok(None)
}

/// Generates the changelog and writes it to the given output.
pub fn generate<W: Write>(&self, out: &mut W) -> Result<()> {
debug!("Generating changelog...");
Expand Down Expand Up @@ -231,7 +252,7 @@ mod test {
body: Some(String::from(
r#"{% if version %}
## Release [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }}
({{ commit_id }}){% else %}
{% if commit_id %}({{ commit_id }}){% endif %}{% else %}
## Unreleased{% endif %}
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group }}{% for group, commits in commits | group_by(attribute="scope") %}
Expand Down Expand Up @@ -499,13 +520,16 @@ mod test {
#[test]
fn changelog_generator() -> Result<()> {
let (config, releases) = get_test_data();
let changelog = Changelog::new(releases, &config)?;
let mut changelog = Changelog::new(releases, &config)?;
changelog.bump_version()?;
changelog.releases[0].timestamp = 0;
let mut out = Vec::new();
changelog.generate(&mut out)?;
assert_eq!(
String::from(
r#"# Changelog
## Unreleased
## Release [v1.1.0] - 1970-01-01
### Bug Fixes
#### app
Expand Down
67 changes: 20 additions & 47 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ fn check_new_version() {
}
}

/// Output of the `process_repository` call.
enum ProcessOutput<'a> {
/// List of releases.
Releases(Vec<Release<'a>>),
/// Semantic version.
Version(String),
}

/// Processes the tags and commits for creating release entries for the
/// changelog.
///
Expand All @@ -82,7 +74,7 @@ fn process_repository<'a>(
repository: &'static Repository,
config: Config,
args: &Opt,
) -> Result<ProcessOutput<'a>> {
) -> Result<Vec<Release<'a>>> {
let mut tags = repository.tags(&config.git.tag_pattern, args.topo_order)?;
let skip_regex = config.git.skip_tags.as_ref();
let ignore_regex = config.git.ignore_tags.as_ref();
Expand Down Expand Up @@ -269,24 +261,7 @@ fn process_repository<'a>(
}
}

// Bump the version.
if (args.bump || args.bumped_version) &&
releases[release_index].version.is_none()
{
let next_version = releases[release_index].calculate_next_version()?;
if args.bumped_version {
return Ok(ProcessOutput::Version(next_version));
}

debug!("Bumping the version to {next_version}");
releases[release_index].version = Some(next_version.to_string());
releases[release_index].timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)?
.as_secs()
.try_into()?;
}

Ok(ProcessOutput::Releases(releases))
Ok(releases)
}

/// Runs `git-cliff`.
Expand Down Expand Up @@ -401,34 +376,32 @@ pub fn run(mut args: Opt) -> Result<()> {
// Process the repository.
let repositories = args.repository.clone().unwrap_or(vec![env::current_dir()?]);
let mut releases = Vec::<Release>::new();
let mut versions = Vec::<String>::new();
for repository in repositories {
let repository = Repository::init(repository)?;
let process_output = process_repository(
releases.extend(process_repository(
Box::leak(Box::new(repository)),
config.clone(),
&args,
)?;

match process_output {
ProcessOutput::Releases(release) => releases.extend(release),
ProcessOutput::Version(version) => versions.push(version),
}
)?);
}

// Generate output.
if !versions.is_empty() {
let buf = versions.join("\n");
if let Some(path) = args.output {
let mut output = File::create(path)?;
output.write_all(buf.as_bytes())?;
} else {
println!("{buf}");
};
return Ok(());
}
// Process commits and releases for the changelog.
let mut changelog = Changelog::new(releases, &config)?;

let changelog = Changelog::new(releases, &config)?;
// Print the result.
if args.bump || args.bumped_version {
if let Some(next_version) = changelog.bump_version()? {
if args.bumped_version {
if let Some(path) = args.output {
let mut output = File::create(path)?;
output.write_all(next_version.as_bytes())?;
} else {
println!("{next_version}");
}
return Ok(());
}
}
}
if args.context {
return if let Some(path) = args.output {
let mut output = File::create(path)?;
Expand Down

0 comments on commit 89e4c72

Please sign in to comment.