Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-cliff-core"
version = "2.10.1" # managed by release.sh
version = "2.10.1" # managed by release.sh
description = "Core library of git-cliff"
authors = ["git-cliff contributors <git-cliff@protonmail.com>"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -75,6 +75,7 @@ urlencoding = "2.1.3"
cacache = { version = "=13.0.0", features = ["mmap"], default-features = false }
time = "0.3.37"
chrono = { version = "0.4.41", features = ["serde"] }
schemars = "1.0.4"

[dependencies.git2]
version = "0.20.2"
Expand All @@ -87,8 +88,9 @@ default-features = false
features = ["toml", "yaml"]

[dependencies.git-conventional]
version = "0.12.7"
features = ["serde"]
# version = "0.12.7"
git = "https://github.com/greenhat616/git-conventional"
features = ["serde", "schemars"]

[dependencies.rust-embed]
version = "8.7.2"
Expand Down
121 changes: 77 additions & 44 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;
use std::io::{Read, Write};
use std::time::{SystemTime, UNIX_EPOCH};

use schemars::JsonSchema;

use crate::commit::Commit;
use crate::config::{Config, GitConfig};
use crate::error::{Error, Result};
Expand Down Expand Up @@ -215,8 +217,8 @@ impl<'a> Changelog<'a> {
release
.previous
.as_ref()
.and_then(|release| release.version.as_ref()) ==
Some(skipped_tag)
.and_then(|release| release.version.as_ref())
== Some(skipped_tag)
}) {
if let Some(previous_release) = self.releases.get_mut(release_index + 1) {
previous_release.previous = None;
Expand Down Expand Up @@ -245,10 +247,12 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "github")]
fn get_github_metadata(&self, ref_name: Option<&str>) -> Result<crate::remote::RemoteMetadata> {
use crate::remote::github;
if self.config.remote.github.is_custom ||
self.body_template
.contains_variable(github::TEMPLATE_VARIABLES) ||
self.footer_template
if self.config.remote.github.is_custom
|| self
.body_template
.contains_variable(github::TEMPLATE_VARIABLES)
|| self
.footer_template
.as_ref()
.map(|v| v.contains_variable(github::TEMPLATE_VARIABLES))
.unwrap_or(false)
Expand Down Expand Up @@ -296,10 +300,12 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "gitlab")]
fn get_gitlab_metadata(&self, ref_name: Option<&str>) -> Result<crate::remote::RemoteMetadata> {
use crate::remote::gitlab;
if self.config.remote.gitlab.is_custom ||
self.body_template
.contains_variable(gitlab::TEMPLATE_VARIABLES) ||
self.footer_template
if self.config.remote.gitlab.is_custom
|| self
.body_template
.contains_variable(gitlab::TEMPLATE_VARIABLES)
|| self
.footer_template
.as_ref()
.map(|v| v.contains_variable(gitlab::TEMPLATE_VARIABLES))
.unwrap_or(false)
Expand Down Expand Up @@ -354,10 +360,12 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "gitea")]
fn get_gitea_metadata(&self, ref_name: Option<&str>) -> Result<crate::remote::RemoteMetadata> {
use crate::remote::gitea;
if self.config.remote.gitea.is_custom ||
self.body_template
.contains_variable(gitea::TEMPLATE_VARIABLES) ||
self.footer_template
if self.config.remote.gitea.is_custom
|| self
.body_template
.contains_variable(gitea::TEMPLATE_VARIABLES)
|| self
.footer_template
.as_ref()
.map(|v| v.contains_variable(gitea::TEMPLATE_VARIABLES))
.unwrap_or(false)
Expand Down Expand Up @@ -408,10 +416,12 @@ impl<'a> Changelog<'a> {
ref_name: Option<&str>,
) -> Result<crate::remote::RemoteMetadata> {
use crate::remote::bitbucket;
if self.config.remote.bitbucket.is_custom ||
self.body_template
.contains_variable(bitbucket::TEMPLATE_VARIABLES) ||
self.footer_template
if self.config.remote.bitbucket.is_custom
|| self
.body_template
.contains_variable(bitbucket::TEMPLATE_VARIABLES)
|| self
.footer_template
.as_ref()
.map(|v| v.contains_variable(bitbucket::TEMPLATE_VARIABLES))
.unwrap_or(false)
Expand Down Expand Up @@ -612,6 +622,18 @@ impl<'a> Changelog<'a> {
writeln!(out, "{output}")?;
Ok(())
}

/// Returns the context json schema for the releases.
pub fn context_schema() -> Result<String> {
use schemars::schema_for;
#[derive(JsonSchema)]
#[repr(transparent)]
/// Context for the changelog.
struct Context<'a>(Vec<Release<'a>>);
let schema = schema_for!(Context);
let schema_json = serde_json::to_string_pretty(&schema)?;
Ok(schema_json)
}
}

fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
Expand Down Expand Up @@ -1032,24 +1054,29 @@ mod test {
timestamp: Some(50000000),
previous: None,
repository: Some(String::from("/root/repo")),
submodule_commits: HashMap::from([(String::from("submodule_one"), vec![
Commit::new(
String::from("sub0jkl12"),
String::from("chore(app): submodule_one do nothing"),
),
Commit::new(
String::from("subqwerty"),
String::from("chore: submodule_one <preprocess>"),
),
Commit::new(
String::from("subqwertz"),
String::from("feat!: submodule_one support breaking commits"),
),
Commit::new(
String::from("subqwert0"),
String::from("match(group): submodule_one support regex-replace for groups"),
),
])]),
submodule_commits: HashMap::from([(
String::from("submodule_one"),
vec![
Commit::new(
String::from("sub0jkl12"),
String::from("chore(app): submodule_one do nothing"),
),
Commit::new(
String::from("subqwerty"),
String::from("chore: submodule_one <preprocess>"),
),
Commit::new(
String::from("subqwertz"),
String::from("feat!: submodule_one support breaking commits"),
),
Commit::new(
String::from("subqwert0"),
String::from(
"match(group): submodule_one support regex-replace for groups",
),
),
],
)]),
statistics: None,
#[cfg(feature = "github")]
github: crate::remote::RemoteReleaseMetadata {
Expand Down Expand Up @@ -1158,14 +1185,20 @@ mod test {
previous: Some(Box::new(test_release)),
repository: Some(String::from("/root/repo")),
submodule_commits: HashMap::from([
(String::from("submodule_one"), vec![
Commit::new(String::from("def349"), String::from("sub_one merge #4")),
Commit::new(String::from("da8912"), String::from("sub_one merge #5")),
]),
(String::from("submodule_two"), vec![Commit::new(
String::from("ab76ef"),
String::from("sub_two bump"),
)]),
(
String::from("submodule_one"),
vec![
Commit::new(String::from("def349"), String::from("sub_one merge #4")),
Commit::new(String::from("da8912"), String::from("sub_one merge #5")),
],
),
(
String::from("submodule_two"),
vec![Commit::new(
String::from("ab76ef"),
String::from("sub_two bump"),
)],
),
]),
statistics: None,
#[cfg(feature = "github")]
Expand Down
15 changes: 8 additions & 7 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use git_conventional::{Commit as ConventionalCommit, Footer as ConventionalFoote
#[cfg(feature = "repo")]
use git2::{Commit as GitCommit, Signature as CommitSignature};
use lazy_regex::{Lazy, Regex, lazy_regex};
use schemars::JsonSchema;
use serde::ser::{SerializeStruct, Serializer};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::Value;
Expand All @@ -14,7 +15,7 @@ use crate::error::{Error as AppError, Result};
static SHA1_REGEX: Lazy<Regex> = lazy_regex!(r#"^\b([a-f0-9]{40})\b (.*)$"#);

/// Object representing a link
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all(serialize = "camelCase"))]
pub struct Link {
/// Text of the link.
Expand All @@ -24,7 +25,7 @@ pub struct Link {
}

/// A conventional commit footer.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
struct Footer<'a> {
/// Token of the footer.
///
Expand Down Expand Up @@ -53,7 +54,7 @@ impl<'a> From<&'a ConventionalFooter<'a>> for Footer<'a> {
}

/// Commit signature that indicates authorship.
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
pub struct Signature {
/// Name on the signature.
pub name: Option<String>,
Expand All @@ -75,7 +76,7 @@ impl<'a> From<CommitSignature<'a>> for Signature {
}

/// Commit range (from..to)
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Range {
/// Full commit SHA the range starts at
from: String,
Expand All @@ -94,7 +95,7 @@ impl Range {
}

/// Common commit object that is parsed from a repository.
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[derive(Debug, Default, Clone, PartialEq, Deserialize, JsonSchema)]
#[serde(rename_all(serialize = "camelCase"))]
pub struct Commit<'a> {
/// Commit ID.
Expand Down Expand Up @@ -259,8 +260,8 @@ impl Commit<'_> {
/// and the commit is breaking, or the parser's `skip` field is None or
/// `false`. Returns `true` otherwise.
fn skip_commit(&self, parser: &CommitParser, protect_breaking: bool) -> bool {
parser.skip.unwrap_or(false) &&
!(self.conv.as_ref().map(|c| c.breaking()).unwrap_or(false) && protect_breaking)
parser.skip.unwrap_or(false)
&& !(self.conv.as_ref().map(|c| c.breaking()).unwrap_or(false) && protect_breaking)
}

/// Parses the commit using [`CommitParser`]s.
Expand Down
Loading
Loading