From 3291eb99acf228086633852fac9b78c30e0db673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Mon, 25 Dec 2023 05:34:15 +0300 Subject: [PATCH] feat(changelog)!: set tag to `0.0.1` via `--bump` if no tags exist closes #344 --- git-cliff-core/src/error.rs | 3 --- git-cliff-core/src/release.rs | 43 +++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/git-cliff-core/src/error.rs b/git-cliff-core/src/error.rs index a3acb63a5f..e9a76d4860 100644 --- a/git-cliff-core/src/error.rs +++ b/git-cliff-core/src/error.rs @@ -65,9 +65,6 @@ pub enum Error { /// requirement. #[error("Semver error: `{0}`")] SemverError(#[from] semver::Error), - /// Error that may occur when a version is not found for the next release. - #[error("Previous version is not found for calculating the next release.")] - PreviousVersionNotFound, } /// Result type of the core library. diff --git a/git-cliff-core/src/release.rs b/git-cliff-core/src/release.rs index 8401a8193f..18296bfbfd 100644 --- a/git-cliff-core/src/release.rs +++ b/git-cliff-core/src/release.rs @@ -1,8 +1,5 @@ use crate::commit::Commit; -use crate::error::{ - Error, - Result, -}; +use crate::error::Result; use next_version::NextVersion; use semver::Version; use serde::{ @@ -30,20 +27,27 @@ pub struct Release<'a> { impl<'a> Release<'a> { /// Calculates the next version based on the commits. pub fn calculate_next_version(&self) -> Result { - let version = self + match self .previous .as_ref() .and_then(|release| release.version.clone()) - .ok_or_else(|| Error::PreviousVersionNotFound)?; - let next_version = Version::parse(version.trim_start_matches('v'))? - .next( - self.commits - .iter() - .map(|commit| commit.message.trim_end().to_string()) - .collect::>(), - ) - .to_string(); - Ok(next_version) + { + Some(version) => { + let next_version = Version::parse(version.trim_start_matches('v'))? + .next( + self.commits + .iter() + .map(|commit| commit.message.trim_end().to_string()) + .collect::>(), + ) + .to_string(); + Ok(next_version) + } + None => { + warn!("No releases found, using 0.0.1 as the next version."); + Ok(String::from("0.0.1")) + } + } } } @@ -88,6 +92,15 @@ mod test { let next_version = release.calculate_next_version()?; assert_eq!(expected_version, next_version); } + let empty_release = Release { + previous: Some(Box::new(Release { + version: None, + ..Default::default() + })), + ..Default::default() + }; + let next_version = empty_release.calculate_next_version()?; + assert_eq!("0.0.1", next_version); Ok(()) } }