Skip to content

Commit 508a97e

Browse files
authored
fix(changelog): include the root commit when --latest is used with one tag (#901)
* feat(git): latest with one tag should include root * feat(git): test root commit with one tag * feat(git): remove the include_root boolean flag Use the fact that a range contains (or doesn't) contain ".." as a discriminant between the two cases: - ".." means full (left-exclusive) range between two commits; - no ".." means everything from the root commit (inclusive) to the commit sha in the range * fix: remove unnecessary reference * nit: gentler English in comment
1 parent f5c39a2 commit 508a97e

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "Initial commit"
4+
GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: initial commit"
55

66
GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat: add feature 1"
77
git tag v0.1.0

.github/fixtures/test-latest-with-one-tag/expected.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66

77
### Feat
88

9+
- Initial commit
910
- Add feature 1
1011

1112
<!-- generated by git-cliff -->

git-cliff-core/src/repo.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ impl Repository {
7878
let mut revwalk = self.inner.revwalk()?;
7979
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
8080
if let Some(range) = range {
81-
revwalk.push_range(range)?;
81+
if range.contains("..") {
82+
revwalk.push_range(range)?;
83+
} else {
84+
// When a single SHA is provided as the "range", start from the root.
85+
revwalk.push(Oid::from_str(range)?)?;
86+
}
8287
} else {
8388
revwalk.push_head()?;
8489
}
@@ -464,6 +469,18 @@ mod test {
464469
.to_string())
465470
}
466471

472+
fn get_root_commit_hash() -> Result<String> {
473+
Ok(str::from_utf8(
474+
Command::new("git")
475+
.args(["rev-list", "--max-parents=0", "HEAD"])
476+
.output()?
477+
.stdout
478+
.as_ref(),
479+
)?
480+
.trim_ascii_end()
481+
.to_string())
482+
}
483+
467484
fn get_last_tag() -> Result<String> {
468485
Ok(str::from_utf8(
469486
Command::new("git")
@@ -586,6 +603,18 @@ mod test {
586603
Ok(())
587604
}
588605

606+
#[test]
607+
fn includes_root_commit() -> Result<()> {
608+
let repository = get_repository()?;
609+
// a close descendant of the root commit
610+
let range = Some("eea3914c7ab07472841aa85c36d11bdb2589a234");
611+
let commits = repository.commits(range, None, None)?;
612+
let root_commit =
613+
AppCommit::from(&commits.last().expect("no commits found").clone());
614+
assert_eq!(get_root_commit_hash()?, root_commit.id);
615+
Ok(())
616+
}
617+
589618
fn create_temp_repo() -> (Repository, TempDir) {
590619
let temp_dir =
591620
TempDir::with_prefix("git-cliff-").expect("failed to create temp dir");

git-cliff/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ fn process_repository<'a>(
171171
commits.last().map(|c| c.id().to_string()),
172172
tags.get_index(0).map(|(k, _)| k),
173173
) {
174-
commit_range = Some(format!("{tag1}..{tag2}"));
174+
if tags.len() == 1 {
175+
commit_range = Some(tag2.to_owned());
176+
} else {
177+
commit_range = Some(format!("{tag1}..{tag2}"));
178+
}
175179
}
176180
} else {
177181
let mut tag_index = tags.len() - 2;

0 commit comments

Comments
 (0)