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

feat(git): improve the set commit range error #990

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub enum Error {
#[cfg(feature = "repo")]
#[error("Git error: `{0}`")]
GitError(#[from] git2::Error),
/// Error that may occur when failed to set a commit range.
#[cfg(feature = "repo")]
#[error(
"Failed to set the commit range: {1}
{0:?} is not a valid commit range. Did you provide the correct arguments?"
)]
SetCommitRangeError(String, #[source] git2::Error),
/// Error variant that represents other repository related errors.
#[cfg(feature = "repo")]
#[error("Git repository error: `{0}`")]
Expand Down
37 changes: 27 additions & 10 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use lazy_regex::{
};
use std::io;
use std::path::PathBuf;
use std::result::Result as StdResult;
use url::Url;

/// Regex for replacing the signature part of a tag message.
Expand Down Expand Up @@ -83,27 +84,43 @@ impl Repository {
path
}

/// Parses and returns the commits.
/// Sets the range for the commit search.
///
/// Sorts the commits by their time.
pub fn commits(
&self,
/// When a single SHA is provided as the range, start from the
/// root.
fn set_commit_range(
revwalk: &mut git2::Revwalk<'_>,
range: Option<&str>,
include_path: Option<Vec<Pattern>>,
exclude_path: Option<Vec<Pattern>>,
) -> Result<Vec<Commit>> {
let mut revwalk = self.inner.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
) -> StdResult<(), git2::Error> {
if let Some(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()?;
}
Ok(())
}

/// Parses and returns the commits.
///
/// Sorts the commits by their time.
pub fn commits(
&self,
range: Option<&str>,
include_path: Option<Vec<Pattern>>,
exclude_path: Option<Vec<Pattern>>,
) -> Result<Vec<Commit>> {
let mut revwalk = self.inner.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
Self::set_commit_range(&mut revwalk, range).map_err(|e| {
Error::SetCommitRangeError(
range.map(String::from).unwrap_or_else(|| "?".to_string()),
e,
)
})?;
let mut commits: Vec<Commit> = revwalk
.filter_map(|id| id.ok())
.filter_map(|id| self.inner.find_commit(id).ok())
Expand Down
Loading