diff --git a/README.md b/README.md index 2957a0f277..688a29326d 100644 --- a/README.md +++ b/README.md @@ -468,6 +468,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 @@ -645,6 +646,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 a12f1af819..f4da5d20cd 100644 --- a/config/cliff.toml +++ b/config/cliff.toml @@ -64,3 +64,5 @@ ignore_tags = "" date_order = false # sort the commits inside sections by oldest/newest order sort_commits = "oldest" +# limit the number of commits inlcuded in the changelog. +# limit_commits = 42 diff --git a/examples/limitedcommits.toml b/examples/limitedcommits.toml new file mode 100644 index 0000000000..0d9f04465c --- /dev/null +++ b/examples/limitedcommits.toml @@ -0,0 +1,22 @@ +# 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 46eb964189..3018fd5669 100644 --- a/git-cliff-core/src/config.rs +++ b/git-cliff-core/src/config.rs @@ -66,6 +66,8 @@ pub struct GitConfig { pub date_order: Option, /// Sorting of the commits inside sections. pub sort_commits: Option, + /// Limit the number of commits inlcuded 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 14de2da07f..ceab46112e 100644 --- a/git-cliff-core/tests/integration_test.rs +++ b/git-cliff-core/tests/integration_test.rs @@ -90,6 +90,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 f56b2faf23..e1d0efc232 100644 --- a/git-cliff/src/changelog.rs +++ b/git-cliff/src/changelog.rs @@ -264,6 +264,7 @@ mod test { 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..9bac8870a1 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -217,8 +217,15 @@ pub fn run(mut args: Opt) -> Result<()> { } } } - let commits = - repository.commits(commit_range, args.include_path, args.exclude_path)?; + let commits = if let Some(commit_limit_value) = config.git.limit_commits { + repository + .commits(commit_range, args.include_path, args.exclude_path)? + .into_iter() + .take(commit_limit_value) + .collect() + } else { + repository.commits(commit_range, args.include_path, args.exclude_path)? + }; // Update tags. if let Some(tag) = args.tag {