Skip to content

Commit

Permalink
feat: support bump a specific version type
Browse files Browse the repository at this point in the history
  • Loading branch information
braineo committed Jul 29, 2024
1 parent 35dc1e4 commit 69fc637
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
3 changes: 3 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ pub struct Bump {
///
/// `commit type` according to the spec is only `[a-zA-Z]+`
pub custom_minor_increment_regex: Option<String>,

/// force to always bump in major, minor or patch.
pub bump_as: Option<String>,
}

/// Parser for grouping commits.
Expand Down
78 changes: 53 additions & 25 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use crate::commit::Commit;
use crate::config::Bump;
use crate::error::Result;
use crate::error::{
Error,
Result,
};
#[cfg(feature = "remote")]
use crate::remote::{
RemoteCommit,
RemoteContributor,
RemotePullRequest,
RemoteReleaseMetadata,
};
use next_version::VersionUpdater;
use crate::{
commit::Commit,
config::Bump,
};

use next_version::{
NextVersion,
VersionUpdater,
};
use semver::Version;
use serde::{
Deserialize,
Expand Down Expand Up @@ -124,15 +133,30 @@ impl<'a> Release<'a> {
custom_minor_increment_regex,
)?;
}
let next_version = next_version
.increment(
&semver?,
self.commits
.iter()
.map(|commit| commit.message.trim_end().to_string())
.collect::<Vec<String>>(),
)
.to_string();

let next_version = if let Some(bump_as) = &config.bump_as {
match bump_as.as_str() {
"major" => semver?.increment_major().to_string(),
"minor" => semver?.increment_minor().to_string(),
"patch" => semver?.increment_patch().to_string(),
_ => {
return Err(Error::ArgumentError(format!(
"{} is not a valid bump type",
bump_as
)))
}
}
} else {
next_version
.increment(
&semver?,
self.commits
.iter()
.map(|commit| commit.message.trim_end().to_string())
.collect::<Vec<String>>(),
)
.to_string()
};
if let Some(prefix) = prefix {
Ok(format!("{prefix}{next_version}"))
} else {
Expand Down Expand Up @@ -277,11 +301,12 @@ mod test {
let release = build_release(version, commits);
let next_version =
release.calculate_next_version_with_config(&Bump {
features_always_bump_minor: Some(false),
breaking_always_bump_major: Some(false),
initial_tag: None,
features_always_bump_minor: Some(false),
breaking_always_bump_major: Some(false),
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_as: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -300,11 +325,12 @@ mod test {
let release = build_release(version, commits);
let next_version =
release.calculate_next_version_with_config(&Bump {
features_always_bump_minor: Some(true),
breaking_always_bump_major: Some(false),
initial_tag: None,
features_always_bump_minor: Some(true),
breaking_always_bump_major: Some(false),
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_as: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -323,11 +349,12 @@ mod test {
let release = build_release(version, commits);
let next_version =
release.calculate_next_version_with_config(&Bump {
features_always_bump_minor: Some(false),
breaking_always_bump_major: Some(true),
initial_tag: None,
features_always_bump_minor: Some(false),
breaking_always_bump_major: Some(true),
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_as: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -346,11 +373,12 @@ mod test {
assert_eq!(
"0.1.0",
empty_release.calculate_next_version_with_config(&Bump {
features_always_bump_minor: Some(features_always_bump_minor),
breaking_always_bump_major: Some(breaking_always_bump_major),
initial_tag: None,
features_always_bump_minor: Some(features_always_bump_minor),
breaking_always_bump_major: Some(breaking_always_bump_major),
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_as: None,
})?
);
}
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 @@ -193,6 +193,9 @@ pub struct Opt {
/// Bumps the version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bump: bool,
/// Bumps the version for unreleased changes with specified change.
#[arg(long, value_name = "BUMP_AS")]
pub bump_as: Option<String>,
/// Prints bumped version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bumped_version: bool,
Expand Down
1 change: 1 addition & 0 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ pub fn run(mut args: Opt) -> Result<()> {
}

// Process commits and releases for the changelog.
config.bump.bump_as = args.bump_as;
let mut changelog = Changelog::new(releases, &config)?;

// Print the result.
Expand Down

0 comments on commit 69fc637

Please sign in to comment.