Skip to content

Commit

Permalink
fix(changelog): include the root commit when --latest is used with …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
rarescosma authored Oct 5, 2024
1 parent f5c39a2 commit 508a97e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
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"

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

0 comments on commit 508a97e

Please sign in to comment.