From 5e6092cc6cb0fa4ae60535331eafc32e5297580b Mon Sep 17 00:00:00 2001 From: Florian Fromm Date: Mon, 3 Oct 2022 12:29:34 +0200 Subject: [PATCH] feat(config): changelog for the last n commits 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: https://github.com/orhun/git-cliff/issues/102 --- README.md | 7 +++++++ config/cliff.toml | 2 ++ examples/limitedcommits.toml | 21 +++++++++++++++++++++ git-cliff-core/src/config.rs | 4 +++- git-cliff-core/tests/integration_test.rs | 1 + git-cliff/src/changelog.rs | 15 ++++++++------- git-cliff/src/lib.rs | 5 ++++- 7 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 examples/limitedcommits.toml diff --git a/README.md b/README.md index 6dac923c59..4c76917ce2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/config/cliff.toml b/config/cliff.toml index a4c50a0e74..33586805d3 100644 --- a/config/cliff.toml +++ b/config/cliff.toml @@ -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 diff --git a/examples/limitedcommits.toml b/examples/limitedcommits.toml new file mode 100644 index 0000000000..3a4632e161 --- /dev/null +++ b/examples/limitedcommits.toml @@ -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 diff --git a/git-cliff-core/src/config.rs b/git-cliff-core/src/config.rs index ca29cfdefb..2250b9b155 100644 --- a/git-cliff-core/src/config.rs +++ b/git-cliff-core/src/config.rs @@ -68,7 +68,9 @@ pub struct GitConfig { /// Whether to sort tags chronologically. pub date_order: Option, /// Sorting of the commits inside sections. - pub sort_commits: Option, + pub sort_commits: Option, + /// Limit the number of commits included in the changelog. + pub limit_commits: Option, } /// Parser for grouping commits. diff --git a/git-cliff-core/tests/integration_test.rs b/git-cliff-core/tests/integration_test.rs index d9e77a4361..792021a807 100644 --- a/git-cliff-core/tests/integration_test.rs +++ b/git-cliff-core/tests/integration_test.rs @@ -91,6 +91,7 @@ fn generate_changelog() -> Result<()> { text: Some(String::from("$1")), }, ]), + limit_commits: None, }; let releases = vec![ diff --git a/git-cliff/src/changelog.rs b/git-cliff/src/changelog.rs index 39ce6f3611..a1653b37da 100644 --- a/git-cliff/src/changelog.rs +++ b/git-cliff/src/changelog.rs @@ -258,13 +258,14 @@ mod test { }, ]), protect_breaking_commits: None, - filter_commits: Some(false), - tag_pattern: None, - skip_tags: Regex::new("v3.*").ok(), - ignore_tags: None, - date_order: Some(false), - sort_commits: Some(String::from("oldest")), - link_parsers: None, + filter_commits: Some(false), + tag_pattern: None, + skip_tags: Regex::new("v3.*").ok(), + ignore_tags: None, + date_order: Some(false), + sort_commits: Some(String::from("oldest")), + link_parsers: None, + limit_commits: None, }, }; let test_release = Release { diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index 467bbaf558..6d81e43c0e 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -217,8 +217,11 @@ 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 {