Skip to content

Commit

Permalink
feat(changelog): support splitting commits by lines (#101)
Browse files Browse the repository at this point in the history
* feat: support splitting commits by lines

* refactor(config): update the description for split_commits

* style(fmt): apply formatting via rustfmt

* refactor(test): create helper method for split_commits test

* fix(changelog): change the default behaviour for split_commits when it is not provided

* test(fixture): add test fixture for splitting commits

* docs(readme): update table of contents

* docs(readme): add warning about split_commits flag

Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
AaronFriel and orhun authored Aug 12, 2022
1 parent f3fada7 commit e3eae33
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 13 deletions.
31 changes: 31 additions & 0 deletions .github/fixtures/test-split-commits/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[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://tera.netlify.app/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 %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# remove the leading and trailing whitespaces from the template
trim = true
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""

[git]
# process each line of a commit as an individual commit
split_commits = true
20 changes: 20 additions & 0 deletions .github/fixtures/test-split-commits/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/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: add feature 1
feat: add feature 2
fix: fix feature 1"

GIT_COMMITTER_DATE="2022-04-06 01:25:10" git commit --allow-empty -m \
"chore: bump deps
style: apply formatting
fix: fix feature 2"

GIT_COMMITTER_DATE="2022-04-06 01:25:11" git commit --allow-empty -m \
"test: add initial tests
test: add more tests
test: update assert statements"

git tag v0.1.0
31 changes: 31 additions & 0 deletions .github/fixtures/test-split-commits/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Changelog

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

## [0.1.0] - 2022-04-05

### Chore

- Bump deps

### Feat

- Add feature 1
- Add feature 2

### Fix

- Fix feature 1
- Fix feature 2

### Style

- Apply formatting

### Test

- Add initial tests
- Add more tests
- Update assert statements

<!-- generated by git-cliff -->
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- [git](#git)
- [conventional_commits](#conventional_commits)
- [filter_unconventional](#filter_unconventional)
- [split_commits](#split_commits)
- [commit_preprocessors](#commit_preprocessors)
- [commit_parsers](#commit_parsers)
- [filter_commits](#filter_commits)
Expand All @@ -76,6 +77,7 @@
- [Templating](#templating)
- [Context](#context)
- [Conventional Commits](#conventional-commits)
- [Footers](#footers)
- [Breaking Changes](#breaking-changes)
- [Non-Conventional Commits](#non-conventional-commits)
- [Syntax](#syntax)
Expand All @@ -86,7 +88,7 @@
- [Scoped (Sorted)](#scoped-sorted)
- [Keep a Changelog](#keep-a-changelog)
- [Unconventional](#unconventional)
- [Similar Projects](#similar-projects)
- [Similar/Related Projects](#similarrelated-projects)
- [License](#license)
- [Copyright](#copyright)

Expand Down Expand Up @@ -432,6 +434,7 @@ This section contains the parsing and git related configuration options.
[git]
conventional_commits = true
filter_unconventional = true
split_commits = false
commit_parsers = [
{ message = "^feat", group = "Features"},
{ message = "^fix", group = "Bug Fixes"},
Expand Down Expand Up @@ -500,6 +503,27 @@ conventional_commits = false
filter_unconventional = false
```

#### split_commits

> This flag violates "conventional commits". It should remain off by default if conventional commits is to be respected.
If set to `true`, each line of a commit is processed individually, as if it were its own commit message. This may cause
a commit to appear multiple times in a changelog, once for each match.

```toml
conventional_commits = true
filter_unconventional = true
split_commits = true
commit_parsers = [
{ message = "^feat", group = "Features"},
]
```

With the configuration above, lines are parsed as conventional commits and unconventional lines are omitted.

If `filter_unconventional = false`, every line will be processes as an unconventional commit, resulting in each line of
a commit being treated as a changelog entry.

#### commit_preprocessors

An array of commit preprocessors for manipulating the commit messages before parsing/grouping them. These regex-based preprocessors can be used for removing or selecting certain parts of the commit message/body to be used in the following processes.
Expand Down
2 changes: 2 additions & 0 deletions config/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for preprocessing the commit messages
commit_preprocessors = [
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/orhun/git-cliff/issues/${2}))"},
Expand Down
2 changes: 2 additions & 0 deletions examples/detailed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
2 changes: 2 additions & 0 deletions examples/keepachangelog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^.*: add", group = "Added"},
Expand Down
2 changes: 2 additions & 0 deletions examples/scoped.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
2 changes: 2 additions & 0 deletions examples/scopesorted.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
2 changes: 2 additions & 0 deletions examples/unconventional.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ footer = """
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = false
# process each line of a commit as an individual commit
split_commits = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
22 changes: 13 additions & 9 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,30 @@ pub struct GitConfig {
pub conventional_commits: Option<bool>,
/// Whether to filter out unconventional commits.
pub filter_unconventional: Option<bool>,
/// Whether to split commits by line, processing each line as an individual
/// commit.
pub split_commits: Option<bool>,

/// Git commit preprocessors.
pub commit_preprocessors: Option<Vec<CommitPreprocessor>>,
pub commit_preprocessors: Option<Vec<CommitPreprocessor>>,
/// Git commit parsers.
pub commit_parsers: Option<Vec<CommitParser>>,
pub commit_parsers: Option<Vec<CommitParser>>,
/// Link parsers.
pub link_parsers: Option<Vec<LinkParser>>,
pub link_parsers: Option<Vec<LinkParser>>,
/// Whether to filter out commits.
pub filter_commits: Option<bool>,
pub filter_commits: Option<bool>,
/// Blob pattern for git tags.
pub tag_pattern: Option<String>,
pub tag_pattern: Option<String>,
#[serde(with = "serde_regex", default)]
/// Regex to skip matched tags.
pub skip_tags: Option<Regex>,
pub skip_tags: Option<Regex>,
#[serde(with = "serde_regex", default)]
/// Regex to ignore matched tags.
pub ignore_tags: Option<Regex>,
pub ignore_tags: Option<Regex>,
/// Whether to sort tags chronologically.
pub date_order: Option<bool>,
pub date_order: Option<bool>,
/// Sorting of the commits inside sections.
pub sort_commits: Option<String>,
pub sort_commits: Option<String>,
}

/// Parser for grouping commits.
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn generate_changelog() -> Result<()> {
let git_config = GitConfig {
conventional_commits: Some(true),
filter_unconventional: Some(true),
split_commits: Some(false),
commit_preprocessors: Some(vec![CommitPreprocessor {
pattern: Regex::new(r#"\(fixes (#[1-9]+)\)"#).unwrap(),
replace: Some(String::from("[closes Issue${1}]")),
Expand Down
Loading

0 comments on commit e3eae33

Please sign in to comment.