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