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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! Errors for [`super::RefStore`] operations.
use std::fmt;
use std::sync::Arc;
use crate::git_dir::GitDirPathOperation;
use crate::objects::{ObjectId, ObjectKind};
use crate::refs::RefnameUnavailable;
use super::{Expected, RawRef};
/// Failure modes for reference store reads and transactions.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum RefStoreError {
/// Reflog contents changed between a snapshot and a locked [`super::RefStore::rewrite_reflog`]
/// re-read. Callers that compare against an earlier snapshot should retry.
#[error("reflog '{name}' changed during rewrite")]
ReflogRewriteStale {
/// Ref whose log no longer matched the expected prefix.
name: String,
},
/// Another writer holds a lock this transaction needs.
///
/// For per-ref backends (files) that is the lock of `name` itself; for a backend with one
/// writer slot for the whole store ([`super::MemoryRefStore`], a database with a single
/// write transaction) it means that slot is busy, whichever refs the holder updates.
/// Transient: [`crate::refs::prepare_with_lock_wait`] retries it.
#[error("cannot lock ref '{name}': lock held")]
LockHeld {
/// A ref of the failed transaction: the locked ref, or the transaction's first ref
/// when the whole store is locked. Always one of the failed transaction's own refs.
name: String,
},
/// Compare-and-swap or existence check failed for an update.
#[error("ref '{name}' did not match expected state")]
ExpectedMismatch {
/// Ref that failed the check.
name: String,
/// What the caller required.
expected: Expected,
/// What was observed (`None` when the ref is absent).
actual: Option<RawRef>,
},
/// A ref name cannot be created because of a directory/file conflict.
#[error("cannot lock ref '{}': {}", reason.refname(), reason.lock_message_suffix())]
NameUnavailable {
/// Structured conflict reason (Git `refs_verify_refname_available` style).
reason: RefnameUnavailable,
},
/// The same ref name appears more than once in one transaction.
#[error("multiple updates for ref '{name}' not allowed")]
DuplicateUpdate {
/// Duplicate ref name.
name: String,
},
/// Symbolic reference resolution exceeded [`crate::refs::SYMREF_MAXDEPTH`].
#[error("symbolic ref loop or chain too deep")]
SymrefLoop,
/// The ref (or the target at the end of its symbolic chain) does not exist.
#[error("ref not found: {name}")]
NotFound {
/// Name of the missing ref: the requested name, or the dangling symbolic target.
name: String,
},
/// Stored data for a ref or reflog is malformed.
#[error("corrupt ref store: {0}")]
Corrupt(String),
/// Filesystem I/O on a git-directory path during ref store work.
#[error("{}", crate::git_dir::io_error::format_git_dir_io_message(.path, *operation, &Self::git_dir_io_source(*.os_error)))]
GitDirPathIo {
/// Display path such as `.git/refs/heads/main` or `.git/worktrees/wt/HEAD.lock`.
path: String,
/// What the operation was trying to do when I/O failed.
operation: GitDirPathOperation,
/// Raw OS error code from the failing I/O call.
os_error: i32,
},
/// Ref name is not safe to store (traversal, illegal component, etc.).
///
/// Every backend checks update names and both sides of reflog and case-only renames
/// in [`super::RefStore::prepare`], before taking locks.
#[error("refusing to update ref with bad name '{name}'")]
InvalidRefName {
/// The rejected ref name.
name: String,
},
/// A symbolic ref would point at a target Git refuses (outside `refs/` for `HEAD`, not
/// safe to store, or the same name as the ref being updated).
#[error("refusing to point '{name}' at symbolic target '{target}'")]
InvalidSymrefTarget {
/// The ref being updated.
name: String,
/// The symbolic target that was rejected.
target: String,
},
/// An update would store the all-zero (null) object id as a direct ref value.
///
/// The null id names no object, so Git treats such a ref as broken. Delete a ref with
/// `new_value: None` instead. Reflog-only updates
/// ([`super::RefUpdateFlags::log_only`]) may still carry the null id.
#[error("refusing to point ref '{name}' at the null object id")]
NullOid {
/// The ref the update targeted.
name: String,
},
/// An update would point a ref at an object the repository does not store.
///
/// Raised by the stores a [`crate::repo::Repository`] hands out (see
/// [`super::RefStore::prepare`]); the bare backends do not look at objects. Set
/// [`super::RefUpdateFlags::skip_oid_verification`] to write such a ref on purpose.
#[error("refusing to point ref '{name}' at {oid}: no such object in the repository")]
MissingObject {
/// The ref the update targeted.
name: String,
/// The object id the update would store.
oid: ObjectId,
},
/// An update would point a branch (`refs/heads/…`) or `HEAD` at something other than a
/// commit.
///
/// Checked alongside [`Self::MissingObject`]. Tags, remote-tracking and other refs may
/// name any object.
#[error("refusing to point '{name}' at {kind} {oid}: branches and HEAD must name a commit")]
NotACommit {
/// The branch or `HEAD` the update targeted.
name: String,
/// The object id the update would store.
oid: ObjectId,
/// What `oid` actually is.
kind: ObjectKind,
},
/// Looking up the object an update would store failed (unreadable pack, I/O, a failing
/// custom object store), so the update could not be checked.
#[error("cannot check object {oid} for ref '{name}': {source}")]
ObjectLookup {
/// The ref the update targeted.
name: String,
/// The object id the update would store.
oid: ObjectId,
/// The object database error, downcastable to [`crate::error::Error`].
source: BackendError,
},
/// Reflog identity or message would forge or corrupt a log line.
#[error(transparent)]
InvalidReflog(#[from] crate::ident::SignatureError),
/// A custom backend's own storage failed (database, network, …).
///
/// Built-in backends never return this; their I/O failures stay typed elsewhere.
#[error(transparent)]
Backend(BackendError),
/// A `reference-transaction` hook refused or could not run on the `prepared` phase.
#[error(transparent)]
Hook(ReferenceTransactionHookErr),
}
/// [`RefStoreError::Hook`] payload ( [`crate::hooks::HookError`] is not `Clone`).
#[derive(Clone, Debug)]
pub struct ReferenceTransactionHookErr(Arc<crate::hooks::HookError>);
impl PartialEq for ReferenceTransactionHookErr {
fn eq(&self, other: &Self) -> bool {
self.0.to_string() == other.0.to_string()
}
}
impl Eq for ReferenceTransactionHookErr {}
impl From<crate::hooks::HookError> for ReferenceTransactionHookErr {
fn from(value: crate::hooks::HookError) -> Self {
Self(Arc::new(value))
}
}
impl ReferenceTransactionHookErr {
pub(crate) fn into_hook_error(self) -> crate::hooks::HookError {
Arc::try_unwrap(self.0).unwrap_or_else(|arc| match arc.as_ref() {
crate::hooks::HookError::Failed {
hook_name,
status,
output,
} => crate::hooks::HookError::Failed {
hook_name: hook_name.clone(),
status: *status,
output: output.clone(),
},
crate::hooks::HookError::Spawn {
hook_name,
path,
source,
output,
} => crate::hooks::HookError::Spawn {
hook_name: hook_name.clone(),
path: path.clone(),
source: std::io::Error::new(source.kind(), source.to_string()),
output: output.clone(),
},
crate::hooks::HookError::Internal(msg) => {
crate::hooks::HookError::Internal(msg.clone())
}
})
}
}
impl std::fmt::Display for ReferenceTransactionHookErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for ReferenceTransactionHookErr {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.0.as_ref())
}
}
impl RefStoreError {
pub(crate) fn git_dir_path_io(
path: String,
operation: GitDirPathOperation,
source: std::io::Error,
) -> Self {
Self::GitDirPathIo {
path,
operation,
os_error: source.raw_os_error().unwrap_or(0),
}
}
pub(crate) fn git_dir_io_source(os_error: i32) -> std::io::Error {
if os_error == 0 {
std::io::Error::from(std::io::ErrorKind::Other)
} else {
std::io::Error::from_raw_os_error(os_error)
}
}
pub(crate) fn not_found(name: &str) -> Self {
Self::NotFound {
name: name.to_owned(),
}
}
/// Wrap a custom backend's storage error as [`Self::Backend`].
pub fn backend(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Backend(BackendError::new(err))
}
}
/// A custom ref backend's storage error, carried by [`RefStoreError::Backend`].
///
/// Cheap to clone (the error is shared). Two values are equal only when they share the same
/// underlying error. Use [`Self::downcast_ref`] to recover the backend's concrete type.
#[derive(Clone)]
pub struct BackendError(Arc<dyn std::error::Error + Send + Sync + 'static>);
impl BackendError {
/// Wrap `err`.
pub fn new(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self(Arc::new(err))
}
/// Borrow the wrapped error.
#[must_use]
pub fn get_ref(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
self.0.as_ref()
}
/// The wrapped error as `E`, when it has that type.
#[must_use]
pub fn downcast_ref<E: std::error::Error + 'static>(&self) -> Option<&E> {
self.0.downcast_ref::<E>()
}
}
impl fmt::Debug for BackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl fmt::Display for BackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl std::error::Error for BackendError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
}
}
impl PartialEq for BackendError {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for BackendError {}
impl From<BackendError> for RefStoreError {
fn from(err: BackendError) -> Self {
Self::Backend(err)
}
}
impl From<RefnameUnavailable> for RefStoreError {
fn from(reason: RefnameUnavailable) -> Self {
Self::NameUnavailable { reason }
}
}