Skip to content

Commit

Permalink
feat(changelog): add --sort argument for sorting commits (#15)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* docs(readme): move the explanation of sort flag to README.md

Co-authored-by: orhun <[email protected]>
  • Loading branch information
kondanta and orhun authored Sep 28, 2021
1 parent 777375f commit 2950a41
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ git-cliff [FLAGS] [OPTIONS] [RANGE]
-t, --tag <TAG> Sets the tag for the latest version [env: TAG=]
-b, --body <TEMPLATE> Sets the template for the changelog body [env: TEMPLATE=]
-s, --strip <PART> Strips the given parts from the changelog [possible values: header, footer, all]
--sort <sort> Sets sorting of the commits inside sections [default: oldest] [possible values: oldest, newest]
```

**Args:**
Expand Down Expand Up @@ -205,6 +206,17 @@ Generate a changelog scoped to a specific directory (useful for monorepos):
git cliff --commit-path project1/
```

Sort the commits inside sections:

```sh
# The oldest commit will be on top.
# (default)
git cliff --sort oldest

# The newest commit will be on top.
git cliff --sort newest
```

Save the changelog file to the specified file:

```sh
Expand Down
8 changes: 8 additions & 0 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@ pub struct Opt {
/// Sets the commit range to process.
#[structopt(value_name = "RANGE")]
pub range: Option<String>,

/// Sets sorting of the commits inside sections.
#[structopt(
long,
possible_values = &["oldest", "newest"],
default_value = "oldest"
)]
pub sort: String,
}
6 changes: 5 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.push(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);
Expand Down

0 comments on commit 2950a41

Please sign in to comment.