Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bump): support bumping to a specific semver type #744

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ impl Remote {
}
}

/// Version bump type
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum BumpType {
/// Bump major version
Major,
/// Bump minor version
Minor,
/// Bump patch version
Patch,
}

/// Bump version configuration.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Bump {
Expand Down Expand Up @@ -218,6 +229,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_type: Option<BumpType>,
}

/// Parser for grouping commits.
Expand Down
44 changes: 32 additions & 12 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::commit::Commit;
use crate::config::Bump;
use crate::error::Result;
#[cfg(feature = "remote")]
use crate::remote::{
Expand All @@ -8,7 +6,16 @@ use crate::remote::{
RemotePullRequest,
RemoteReleaseMetadata,
};
use next_version::VersionUpdater;
use crate::{
commit::Commit,
config::Bump,
config::BumpType,
};

use next_version::{
NextVersion,
VersionUpdater,
};
use semver::Version;
use serde::{
Deserialize,
Expand Down Expand Up @@ -124,15 +131,24 @@ 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_type) = &config.bump_type {
match bump_type {
BumpType::Major => semver?.increment_major().to_string(),
BumpType::Minor => semver?.increment_minor().to_string(),
BumpType::Patch => semver?.increment_patch().to_string(),
}
} 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 @@ -282,6 +298,7 @@ mod test {
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_type: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -305,6 +322,7 @@ mod test {
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_type: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -328,6 +346,7 @@ mod test {
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_type: None,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -351,6 +370,7 @@ mod test {
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_type: None,
})?
);
}
Expand Down
62 changes: 59 additions & 3 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use clap::{
ValueEnum,
};
use git_cliff_core::{
config::BumpType,
config::Remote,
DEFAULT_CONFIG,
DEFAULT_OUTPUT,
Expand All @@ -29,6 +30,12 @@ pub enum Strip {
All,
}

#[derive(Debug, Clone)]
pub enum BumpOption {
Auto,
Specific(BumpType),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Sort {
Oldest,
Expand Down Expand Up @@ -190,9 +197,16 @@ pub struct Opt {
allow_hyphen_values = true
)]
pub tag: Option<String>,
/// Bumps the version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bump: bool,
/// Bumps the version for unreleased changes. Optionally with specified
/// version.
#[arg(
long,
value_name = "BUMP",
value_enum,
num_args = 0..=1,
default_missing_value = "auto",
value_parser = clap::value_parser!(BumpOption))]
pub bump: Option<BumpOption>,
/// Prints bumped version for unreleased changes.
#[arg(long, help_heading = Some("FLAGS"))]
pub bumped_version: bool,
Expand Down Expand Up @@ -352,6 +366,48 @@ impl TypedValueParser for RemoteValueParser {
}
}

impl ValueParserFactory for BumpOption {
type Parser = BumpOptionParser;
fn value_parser() -> Self::Parser {
BumpOptionParser
}
}

/// Parsr for bump type
#[derive(Clone, Debug)]
pub struct BumpOptionParser;

impl TypedValueParser for BumpOptionParser {
type Value = BumpOption;
fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let inner = clap::builder::StringValueParser::new();
let value = inner.parse_ref(cmd, arg, value)?;
match value.as_str() {
"auto" => Ok(BumpOption::Auto),
"major" => Ok(BumpOption::Specific(BumpType::Major)),
"minor" => Ok(BumpOption::Specific(BumpType::Minor)),
"patch" => Ok(BumpOption::Specific(BumpType::Patch)),
_ => {
let mut err =
clap::Error::new(ErrorKind::ValueValidation).with_cmd(cmd);
if let Some(arg) = arg {
err.insert(
ContextKind::InvalidArg,
ContextValue::String(arg.to_string()),
);
}
err.insert(ContextKind::InvalidValue, ContextValue::String(value));
Err(err)
}
}
}
}

impl Opt {
/// Custom string parser for directories.
///
Expand Down
6 changes: 5 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod logger;
extern crate log;

use args::{
BumpOption,
Opt,
Sort,
Strip,
Expand Down Expand Up @@ -546,10 +547,13 @@ pub fn run(mut args: Opt) -> Result<()> {
}

// Process commits and releases for the changelog.
if let Some(BumpOption::Specific(bump_type)) = args.bump {
config.bump.bump_type = Some(bump_type)
}
let mut changelog = Changelog::new(releases, &config)?;

// Print the result.
if args.bump || args.bumped_version {
if args.bump.is_some() || args.bumped_version {
let next_version = if let Some(next_version) = changelog.bump_version()? {
next_version
} else if let Some(last_version) =
Expand Down
2 changes: 1 addition & 1 deletion website/docs/usage/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
-h, --help Prints help information
-V, --version Prints version information
-v, --verbose... Increases the logging verbosity
--bump Bumps the version for unreleased changes
--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 All @@ -28,6 +27,7 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]

```
-i, --init [<CONFIG>] Writes the default configuration file to cliff.toml
--bump Bumps the version for unreleased changes [default: auto] [possible values: auto, major, minor, patch]
-c, --config <PATH> Sets the configuration file [env: GIT_CLIFF_CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: GIT_CLIFF_WORKDIR=]
-r, --repository <PATH>... Sets the git repository [env: GIT_CLIFF_REPOSITORY=]
Expand Down
9 changes: 9 additions & 0 deletions website/docs/usage/bump-version.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ A switch from `0` to `1` should indicate a higher API stability level.

You can modify the bumping rules to preserve the zero-based versioning scheme in the
[configuration file](/docs/configuration/bump).


## Bump to specific version type

Optionally you can specify bump type in `--bump`:

```bash
git cliff --bump [major|minor|patch]
```
Loading