Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
oknozor committed Oct 22, 2021
1 parent 10771f4 commit 7088bc6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,6 @@

# Ignore library's lock file
git-cliff-core/Cargo.lock

# Jetbrains IDE
.idea
61 changes: 12 additions & 49 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions git-cliff-core/Cargo.toml
Expand Up @@ -18,6 +18,7 @@ regex = "1.5.4"
serde_regex = "1.1.0"
indexmap = "1.7.0"
toml = "0.5.8"
conventional_commit_parser = "0.7.0"

[dependencies.git2]
version = "0.13.22"
Expand All @@ -28,10 +29,6 @@ version = "0.11.0"
default-features = false
features = ["toml", "yaml"]

[dependencies.git-conventional]
version = "0.11.0"
features = ["serde"]

[dependencies.rust-embed]
version = "6.2.0"
features = ["debug-embed"]
Expand Down
30 changes: 12 additions & 18 deletions git-cliff-core/src/commit.rs
Expand Up @@ -3,14 +3,13 @@ use crate::error::{
Error as AppError,
Result,
};
use conventional_commit_parser::commit::ConventionalCommit;
use git2::Commit as GitCommit;
use git_conventional::Commit as ConventionalCommit;
use serde::ser::{
Serialize,
SerializeStruct,
Serializer,
};

/// Common commit object that is parsed from a repository.
#[derive(Debug, Clone, PartialEq, serde_derive::Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -71,11 +70,9 @@ impl Commit<'_> {

/// Returns the commit with its conventional type set.
pub fn into_conventional(mut self) -> Result<Self> {
match ConventionalCommit::parse(Box::leak(
self.message.to_string().into_boxed_str(),
)) {
match conventional_commit_parser::parse(Box::leak(self.message.to_string().into_boxed_str())) {
Ok(conv) => {
self.conv = Some(conv);
self.conv = Some(conv.clone());
Ok(self)
}
Err(e) => Err(AppError::ParseError(e)),
Expand Down Expand Up @@ -126,32 +123,29 @@ impl Serialize for Commit<'_> {
commit.serialize_field("id", &self.id)?;
match &self.conv {
Some(conv) => {
commit.serialize_field("message", conv.description())?;
commit.serialize_field("body", &conv.body())?;
commit.serialize_field("message", conv.summary)?;
commit.serialize_field("body", &conv.body)?;
commit.serialize_field(
"footers",
&conv
.footers()
.to_vec()
.footers
.iter()
.map(|f| f.value())
.map(|f| f.content)
.collect::<Vec<&str>>(),
)?;
commit.serialize_field(
"group",
self.group.as_ref().unwrap_or(&conv.type_().to_string()),
self.group.as_ref().unwrap_or(&conv.commit_type.to_string()),
)?;

commit.serialize_field(
"breaking_description",
&conv.breaking_description(),
&conv.breaking_change_description()
)?;
commit.serialize_field("breaking", &conv.breaking())?;
commit.serialize_field("breaking", &conv.is_breaking_change)?;
commit.serialize_field(
"scope",
&conv
.scope()
.map(|v| v.as_str())
.or_else(|| self.scope.as_deref()),
&conv.scope
)?;
}
None => {
Expand Down
16 changes: 7 additions & 9 deletions git-cliff-core/src/error.rs
Expand Up @@ -19,7 +19,7 @@ pub enum Error {
/// When commit's not follow the conventional commit structure we throw this
/// error.
#[error("Cannot parse the commit: `{0}`")]
ParseError(#[from] git_conventional::Error),
ParseError(#[from] conventional_commit_parser::error::ParseError),
/// Error that may occur while grouping commits.
#[error("Grouping error: `{0}`")]
GroupError(String),
Expand All @@ -46,21 +46,19 @@ pub type Result<T> = core::result::Result<T, Error>;
#[cfg(test)]
mod test {
use super::*;
use git_conventional::{
Commit,
ErrorKind,
};
fn mock_function() -> super::Result<Commit<'static>> {
Ok(Commit::parse("test")?)
use conventional_commit_parser::commit::ConventionalCommit;
use conventional_commit_parser::error::ParseErrorKind;

fn mock_function() -> super::Result<ConventionalCommit<'static>> {
Ok(conventional_commit_parser::parse("test")?)
}

#[test]
fn throw_parse_error() {
let actual_error = mock_function().unwrap_err();
let expected_error_kind = ErrorKind::InvalidFormat;
match actual_error {
Error::ParseError(e) => {
assert_eq!(expected_error_kind, e.kind());
assert_eq!(ParseErrorKind::MissingSeparator, e.kind);
}
_ => {
unreachable!()
Expand Down
4 changes: 2 additions & 2 deletions git-cliff-core/src/repo.rs
Expand Up @@ -112,10 +112,10 @@ impl Repository {
mod test {
use super::*;
use crate::commit::Commit as AppCommit;
use git_conventional::ErrorKind;
use std::env;
use std::process::Command;
use std::str;
use conventional_commit_parser::error::ParseErrorKind;

fn get_last_commit_hash() -> Result<String> {
Ok(str::from_utf8(
Expand Down Expand Up @@ -157,7 +157,7 @@ mod test {
if let Err(e) = last_commit.into_conventional() {
match e {
Error::ParseError(e) => {
assert_eq!(ErrorKind::InvalidFormat, e.kind())
assert_eq!(ParseErrorKind::MissingSeparator, e.kind)
}
_ => {
unreachable!()
Expand Down
2 changes: 1 addition & 1 deletion git-cliff/src/changelog.rs
Expand Up @@ -55,7 +55,7 @@ impl<'a> Changelog<'a> {
Err(e) => {
trace!(
"{} - {} ({})",
commit.id[..7].to_string(),
commit.id[..6].to_string(),
e,
commit
.message
Expand Down

0 comments on commit 7088bc6

Please sign in to comment.