diff --git a/.github/fixtures/test-latest-with-one-tag/commit.sh b/.github/fixtures/test-latest-with-one-tag/commit.sh index 6c5c90caaf..361943d085 100755 --- a/.github/fixtures/test-latest-with-one-tag/commit.sh +++ b/.github/fixtures/test-latest-with-one-tag/commit.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "Initial commit" +GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "feat: initial commit" GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat: add feature 1" git tag v0.1.0 diff --git a/.github/fixtures/test-latest-with-one-tag/expected.md b/.github/fixtures/test-latest-with-one-tag/expected.md index 1b15d95c46..e7fdd52b94 100644 --- a/.github/fixtures/test-latest-with-one-tag/expected.md +++ b/.github/fixtures/test-latest-with-one-tag/expected.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Feat +- Initial commit - Add feature 1 diff --git a/git-cliff-core/src/repo.rs b/git-cliff-core/src/repo.rs index 8fe1f08731..143809831f 100644 --- a/git-cliff-core/src/repo.rs +++ b/git-cliff-core/src/repo.rs @@ -78,7 +78,12 @@ impl Repository { let mut revwalk = self.inner.revwalk()?; revwalk.set_sorting(Sort::TOPOLOGICAL)?; if let Some(range) = range { - revwalk.push_range(range)?; + if range.contains("..") { + revwalk.push_range(range)?; + } else { + // When a single SHA is provided as the "range", start from the root. + revwalk.push(Oid::from_str(range)?)?; + } } else { revwalk.push_head()?; } @@ -464,6 +469,18 @@ mod test { .to_string()) } + fn get_root_commit_hash() -> Result { + Ok(str::from_utf8( + Command::new("git") + .args(["rev-list", "--max-parents=0", "HEAD"]) + .output()? + .stdout + .as_ref(), + )? + .trim_ascii_end() + .to_string()) + } + fn get_last_tag() -> Result { Ok(str::from_utf8( Command::new("git") @@ -586,6 +603,18 @@ mod test { Ok(()) } + #[test] + fn includes_root_commit() -> Result<()> { + let repository = get_repository()?; + // a close descendant of the root commit + let range = Some("eea3914c7ab07472841aa85c36d11bdb2589a234"); + let commits = repository.commits(range, None, None)?; + let root_commit = + AppCommit::from(&commits.last().expect("no commits found").clone()); + assert_eq!(get_root_commit_hash()?, root_commit.id); + Ok(()) + } + fn create_temp_repo() -> (Repository, TempDir) { let temp_dir = TempDir::with_prefix("git-cliff-").expect("failed to create temp dir"); diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index b2c29f95d9..95d37b738e 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -171,7 +171,11 @@ fn process_repository<'a>( commits.last().map(|c| c.id().to_string()), tags.get_index(0).map(|(k, _)| k), ) { - commit_range = Some(format!("{tag1}..{tag2}")); + if tags.len() == 1 { + commit_range = Some(tag2.to_owned()); + } else { + commit_range = Some(format!("{tag1}..{tag2}")); + } } } else { let mut tag_index = tags.len() - 2;