Skip to content

Commit

Permalink
feat(changelog): support having both conventional and unconventional …
Browse files Browse the repository at this point in the history
…commits in the changelog
  • Loading branch information
orhun committed Dec 11, 2021
1 parent 1a9c3e3 commit 8445313
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 81 deletions.
137 changes: 134 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- [footer](#footer)
- [git](#git)
- [conventional_commits](#conventional_commits)
- [filter_unconventional](#filter_unconventional)
- [commit_parsers](#commit_parsers)
- [filter_commits](#filter_commits)
- [tag_pattern](#tag_pattern)
Expand All @@ -78,6 +79,7 @@
- [Scoped](#scoped)
- [Scoped (Sorted)](#scoped-sorted)
- [Keep a Changelog](#keep-a-changelog)
- [Unconventional](#unconventional)
- [Similar Projects](#similar-projects)
- [License](#license)
- [Copyright](#copyright)
Expand Down Expand Up @@ -375,6 +377,7 @@ This section contains the parsing and git related configuration options.
```toml
[git]
conventional_commits = true
filter_unconventional = true
commit_parsers = [
{ message = "^feat", group = "Features"},
{ message = "^fix", group = "Bug Fixes"},
Expand All @@ -394,7 +397,7 @@ sort_commits = "oldest"

#### conventional_commits

If set to `true`, parses the commits according to the [Conventional Commits specifications](https://www.conventionalcommits.org).
If set to `true`, commits are parsed according to the [Conventional Commits specifications](https://www.conventionalcommits.org).

> The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.

Expand All @@ -410,6 +413,35 @@ If set to `true`, parses the commits according to the [Conventional Commits spec
e.g. `feat(parser): add ability to parse arrays`
#### filter_unconventional
If set to `true`, commits that are not conventional are excluded. This option can be used to generate changelogs with conventional and unconvential commits mixed together. For example:
```toml
conventional_commits = true
filter_unconventional = false
commit_parsers = [
{ message = ".*", group = "Other", default_scope = "other"},
]
```

With the configuration above, conventional commits are parsed as usual and unconventional commits will be also included in the changelog as "Other".

To completely exclude unconventional commits from the changelog:

```toml
# default behaviour
conventional_commits = true
filter_unconventional = true
```

To include any type of commit in the changelog without parsing:

```toml
conventional_commits = false
filter_unconventional = false
```

#### commit_parsers

An array of commit parsers for determining the commit groups by using regex.
Expand Down Expand Up @@ -504,7 +536,8 @@ following context is generated to use for templating:
"body": "[body]",
"footers": ["[footer]", "[footer]"],
"breaking_description": "<description>",
"breaking": false
"breaking": false,
"conventional": true
}
],
"commit_id": "a440c6eb26404be4877b7e3ad592bfaa5d4eb210 (release commit)",
Expand Down Expand Up @@ -546,7 +579,9 @@ If [conventional_commits](#conventional_commits) is set to `false`, then some of
{
"id": "e795460c9bb7275294d1fa53a9d73258fb51eb10",
"group": "(overrided by commit_parsers)",
"message": "(whole commit message including description, footers, etc.)"
"scope": "(overrided by commit_parsers)",
"message": "(full commit message including description, footers, etc.)",
"conventional": false,
}
],
"commit_id": "a440c6eb26404be4877b7e3ad592bfaa5d4eb210 (release commit)",
Expand Down Expand Up @@ -1081,6 +1116,102 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

</details>

#### [Unconventional](./examples/unconventional.toml)

<details>
<summary>Raw Output</summary>

```
# Changelog
All notable changes to this project will be documented in this file.
## [unreleased]
### Features
- Support multiple file formats ✔️
- Use cache while fetching pages ✔️
## [1.0.1] - 2021-07-18
### Miscellaneous Tasks
- Add release script ✔️
### Refactor
- Expose string functions ✔️
## [1.0.0] - 2021-07-18
### Bug Fixes
- Rename help argument due to conflict ✔️
### Documentation
- Add README.md ✔️
- Add tested usage example ✔️
### Features
- Add ability to parse arrays ✔️
### Other (unconventional)
- Initial commit ❌
<!-- generated by git-cliff -->
```

</details>

<details>
<summary>Rendered Output</summary>

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

## [unreleased]

### Features

- Support multiple file formats ✔️
- Use cache while fetching pages ✔️

## [1.0.1] - 2021-07-18

### Miscellaneous Tasks

- Add release script ✔️

### Refactor

- Expose string functions ✔️

## [1.0.0] - 2021-07-18

### Bug Fixes

- Rename help argument due to conflict ✔️

### Documentation

- Add README.md ✔️
- Add tested usage example ✔️

### Features

- Add ability to parse arrays ✔️

### Other (unconventional)

- Initial commit ❌

<!-- generated by git-cliff -->

</details>

## Similar Projects

- [git-journal](https://github.com/saschagrunert/git-journal) - The Git Commit Message and Changelog Generation Framework
Expand Down
5 changes: 3 additions & 2 deletions config/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ footer = """
"""

[git]
# allow only conventional commits
# https://www.conventionalcommits.org
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
5 changes: 3 additions & 2 deletions examples/detailed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ footer = """
"""

[git]
# allow only conventional commits
# https://www.conventionalcommits.org
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
5 changes: 3 additions & 2 deletions examples/keepachangelog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ footer = """
"""

[git]
# allow only conventional commits
# https://www.conventionalcommits.org
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^.*: add", group = "Added"},
Expand Down
5 changes: 3 additions & 2 deletions examples/scoped.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ footer = """
"""

[git]
# allow only conventional commits
# https://www.conventionalcommits.org
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
5 changes: 3 additions & 2 deletions examples/scopesorted.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ footer = """
"""

[git]
# allow only conventional commits
# https://www.conventionalcommits.org
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
Expand Down
61 changes: 61 additions & 0 deletions examples/unconventional.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# configuration file for git-cliff (0.1.0)

[changelog]
# changelog header
header = """
# Changelog
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 %}
- {{ commit.message | upper_first | trim_end }} {% if commit.conventional %}✔️{% else %}❌{% endif %}\
{% endfor %}
{% endfor %}\n
"""
# remove the leading and trailing whitespaces from the template
trim = true
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""

[git]
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = false
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
{ message = "^fix", group = "Bug Fixes"},
{ message = "^doc", group = "Documentation"},
{ message = "^perf", group = "Performance"},
{ message = "^refactor", group = "Refactor"},
{ message = "^style", group = "Styling"},
{ message = "^test", group = "Testing"},
{ message = "^chore\\(release\\): prepare for", skip = true},
{ message = "^chore", group = "Miscellaneous Tasks"},
{ body = ".*security", group = "Security"},
{ body = ".*", group = "Other (unconventional)"},
]
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
# regex for ignoring tags
ignore_tags = ""
# sort the tags topologically
topo_order = false
# sort the commits inside sections by oldest/newest order
sort_commits = "oldest"
29 changes: 17 additions & 12 deletions git-cliff-core/src/commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::config::CommitParser;
use crate::config::{
CommitParser,
GitConfig,
};
use crate::error::{
Error as AppError,
Result,
Expand Down Expand Up @@ -53,18 +56,18 @@ impl Commit<'_> {
///
/// * converts commit to a conventional commit
/// * sets the group for the commit
pub fn process(
&self,
parsers: Option<&Vec<CommitParser>>,
filter_commits: Option<bool>,
conventional_commits: bool,
) -> Result<Self> {
pub fn process(&self, config: &GitConfig) -> Result<Self> {
let mut commit = self.clone();
if conventional_commits {
commit = commit.into_conventional()?;
if config.conventional_commits {
if config.filter_unconventional.unwrap_or(true) {
commit = commit.into_conventional()?;
} else if let Ok(conv_commit) = commit.clone().into_conventional() {
commit = conv_commit;
}
}
if let Some(parsers) = parsers {
commit = commit.parse(parsers, filter_commits.unwrap_or(false))?;
if let Some(parsers) = &config.commit_parsers {
commit =
commit.parse(parsers, config.filter_commits.unwrap_or(false))?;
}
Ok(commit)
}
Expand Down Expand Up @@ -122,7 +125,7 @@ impl Serialize for Commit<'_> {
where
S: Serializer,
{
let mut commit = serializer.serialize_struct("Commit", 8)?;
let mut commit = serializer.serialize_struct("Commit", 9)?;
commit.serialize_field("id", &self.id)?;
match &self.conv {
Some(conv) => {
Expand Down Expand Up @@ -157,8 +160,10 @@ impl Serialize for Commit<'_> {
None => {
commit.serialize_field("message", &self.message)?;
commit.serialize_field("group", &self.group)?;
commit.serialize_field("scope", &self.scope)?;
}
}
commit.serialize_field("conventional", &self.conv.is_some())?;
commit.end()
}
}
Expand Down
Loading

0 comments on commit 8445313

Please sign in to comment.