grit-lib 0.5.30

Core library for the grit Git implementation
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Pluggable reference storage: typed transactions and backends.
//!
//! The [`RefStore`] trait models Git's ref backend vtable (prepare/commit with
//! locking, raw reads, iteration, and reflog helpers). [`MemoryRefStore`] is an
//! embeddable in-memory implementation used by tests and callers that keep refs
//! outside a repository directory.
//!
//! To write your own backend, build `prepare` and `commit` on the [`backend`] helpers,
//! implement [`RefStore::rewrite_reflog`] with the same writer exclusion as `prepare`/`commit`,
//! and check it with the `grit_test_support::refstore_conformance` suite.

mod apply;
pub mod backend;
mod checked;
mod error;
mod files;
pub(crate) mod gate;
mod linked_worktree;
mod memory;
mod open;
pub mod paths;
pub(crate) mod reference_transaction;
mod reftable;
mod routing;
mod semantics;
mod transaction;
mod types;
mod validation;

pub(crate) use checked::{open_checked_ref_store, ObjectCheckedRefStore};
pub use error::{BackendError, RefStoreError};
pub use files::{FilesRefStore, FilesRefStoreConfig};
pub use gate::{SingleWriterGate, SingleWriterTicket};
pub(crate) use linked_worktree::LinkedWorktreeRefStore;
pub use memory::MemoryRefStore;
pub(crate) use open::open_ref_store;
pub(crate) use open::{initialize_head, open_files_ref_store, open_ref_store_uncached};
pub use reftable::ReftableRefStore;
pub(crate) use semantics::format_reflog_identity;
pub use transaction::RefTransaction;
pub use types::{
    Expected, RawRef, RefCaseOnlyRename, RefEntry, RefName, RefNameRef, RefReflogRename,
    RefStorageFormat, RefUpdate, RefUpdateFlags, ReflogUpdate,
};
pub(crate) use validation::{
    validate_reflog_identity_string, validate_reflog_message, validate_reflog_update,
};

use std::fmt::Debug;
use std::ops::ControlFlow;

use crate::error::Result;
use crate::lockfile::LockWait;
use crate::objects::ObjectId;
use crate::reflog::ReflogEntry;
use crate::refs::SYMREF_MAXDEPTH;

type StoreResult<T> = std::result::Result<T, RefStoreError>;

/// Committable ref update batch returned from [`RefStore::prepare`].
///
/// The value owns the locks `prepare` took. Exactly one of three things releases them:
/// [`Self::commit`], [`Self::abort`], or dropping the value. Implementations must release
/// their locks in `Drop` when neither method ran (callers bail out with `?` between prepare
/// and commit), leaving stored refs unchanged, so the next `prepare` does not fail with
/// [`RefStoreError::LockHeld`].
pub trait PreparedRefTransaction: Send {
    /// Apply queued updates and release locks.
    ///
    /// Every update becomes visible to readers, or (on error) none does where the backend
    /// can guarantee it. On the files backend, a compare-and-swap failure writes neither the
    /// ref nor its reflog entry.
    ///
    /// # Errors
    ///
    /// Returns [`RefStoreError`] when the batch can no longer be applied.
    fn commit(self: Box<Self>) -> StoreResult<()>;

    /// Updates [`RefTransaction::allowing_failures`] skipped during [`Self::commit`]'s prepare.
    ///
    /// Empty when the transaction was not prepared with [`RefTransaction::allowing_failures`].
    fn rejected_updates(&self) -> &[(RefName, RefStoreError)] {
        &[]
    }

    /// Release locks without changing stored refs.
    ///
    /// # Errors
    ///
    /// Propagates backend errors while releasing resources.
    fn abort(self: Box<Self>) -> StoreResult<()>;
}

/// Reference storage backend (loose/packed files, reftable, or in-memory).
///
/// # Re-entrant callbacks
///
/// The callbacks passed to [`Self::for_each_ref`], [`Self::for_each_reflog_entry`] and
/// [`Self::for_each_reflog_ref`] may call back into the same store, both to read (resolve a
/// symref, read each listed ref's reflog) and to write. The set of names to visit is fixed when
/// the walk starts, so a write made from the callback may not be seen by the walk in progress.
/// Implementations must not hold internal locks while a callback runs.
///
/// # Implementing a backend
///
/// Refs are full names (`HEAD`, `refs/heads/main`, …) mapped to a [`RawRef`]. Reads never
/// resolve symbolic refs except in [`Self::resolve`] and [`RefEntry::peeled`]. Writes only go
/// through [`Self::prepare`]; see its documentation for the checks it must make. The
/// [`backend`] module implements those checks and the commit semantics, and the
/// `grit_test_support::refstore_conformance` suite verifies a store agrees with the built-in
/// backends. You must implement [`Self::rewrite_reflog`] with the same writer exclusion as
/// `prepare`/`commit` for that ref; the suite includes a case that fails unsafe read-transform-write
/// paths. Report failures of your own storage as [`RefStoreError::Backend`] (or
/// [`crate::error::Error::RefStore`] wrapping it from the `Result` methods).
pub trait RefStore: Send + Sync + Debug {
    /// Which on-disk or logical format this store uses.
    ///
    /// Custom backends return [`RefStorageFormat::Custom`].
    fn format(&self) -> RefStorageFormat;

    /// Read the stored value for `name` without resolving symbolic targets.
    ///
    /// A missing ref returns `Ok(None)` without allocating a new name string.
    fn read_raw(&self, name: &RefNameRef) -> Result<Option<RawRef>>;

    /// [`Self::read_raw`] with a trusted UTF-8 name string (from the caller or storage).
    ///
    /// Default implementation delegates to [`Self::read_raw`]. Prefer
    /// [`Self::read_raw`] when you already hold a [`RefName`] / [`RefNameRef`].
    fn read_raw_str(&self, name: &RefNameRef) -> Result<Option<RawRef>> {
        self.read_raw(name)
    }

    /// Resolve `name` to an object id, following symbolic refs.
    ///
    /// Default implementation uses [`Self::read_raw`] and stops after
    /// [`SYMREF_MAXDEPTH`] hops.
    ///
    /// # Errors
    ///
    /// Returns [`RefStoreError::SymrefLoop`] when the chain is too deep and
    /// [`RefStoreError::NotFound`] when `name` or a symbolic target is missing.
    fn resolve(&self, name: &RefNameRef) -> Result<ObjectId> {
        resolve_store(self, name)
    }

    /// [`Self::resolve`] for a trusted UTF-8 ref name string.
    fn resolve_str(&self, name: &RefNameRef) -> Result<ObjectId> {
        resolve_store(self, name)
    }

    /// Invoke `f` for each ref whose name starts with `prefix`, in sorted name order.
    ///
    /// A non-empty `prefix` is a literal name prefix, so `"H"` or `"HEAD"` lists `HEAD` and
    /// `"ORIG"` lists `ORIG_HEAD`. An empty `prefix` lists only the refs under `refs/`, like
    /// `git for-each-ref` with no pattern: root refs such as `HEAD` and `ORIG_HEAD` are left
    /// out and stay reachable through [`Self::read_raw`] or a non-empty prefix they match.
    ///
    /// `f` may call back into the store; see [re-entrant callbacks](Self#re-entrant-callbacks).
    fn for_each_ref(
        &self,
        prefix: &str,
        f: &mut dyn FnMut(&RefEntry) -> ControlFlow<()>,
    ) -> Result<()>;

    /// Walk refs stored in this handle's physical reftable stack (`reftable/` under the
    /// store git directory). On the files backend this matches [`Self::for_each_ref`].
    fn for_each_physical_stack_ref(
        &self,
        prefix: &str,
        f: &mut dyn FnMut(&RefEntry) -> ControlFlow<()>,
    ) -> Result<()> {
        self.for_each_ref(prefix, f)
    }

    /// Validate `txn`, take locks, and return a committable batch.
    ///
    /// Every check runs here, before anything is written, and a failure leaves the store
    /// unchanged and unlocked. A store must:
    ///
    /// 1. Before taking locks, reject names Git refuses to store (an update name or either
    ///    side of a reflog or case-only rename) with [`RefStoreError::InvalidRefName`], a
    ///    symbolic value Git refuses with [`RefStoreError::InvalidSymrefTarget`], an update
    ///    other than a reflog-only one that would store the null object id with
    ///    [`RefStoreError::NullOid`], and malformed reflog identity or message fields with
    ///    [`RefStoreError::InvalidReflog`].
    /// 2. While another writer holds a lock this transaction needs, fail promptly with
    ///    [`RefStoreError::LockHeld`] naming this transaction's first ref when the caller
    ///    uses [`Self::prepare`] or [`Self::prepare_for`] with [`LockWait::Immediately`].
    ///    Cross-process contention (another process holding a ref lock file) should fail fast
    ///    so grit-lib can retry with jittered backoff for `core.filesRefLockTimeout`. The
    ///    files backend takes one lock per ref (plus packed-refs when needed), so only the
    ///    same ref contends in that case. A store with one in-process writer slot for
    ///    everything must wait inside [`Self::prepare_for`] using [`SingleWriterGate`] (or
    ///    equivalent FIFO queueing) for the supplied [`LockWait`]; exponential backoff between
    ///    immediate [`RefStoreError::LockHeld`] responses is not fair and will starve waiters
    ///    under load. Only return [`RefStoreError::LockHeld`] from `prepare_for` after the
    ///    wait budget elapses or for immediate attempts while the slot is busy.
    /// 3. Reject a created ref whose name is a directory prefix of a stored ref or of
    ///    another ref in the batch, or the other way round (`a` vs `a/b`), with
    ///    [`RefStoreError::NameUnavailable`]. Deletes in the same batch do not free a name.
    /// 4. Check each [`RefUpdate::expected`] against the state left by the earlier updates
    ///    in the batch ([`RefStoreError::ExpectedMismatch`]). For a direct-value update
    ///    through a symbolic ref, an [`Expected::Oid`] is compared with the resolved id.
    /// 5. Reject two updates that write the same stored ref, including through symbolic
    ///    refs, with [`RefStoreError::DuplicateUpdate`].
    ///
    /// `backend::check_transaction` performs steps 1 and 3–5 and
    /// `backend::apply_transaction` defines what [`PreparedRefTransaction::commit`] stores,
    /// including appended reflog lines and removing a deleted ref's reflog.
    ///
    /// A backend does not look at objects. The store
    /// [`Repository::ref_store`](crate::repo::Repository::ref_store) returns (whatever backend it
    /// wraps, including one injected with
    /// [`RepositoryOptions::with_ref_store`](crate::environment::RepositoryOptions::with_ref_store)) adds one
    /// more check before calling the backend. Each direct value must name an object the
    /// repository stores ([`RefStoreError::MissingObject`]). A branch (`refs/heads/…`) or
    /// `HEAD` must name a commit ([`RefStoreError::NotACommit`]). An object that cannot be
    /// read fails with [`RefStoreError::ObjectLookup`]. Deletes and symbolic values are not
    /// looked up. Reflog-only updates and explicit reflog object ids are checked like direct
    /// values. Updates flagged [`RefUpdateFlags::skip_oid_verification`] are not looked up.
    fn prepare(
        &self,
        txn: RefTransaction,
    ) -> StoreResult<Box<dyn PreparedRefTransaction + Send + '_>>;

    /// Like [`Self::prepare`], waiting up to `wait` for an in-process writer slot.
    ///
    /// Backends with one writer slot should block here (typically via [`SingleWriterGate`])
    /// until the slot is free or `wait` expires. Backends that already wait on lock files
    /// inside `prepare` may ignore `wait` and delegate to [`Self::prepare`]; grit-lib passes
    /// a shrinking remaining budget when it retries cross-process [`RefStoreError::LockHeld`].
    fn prepare_for(
        &self,
        txn: RefTransaction,
        wait: LockWait,
    ) -> StoreResult<Box<dyn PreparedRefTransaction + Send + '_>> {
        let _ = wait;
        self.prepare(txn)
    }

    /// Check the objects `updates` would point refs at, without taking any lock or writing.
    ///
    /// This is the object check [`Self::prepare`] makes on the store
    /// [`Repository::ref_store`](crate::repo::Repository::ref_store) returns. Writers that update a ref
    /// without a transaction (the files-backend fast path behind
    /// [`update_branch_for_commit_on_store`](crate::refs::update_branch_for_commit_on_store))
    /// call it before locking. Backends do not look at objects, so the default accepts
    /// everything; there is no need to override it.
    ///
    /// # Errors
    ///
    /// On the repository's store: [`RefStoreError::MissingObject`],
    /// [`RefStoreError::NotACommit`] or [`RefStoreError::ObjectLookup`] for the first
    /// offending update.
    fn verify_update_targets(&self, updates: &[RefUpdate]) -> StoreResult<()> {
        let _ = updates;
        Ok(())
    }

    /// Hook context when this store runs `reference-transaction` hooks.
    #[doc(hidden)]
    fn ref_hook_context(&self) -> Option<&crate::hooks::RefHookContext> {
        let _ = self;
        None
    }

    /// Null object id width for `reference-transaction` stdin on this store.
    #[doc(hidden)]
    fn reference_transaction_null_oid(&self) -> ObjectId {
        crate::diff::zero_oid()
    }

    /// Whether a reflog is present for `name`.
    fn reflog_exists(&self, name: &RefNameRef) -> Result<bool>;

    /// [`Self::reflog_exists`] for a trusted UTF-8 ref name string.
    fn reflog_exists_str(&self, name: &RefNameRef) -> Result<bool> {
        self.reflog_exists(name)
    }

    /// Iterate reflog entries for `name` (oldest first unless `reverse`).
    ///
    /// `f` may call back into the store; see [re-entrant callbacks](Self#re-entrant-callbacks).
    fn for_each_reflog_entry(
        &self,
        name: &RefNameRef,
        reverse: bool,
        f: &mut dyn FnMut(&ReflogEntry) -> ControlFlow<()>,
    ) -> Result<()>;

    /// [`Self::for_each_reflog_entry`] for a trusted UTF-8 ref name string.
    fn for_each_reflog_entry_str(
        &self,
        name: &RefNameRef,
        reverse: bool,
        f: &mut dyn FnMut(&ReflogEntry) -> ControlFlow<()>,
    ) -> Result<()> {
        self.for_each_reflog_entry(name, reverse, f)
    }

    /// Create an empty reflog file/record for `name`.
    fn create_reflog(&self, name: &RefNameRef) -> Result<()>;

    /// [`Self::create_reflog`] for a trusted UTF-8 ref name string.
    fn create_reflog_str(&self, name: &RefNameRef) -> Result<()> {
        self.create_reflog(name)
    }

    /// Remove the reflog for `name`.
    fn delete_reflog(&self, name: &RefNameRef) -> Result<()>;

    /// [`Self::delete_reflog`] for a trusted UTF-8 ref name string.
    fn delete_reflog_str(&self, name: &RefNameRef) -> Result<()> {
        self.delete_reflog(name)
    }

    /// Replace the entire reflog (used by expire/delete-entries).
    fn replace_reflog(&self, name: &RefNameRef, entries: Vec<ReflogEntry>) -> Result<()>;

    /// [`Self::replace_reflog`] for a trusted UTF-8 ref name string.
    fn replace_reflog_str(&self, name: &RefNameRef, entries: Vec<ReflogEntry>) -> Result<()> {
        self.replace_reflog(name, entries)
    }

    /// Read a reflog under the backend writer lock, transform it, and write it back atomically.
    ///
    /// Custom backends must implement this method; there is no default. Hold the same writer
    /// exclusion as [`Self::prepare`] / [`PreparedRefTransaction::commit`] for the whole
    /// read-transform-write on `name`, pass the locked re-read of entries into `transform`, and
    /// do not call back into this store from `transform` (that deadlocks backends that hold a
    /// mutex across the call).
    ///
    /// # Errors
    ///
    /// Propagates lock, I/O, and transform failures. [`RefStoreError::ReflogRewriteStale`] is
    /// returned when a caller compares against a snapshot and the locked re-read no longer
    /// matches (retry the outer operation).
    fn rewrite_reflog(
        &self,
        name: &RefNameRef,
        transform: &mut dyn FnMut(Vec<ReflogEntry>) -> Result<Vec<ReflogEntry>>,
    ) -> Result<()>;

    /// [`Self::rewrite_reflog`] for a trusted UTF-8 ref name string.
    fn rewrite_reflog_str(
        &self,
        name: &RefNameRef,
        transform: &mut dyn FnMut(Vec<ReflogEntry>) -> Result<Vec<ReflogEntry>>,
    ) -> Result<()> {
        self.rewrite_reflog(name, transform)
    }

    /// Invoke `f` for each ref name that has a reflog, in sorted order.
    ///
    /// `f` may call back into the store; see [re-entrant callbacks](Self#re-entrant-callbacks).
    fn for_each_reflog_ref(&self, f: &mut dyn FnMut(&RefNameRef) -> ControlFlow<()>) -> Result<()>;

    /// The files backend behind this store, when it is one.
    ///
    /// Lets callers reuse state the files backend keeps between calls (such as its
    /// `packed-refs` snapshot) instead of reading the same files again. Other backends,
    /// including injected ones, return `None`.
    fn as_files(&self) -> Option<&FilesRefStore> {
        None
    }

    /// How long grit-lib retries [`RefStoreError::LockHeld`] from [`Self::prepare`].
    ///
    /// The files backend reads `core.filesRefLockTimeout` from its git directory. Injected
    /// backends return Git's 100 ms default unless a wrapper (such as the store returned by
    /// [`crate::repo::Repository::ref_store`]) overrides this from the repository git directory.
    fn ref_lock_wait(&self) -> LockWait {
        self.as_files()
            .map(|files| LockWait::files_ref_lock_for_git_dir(files.git_dir()))
            .unwrap_or_else(|| LockWait::files_ref_lock(None))
    }
}

/// Resolve `name` through `store`, following symbolic refs.
pub fn resolve_store<S: RefStore + ?Sized>(store: &S, name: &RefNameRef) -> Result<ObjectId> {
    resolve_via_raw(store, name, 0).map_err(Into::into)
}

fn resolve_via_raw<S: RefStore + ?Sized>(
    store: &S,
    name: &RefNameRef,
    depth: usize,
) -> StoreResult<ObjectId> {
    if depth >= SYMREF_MAXDEPTH {
        return Err(RefStoreError::SymrefLoop);
    }
    match store.read_raw(name).map_err(|err| match err {
        crate::error::Error::RefStore(err) => err,
        other => RefStoreError::Corrupt(other.to_string()),
    })? {
        None => Err(RefStoreError::not_found(name.as_str())),
        Some(RawRef::Direct(oid)) => Ok(oid),
        Some(RawRef::Symbolic(target)) => {
            resolve_via_raw(store, RefNameRef::from_trusted(&target), depth + 1)
        }
    }
}