Skip to content

Commit c62ca03

Browse files
committed
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@taylandogan.info>
1 parent 095525c commit c62ca03

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

git-cliff/src/args.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ pub struct Opt {
7777
/// Sets the commit range to process.
7878
#[structopt(value_name = "RANGE")]
7979
pub range: Option<String>,
80+
81+
/// Sets sorting of the commits inside the sections
82+
///
83+
/// If sort is `oldest`, the oldest commit will be on top. {n}
84+
/// - PR #1 {n}
85+
/// - PR #2
86+
///
87+
/// If sort is `newest`, the newest commit will be on top. {n}
88+
/// - PR #2 {n}
89+
/// - PR #1 {n}
90+
#[structopt(
91+
long,
92+
possible_values = &["oldest", "newest"],
93+
default_value = "oldest"
94+
)]
95+
pub sort: String,
8096
}

git-cliff/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ pub fn run(mut args: Opt) -> Result<()> {
145145
for git_commit in commits.into_iter().rev() {
146146
let commit = Commit::from(&git_commit);
147147
let commit_id = commit.id.to_string();
148-
releases[release_index].commits.insert(0, commit);
148+
if args.sort == "newest" {
149+
releases[release_index].commits.insert(0, commit);
150+
} else {
151+
releases[release_index].commits.push(commit);
152+
}
149153
if let Some(tag) = tags.get(&commit_id) {
150154
releases[release_index].version = Some(tag.to_string());
151155
releases[release_index].commit_id = Some(commit_id);

0 commit comments

Comments
 (0)