Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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 <[email protected]>"]
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
14 changes: 14 additions & 0 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 @@ -612,6 +614,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
2 changes: 0 additions & 2 deletions git-cliff-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ pub fn run(command: &str, input: Option<String>, envs: Vec<(&str, &str)>) -> Res

#[cfg(test)]
mod test {
use super::*;

#[test]
#[cfg(target_family = "unix")]
fn run_os_command() -> Result<()> {
Expand Down
11 changes: 6 additions & 5 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
3 changes: 2 additions & 1 deletion git-cliff-core/src/contributor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::hash::{Hash, Hasher};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Representation of a remote contributor.
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
pub struct RemoteContributor {
/// Username.
pub username: Option<String>,
Expand Down
3 changes: 2 additions & 1 deletion git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use next_version::{NextVersion, VersionUpdater};
use schemars::JsonSchema;
use semver::Version;
use serde::{Deserialize, Serialize};
use serde_json::value::Value;
Expand All @@ -16,7 +17,7 @@ use crate::{
};

/// Representation of a release.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all(serialize = "camelCase"))]
pub struct Release<'a> {
/// Release version, git tag.
Expand Down
3 changes: 2 additions & 1 deletion git-cliff-core/src/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheO
use reqwest::Client;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use schemars::JsonSchema;
use secrecy::ExposeSecret;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -100,7 +101,7 @@ dyn_clone::clone_trait_object!(RemotePullRequest);
pub type RemoteMetadata = (Vec<Box<dyn RemoteCommit>>, Vec<Box<dyn RemotePullRequest>>);

/// Metadata of a remote release.
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
pub struct RemoteReleaseMetadata {
/// Contributors.
pub contributors: Vec<RemoteContributor>,
Expand Down
5 changes: 3 additions & 2 deletions git-cliff-core/src/statistics.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::collections::HashMap;

use chrono::{TimeZone, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::release::Release;

/// Aggregated information about how many times a specific link appeared in
/// commit messages.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct LinkCount {
/// Text of the link.
Expand All @@ -19,7 +20,7 @@ pub struct LinkCount {
}

/// Aggregated statistics about commits in the release.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Statistics {
/// The total number of commits included in the release.
pub commit_count: usize,
Expand Down
8 changes: 8 additions & 0 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ pub struct Opt {
/// Disables the external command execution.
#[arg(long, help_heading = Some("FLAGS"))]
pub no_exec: bool,
#[arg(long,
global = true,
value_name = "PATH",
value_parser = Opt::parse_dir,
help = "Dump the context schema to a file"
)]
/// Dumps the context schema to a file.
pub dump_context_schema: Option<PathBuf>,
/// Prints changelog context as JSON.
#[arg(short = 'x', long, help_heading = Some("FLAGS"))]
pub context: bool,
Expand Down
6 changes: 6 additions & 0 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,9 @@ pub fn run_with_changelog_modifier(

Ok(())
}

pub fn dump_context_schema(path: &Path) -> Result<()> {
let schema_json = Changelog::context_schema()?;
fs::write(path, schema_json)?;
Ok(())
}
5 changes: 5 additions & 0 deletions git-cliff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ fn main() -> Result<()> {
_profiler_guard = Some(());
}

if let Some(path) = args.dump_context_schema {
git_cliff::dump_context_schema(&path)?;
return Ok(());
}

// Run git-cliff
let exit_code = match git_cliff::run(args) {
Ok(()) => 0,
Expand Down
Loading