Repository: orhun/git-cliff
Pull request #701 (2024-06-15 02:24:52)

feat(bump): support setting the `initial_tag`
## Description
When using `bump` and no tags are found we can modify default hardcoded `0.1.0`
<!--- Describe your changes in detail -->

## Motivation and Context
When using tags like `package-name-v0.0.0` I would like proper initial version.

<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

## How Has This Been Tested?
Fixtures included.

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Screenshots / Logs (if applicable)

## Types of Changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [x] Documentation (no code change)
- [ ] Refactor (refactoring production code)
- [ ] Other <!--- (provide information) -->

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] My code follows the code style of this project.
- [x] I have updated the documentation accordingly.
- [x] I have formatted the code with [rustfmt](https://github.com/rust-lang/rustfmt).
- [x] I checked the lints with [clippy](https://github.com/rust-lang/rust-clippy).
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.

<!--- Thank you for contributing to git-cliff! ⛰️  -->



Patch:
diff --git a/git-cliff-core/src/config.rs b/git-cliff-core/src/config.rs
index bd20264..be932de 100644
--- a/git-cliff-core/src/config.rs
+++ b/git-cliff-core/src/config.rs
@@ -192,6 +192,11 @@ pub struct Bump {
 	/// - A minor version update if the major version is 0.
 	/// - A major version update otherwise.
 	pub breaking_always_bump_major: Option<bool>,
+
+	/// Configures the initial version of the project.
+	///
+	/// When set, the version will be set to this value if no tags are found.
+	pub initial_tag: Option<String>,
 }
 
 /// Parser for grouping commits.
diff --git a/git-cliff-core/src/release.rs b/git-cliff-core/src/release.rs
index b256813..8cf54ef 100644
--- a/git-cliff-core/src/release.rs
+++ b/git-cliff-core/src/release.rs
@@ -114,10 +114,20 @@ impl<'a> Release<'a> {
 					Ok(next_version)
 				}
 			}
-			None => {
-				warn!("No releases found, using 0.1.0 as the next version.");
-				Ok(String::from("0.1.0"))
-			}
+			None => match config.initial_tag.clone() {
+				Some(tag) => {
+					warn!(
+						"No releases found, using initial tag '{}' as the next \
+						 version.",
+						tag
+					);
+					Ok(tag)
+				}
+				None => {
+					warn!("No releases found, using 0.1.0 as the next version.");
+					Ok(String::from("0.1.0"))
+				}
+			},
 		}
 	}
 }
@@ -239,6 +249,7 @@ mod test {
 				release.calculate_next_version_with_config(&Bump {
 					features_always_bump_minor: Some(false),
 					breaking_always_bump_major: Some(false),
+					initial_tag:                None,
 				})?;
 			assert_eq!(expected_version, &next_version);
 		}
@@ -259,6 +270,7 @@ mod test {
 				release.calculate_next_version_with_config(&Bump {
 					features_always_bump_minor: Some(true),
 					breaking_always_bump_major: Some(false),
+					initial_tag:                None,
 				})?;
 			assert_eq!(expected_version, &next_version);
 		}
@@ -279,6 +291,7 @@ mod test {
 				release.calculate_next_version_with_config(&Bump {
 					features_always_bump_minor: Some(false),
 					breaking_always_bump_major: Some(true),
+					initial_tag:                None,
 				})?;
 			assert_eq!(expected_version, &next_version);
 		}
@@ -299,6 +312,7 @@ mod test {
 				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,
 				})?
 			);
 		}
diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs
index f8cc86c..9c6d751 100644
--- a/git-cliff/src/lib.rs
+++ b/git-cliff/src/lib.rs
@@ -473,6 +473,10 @@ pub fn run(mut args: Opt) -> Result<()> {
 		config.git.tag_pattern.clone_from(&args.tag_pattern);
 	}
 
+	if args.tag.is_some() {
+		config.bump.initial_tag.clone_from(&args.tag);
+	}
+
 	// Process the repositories.
 	let repositories = args.repository.clone().unwrap_or(vec![env::current_dir()?]);
 	let mut releases = Vec::<Release>::new();
diff --git a/website/docs/integration/github.md b/website/docs/integration/github.md
index 98e40aa..cc3a918 100644
--- a/website/docs/integration/github.md
+++ b/website/docs/integration/github.md
@@ -208,4 +208,4 @@ This will generate a changelog such as:
 <!-- generated by git-cliff -->
 ```
 
-Alternatively, you can use [`github-keepachangelog.toml`](https://github.com/orhun/git-cliff/tree/main/examples/github-keepachangelog.toml) template which is a mix of GitHub and [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) formats.
+Alternatively, you can use [`github-keepachangelog.toml`](https://github.com/orhun/git-cliff/tree/main/examples/github.toml) template which is a mix of GitHub and [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) formats.
diff --git a/website/docs/usage/bump-version.md b/website/docs/usage/bump-version.md
index 6478af9..902d92b 100644
--- a/website/docs/usage/bump-version.md
+++ b/website/docs/usage/bump-version.md
@@ -10,11 +10,7 @@ To calculate and set the next semantic version (i.e. _bump the version_) for the
 git cliff --bump
 ```
 
-- Basic:
-  - For example, if you have `1.0.0` and committed "feat: xyz", `git-cliff --bump --unreleased` will create a changelog for `1.1.0`.
-
-- Tag prefixes:
-    - Tag prefixes are also supported, for example `testing/v1.0.0-beta.1` can be updated to `testing/v1.0.0-beta.2`
+For example, if you have `1.0.0` and committed "feat: xyz", `git-cliff --bump --unreleased` will create a changelog for `1.1.0`.
 
 How it works is that for a semantic versioning such as `<MAJOR>.<MINOR>.<PATCH>`:
 


Test patch:
diff --git a/.github/fixtures/test-bump-initial-tag-cli-arg/cliff.toml b/.github/fixtures/test-bump-initial-tag-cli-arg/cliff.toml
new file mode 100644
index 00000000..d001b84
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-cli-arg/cliff.toml
@@ -0,0 +1,37 @@
+[changelog]
+# changelog header
+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 %}
+        - {{ 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
+
+[git]
+# regex for parsing and grouping commits
+commit_parsers = [
+    { message = "^feat", group = "Features", default_scope = "app" },
+    { message = "^fix", group = "Bug Fixes", scope = "cli" },
+]
+
+[bump]
+initial_tag = "asdasd"
\ No newline at end of file
diff --git a/.github/fixtures/test-bump-initial-tag-cli-arg/commit.sh b/.github/fixtures/test-bump-initial-tag-cli-arg/commit.sh
new file mode 100755
index 00000000..8897f4d
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-cli-arg/commit.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -e
+
+GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests"
diff --git a/.github/fixtures/test-bump-initial-tag-cli-arg/expected.md b/.github/fixtures/test-bump-initial-tag-cli-arg/expected.md
new file mode 100644
index 00000000..f7e325a
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-cli-arg/expected.md
@@ -0,0 +1,11 @@
+# Changelog
+
+All notable changes to this project will be documented in this file.
+
+## [2.1.1]
+
+### Test
+
+- Add tests
+
+<!-- generated by git-cliff -->
diff --git a/.github/fixtures/test-bump-initial-tag-default/cliff.toml b/.github/fixtures/test-bump-initial-tag-default/cliff.toml
new file mode 100644
index 00000000..5da431c
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-default/cliff.toml
@@ -0,0 +1,34 @@
+[changelog]
+# changelog header
+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 %}
+        - {{ 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
+
+[git]
+# regex for parsing and grouping commits
+commit_parsers = [
+    { message = "^feat", group = "Features", default_scope = "app" },
+    { message = "^fix", group = "Bug Fixes", scope = "cli" },
+]
diff --git a/.github/fixtures/test-bump-initial-tag-default/commit.sh b/.github/fixtures/test-bump-initial-tag-default/commit.sh
new file mode 100755
index 00000000..8897f4d
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-default/commit.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -e
+
+GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests"
diff --git a/.github/fixtures/test-bump-initial-tag-default/expected.md b/.github/fixtures/test-bump-initial-tag-default/expected.md
new file mode 100644
index 00000000..8b2acf5
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag-default/expected.md
@@ -0,0 +1,11 @@
+# Changelog
+
+All notable changes to this project will be documented in this file.
+
+## [0.1.0]
+
+### Test
+
+- Add tests
+
+<!-- generated by git-cliff -->
diff --git a/.github/fixtures/test-bump-initial-tag/cliff.toml b/.github/fixtures/test-bump-initial-tag/cliff.toml
new file mode 100644
index 00000000..38080e4
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag/cliff.toml
@@ -0,0 +1,37 @@
+[changelog]
+# changelog header
+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 %}
+        - {{ 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
+
+[git]
+# regex for parsing and grouping commits
+commit_parsers = [
+    { message = "^feat", group = "Features", default_scope = "app" },
+    { message = "^fix", group = "Bug Fixes", scope = "cli" },
+]
+
+[bump]
+initial_tag = "1.15.5"
\ No newline at end of file
diff --git a/.github/fixtures/test-bump-initial-tag/commit.sh b/.github/fixtures/test-bump-initial-tag/commit.sh
new file mode 100755
index 00000000..8897f4d
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag/commit.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -e
+
+GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests"
diff --git a/.github/fixtures/test-bump-initial-tag/expected.md b/.github/fixtures/test-bump-initial-tag/expected.md
new file mode 100644
index 00000000..d3b19c4
--- /dev/null
+++ b/.github/fixtures/test-bump-initial-tag/expected.md
@@ -0,0 +1,11 @@
+# Changelog
+
+All notable changes to this project will be documented in this file.
+
+## [1.15.5]
+
+### Test
+
+- Add tests
+
+<!-- generated by git-cliff -->
diff --git a/.github/workflows/test-fixtures.yml b/.github/workflows/test-fixtures.yml
index 2e35cf7..c275923 100644
--- a/.github/workflows/test-fixtures.yml
+++ b/.github/workflows/test-fixtures.yml
@@ -68,6 +68,12 @@ jobs:
           - fixtures-name: test-custom-tag-pattern
             command: --tag-pattern "alpha.*"
           - fixtures-name: test-configure-from-cargo-toml
+          - fixtures-name: test-bump-initial-tag
+            command: --bump
+          - fixtures-name: test-bump-initial-tag-default
+            command: --bump
+          - fixtures-name: test-bump-initial-tag-cli-arg
+            command: --bump --tag=2.1.1
     steps:
       - name: Checkout
         uses: actions/checkout@v4