Skip to content

Commit c3e25c3

Browse files
SebClapieorhun
andauthored
feat(git): support disabling sorting commits topologically (#804) (#1121)
* feat(git): disable sorting commits topologically (#804) * Update config/cliff.toml Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com> * Update website/docs/configuration/git.md Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com> * Update .github/fixtures/test-topo-order-commits/cliff.toml Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com> * Update website/docs/configuration/git.md Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com> * Update website documentation * Simplify configuration --------- Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
1 parent 9e4bd07 commit c3e25c3

File tree

11 files changed

+94
-4
lines changed

11 files changed

+94
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# git-cliff ~ configuration file
2+
# https://git-cliff.org/docs/configuration
3+
4+
5+
[git]
6+
7+
topo_order_commits = false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
GIT_COMMITTER_DATE="2022-04-06 03:25:08" git commit --allow-empty -m "Initial commit"
5+
GIT_COMMITTER_DATE="2022-04-06 03:25:09" git commit --allow-empty -m "feat: add feature 1"
6+
GIT_COMMITTER_DATE="2022-04-06 03:25:10" git commit --allow-empty -m "fix: fix feature 1"
7+
git tag v0.1.0
8+
GIT_COMMITTER_DATE="2022-04-06 03:25:11" git commit --allow-empty -m "feat(gui): add feature 2"
9+
GIT_COMMITTER_DATE="2022-04-06 03:25:12" git commit --allow-empty -m "fix(gui): fix feature 2"
10+
git tag v0.2.0
11+
GIT_COMMITTER_DATE="2022-04-06 03:25:25" git commit --allow-empty -m "test: add tests"
12+
GIT_COMMITTER_DATE="2022-04-06 03:25:25" git checkout v0.1.0
13+
GIT_COMMITTER_DATE="2022-04-06 03:25:13" git commit --allow-empty -m "fix: fix again feature 1"
14+
git tag v0.1.1
15+
git checkout master
16+
git merge v0.1.1 --no-edit
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [unreleased]
6+
7+
### 🧪 Testing
8+
9+
- Add tests
10+
11+
## [0.1.1] - 2022-04-06
12+
13+
### 🐛 Bug Fixes
14+
15+
- Fix again feature 1
16+
17+
## [0.2.0] - 2022-04-06
18+
19+
### 🚀 Features
20+
21+
- *(gui)* Add feature 2
22+
23+
### 🐛 Bug Fixes
24+
25+
- *(gui)* Fix feature 2
26+
27+
## [0.1.0] - 2022-04-06
28+
29+
### 🚀 Features
30+
31+
- Add feature 1
32+
33+
### 🐛 Bug Fixes
34+
35+
- Fix feature 1
36+
37+
<!-- generated by git-cliff -->

.github/workflows/test-fixtures.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ jobs:
110110
- fixtures-name: test-require-conventional-skipped
111111
- fixtures-name: test-remote-config
112112
command: --config-url "https://github.com/orhun/git-cliff/blob/main/.github/fixtures/new-fixture-template/cliff.toml?raw=true"
113+
- fixtures-name: test-topo-order-commits
113114

114115
steps:
115116
- name: Checkout

config/cliff.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ link_parsers = []
9494
use_branch_tags = false
9595
# Order releases topologically instead of chronologically.
9696
topo_order = false
97+
# Order releases topologically instead of chronologically.
98+
topo_order_commits = true
9799
# Order of commits in each group/release within the changelog.
98100
# Allowed values: newest, oldest
99101
sort_commits = "oldest"

git-cliff-core/src/changelog.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ mod test {
862862
count_tags: None,
863863
use_branch_tags: false,
864864
topo_order: false,
865+
topo_order_commits: true,
865866
sort_commits: String::from("oldest"),
866867
link_parsers: [].to_vec(),
867868
limit_commits: None,

git-cliff-core/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ pub struct GitConfig {
130130
pub use_branch_tags: bool,
131131
/// Order releases topologically instead of chronologically.
132132
pub topo_order: bool,
133+
/// Order commits chronologically instead of topologically.
134+
pub topo_order_commits: bool,
133135
/// How to order commits in each group/release within the changelog.
134136
pub sort_commits: String,
135137
/// Limit the total number of commits included in the changelog.

git-cliff-core/src/repo.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ impl Repository {
118118
range: Option<&str>,
119119
include_path: Option<Vec<Pattern>>,
120120
exclude_path: Option<Vec<Pattern>>,
121+
topo_order_commits: bool,
121122
) -> Result<Vec<Commit>> {
122123
let mut revwalk = self.inner.revwalk()?;
123-
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
124+
if topo_order_commits {
125+
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
126+
} else {
127+
revwalk.set_sorting(Sort::TIME)?;
128+
}
129+
124130
Self::set_commit_range(&mut revwalk, range).map_err(|e| {
125131
Error::SetCommitRangeError(
126132
range.map(String::from).unwrap_or_else(|| "?".to_string()),
@@ -621,7 +627,7 @@ mod test {
621627
#[test]
622628
fn get_latest_commit() -> Result<()> {
623629
let repository = get_repository()?;
624-
let commits = repository.commits(None, None, None)?;
630+
let commits = repository.commits(None, None, None, false)?;
625631
let last_commit =
626632
AppCommit::from(&commits.first().expect("no commits found").clone());
627633
assert_eq!(get_last_commit_hash()?, last_commit.id);
@@ -739,7 +745,7 @@ mod test {
739745
let repository = get_repository()?;
740746
// a close descendant of the root commit
741747
let range = Some("eea3914c7ab07472841aa85c36d11bdb2589a234");
742-
let commits = repository.commits(range, None, None)?;
748+
let commits = repository.commits(range, None, None, false)?;
743749
let root_commit =
744750
AppCommit::from(&commits.last().expect("no commits found").clone());
745751
assert_eq!(get_root_commit_hash()?, root_commit.id);

git-cliff-core/tests/integration_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ fn generate_changelog() -> Result<()> {
124124
count_tags: None,
125125
use_branch_tags: false,
126126
topo_order: false,
127+
topo_order_commits: true,
127128
sort_commits: String::from("oldest"),
128129
link_parsers: vec![
129130
LinkParser {

git-cliff/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ fn process_repository<'a>(
173173
}
174174
} else if args.latest || args.current {
175175
if tags.len() < 2 {
176-
let commits = repository.commits(None, None, None)?;
176+
let commits = repository.commits(
177+
None,
178+
None,
179+
None,
180+
config.git.topo_order_commits,
181+
)?;
177182
if let (Some(tag1), Some(tag2)) = (
178183
commits.last().map(|c| c.id().to_string()),
179184
tags.get_index(0).map(|(k, _)| k),
@@ -241,6 +246,7 @@ fn process_repository<'a>(
241246
commit_range.as_deref(),
242247
include_path,
243248
args.exclude_path.clone(),
249+
config.git.topo_order_commits,
244250
)?;
245251
if let Some(commit_limit_value) = config.git.limit_commits {
246252
commits.truncate(commit_limit_value);

0 commit comments

Comments
 (0)