gix_ref/store/file/transaction/
commit.rs

1use crate::{
2    store_impl::file::{transaction::PackedRefs, Transaction},
3    transaction::{Change, LogChange, RefEdit, RefLog},
4    Target,
5};
6
7impl Transaction<'_, '_> {
8    /// Make all [prepared][Transaction::prepare()] permanent and return the performed edits which represent the current
9    /// state of the affected refs in the ref store in that instant. Please note that the obtained edits may have been
10    /// adjusted to contain more dependent edits or additional information.
11    /// `committer` is used in the reflog and only if the reflog is actually written, which is why it is optional. Please note
12    /// that if `None` is passed and the reflog needs to be written, the operation will be aborted late and a few refs may have been
13    /// successfully committed already, making clear the non-atomic nature of multi-file edits.
14    ///
15    /// On error the transaction may have been performed partially, depending on the nature of the error, and no attempt to roll back
16    /// partial changes is made.
17    ///
18    /// In this stage, we perform the following operations:
19    ///
20    /// * update the ref log
21    /// * move updated refs into place
22    /// * delete reflogs and empty parent directories
23    /// * delete packed refs
24    /// * delete their corresponding reference (if applicable)
25    ///   along with empty parent directories
26    ///
27    /// Note that transactions will be prepared automatically as needed.
28    pub fn commit<'a>(self, committer: impl Into<Option<gix_actor::SignatureRef<'a>>>) -> Result<Vec<RefEdit>, Error> {
29        self.commit_inner(committer.into())
30    }
31
32    fn commit_inner(self, committer: Option<gix_actor::SignatureRef<'_>>) -> Result<Vec<RefEdit>, Error> {
33        let mut updates = self.updates.expect("BUG: must call prepare before commit");
34        let delete_loose_refs = matches!(
35            self.packed_refs,
36            PackedRefs::DeletionsAndNonSymbolicUpdatesRemoveLooseSourceReference(_)
37        );
38
39        // Perform updates first so live commits remain referenced
40        for change in &mut updates {
41            assert!(!change.update.deref, "Deref mode is turned into splits and turned off");
42            match &change.update.change {
43                // reflog first, then reference
44                Change::Update { log, new, expected } => {
45                    let lock = change.lock.take();
46                    let (update_ref, update_reflog) = match log.mode {
47                        RefLog::Only => (false, true),
48                        RefLog::AndReference => (true, true),
49                    };
50                    if update_reflog {
51                        let log_update = match new {
52                            Target::Symbolic(_) => {
53                                // Special HACK: no reflog for symref changes as there is no OID involved which the reflog needs.
54                                // Unless, the ref is new and we can obtain a peeled id
55                                // identified by the expectation of what could be there, as is the case when cloning.
56                                match expected {
57                                    PreviousValue::ExistingMustMatch(Target::Object(oid)) => {
58                                        Some((Some(gix_hash::ObjectId::null(oid.kind())), oid))
59                                    }
60                                    _ => None,
61                                }
62                            }
63                            Target::Object(new_oid) => {
64                                let previous = match expected {
65                                    // Here, this means that the ref already existed, and that it will receive (even transitively)
66                                    // the given value
67                                    PreviousValue::MustExistAndMatch(Target::Object(oid)) => Some(oid.to_owned()),
68                                    _ => None,
69                                }
70                                .or(change.leaf_referent_previous_oid);
71                                Some((previous, new_oid))
72                            }
73                        };
74                        if let Some((previous, new_oid)) = log_update {
75                            let do_update = previous.as_ref() != Some(new_oid);
76                            if do_update {
77                                self.store.reflog_create_or_append(
78                                    change.update.name.as_ref(),
79                                    previous,
80                                    new_oid,
81                                    committer,
82                                    log.message.as_ref(),
83                                    log.force_create_reflog,
84                                )?;
85                            }
86                        }
87                    }
88                    // Don't do anything else while keeping the lock after potentially updating the reflog.
89                    // We delay deletion of the reference and dropping the lock to after the packed-refs were
90                    // safely written.
91                    if delete_loose_refs && matches!(new, Target::Object(_)) {
92                        change.lock = lock;
93                        continue;
94                    }
95                    if update_ref {
96                        if let Some(Err(err)) = lock.map(gix_lock::Marker::commit) {
97                            // TODO: when Kind::IsADirectory becomes stable, use that.
98                            let err = if err.instance.resource_path().is_dir() {
99                                gix_tempfile::remove_dir::empty_depth_first(err.instance.resource_path())
100                                    .map_err(|io_err| std::io::Error::new(std::io::ErrorKind::Other, io_err))
101                                    .and_then(|_| err.instance.commit().map_err(|err| err.error))
102                                    .err()
103                            } else {
104                                Some(err.error)
105                            };
106
107                            if let Some(err) = err {
108                                return Err(Error::LockCommit {
109                                    source: err,
110                                    full_name: change.name(),
111                                });
112                            }
113                        }
114                    }
115                }
116                Change::Delete { .. } => {}
117            }
118        }
119
120        for change in &mut updates {
121            let (reflog_root, relative_name) = self.store.reflog_base_and_relative_path(change.update.name.as_ref());
122            match &change.update.change {
123                Change::Update { .. } => {}
124                Change::Delete { .. } => {
125                    // Reflog deletion happens first in case it fails a ref without log is less terrible than
126                    // a log without a reference.
127                    let reflog_path = reflog_root.join(relative_name);
128                    if let Err(err) = std::fs::remove_file(&reflog_path) {
129                        if err.kind() != std::io::ErrorKind::NotFound {
130                            return Err(Error::DeleteReflog {
131                                source: err,
132                                full_name: change.name(),
133                            });
134                        }
135                    } else {
136                        gix_tempfile::remove_dir::empty_upward_until_boundary(
137                            reflog_path.parent().expect("never without parent"),
138                            &reflog_root,
139                        )
140                        .ok();
141                    }
142                }
143            }
144        }
145
146        if let Some(t) = self.packed_transaction {
147            t.commit().map_err(Error::PackedTransactionCommit)?;
148            // Always refresh ourselves right away to avoid races. We ignore errors as there may be many reasons this fails, and it's not
149            // critical to be done here. In other words, the pack may be refreshed at a later time and then it might work.
150            self.store.force_refresh_packed_buffer().ok();
151        }
152
153        for change in &mut updates {
154            let take_lock_and_delete = match &change.update.change {
155                Change::Update {
156                    log: LogChange { mode, .. },
157                    new,
158                    ..
159                } => delete_loose_refs && *mode == RefLog::AndReference && matches!(new, Target::Object(_)),
160                Change::Delete { log: mode, .. } => *mode == RefLog::AndReference,
161            };
162            if take_lock_and_delete {
163                let lock = change.lock.take();
164                let reference_path = self.store.reference_path(change.update.name.as_ref());
165                if let Err(err) = std::fs::remove_file(reference_path) {
166                    if err.kind() != std::io::ErrorKind::NotFound {
167                        return Err(Error::DeleteReference {
168                            err,
169                            full_name: change.name(),
170                        });
171                    }
172                }
173                drop(lock);
174            }
175        }
176        Ok(updates.into_iter().map(|edit| edit.update).collect())
177    }
178}
179mod error {
180    use gix_object::bstr::BString;
181
182    use crate::store_impl::{file, packed};
183
184    /// The error returned by various [`Transaction`][super::Transaction] methods.
185    #[derive(Debug, thiserror::Error)]
186    #[allow(missing_docs)]
187    pub enum Error {
188        #[error("The packed-ref transaction could not be committed")]
189        PackedTransactionCommit(#[source] packed::transaction::commit::Error),
190        #[error("Edit preprocessing failed with error")]
191        PreprocessingFailed { source: std::io::Error },
192        #[error("The change for reference {full_name:?} could not be committed")]
193        LockCommit { source: std::io::Error, full_name: BString },
194        #[error("The reference {full_name} could not be deleted")]
195        DeleteReference { full_name: BString, err: std::io::Error },
196        #[error("The reflog of reference {full_name:?} could not be deleted")]
197        DeleteReflog { full_name: BString, source: std::io::Error },
198        #[error("The reflog could not be created or updated")]
199        CreateOrUpdateRefLog(#[from] file::log::create_or_update::Error),
200    }
201}
202pub use error::Error;
203
204use crate::transaction::PreviousValue;