1
1
mirror of https://github.com/orhun/git-cliff.git synced 2025-01-05 15:58:24 +03:00

feat(config): support placing configuration inside pyproject.toml (#147)

This commit is contained in:
Radu Suciu 2023-03-31 12:49:47 -07:00 committed by GitHub
parent 104aac93b4
commit fe5e5b841a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -76,6 +76,7 @@
- [link_parsers](#link_parsers)
- [Project Integration](#project-integration)
- [Rust](#rust)
- [Python](#python)
- [Templating](#templating)
- [Context](#context)
- [Conventional Commits](#conventional-commits)
@ -721,6 +722,28 @@ filter_commits = false
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>]`)
### Python
For Python projects, **git-cliff** can be configured in `pyproject.toml` via the [tool table](https://peps.python.org/pep-0518/#tool-table). To do this, simply replace the available configuration sections with `[tool.git-cliff.<section>]` and place them inside `pyproject.toml`. For example:
```toml
name = "..."
[project]
dependencies = []
[tool.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
[tool.git-cliff.git]
conventional_commits = true
commit_parsers = []
filter_commits = false
# see [git] section for more keys
```
## Templating
A template is a text where variables and expressions get replaced with values when it is rendered.

View File

@ -11,6 +11,9 @@ use std::path::Path;
const CARGO_METADATA_REGEX: &str =
r"^\[(?:workspace|package)\.metadata\.git\-cliff\.";
/// Regex for matching the metadata in pyproject.toml
const PYPROJECT_METADATA_REGEX: &str = r"^\[(?:tool)\.git\-cliff\.";
/// Configuration values.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Config {
@ -119,11 +122,19 @@ pub struct LinkParser {
impl Config {
/// Parses the config file and returns the values.
pub fn parse(path: &Path) -> Result<Config> {
let config_builder = if path.file_name() == Some(OsStr::new("Cargo.toml")) {
let config_builder = if path.file_name() == Some(OsStr::new("Cargo.toml")) ||
path.file_name() == Some(OsStr::new("pyproject.toml"))
{
let contents = fs::read_to_string(path)?;
let metadata_regex = RegexBuilder::new(CARGO_METADATA_REGEX)
.multi_line(true)
.build()?;
let metadata_regex = RegexBuilder::new(
if path.file_name() == Some(OsStr::new("Cargo.toml")) {
CARGO_METADATA_REGEX
} else {
PYPROJECT_METADATA_REGEX
},
)
.multi_line(true)
.build()?;
let contents = metadata_regex.replace_all(&contents, "[");
config::Config::builder().add_source(config::File::from_str(
&contents,