Skip to content

Commit

Permalink
feat(changelog)!: set tag to 0.0.1 via --bump if no tags exist
Browse files Browse the repository at this point in the history
closes #344
  • Loading branch information
orhun committed Dec 25, 2023
1 parent 514ca4b commit 3291eb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
3 changes: 0 additions & 3 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 28 additions & 15 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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<String> {
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::<Vec<String>>(),
)
.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::<Vec<String>>(),
)
.to_string();
Ok(next_version)
}
None => {
warn!("No releases found, using 0.0.1 as the next version.");
Ok(String::from("0.0.1"))
}
}
}
}

Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 3291eb9

Please sign in to comment.