Skip to main content

grit_lib/porcelain/
commit_error.rs

1//! Typed failures for commit, identity, signing, and patch-scoped commits.
2
3use thiserror::Error;
4
5use crate::config::ident_resolve::IdentityError;
6use crate::error::ApplyError;
7use crate::ident::CommitIdentityError;
8use crate::porcelain::hunk_select::HunkSelectError;
9
10/// Commit, identity, signing, and patch-scoped commit failures.
11///
12/// Wrapped by [`Error::Commit`](crate::error::Error::Commit). Stable [`kind_name`](Self::kind_name)
13/// slugs match the former umbrella variant names for scripts and the CLI error map.
14#[derive(Debug, Error)]
15#[non_exhaustive]
16pub enum CommitError {
17    /// A commit was requested with an empty message body after normalization.
18    #[error("empty commit message")]
19    EmptyCommitMessage,
20
21    /// [`crate::porcelain::commit::commit_with_scope`] was called with an empty
22    /// [`CommitScope::Paths`](crate::porcelain::commit::CommitScope::Paths) pathspec list.
23    #[error("you must specify path(s)")]
24    CommitPathsRequired,
25
26    /// [`crate::porcelain::commit::CommitScope::Paths`] was used while a merge is in progress.
27    #[error("cannot do a partial commit during a merge")]
28    PartialCommitDuringMerge,
29
30    /// Partial commit, amend, or patch commit while a cherry-pick or revert is in progress.
31    #[error("cannot do a partial commit during a cherry-pick or revert")]
32    PartialCommitDuringReplay,
33
34    /// The index tree matches the parent commit tree and `allow_empty` is false.
35    #[error("nothing to commit")]
36    NothingToCommit,
37
38    /// No staged changes for a staged-only commit, but the work tree has unstaged or untracked paths.
39    #[error("nothing staged to commit")]
40    NothingStagedToCommit {
41        /// Tracked paths that differ between the index and the work tree.
42        unstaged: usize,
43        /// Untracked paths under the work tree.
44        untracked: usize,
45    },
46
47    /// [`CommitRequest::amend`](crate::porcelain::commit::CommitRequest::amend) on an unborn branch.
48    #[error("cannot amend: no commits on this branch yet")]
49    AmendUnborn,
50
51    /// Author or committer on [`crate::porcelain::commit::CommitRequest`] is invalid.
52    #[error(transparent)]
53    InvalidIdentity(#[from] CommitIdentityError),
54
55    /// No usable author or committer identity is configured.
56    #[error(transparent)]
57    Identity(#[from] IdentityError),
58
59    /// Commit or tag signing failed.
60    #[error("commit signing failed")]
61    Signing {
62        /// Signing program invoked (`gpg`, `ssh-keygen`, …).
63        program: String,
64        /// Subprocess exit status when the program ran.
65        status: Option<i32>,
66        /// Subprocess stderr (or a short summary when spawn failed).
67        stderr: Box<String>,
68    },
69
70    /// Hunk id resolution failed during a patch-scoped commit.
71    #[error(transparent)]
72    HunkSelect(#[from] HunkSelectError),
73
74    /// Patch apply failed after hunk resolution.
75    #[error(transparent)]
76    PatchApply(#[from] ApplyError),
77
78    /// Commit message bytes are not valid UTF-8 after normalization.
79    #[error("invalid commit message")]
80    InvalidCommitMessage,
81
82    /// Change-id generation failed for a new commit.
83    #[error(transparent)]
84    ChangeId(#[from] crate::change_id::ChangeIdError),
85}
86
87impl CommitError {
88    /// Stable snake_case slug for this failure (matches former [`Error::kind_name`](crate::error::Error::kind_name) values).
89    #[must_use]
90    pub fn kind_name(&self) -> &'static str {
91        match self {
92            Self::EmptyCommitMessage => "empty_commit_message",
93            Self::CommitPathsRequired => "commit_paths_required",
94            Self::PartialCommitDuringMerge => "partial_commit_during_merge",
95            Self::PartialCommitDuringReplay => "partial_commit_during_replay",
96            Self::NothingToCommit => "nothing_to_commit",
97            Self::NothingStagedToCommit { .. } => "nothing_staged_to_commit",
98            Self::AmendUnborn => "amend_unborn",
99            Self::InvalidIdentity(..) => "invalid_identity",
100            Self::Identity(..) => "identity",
101            Self::Signing { .. } => "signing",
102            Self::HunkSelect(..) => "hunk_selection",
103            Self::PatchApply(..) => "apply",
104            Self::InvalidCommitMessage => "empty_commit_message",
105            Self::ChangeId(..) => "message",
106        }
107    }
108}
109
110impl CommitError {
111    /// Build [`Self::Signing`] when only a free-form summary is available (program and status unknown).
112    #[must_use]
113    pub fn signing_from_message(message: String) -> Self {
114        Self::Signing {
115            program: String::new(),
116            status: None,
117            stderr: Box::new(message),
118        }
119    }
120
121    /// Commit or tag signing failed running `program` with subprocess `status` and captured `stderr`.
122    #[must_use]
123    pub fn signing_failure(
124        program: impl Into<String>,
125        status: Option<i32>,
126        stderr: impl Into<String>,
127    ) -> Self {
128        Self::Signing {
129            program: program.into(),
130            status,
131            stderr: Box::new(stderr.into()),
132        }
133    }
134}