Skip to content

Commit

Permalink
feat(git): remove the include_root boolean flag
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rarescosma committed Oct 5, 2024
1 parent 6ec995c commit 06f96c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
17 changes: 8 additions & 9 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,17 @@ 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 {
// 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 {
if range.contains("..") {
revwalk.push_range(range)?;
} else {
// when we get a single sha as our "range" => start at root.
revwalk.push(Oid::from_str(&range)?)?;
}
} else {
revwalk.push_head()?;
Expand Down Expand Up @@ -508,7 +505,7 @@ mod test {
#[test]
fn get_latest_commit() -> Result<()> {
let repository = get_repository()?;
let commits = repository.commits(None, false, None, None)?;
let commits = repository.commits(None, 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 @@ -609,7 +606,9 @@ mod test {
#[test]
fn includes_root_commit() -> Result<()> {
let repository = get_repository()?;
let commits = repository.commits(None, true, None, None)?;
// 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);
Expand Down
11 changes: 6 additions & 5 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,22 @@ 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, false, None, None)?;
let commits = repository.commits(None, 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;
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 Expand Up @@ -210,7 +212,6 @@ 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 06f96c2

Please sign in to comment.