Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(changelog): include the root commit when --latest is used with one tag #901

Merged
merged 5 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/fixtures/test-latest-with-one-tag/commit.sh
Original file line number Diff line number Diff line change
@@ -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"
orhun marked this conversation as resolved.
Show resolved Hide resolved

GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat: add feature 1"
git tag v0.1.0
1 change: 1 addition & 0 deletions .github/fixtures/test-latest-with-one-tag/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

### Feat

- Initial commit
- Add feature 1

<!-- generated by git-cliff -->
31 changes: 30 additions & 1 deletion git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
}
Expand Down Expand Up @@ -464,6 +469,18 @@ mod test {
.to_string())
}

fn get_root_commit_hash() -> Result<String> {
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<String> {
Ok(str::from_utf8(
Command::new("git")
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading