From bba70d9d4d792e5b0404c729e9da0a9ea224c7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rare=C8=99=20Cosma?= Date: Wed, 2 Oct 2024 17:02:29 +0300 Subject: [PATCH] feat(git): latest with one tag should include root --- git-cliff-core/src/repo.rs | 12 ++++++++++-- git-cliff/src/lib.rs | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/git-cliff-core/src/repo.rs b/git-cliff-core/src/repo.rs index 8fe1f08731..35bd1403bd 100644 --- a/git-cliff-core/src/repo.rs +++ b/git-cliff-core/src/repo.rs @@ -72,13 +72,21 @@ impl Repository { pub fn commits( &self, range: Option<&str>, + include_root: bool, include_path: Option>, exclude_path: Option>, ) -> Result> { 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)?; + } } else { revwalk.push_head()?; } @@ -488,7 +496,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); diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index b2c29f95d9..ff72c343ae 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -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}")); + include_root = true; } } else { let mut tag_index = tags.len() - 2; @@ -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(), )?;