From 095525c2e0f088fa85ef514dd37701f12edcbec6 Mon Sep 17 00:00:00 2001 From: Taylan Dogan Date: Mon, 20 Sep 2021 01:11:33 +0300 Subject: [PATCH 1/3] refactor(changelog): sort commits by newest in CHANGELOG Previously, cliff was sorting the commits by oldest first. Like: ``` - Support parsing the missing scopes with `default_scope` (#8) - Support generating a changelog scoped to a directory (#11) ``` As the PR numbers indicate, the first bullet point is definitely older than the latter. With this update, it will look like this: ``` - Support generating a changelog scoped to a directory (#11) - Support parsing the missing scopes with `default_scope` (#8) ``` Signed-off-by: Taylan Dogan --- git-cliff/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index 0a17e800e3..e9ebdd5637 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -145,7 +145,7 @@ pub fn run(mut args: Opt) -> Result<()> { for git_commit in commits.into_iter().rev() { let commit = Commit::from(&git_commit); let commit_id = commit.id.to_string(); - releases[release_index].commits.push(commit); + releases[release_index].commits.insert(0, commit); if let Some(tag) = tags.get(&commit_id) { releases[release_index].version = Some(tag.to_string()); releases[release_index].commit_id = Some(commit_id); From c62ca036521d132842e8baada3d0c2b76d36c999 Mon Sep 17 00:00:00 2001 From: Taylan Dogan Date: Tue, 28 Sep 2021 17:34:35 +0300 Subject: [PATCH 2/3] refactor: add sorting flag I don't expect any other sorting types will be added so the logic consist of checking whether it is `newest` or not. One could argue with why wouldn't I make this a boolean. My answer would be, in my opinion, it lose its meaning because this is not something we want to enable or disable but something that we want to decide which pattern we want to use. So it is more like a semantic choice. Signed-off-by: Taylan Dogan --- git-cliff/src/args.rs | 16 ++++++++++++++++ git-cliff/src/lib.rs | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/git-cliff/src/args.rs b/git-cliff/src/args.rs index 8ef1d95f88..827ae36837 100644 --- a/git-cliff/src/args.rs +++ b/git-cliff/src/args.rs @@ -77,4 +77,20 @@ pub struct Opt { /// Sets the commit range to process. #[structopt(value_name = "RANGE")] pub range: Option, + + /// Sets sorting of the commits inside the sections + /// + /// If sort is `oldest`, the oldest commit will be on top. {n} + /// - PR #1 {n} + /// - PR #2 + /// + /// If sort is `newest`, the newest commit will be on top. {n} + /// - PR #2 {n} + /// - PR #1 {n} + #[structopt( + long, + possible_values = &["oldest", "newest"], + default_value = "oldest" + )] + pub sort: String, } diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index e9ebdd5637..84420aa78f 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -145,7 +145,11 @@ pub fn run(mut args: Opt) -> Result<()> { for git_commit in commits.into_iter().rev() { let commit = Commit::from(&git_commit); let commit_id = commit.id.to_string(); - releases[release_index].commits.insert(0, commit); + if args.sort == "newest" { + releases[release_index].commits.insert(0, commit); + } else { + releases[release_index].commits.push(commit); + } if let Some(tag) = tags.get(&commit_id) { releases[release_index].version = Some(tag.to_string()); releases[release_index].commit_id = Some(commit_id); From a0635ee8dfd718b66c9dda19b71456ee4a456c1a Mon Sep 17 00:00:00 2001 From: orhun Date: Tue, 28 Sep 2021 22:50:19 +0300 Subject: [PATCH 3/3] docs(readme): move the explanation of sort flag to README.md --- README.md | 12 ++++++++++++ git-cliff/src/args.rs | 10 +--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d7b38d86c5..621a9bad59 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ git-cliff [FLAGS] [OPTIONS] [RANGE] -t, --tag Sets the tag for the latest version [env: TAG=] -b, --body