Skip to content

Commit

Permalink
feat(config): changelog for the last n commits
Browse files Browse the repository at this point in the history
Adds a additional configuration variable `limit_commits` to the
configuration struct.
`limit_commits` can be set to a positive integer number to limit the
commits contained in the generated changelog.

Also adjusts the default config file to contain `limit_commits` as a
commented out line.

Extends documentation in README.md to also cover the introduced
configuration value.

issue: #102
  • Loading branch information
FlrnFrmm committed Oct 5, 2022
1 parent 1c98995 commit 6f94622
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ link_parsers = [
{ pattern = "#(\\d+)", href = "https://github.com/orhun/git-cliff/issues/$1"},
{ pattern = "RFC(\\d+)", text = "ietf-rfc$1", href = "https://datatracker.ietf.org/doc/html/rfc$1"},
]
limit_commits = 42
```

#### conventional_commits
Expand Down Expand Up @@ -652,6 +653,12 @@ Examples:
- `{ pattern = "RFC(\\d+)", text = "ietf-rfc$1", href = "https://datatracker.ietf.org/doc/html/rfc$1"}`,
- Extract mentions of IETF RFCs and generate URLs linking to them. It also rewrites the text as "ietf-rfc...".

#### limit_commits

`limit_commits` is a **optional** positive integer number that limits the number of included commits in the generated changelog.

`limit_commits` is not part of the default configuration.

## Project Integration

### Rust
Expand Down Expand Up @@ -1018,6 +1025,38 @@ All notable changes to this project will be documented in this file.

</details>

#### [Limited Commits](./examples/limitedcommits.toml)

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

```
## [unreleased]
### Feat
- Support multiple file formats
- Use cache while fetching pages
## [1.0.1] - 2021-07-18
### Chore
- Add release script
```

</details>

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

## [unreleased]
### Feat
- Support multiple file formats
- Use cache while fetching pages

## [1.0.1] - 2021-07-18
### Chore
- Add release script

</details>

#### [Detailed](./examples/detailed.toml)

<details>
Expand Down
2 changes: 2 additions & 0 deletions config/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ ignore_tags = ""
date_order = false
# sort the commits inside sections by oldest/newest order
sort_commits = "oldest"
# limit the number of commits included in the changelog.
# limit_commits = 42
21 changes: 21 additions & 0 deletions examples/limitedcommits.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# configuration file for git-cliff (0.1.0)

[changelog]
# 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
"""

[git]
limit_commits = 3
2 changes: 2 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub struct GitConfig {
pub date_order: Option<bool>,
/// Sorting of the commits inside sections.
pub sort_commits: Option<String>,
/// Limit the number of commits included in the changelog.
pub limit_commits: Option<usize>,
}

/// 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 @@ -91,6 +91,7 @@ fn generate_changelog() -> Result<()> {
text: Some(String::from("$1")),
},
]),
limit_commits: None,
};

let releases = vec![
Expand Down
1 change: 1 addition & 0 deletions git-cliff/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ mod test {
date_order: Some(false),
sort_commits: Some(String::from("oldest")),
link_parsers: None,
limit_commits: None,
},
};
let test_release = Release {
Expand Down
7 changes: 6 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ pub fn run(mut args: Opt) -> Result<()> {
}
}
}
let commits =
let mut commits =
repository.commits(commit_range, args.include_path, args.exclude_path)?;
if let Some(commit_limit_value) = config.git.limit_commits {
commits = commits
.drain(..commit_limit_value)
.collect();
}

// Update tags.
if let Some(tag) = args.tag {
Expand Down

0 comments on commit 6f94622

Please sign in to comment.