1
1
mirror of https://github.com/orhun/git-cliff.git synced 2025-01-06 01:04:10 +03:00

feat(config): support placing configuration inside Cargo.toml (#46)

This commit is contained in:
Orhun Parmaksız 2022-03-27 15:42:33 +03:00
parent 95ad55d542
commit f48d2077c3
No known key found for this signature in database
GPG Key ID: B928720AEC532117
3 changed files with 57 additions and 3 deletions

View File

@ -70,6 +70,8 @@
- [date_order](#date_order)
- [sort_commits](#sort_commits)
- [link_parsers](#link_parsers)
- [Project Integration](#project-integration)
- [Rust](#rust)
- [Templating](#templating)
- [Context](#context)
- [Conventional Commits](#conventional-commits)
@ -563,6 +565,34 @@ Examples:
- `{ pattern = "RFC(\\d+)", text = "ietf-rfc$1", href = "https://datatracker.ietf.org/doc/html/rfc$1"}`,
- Extract mentions of IETF RFCs and generate URLs linking to them. It also rewrites the text as "ietf-rfc...".
## Project Integration
### Rust
For Rust projects, **git-cliff** can be configured in `Cargo.toml` via [metadata table](https://doc.rust-lang.org/cargo/reference/manifest.html#the-metadata-table). To do this, simply replace the available configuration sections with `[package.metadata.git-cliff.<section>]` and place them inside `Cargo.toml`. For example:
```toml
[package]
name = "..."
[dependencies]
# ...
[package.metadata.git-cliff.changelog]
header = "All notable changes to this project will be documented in this file."
body = "..."
footer = "<!-- generated by git-cliff -->"
# see [changelog] section for more keys
[package.metadata.git-cliff.git]
conventional_commits = true
commit_parsers = []
filter_commits = false
# see [git] section for more keys
```
For Cargo workspaces, [`workspace.metadata`](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-workspacemetadata-table) table can be used. (e.g. `[workspace.metadata.git-cliff.<section>]`)
## Templating
A template is a text where variables and expressions get replaced with values when it is rendered.

View File

@ -1,7 +1,16 @@
use crate::error::Result;
use regex::Regex;
use regex::{
Regex,
RegexBuilder,
};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
/// Regex for matching the metadata in Cargo.toml
const CARGO_METADATA_REGEX: &str =
r"^\[(?:workspace|package)\.metadata\.git\-cliff\.";
/// Configuration values.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Config {
@ -85,8 +94,20 @@ pub struct LinkParser {
impl Config {
/// Parses the config file and returns the values.
pub fn parse(path: &Path) -> Result<Config> {
Ok(config::Config::builder()
.add_source(config::File::from(path))
let config_builder = if path.file_name() == Some(OsStr::new("Cargo.toml")) {
let contents = fs::read_to_string(&path)?;
let metadata_regex = RegexBuilder::new(CARGO_METADATA_REGEX)
.multi_line(true)
.build()?;
let contents = metadata_regex.replace_all(&contents, "[");
config::Config::builder().add_source(config::File::from_str(
&contents,
config::FileFormat::Toml,
))
} else {
config::Config::builder().add_source(config::File::from(path))
};
Ok(config_builder
.add_source(config::Environment::with_prefix("CLIFF").separator("_"))
.build()?
.try_deserialize()?)

View File

@ -47,6 +47,9 @@ pub enum Error {
/// Errors that may occur while de/serializing JSON format.
#[error("Cannot de/serialize JSON: `{0}`")]
JsonError(#[from] serde_json::Error),
/// Errors that may occur during parsing or compiling a regular expression.
#[error("Cannot parse/compile regex: `{0}`")]
RegexError(#[from] regex::Error),
}
/// Result type of the core library.