Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): support using regex scope values #372

Merged
merged 2 commits into from
Dec 7, 2023
Merged
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
9 changes: 3 additions & 6 deletions .github/fixtures/new-fixture-template/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ body = """
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for group, commits in commits | group_by(attribute="scope") %}
#### {{ group | upper_first }}
{% for commit in commits %}
- {{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\
{% for commit in commits %}
- {{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
Expand Down
10 changes: 2 additions & 8 deletions .github/fixtures/new-fixture-template/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,26 @@ All notable changes to this project will be documented in this file.

### Test

- Add tests

## [0.2.0] - 2022-04-06

### Bug Fixes

#### Cli

- Fix feature 2

### Features

#### Gui

- Add feature 2

## [0.1.0] - 2022-04-06

### Bug Fixes

#### Cli

- Fix feature 1

### Features

#### App

- Add feature 1

<!-- generated by git-cliff -->
35 changes: 35 additions & 0 deletions .github/fixtures/test-regex-replace-parser/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[changelog]
# changelog header
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://keats.github.io/tera/docs/#introduction
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- ({{ commit.scope }}) {{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
footer = """
<!-- generated by git-cliff -->
"""
# remove the leading and trailing whitespace from the templates
trim = true

[git]
# regex for parsing and grouping commits
commit_parsers = [
{ message = '^fix\((.*)\)', group = 'Fix (${1}) 🧰' },
{ message = '^feat\((.*)\)', group = 'Feature (${1}) 🚀' },
{ message = "^\\[(.*)\\]", group = "Changes to ${1}", scope = "${1}" },
]
14 changes: 14 additions & 0 deletions .github/fixtures/test-regex-replace-parser/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2022-04-06 01:25:08" git commit --allow-empty -m "Initial commit"
GIT_COMMITTER_DATE="2022-04-06 01:25:09" git commit --allow-empty -m "feat(config): add feature 1"
GIT_COMMITTER_DATE="2022-04-06 01:25:10" git commit --allow-empty -m "fix(config): fix feature 1"
git tag v0.1.0
GIT_COMMITTER_DATE="2022-04-06 01:25:11" git commit --allow-empty -m "feat(gui): add feature 2"
GIT_COMMITTER_DATE="2022-04-06 01:25:12" git commit --allow-empty -m "fix(gui): fix feature 2"
git tag v0.2.0
GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "[tests]: add tests"
GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "[codebase]: refactor stuff"
GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "[codebase]: refactor more stuff"
GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "[security]: rewrite everything in Rust"
40 changes: 40 additions & 0 deletions .github/fixtures/test-regex-replace-parser/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Changelog

All notable changes to this project will be documented in this file.

## [unreleased]

### Changes to codebase

- (codebase) Refactor stuff
- (codebase) Refactor more stuff

### Changes to security

- (security) Rewrite everything in Rust

### Changes to tests

- (tests) Add tests

## [0.2.0] - 2022-04-05

### Feature (gui) 🚀

- (gui) Add feature 2

### Fix (gui) 🧰

- (gui) Fix feature 2

## [0.1.0] - 2022-04-05

### Feature (config) 🚀

- (config) Add feature 1

### Fix (config) 🧰

- (config) Fix feature 1

<!-- generated by git-cliff -->
19 changes: 10 additions & 9 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,17 @@ impl Commit<'_> {
"Skipping commit",
)));
} else {
let regex_replace = |mut value: String| {
for mat in regex.find_iter(&text) {
value =
regex.replace(mat.as_str(), value).to_string();
}
value
};
self.group =
parser.group.as_ref().cloned().map(|mut group| {
for mat in regex.find_iter(&text) {
group = regex
.replace(mat.as_str(), group)
.to_string();
}
group
});
self.scope = parser.scope.as_ref().cloned();
parser.group.as_ref().cloned().map(regex_replace);
self.scope =
parser.scope.as_ref().cloned().map(regex_replace);
self.default_scope = parser.default_scope.as_ref().cloned();
return Ok(self);
}
Expand Down