Skip to content

Commit

Permalink
feat(release): allow preserving zero-based versions
Browse files Browse the repository at this point in the history
closes #447
  • Loading branch information
jsurkont committed Mar 4, 2024
1 parent 8f8e221 commit cd51f5e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
7 changes: 4 additions & 3 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ impl<'a> Changelog<'a> {
}

/// Increments the version for the unreleased changes based on semver.
pub fn bump_version(&mut self) -> Result<Option<String>> {
pub fn bump_version(&mut self, keep_zerover: bool) -> 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()?;
let next_version =
last_release.calculate_next_version(keep_zerover)?;
debug!("Bumping the version to {next_version}");
last_release.version = Some(next_version.to_string());
last_release.timestamp = SystemTime::now()
Expand Down Expand Up @@ -608,7 +609,7 @@ mod test {
fn changelog_generator() -> Result<()> {
let (config, releases) = get_test_data();
let mut changelog = Changelog::new(releases, &config)?;
changelog.bump_version()?;
changelog.bump_version(false)?;
changelog.releases[0].timestamp = 0;
let mut out = Vec::new();
changelog.generate(&mut out)?;
Expand Down
85 changes: 59 additions & 26 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> Release<'a> {
}

/// Calculates the next version based on the commits.
pub fn calculate_next_version(&self) -> Result<String> {
pub fn calculate_next_version(&self, keep_zerover: bool) -> Result<String> {
match self
.previous
.as_ref()
Expand Down Expand Up @@ -126,8 +126,8 @@ impl<'a> Release<'a> {
}
}
let next_version = VersionUpdater::new()
.with_features_always_increment_minor(true)
.with_breaking_always_increment_major(true)
.with_features_always_increment_minor(!keep_zerover)
.with_breaking_always_increment_major(!keep_zerover)
.increment(
&semver?,
self.commits
Expand Down Expand Up @@ -169,7 +169,27 @@ mod test {
use super::*;
#[test]
fn bump_version() -> Result<()> {
for (version, expected_version, commits) in [
fn build_release<'a>(version: &str, commits: &'a Vec<&str>) -> Release<'a> {
Release {
version: None,
commits: commits
.into_iter()
.map(|v| Commit::from(v.to_string()))
.collect(),
commit_id: None,
timestamp: 0,
previous: Some(Box::new(Release {
version: Some(String::from(version)),
..Default::default()
})),
#[cfg(feature = "github")]
github: crate::github::GitHubReleaseMetadata {
contributors: vec![],
},
}
}

let test_shared = [
("1.0.0", "1.1.0", vec!["feat: add xyz", "fix: fix xyz"]),
("1.0.0", "1.0.1", vec!["fix: add xyz", "fix: aaaaaa"]),
("1.0.0", "2.0.0", vec!["feat!: add xyz", "feat: zzz"]),
Expand Down Expand Up @@ -202,36 +222,49 @@ mod test {
"aaa#/@#$@9384!#%^#@#@!#!239432413-idk-9999.2200.5932-alpha.420",
vec!["feat: damn this is working"],
),
] {
let release = Release {
version: None,
commits: commits
.into_iter()
.map(|v| Commit::from(v.to_string()))
.collect(),
commit_id: None,
timestamp: 0,
previous: Some(Box::new(Release {
version: Some(String::from(version)),
..Default::default()
})),
#[cfg(feature = "github")]
github: crate::github::GitHubReleaseMetadata {
contributors: vec![],
},
};
let next_version = release.calculate_next_version()?;
assert_eq!(expected_version, next_version);
];

for (version, expected_version, commits) in test_shared.iter().chain(
[
("0.0.1", "0.0.2", vec!["fix: fix xyz"]),
("0.0.1", "0.1.0", vec!["feat: add xyz", "fix: fix xyz"]),
("0.0.1", "1.0.0", vec!["feat!: add xyz", "feat: zzz"]),
("0.1.0", "0.1.1", vec!["fix: fix xyz"]),
("0.1.0", "0.2.0", vec!["feat: add xyz", "fix: fix xyz"]),
("0.1.0", "1.0.0", vec!["feat!: add xyz", "feat: zzz"]),
]
.iter(),
) {
let release = build_release(version, commits);
let next_version = release.calculate_next_version(false)?;
assert_eq!(expected_version, &next_version);
}

for (version, expected_version, commits) in test_shared.iter().chain(
[
("0.0.1", "0.0.2", vec!["fix: fix xyz"]),
("0.0.1", "0.0.2", vec!["feat: add xyz", "fix: fix xyz"]),
("0.0.1", "0.0.2", vec!["feat!: add xyz", "feat: zzz"]),
("0.1.0", "0.1.1", vec!["fix: fix xyz"]),
("0.1.0", "0.1.1", vec!["feat: add xyz", "fix: fix xyz"]),
("0.1.0", "0.2.0", vec!["feat!: add xyz", "feat: zzz"]),
]
.iter(),
) {
let release = build_release(version, commits);
let next_version = release.calculate_next_version(true)?;
assert_eq!(expected_version, &next_version);
}

let empty_release = Release {
previous: Some(Box::new(Release {
version: None,
..Default::default()
})),
..Default::default()
};
let next_version = empty_release.calculate_next_version()?;
assert_eq!("0.1.0", next_version);
assert_eq!("0.1.0", empty_release.calculate_next_version(false)?);
assert_eq!("0.1.0", empty_release.calculate_next_version(true)?);
Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub struct Opt {
/// Bumps the version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bump: bool,
/// Keeps leading zeros (0.0.x, 0.x.y) when bumping the version.
#[arg(long, help_heading = Some("FLAGS"))]
pub keep_zerover: bool,
/// Prints bumped version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bumped_version: bool,
Expand Down
2 changes: 1 addition & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ pub fn run(mut args: Opt) -> Result<()> {

// Print the result.
if args.bump || args.bumped_version {
if let Some(next_version) = changelog.bump_version()? {
if let Some(next_version) = changelog.bump_version(args.keep_zerover)? {
if args.bumped_version {
if let Some(path) = args.output {
let mut output = File::create(path)?;
Expand Down
1 change: 1 addition & 0 deletions website/docs/usage/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
-V, --version Prints version information
-v, --verbose... Increases the logging verbosity
--bump Bumps the version for unreleased changes
--keep-zerover Keeps leading zeros (0.0.x, 0.x.y) when bumping the version.
--bumped-version Prints bumped version for unreleased changes
-l, --latest Processes the commits starting from the latest tag
--current Processes the commits that belong to the current tag
Expand Down

0 comments on commit cd51f5e

Please sign in to comment.