Skip to content

Commit

Permalink
feat(bump): support bumping to a specific semver type (#744)
Browse files Browse the repository at this point in the history
* feat: support bump a specific version type

* let --bump accept optional enum

* doc: update document for --bump

* test: add bump version fixtures tests

* refactor: polish implementation

* refactor: fix typo

---------

Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
  • Loading branch information
braineo and orhun authored Jul 31, 2024
1 parent e2fb043 commit 9dbf47b
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 18 deletions.
27 changes: 27 additions & 0 deletions .github/fixtures/test-bump-version-major/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[changelog]
# template for the changelog footer
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}]
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
footer = """
<!-- generated by git-cliff -->
"""
# remove the leading and trailing whitespace from the templates
trim = true
9 changes: 9 additions & 0 deletions .github/fixtures/test-bump-version-major/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 1"
GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 2"
git tag v0.1.0

GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "fix: fix feature 1"
GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "fix: fix feature 2"
19 changes: 19 additions & 0 deletions .github/fixtures/test-bump-version-major/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

## [1.0.0]

### Fix

- Fix feature 1
- Fix feature 2

## [0.1.0]

### Feat

- Add feature 1
- Add feature 2

<!-- generated by git-cliff -->
27 changes: 27 additions & 0 deletions .github/fixtures/test-bump-version-minor/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[changelog]
# template for the changelog footer
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}]
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
footer = """
<!-- generated by git-cliff -->
"""
# remove the leading and trailing whitespace from the templates
trim = true
9 changes: 9 additions & 0 deletions .github/fixtures/test-bump-version-minor/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 1"
GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 2"
git tag v0.1.0

GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat!: add breaking feature"
GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "fix: fix feature 2"
22 changes: 22 additions & 0 deletions .github/fixtures/test-bump-version-minor/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

All notable changes to this project will be documented in this file.

## [0.2.0]

### Feat

- [**breaking**] Add breaking feature

### Fix

- Fix feature 2

## [0.1.0]

### Feat

- Add feature 1
- Add feature 2

<!-- generated by git-cliff -->
27 changes: 27 additions & 0 deletions .github/fixtures/test-bump-version-patch/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[changelog]
# template for the changelog footer
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}]
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
footer = """
<!-- generated by git-cliff -->
"""
# remove the leading and trailing whitespace from the templates
trim = true
9 changes: 9 additions & 0 deletions .github/fixtures/test-bump-version-patch/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 1"
GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: add feature 2"
git tag v0.1.0

GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat!: add breaking feature"
GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "fix: fix feature 2"
22 changes: 22 additions & 0 deletions .github/fixtures/test-bump-version-patch/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

All notable changes to this project will be documented in this file.

## [0.1.1]

### Feat

- [**breaking**] Add breaking feature

### Fix

- Fix feature 2

## [0.1.0]

### Feat

- Add feature 1
- Add feature 2

<!-- generated by git-cliff -->
6 changes: 6 additions & 0 deletions .github/workflows/test-fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jobs:
- fixtures-name: test-split-commits
- fixtures-name: test-bump-version
command: --bump
- fixtures-name: test-bump-version-major
command: --bump major
- fixtures-name: test-bump-version-minor
command: --bump minor
- fixtures-name: test-bump-version-patch
command: --bump patch
- fixtures-name: test-bump-version-custom-minor
command: --bump
- fixtures-name: test-bumped-version
Expand Down
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, Eq, PartialEq)]
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
43 changes: 31 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,23 @@ 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 +297,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 +321,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 +345,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 +369,7 @@ mod test {
initial_tag: None,
custom_major_increment_regex: None,
custom_minor_increment_regex: None,
bump_type: None,
})?
);
}
Expand Down
Loading

0 comments on commit 9dbf47b

Please sign in to comment.