use thiserror::Error;
use crate::error::PathIoError;
use crate::objects::ObjectId;
use crate::porcelain::branch_error::BranchError;
use crate::porcelain::hunk_select::HunkSelectError;
pub type StashResult<T> = std::result::Result<T, StashError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CorruptStashReason {
InsufficientParents,
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum StashPushValidationError {
#[error(transparent)]
InvalidSignature(#[from] crate::ident::SignatureError),
#[error("cannot combine --staged with --include-untracked")]
StagedWithUntracked,
#[error("cannot combine --staged with --hunks or --lines")]
StagedWithSelection,
#[error("cannot combine --keep-index with --hunks or --lines")]
KeepIndexWithSelection,
#[error("cannot combine --include-untracked with --hunks or --lines")]
UntrackedWithSelection,
#[error("cannot stash staged changes for '{path}': worktree has unstaged changes")]
StagedWorktreeDirty {
path: String,
},
#[error("dry-run is not supported on push; use preview instead")]
DryRunUsePreview,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StashError {
#[error(transparent)]
Library(#[from] Box<crate::error::Error>),
#[error("corrupt stash commit {oid}: {reason}")]
CorruptStash {
oid: ObjectId,
reason: CorruptStashReason,
},
#[error("stash entry stash@{{{n}}} not found")]
NotFound {
n: usize,
},
#[error("stash commit {oid} is not on the stash stack")]
CommitNotOnStack {
oid: ObjectId,
},
#[error("cannot stash without a commit")]
NoInitialCommit,
#[error("cannot stash in a bare repository")]
BareRepository,
#[error("conflicts in index while applying stash with --index")]
IndexConflict,
#[error("missing HEAD while applying stash")]
MissingHead,
#[error(
"your local changes to the following files would be overwritten by stash apply: {paths}"
)]
WouldOverwriteLocalChanges {
paths: String,
},
#[error("writing index after stash apply")]
IndexWrite {
#[source]
source: PathIoError,
},
#[error("symlink target is not UTF-8")]
SymlinkNotUtf8,
#[error("stash untracked restore blocked: '{path}' already exists on disk")]
CouldNotRestoreUntracked {
path: String,
},
#[error(transparent)]
HunkSelection(#[from] HunkSelectError),
#[error(transparent)]
Branch(#[from] BranchError),
#[error(transparent)]
Apply(#[from] crate::error::ApplyError),
#[error(transparent)]
InvalidSignature(#[from] crate::ident::SignatureError),
#[error("operation cancelled")]
Cancelled,
#[error(transparent)]
PushValidation(#[from] StashPushValidationError),
#[error("could not update '{path}' while finishing stash: {message}")]
WorktreeUpdateFailed {
path: String,
message: String,
},
}
impl StashError {
pub(crate) fn corrupt_stash(oid: ObjectId, reason: CorruptStashReason) -> Self {
Self::CorruptStash { oid, reason }
}
pub(crate) fn library(err: crate::error::Error) -> Self {
match err {
crate::error::Error::Cancelled => Self::Cancelled,
other => Self::Library(Box::new(other)),
}
}
pub(crate) fn from_apply_worktree(err: crate::error::ApplyError) -> Self {
match err {
crate::error::ApplyError::Cancelled => Self::Cancelled,
crate::error::ApplyError::Failed { path, message } if !path.is_empty() => {
Self::WorktreeUpdateFailed { path, message }
}
other => Self::Apply(other),
}
}
pub(crate) fn into_error(self) -> crate::error::Error {
match self {
Self::Cancelled => crate::error::Error::Cancelled,
Self::Library(boxed) => *boxed,
other => crate::error::Error::Message(other.to_string()),
}
}
}
impl From<crate::error::Error> for StashError {
fn from(err: crate::error::Error) -> Self {
match err {
crate::error::Error::Cancelled => Self::Cancelled,
other => Self::Library(Box::new(other)),
}
}
}
impl From<crate::index::IndexError> for StashError {
fn from(err: crate::index::IndexError) -> Self {
Self::library(crate::error::Error::from(err))
}
}
impl From<StashError> for crate::error::Error {
fn from(err: StashError) -> Self {
err.into_error()
}
}
#[cfg(feature = "serde-models")]
impl StashError {
pub(crate) fn legacy_kind(&self) -> &'static str {
match self {
Self::CorruptStash { .. } => "corrupt_stash",
Self::NotFound { .. } => "stash_not_found",
Self::CommitNotOnStack { .. } => "stash_commit_not_on_stack",
Self::NoInitialCommit => "stash_no_initial_commit",
Self::BareRepository => "stash_bare_repository",
Self::IndexConflict => "stash_index_conflict",
Self::MissingHead => "stash_missing_head",
Self::WouldOverwriteLocalChanges { .. } => "stash_would_overwrite_local_changes",
Self::IndexWrite { .. } => "stash_index_write",
Self::SymlinkNotUtf8 => "stash_symlink_not_utf8",
Self::CouldNotRestoreUntracked { .. } => "stash_could_not_restore_untracked",
Self::PushValidation(_) => "stash_option_conflict",
Self::Cancelled => "cancelled",
Self::HunkSelection(_) => "hunk_selection",
Self::Branch(_) => "branch",
Self::Apply(_) => "apply",
Self::InvalidSignature(_) => "invalid_signature",
Self::WorktreeUpdateFailed { .. } => "stash_worktree_update_failed",
Self::Library(boxed) => boxed.kind_name(),
}
}
}
#[cfg(feature = "serde-models")]
impl serde::Serialize for StashError {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
match self {
Self::CouldNotRestoreUntracked { path } => {
let mut out = serializer.serialize_struct("Error", 3)?;
out.serialize_field("kind", self.legacy_kind())?;
out.serialize_field("message", &self.to_string())?;
out.serialize_field("path", path)?;
out.end()
}
_ => {
let mut out = serializer.serialize_struct("Error", 2)?;
out.serialize_field("kind", self.legacy_kind())?;
out.serialize_field("message", &self.to_string())?;
out.end()
}
}
}
}
impl std::fmt::Display for CorruptStashReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InsufficientParents => f.write_str("expected at least 2 parents"),
}
}
}