use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::domain::{
ApplyRefusal, ConflictRefusal, LfsUnavailableReason, PushRejection, RebaseRefusal,
RewordRefusal, RewriteRefusal, SplitRefusal, UndoRefusal,
};
use crate::failure::ErrorDetail;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("operation not supported by this backend: {operation}")]
Unsupported { operation: &'static str },
#[error("repository not found at {0}")]
RepoNotFound(PathBuf),
#[error("working tree is not clean, operation requires a clean tree: {operation}")]
DirtyWorkingTree { operation: &'static str },
#[error("conflict encountered during {operation}")]
Conflict { operation: &'static str },
#[error("{} rejected the push of {}: {}", .0.remote, .0.branch, reject_reason(.0))]
PushRejected(PushRejection),
#[error(
"a grouped push or pull of {branch} is waiting (run {run_id}): continue or abort it first"
)]
RemoteGroupPending { branch: String, run_id: String },
#[error("cannot fast-forward: the two have diverged")]
NotFastForward,
#[error("cannot change that commit: {}", reword_reason(*.0))]
RewordRefused(RewordRefusal),
#[error("cannot split that commit: {}", split_reason(*.0))]
SplitRefused(SplitRefusal),
#[error("cannot rewrite history: {}{}", rewrite_reason(*.0), rewrite_detail(.1))]
RewriteRefused(RewriteRefusal, String),
#[error("cannot apply that commit: {}", apply_reason(*.0))]
ApplyRefused(ApplyRefusal),
#[error("cannot rebase: {}", rebase_reason(*.0))]
RebaseRefused(RebaseRefusal),
#[error("cannot resolve: {}", .0.message())]
ConflictRefused(ConflictRefusal),
#[error("cannot undo/redo: {}", .0.message())]
UndoRefused(UndoRefusal),
#[error("confirmation needed: {0}")]
NeedsConfirmation(String),
#[error("{}", .reason.message())]
LfsUnavailable { reason: LfsUnavailableReason },
#[error("the LFS server behind {remote} does not support file locking")]
LfsLockingUnsupported { remote: String },
#[error(
"{operation} is blocked by local changes; stash them automatically \
(--autostash, or `general.auto_stash = \"always\"`) or commit or discard them first"
)]
NeedsAutoStash { operation: String },
#[error("{operation} is blocked: an auto-stash (stash@{{{}}}) is still waiting; {hint}", .stash.0)]
PendingAutoStash {
operation: String,
stash: crate::domain::StashId,
hint: String,
},
#[error("{feature} is not built into this binary")]
NotBuilt { feature: &'static str },
#[error("this repository's authentication cannot be used: {reason}")]
RepoAuthUnavailable { reason: String },
#[error("the token command failed{}: {stderr_tail}", exit.map(|c| format!(" (exit {c})")).unwrap_or_default())]
TokenCommandFailed {
exit: Option<i32>,
stderr_tail: String,
},
#[error("{path} changed since it was confirmed as this repository's credential command: confirm the command again")]
RepoAuthCommandChanged { path: String },
#[error("GitPoulpi did not send the account's token to {host}: it is not {expected}, the address that account is bound to")]
GrantRefused { host: String, expected: String },
#[error("GitPoulpi's credential helper could not reach the daemon")]
CredentialHelperUnreachable,
#[error("canceled (job {job})")]
Canceled { job: u64 },
#[error("cannot be canceled: {}", .reason.message())]
NotCancellable { reason: NotCancellableReason },
#[error("already running as job {existing}")]
JobConflict { existing: u64 },
#[error(
"the repository's workflow hooks are not trusted ({} command(s), hash {hash}); review \
them, then `gitpoulpi flow trust-hooks --hash {hash}`",
.hooks.len()
)]
HooksNotTrusted {
hash: String,
hooks: Vec<crate::workflow::HookInfo>,
},
#[error(
"sensitive files were not staged: {}; ask the user, then stage again allowing \
sensitive files",
crate::privacy::summary(.files)
)]
SensitiveStage {
files: Vec<crate::privacy::Sensitive>,
},
#[error("backend error: {0}")]
Backend(String),
#[error("backend error: {message}")]
Git {
message: String,
detail: Box<ErrorDetail>,
},
#[error("io error: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NotCancellableReason {
Integrating,
RewritingFiles,
InSteps,
Sent,
Purging,
Finished,
UnknownJob,
}
impl NotCancellableReason {
pub fn message(self) -> &'static str {
match self {
Self::Integrating => "merging - cannot stop safely",
Self::RewritingFiles => "rewriting files - cannot stop safely",
Self::InSteps => "running a multi-step workflow - cannot stop between its steps yet",
Self::Sent => "sent to the host, waiting for its answer",
Self::Purging => "deleting the old history - cannot stop safely",
Self::Finished => "it has already ended",
Self::UnknownJob => "no such job",
}
}
}
pub const BUILD_FEATURES: &[&str] = &[
"ai",
"hosting",
"github",
"gitlab",
"mcp",
"lfs",
"worktree",
"submodule",
"avatar-fetch",
"history-rewrite",
];
fn known_feature(name: &str) -> &'static str {
BUILD_FEATURES
.iter()
.find(|f| **f == name)
.copied()
.unwrap_or("unknown")
}
fn reject_reason(rejection: &PushRejection) -> &'static str {
match rejection.reason {
crate::domain::PushRejectReason::NonFastForward => {
"the remote has commits this branch does not (pull first, or force push)"
}
crate::domain::PushRejectReason::StaleLease => {
"the remote branch moved since the last fetch, so the force push was refused"
}
crate::domain::PushRejectReason::ProtectedByHost => {
"the host's branch protection refused the push"
}
crate::domain::PushRejectReason::RemoteRefused => "the remote refused the push",
crate::domain::PushRejectReason::Unknown => "the remote rejected the push",
}
}
fn reword_reason(refusal: RewordRefusal) -> &'static str {
match refusal {
RewordRefusal::NotOnCurrentBranch => {
"the commit is not on the current branch (it is not HEAD or an ancestor of HEAD)"
}
RewordRefusal::OperationInProgress => {
"a merge, rebase, cherry-pick, revert or bisect is in progress"
}
RewordRefusal::EmptyMessage => "the new message is empty",
RewordRefusal::InvalidIdentity => {
"a name or email is empty or has a character that cannot be stored (< > or a line break)"
}
RewordRefusal::InvalidDate => "a date is not in the raw format or ISO 8601",
}
}
fn rewrite_detail(detail: &str) -> String {
if detail.is_empty() {
String::new()
} else {
format!(" ({detail})")
}
}
fn rewrite_reason(refusal: RewriteRefusal) -> &'static str {
use RewriteRefusal as R;
match refusal {
R::OperationInProgress => "a merge, rebase, cherry-pick, revert or bisect is in progress",
R::AutoStashPending => "an auto-stash is waiting to be restored",
R::Shallow => "this is a shallow clone, its history is incomplete",
R::PartialClone => "this is a partial clone and the rules read file content",
R::UnsupportedObjectFormat => "SHA-256 repositories are not supported",
R::EmptyScope => "the scope contains no commit or ref",
R::NoRule => "no rule is enabled",
R::InvalidRule => "a rule is not valid",
R::DirtyPathsTouched => "files the rewrite would change have uncommitted edits",
R::CheckedOutElsewhere => "a branch is checked out in another worktree",
R::ProtectedRef => "a pushed protected branch is in scope and was not included",
R::Stale => "the repository changed since the preview, preview again",
R::ToolMissing => "a tool the rules need is not installed",
R::ScriptFailed => "a script failed",
R::Purged => "the old history was purged, there is nothing to restore",
R::ScriptsDisabled => {
"this kind of script is switched off in the settings (rewrite.external_scripts)"
}
R::ScriptNotTrusted => "the script is not trusted yet",
R::SignatureChoiceNeeded => {
"signed commits are affected: choose to strip, re-sign signed or re-sign all"
}
R::ExtractModeNeeded => "choose how to extract: clone, in place or submodule",
R::SigningFailed => {
"signing does not work here (key, agent or program), check the signing setup"
}
R::LfsObjectsMissing => {
"LFS objects the export needs are not in the local LFS store: fetch them first"
}
R::PathCollision => "a path rule would put a file where one already exists",
}
}
fn apply_reason(refusal: ApplyRefusal) -> &'static str {
match refusal {
ApplyRefusal::MergeCommit => "a merge commit has no single set of changes to apply",
}
}
fn split_reason(refusal: SplitRefusal) -> &'static str {
match refusal {
SplitRefusal::NotOnCurrentBranch => {
"the commit is not on the current branch (it is not HEAD or an ancestor of HEAD)"
}
SplitRefusal::MergeCommit => "a merge commit cannot be split",
SplitRefusal::EmptyCommit => "the commit changes nothing",
SplitRefusal::OperationInProgress => {
"a merge, rebase, cherry-pick, revert or bisect is in progress"
}
SplitRefusal::TooFewParts => "a split needs at least two commits",
SplitRefusal::EmptyPart => "a new commit has no changes assigned to it",
SplitRefusal::EmptyMessage => "a new commit has an empty message",
SplitRefusal::Unassigned => "some changes are not assigned to any commit",
SplitRefusal::AssignedTwice => "some changes are assigned to more than one commit",
SplitRefusal::UnknownPiece => "a piece does not exist in the commit or cannot be selected",
SplitRefusal::WholeFileOnly => "that file can only be assigned as a whole",
SplitRefusal::SigningFailed => "signing the new commits failed",
}
}
fn rebase_reason(refusal: RebaseRefusal) -> &'static str {
match refusal {
RebaseRefusal::OperationInProgress => {
"a merge, rebase, cherry-pick, revert or bisect is already in progress"
}
RebaseRefusal::UncommittedChanges => "the working tree has uncommitted changes",
RebaseRefusal::NotOnCurrentBranch => {
"the target is not on the current branch, or HEAD is detached"
}
RebaseRefusal::MergeInRange => "the range contains a merge commit",
RebaseRefusal::PlanMismatch => {
"the plan is not exactly a reordering of the commits being rebased"
}
RebaseRefusal::SquashWithoutPredecessor => {
"the oldest step cannot be squash or fixup: it has nothing to combine into"
}
RebaseRefusal::EmptyRewordMessage => "a reword step has no message",
RebaseRefusal::NoRebaseInProgress => "no rebase is in progress",
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TypedError {
PushRejected(PushRejection),
RemoteGroupPending {
branch: String,
run_id: String,
},
NotFastForward,
RewordRefused(RewordRefusal),
SplitRefused(SplitRefusal),
ApplyRefused(ApplyRefusal),
RewriteRefused(RewriteRefusal, String),
RebaseRefused(RebaseRefusal),
ConflictRefused(ConflictRefusal),
UndoRefused(UndoRefusal),
NeedsConfirmation(String),
LfsUnavailable(LfsUnavailableReason),
LfsLockingUnsupported {
remote: String,
},
NeedsAutoStash {
operation: String,
},
PendingAutoStash {
operation: String,
stash: crate::domain::StashId,
hint: String,
},
NotBuilt {
feature: String,
},
RepoAuthUnavailable {
reason: String,
},
TokenCommandFailed {
exit: Option<i32>,
stderr_tail: String,
},
RepoAuthCommandChanged {
path: String,
},
GrantRefused {
host: String,
expected: String,
},
CredentialHelperUnreachable,
Canceled {
job: u64,
},
NotCancellable {
reason: NotCancellableReason,
},
JobConflict {
existing: u64,
},
HooksNotTrusted {
hash: String,
hooks: Vec<crate::workflow::HookInfo>,
},
SensitiveStage {
files: Vec<crate::privacy::Sensitive>,
},
}
impl Error {
pub fn typed(&self) -> Option<TypedError> {
match self {
Error::PushRejected(r) => Some(TypedError::PushRejected(r.clone())),
Error::RemoteGroupPending { branch, run_id } => Some(TypedError::RemoteGroupPending {
branch: branch.clone(),
run_id: run_id.clone(),
}),
Error::NotFastForward => Some(TypedError::NotFastForward),
Error::RewordRefused(r) => Some(TypedError::RewordRefused(*r)),
Error::SplitRefused(r) => Some(TypedError::SplitRefused(*r)),
Error::ApplyRefused(r) => Some(TypedError::ApplyRefused(*r)),
Error::RewriteRefused(r, d) => Some(TypedError::RewriteRefused(*r, d.clone())),
Error::RebaseRefused(r) => Some(TypedError::RebaseRefused(*r)),
Error::ConflictRefused(r) => Some(TypedError::ConflictRefused(r.clone())),
Error::UndoRefused(r) => Some(TypedError::UndoRefused(r.clone())),
Error::NeedsConfirmation(p) => Some(TypedError::NeedsConfirmation(p.clone())),
Error::LfsUnavailable { reason } => Some(TypedError::LfsUnavailable(reason.clone())),
Error::LfsLockingUnsupported { remote } => Some(TypedError::LfsLockingUnsupported {
remote: remote.clone(),
}),
Error::NeedsAutoStash { operation } => Some(TypedError::NeedsAutoStash {
operation: operation.clone(),
}),
Error::PendingAutoStash {
operation,
stash,
hint,
} => Some(TypedError::PendingAutoStash {
operation: operation.clone(),
stash: *stash,
hint: hint.clone(),
}),
Error::NotBuilt { feature } => Some(TypedError::NotBuilt {
feature: (*feature).to_string(),
}),
Error::RepoAuthUnavailable { reason } => Some(TypedError::RepoAuthUnavailable {
reason: reason.clone(),
}),
Error::TokenCommandFailed { exit, stderr_tail } => {
Some(TypedError::TokenCommandFailed {
exit: *exit,
stderr_tail: stderr_tail.clone(),
})
}
Error::RepoAuthCommandChanged { path } => {
Some(TypedError::RepoAuthCommandChanged { path: path.clone() })
}
Error::CredentialHelperUnreachable => Some(TypedError::CredentialHelperUnreachable),
Error::GrantRefused { host, expected } => Some(TypedError::GrantRefused {
host: host.clone(),
expected: expected.clone(),
}),
Error::Canceled { job } => Some(TypedError::Canceled { job: *job }),
Error::NotCancellable { reason } => {
Some(TypedError::NotCancellable { reason: *reason })
}
Error::JobConflict { existing } => Some(TypedError::JobConflict {
existing: *existing,
}),
Error::HooksNotTrusted { hash, hooks } => Some(TypedError::HooksNotTrusted {
hash: hash.clone(),
hooks: hooks.clone(),
}),
Error::SensitiveStage { files } => Some(TypedError::SensitiveStage {
files: files.clone(),
}),
_ => None,
}
}
pub fn detail(&self) -> Option<&ErrorDetail> {
match self {
Error::Git { detail, .. } => Some(detail),
Error::PushRejected(r) => r.detail.as_deref(),
_ => None,
}
}
pub fn git(message: impl Into<String>, detail: ErrorDetail) -> Self {
Error::Git {
message: message.into(),
detail: Box::new(detail),
}
}
pub fn from_wire(message: String, detail: Option<ErrorDetail>) -> Self {
match (Self::from_wire_message(message), detail) {
(Error::Backend(message), Some(detail)) => Error::git(message, detail),
(other, _) => other,
}
}
pub fn from_wire_message(message: String) -> Self {
match message.strip_prefix("backend error: ") {
Some(rest) => Error::Backend(rest.to_string()),
None => Error::Backend(message),
}
}
pub fn from_typed(typed: TypedError) -> Self {
match typed {
TypedError::PushRejected(r) => Error::PushRejected(r),
TypedError::RemoteGroupPending { branch, run_id } => {
Error::RemoteGroupPending { branch, run_id }
}
TypedError::NotFastForward => Error::NotFastForward,
TypedError::RewordRefused(r) => Error::RewordRefused(r),
TypedError::SplitRefused(r) => Error::SplitRefused(r),
TypedError::ApplyRefused(r) => Error::ApplyRefused(r),
TypedError::RewriteRefused(r, d) => Error::RewriteRefused(r, d),
TypedError::RebaseRefused(r) => Error::RebaseRefused(r),
TypedError::ConflictRefused(r) => Error::ConflictRefused(r),
TypedError::UndoRefused(r) => Error::UndoRefused(r),
TypedError::NeedsConfirmation(p) => Error::NeedsConfirmation(p),
TypedError::LfsUnavailable(reason) => Error::LfsUnavailable { reason },
TypedError::LfsLockingUnsupported { remote } => Error::LfsLockingUnsupported { remote },
TypedError::NeedsAutoStash { operation } => Error::NeedsAutoStash { operation },
TypedError::PendingAutoStash {
operation,
stash,
hint,
} => Error::PendingAutoStash {
operation,
stash,
hint,
},
TypedError::NotBuilt { feature } => Error::NotBuilt {
feature: known_feature(&feature),
},
TypedError::RepoAuthUnavailable { reason } => Error::RepoAuthUnavailable { reason },
TypedError::TokenCommandFailed { exit, stderr_tail } => {
Error::TokenCommandFailed { exit, stderr_tail }
}
TypedError::RepoAuthCommandChanged { path } => Error::RepoAuthCommandChanged { path },
TypedError::GrantRefused { host, expected } => Error::GrantRefused { host, expected },
TypedError::CredentialHelperUnreachable => Error::CredentialHelperUnreachable,
TypedError::Canceled { job } => Error::Canceled { job },
TypedError::NotCancellable { reason } => Error::NotCancellable { reason },
TypedError::JobConflict { existing } => Error::JobConflict { existing },
TypedError::HooksNotTrusted { hash, hooks } => Error::HooksNotTrusted { hash, hooks },
TypedError::SensitiveStage { files } => Error::SensitiveStage { files },
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::PushRejectReason;
#[test]
fn a_backend_error_crossing_the_wire_keeps_one_prefix() {
let sent = Error::Backend("x is not in commit abc".into()).to_string();
assert_eq!(sent, "backend error: x is not in commit abc");
let received = Error::from_wire_message(sent.clone());
assert_eq!(received.to_string(), sent);
assert_eq!(
Error::from_wire_message("boom".into()).to_string(),
"backend error: boom"
);
}
#[test]
fn a_typed_error_round_trips_through_json() {
let original = Error::PushRejected(PushRejection {
remote: "origin".into(),
branch: "main".into(),
reason: PushRejectReason::NonFastForward,
detail: None,
});
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
let back = Error::from_typed(serde_json::from_str(&wire).unwrap());
assert!(matches!(
back,
Error::PushRejected(PushRejection { ref remote, reason: PushRejectReason::NonFastForward, .. })
if remote == "origin"
));
assert!(Error::Backend("x".into()).typed().is_none());
}
#[test]
fn split_refusals_round_trip_through_json() {
let original = Error::SplitRefused(SplitRefusal::Unassigned);
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
let back = Error::from_typed(serde_json::from_str(&wire).unwrap());
assert!(matches!(
back,
Error::SplitRefused(SplitRefusal::Unassigned)
));
assert!(back.to_string().contains("not assigned"));
}
#[test]
fn rewrite_refusals_round_trip_through_json_with_their_detail() {
let original = Error::RewriteRefused(
RewriteRefusal::InvalidRule,
"rule 2: the pattern is empty".into(),
);
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
let back = Error::from_typed(serde_json::from_str(&wire).unwrap());
match &back {
Error::RewriteRefused(RewriteRefusal::InvalidRule, detail) => {
assert_eq!(detail, "rule 2: the pattern is empty");
}
other => panic!("{other:?}"),
}
assert!(back.to_string().contains("a rule is not valid"));
assert!(back.to_string().contains("rule 2"));
let bare = Error::RewriteRefused(RewriteRefusal::NoRule, String::new());
assert_eq!(
bare.to_string(),
"cannot rewrite history: no rule is enabled"
);
}
#[test]
fn lfs_errors_round_trip_and_name_the_install_page() {
let original = Error::LfsUnavailable {
reason: LfsUnavailableReason::Missing,
};
assert!(original.to_string().contains("git-lfs.com"));
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
let back = Error::from_typed(serde_json::from_str(&wire).unwrap());
assert!(matches!(
back,
Error::LfsUnavailable {
reason: LfsUnavailableReason::Missing
}
));
let locking = Error::LfsLockingUnsupported {
remote: "origin".into(),
};
let wire = serde_json::to_string(&locking.typed().unwrap()).unwrap();
assert!(matches!(
Error::from_typed(serde_json::from_str(&wire).unwrap()),
Error::LfsLockingUnsupported { ref remote } if remote == "origin"
));
}
#[test]
fn job_errors_round_trip_with_stable_kinds() {
let canceled = Error::Canceled { job: 7 };
let wire = serde_json::to_string(&canceled.typed().unwrap()).unwrap();
assert_eq!(wire, r#"{"Canceled":{"job":7}}"#);
assert!(matches!(
Error::from_typed(serde_json::from_str(&wire).unwrap()),
Error::Canceled { job: 7 }
));
let refused = Error::NotCancellable {
reason: NotCancellableReason::Integrating,
};
assert!(refused.to_string().contains("merging"));
assert!(matches!(
Error::from_typed(refused.typed().unwrap()),
Error::NotCancellable {
reason: NotCancellableReason::Integrating
}
));
assert!(matches!(
Error::from_typed(Error::JobConflict { existing: 3 }.typed().unwrap()),
Error::JobConflict { existing: 3 }
));
}
#[test]
fn hooks_not_trusted_round_trips_with_its_commands() {
let original = Error::HooksNotTrusted {
hash: "ab".repeat(32),
hooks: vec![crate::workflow::HookInfo {
phase: "after_start".into(),
command: "make".into(),
personal: false,
}],
};
assert!(original.to_string().contains("not trusted"));
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
match Error::from_typed(serde_json::from_str(&wire).unwrap()) {
Error::HooksNotTrusted { hash, hooks } => {
assert_eq!(hash.len(), 64);
assert_eq!(hooks[0].command, "make");
}
other => panic!("{other:?}"),
}
}
#[test]
fn sensitive_stage_round_trips_with_reasons_and_no_value() {
use crate::privacy::{Reason, Sensitive};
let original = Error::SensitiveStage {
files: vec![
Sensitive {
path: ".env".into(),
reason: Reason::Pattern(".env".into()),
line: None,
},
Sensitive {
path: "a.txt".into(),
reason: Reason::Secret {
rule: "github-token".into(),
preview: "ghp_****".into(),
},
line: Some(3),
},
],
};
assert!(original.to_string().contains(".env, a.txt"));
let wire = serde_json::to_string(&original.typed().unwrap()).unwrap();
match Error::from_typed(serde_json::from_str(&wire).unwrap()) {
Error::SensitiveStage { files } => {
assert_eq!(files.len(), 2);
assert_eq!(files[1].path, "a.txt");
assert_eq!(files[1].line, Some(3));
}
other => panic!("{other:?}"),
}
}
#[test]
fn not_built_round_trips_and_serialises_with_a_stable_kind() {
let original = Error::NotBuilt { feature: "lfs" };
assert_eq!(original.to_string(), "lfs is not built into this binary");
let typed = original.typed().unwrap();
let wire = serde_json::to_string(&typed).unwrap();
assert_eq!(wire, r#"{"NotBuilt":{"feature":"lfs"}}"#);
assert!(matches!(
Error::from_typed(serde_json::from_str(&wire).unwrap()),
Error::NotBuilt { feature: "lfs" }
));
assert!(matches!(
Error::from_typed(TypedError::NotBuilt {
feature: "zzz".into()
}),
Error::NotBuilt { feature: "unknown" }
));
}
}