Skip to content

Commit

Permalink
feat(git): latest with one tag should include root
Browse files Browse the repository at this point in the history
  • Loading branch information
rarescosma committed Oct 2, 2024
1 parent f5c39a2 commit bba70d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 10 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)?;
}
} else {
revwalk.push_head()?;
}
Expand Down Expand Up @@ -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);
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}"));
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

0 comments on commit bba70d9

Please sign in to comment.