Skip to content

Commit

Permalink
feat(git): improve the set commit range error
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 19, 2024
1 parent e85888f commit e9066ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
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

0 comments on commit e9066ce

Please sign in to comment.