gix_ref/store/file/transaction/
mod.rs

1use std::fmt::Formatter;
2
3use gix_hash::ObjectId;
4use gix_object::bstr::BString;
5
6use crate::{
7    store_impl::{file, file::Transaction},
8    transaction::RefEdit,
9};
10
11/// How to handle packed refs during a transaction
12#[derive(Default)]
13pub enum PackedRefs<'a> {
14    /// Only propagate deletions of references. This is the default.
15    /// This means deleted references are removed from disk if they are loose and from the packed-refs file if they are present.
16    #[default]
17    DeletionsOnly,
18    /// Propagate deletions as well as updates to references which are peeled and contain an object id.
19    ///
20    /// This means deleted references are removed from disk if they are loose and from the packed-refs file if they are present,
21    /// while updates are also written into the loose file as well as into packed-refs, potentially creating an entry.
22    DeletionsAndNonSymbolicUpdates(Box<dyn gix_object::Find + 'a>),
23    /// Propagate deletions as well as updates to references which are peeled and contain an object id. Furthermore delete the
24    /// reference which is originally updated if it exists. If it doesn't, the new value will be written into the packed ref right away.
25    /// Note that this doesn't affect symbolic references at all, which can't be placed into packed refs.
26    ///
27    /// Thus, this is similar to `DeletionsAndNonSymbolicUpdates`, but removes the loose reference after the update, leaving only their copy
28    /// in `packed-refs`.
29    DeletionsAndNonSymbolicUpdatesRemoveLooseSourceReference(Box<dyn gix_object::Find + 'a>),
30}
31
32#[derive(Debug)]
33pub(in crate::store_impl::file) struct Edit {
34    update: RefEdit,
35    lock: Option<gix_lock::Marker>,
36    /// Set if this update is coming from a symbolic reference and used to make it appear like it is the one that is handled,
37    /// instead of the referent reference.
38    parent_index: Option<usize>,
39    /// For symbolic refs, this is the previous OID to put into the reflog instead of our own previous value. It's the
40    /// peeled value of the leaf referent.
41    leaf_referent_previous_oid: Option<ObjectId>,
42}
43
44impl Edit {
45    fn name(&self) -> BString {
46        self.update.name.0.clone()
47    }
48}
49
50impl std::borrow::Borrow<RefEdit> for Edit {
51    fn borrow(&self) -> &RefEdit {
52        &self.update
53    }
54}
55
56impl std::borrow::BorrowMut<RefEdit> for Edit {
57    fn borrow_mut(&mut self) -> &mut RefEdit {
58        &mut self.update
59    }
60}
61
62/// Edits
63impl file::Store {
64    /// Open a transaction with the given `edits`, and determine how to fail if a `lock` cannot be obtained.
65    /// A snapshot of packed references will be obtained automatically if needed to fulfill this transaction
66    /// and will be provided as result of a successful transaction. Note that upon transaction failure, packed-refs
67    /// will never have been altered.
68    ///
69    /// The transaction inherits the parent namespace.
70    pub fn transaction(&self) -> Transaction<'_, '_> {
71        Transaction {
72            store: self,
73            packed_transaction: None,
74            updates: None,
75            packed_refs: PackedRefs::default(),
76        }
77    }
78}
79
80impl<'p> Transaction<'_, 'p> {
81    /// Configure the way packed refs are handled during the transaction
82    pub fn packed_refs(mut self, packed_refs: PackedRefs<'p>) -> Self {
83        self.packed_refs = packed_refs;
84        self
85    }
86}
87
88impl std::fmt::Debug for Transaction<'_, '_> {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        f.debug_struct("Transaction")
91            .field("store", self.store)
92            .field("edits", &self.updates.as_ref().map(Vec::len))
93            .finish_non_exhaustive()
94    }
95}
96
97///
98pub mod prepare;
99
100///
101pub mod commit;