Skip to content

Commit

Permalink
feat(config): support placing configuration inside pyproject.toml (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
radusuciu committed Mar 10, 2023
1 parent 0e710c2 commit 64f29ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- [link_parsers](#link_parsers)
- [Project Integration](#project-integration)
- [Rust](#rust)
- [Python](#python)
- [Templating](#templating)
- [Context](#context)
- [Conventional Commits](#conventional-commits)
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 15 additions & 4 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 64f29ab

Please sign in to comment.