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 2 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 -->
34 changes: 32 additions & 2 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,21 @@ impl Repository {
pub fn commits(
&self,
range: Option<&str>,
include_root: bool,
include_path: Option<Vec<Pattern>>,
exclude_path: Option<Vec<Pattern>>,
) -> Result<Vec<Commit>> {
let mut revwalk = self.inner.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
if let Some(range) = range {
revwalk.push_range(range)?;
// Push only the last range commit if we need to start at root.
if include_root {
if let Some(dots_index) = range.find("..") {
revwalk.push(Oid::from_str(&range[dots_index + 2..])?)?;
}
} else {
revwalk.push_range(range)?;
}
rarescosma marked this conversation as resolved.
Show resolved Hide resolved
} else {
revwalk.push_head()?;
}
Expand Down Expand Up @@ -464,6 +472,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 All @@ -488,7 +508,7 @@ mod test {
#[test]
fn get_latest_commit() -> Result<()> {
let repository = get_repository()?;
let commits = repository.commits(None, None, None)?;
let commits = repository.commits(None, false, None, None)?;
let last_commit =
AppCommit::from(&commits.first().expect("no commits found").clone());
assert_eq!(get_last_commit_hash()?, last_commit.id);
Expand Down Expand Up @@ -586,6 +606,16 @@ mod test {
Ok(())
}

#[test]
fn includes_root_commit() -> Result<()> {
let repository = get_repository()?;
let commits = repository.commits(None, true, 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
5 changes: 4 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,20 @@ fn process_repository<'a>(

// Parse commits.
let mut commit_range = args.range.clone();
let mut include_root = false;
if args.unreleased {
if let Some(last_tag) = tags.last().map(|(k, _)| k) {
commit_range = Some(format!("{last_tag}..HEAD"));
}
} else if args.latest || args.current {
if tags.len() < 2 {
let commits = repository.commits(None, None, None)?;
let commits = repository.commits(None, false, None, None)?;
if let (Some(tag1), Some(tag2)) = (
commits.last().map(|c| c.id().to_string()),
tags.get_index(0).map(|(k, _)| k),
) {
commit_range = Some(format!("{tag1}..{tag2}"));
rarescosma marked this conversation as resolved.
Show resolved Hide resolved
include_root = true;
}
} else {
let mut tag_index = tags.len() - 2;
Expand Down Expand Up @@ -208,6 +210,7 @@ fn process_repository<'a>(
}
let mut commits = repository.commits(
commit_range.as_deref(),
include_root,
args.include_path.clone(),
args.exclude_path.clone(),
)?;
Expand Down
Loading