Skip to content

Commit

Permalink
feat(bump): support bumping based on configurable custom pattern (#725)
Browse files Browse the repository at this point in the history
* chore(deps): bump next_version from 0.2.17 to 0.2.18

Bumps [next_version](https://github.com/MarcoIeni/release-plz) from 0.2.17 to 0.2.18.
- [Release notes](https://github.com/MarcoIeni/release-plz/releases)
- [Changelog](https://github.com/MarcoIeni/release-plz/blob/main/CHANGELOG.md)
- [Commits](release-plz/release-plz@next_version-v0.2.17...next_version-v0.2.18)

---
updated-dependencies:
- dependency-name: next_version
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* feat: Support bumping version based on configurable custom pattern

closes #717

* refactor: clean up implementation

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2024
1 parent d642d33 commit 8e03356
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 17 deletions.
32 changes: 32 additions & 0 deletions .github/fixtures/test-bump-version-custom-minor/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[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

[bump]
features_always_bump_minor = true
breaking_always_bump_major = true
custom_minor_increment_regex = "minor|more"
9 changes: 9 additions & 0 deletions .github/fixtures/test-bump-version-custom-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 "minor: add minor"
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-custom-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]

### Fix

- Fix feature 2

### Minor

- Add minor

## [0.1.0]

### Feat

- Add feature 1
- Add feature 2

<!-- generated by git-cliff -->
2 changes: 2 additions & 0 deletions .github/workflows/test-fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
- fixtures-name: test-split-commits
- fixtures-name: test-bump-version
command: --bump
- fixtures-name: test-bump-version-custom-minor
command: --bump
- fixtures-name: test-bumped-version
command: --bumped-version
- fixtures-name: test-footer-template
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tera = "1.20.0"
indexmap = { version = "2.2.6", optional = true }
toml = "0.8.14"
lazy-regex = "3.1.0"
next_version = "0.2.17"
next_version = "0.2.18"
semver = "1.0.23"
document-features = { version = "0.2.8", optional = true }
reqwest = { version = "0.12.5", default-features = false, features = [
Expand Down
18 changes: 18 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ pub struct Bump {
///
/// When set, the version will be set to this value if no tags are found.
pub initial_tag: Option<String>,

/// Configure a custom regex pattern for major version increments.
///
/// This will check only the type of the commit against the given pattern.
///
/// ### Note
///
/// `commit type` according to the spec is only `[a-zA-Z]+`
pub custom_major_increment_regex: Option<String>,

/// Configure a custom regex pattern for minor version increments.
///
/// This will check only the type of the commit against the given pattern.
///
/// ### Note
///
/// `commit type` according to the spec is only `[a-zA-Z]+`
pub custom_minor_increment_regex: Option<String>,
}

/// Parser for grouping commits.
Expand Down
51 changes: 37 additions & 14 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,28 @@ impl<'a> Release<'a> {
}
}
}
let next_version = VersionUpdater::new()
let mut next_version = VersionUpdater::new()
.with_features_always_increment_minor(
config.features_always_bump_minor.unwrap_or(true),
)
.with_breaking_always_increment_major(
config.breaking_always_bump_major.unwrap_or(true),
)
);
if let Some(custom_major_increment_regex) =
&config.custom_major_increment_regex
{
next_version = next_version.with_custom_major_increment_regex(
custom_major_increment_regex,
)?;
}
if let Some(custom_minor_increment_regex) =
&config.custom_minor_increment_regex
{
next_version = next_version.with_custom_minor_increment_regex(
custom_minor_increment_regex,
)?;
}
let next_version = next_version
.increment(
&semver?,
self.commits
Expand Down Expand Up @@ -256,9 +271,11 @@ 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,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -277,9 +294,11 @@ 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,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -298,9 +317,11 @@ 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,
})?;
assert_eq!(expected_version, &next_version);
}
Expand All @@ -319,9 +340,11 @@ 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,
})?
);
}
Expand Down
48 changes: 48 additions & 0 deletions website/docs/configuration/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,51 @@ When `false`, a breaking change commit will trigger:
Configures the initial version of the project.

When set, the version will be set to this value if no tags are found.

### custom_major_increment_regex & custom_minor_increment_regex

Configures additional commit types that should increment the major or minor accordingly.

They should be used rarely, only in the case you have a spacial case for incrementing versions.

Expects a valid regex pattern.

For example:

```toml
[bump]
features_always_bump_minor = true
breaking_always_bump_major = true
custom_major_increment_regex = "major"
custom_minor_increment_regex = "minor|more"
```

with this history:

```
5189568 (HEAD -> main) major: 1
0b17b48 (tag: 0.1.0) initial commit
```

will result in:

```bash
git-cliff --bumped-version

1.0.0
```

or, with history:

```
47206d0 (HEAD -> main) more: 1
0b17b48 (tag: 0.1.0) initial commit
```

will result in:

```bash
git-cliff --bumped-version

0.2.0
```

0 comments on commit 8e03356

Please sign in to comment.