fix: Add error handling for invalid range

This commit is contained in:
Sahaj Jain 2024-11-22 01:45:51 +05:30
parent fece7a8125
commit 45405417ce

View file

@ -1,6 +1,8 @@
use crate::error::LumenError;
use thiserror::Error;
use super::commit::Commit;
#[derive(Error, Debug)]
pub enum DiffError {
#[error("diff{} is empty", if *staged { " (staged)" } else { "" })]
@ -39,11 +41,15 @@ impl Diff {
}
pub fn from_commits_range(from: &str, to: &str) -> Result<Self, LumenError> {
let _ = Commit::is_valid_commit(from)?;
let _ = Commit::is_valid_commit(to)?;
let output = std::process::Command::new("git")
.args(["diff", from, to])
.output()?;
let diff = String::from_utf8(output.stdout)?;
if diff.is_empty() {
return Err(DiffError::EmptyDiff { staged: false }.into());
}