summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/reference/edits.rs
blob: c6510c2e00b5ec9b635cbb896b18dd680aca63cc (plain)
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
///
pub mod set_target_id {
    use gix_ref::{transaction::PreviousValue, Target};

    use crate::{bstr::BString, Reference};

    mod error {
        use gix_ref::FullName;

        /// The error returned by [`Reference::set_target_id()`][super::Reference::set_target_id()].
        #[derive(Debug, thiserror::Error)]
        #[allow(missing_docs)]
        pub enum Error {
            #[error("Cannot change symbolic reference {name:?} into a direct one by setting it to an id")]
            SymbolicReference { name: FullName },
            #[error(transparent)]
            ReferenceEdit(#[from] crate::reference::edit::Error),
        }
    }
    pub use error::Error;

    impl<'repo> Reference<'repo> {
        /// Set the id of this direct reference to `id` and use `reflog_message` for the reflog (if enabled in the repository).
        ///
        /// Note that the operation will fail on symbolic references, to change their type use the lower level reference database,
        /// or if the reference was deleted or changed in the mean time.
        /// Furthermore, refrain from using this method for more than a one-off change as it creates a transaction for each invocation.
        /// If multiple reference should be changed, use [`Repository::edit_references()`][crate::Repository::edit_references()]
        /// or the lower level reference database instead.
        #[allow(clippy::result_large_err)]
        pub fn set_target_id(
            &mut self,
            id: impl Into<gix_hash::ObjectId>,
            reflog_message: impl Into<BString>,
        ) -> Result<(), Error> {
            match &self.inner.target {
                Target::Symbolic(name) => return Err(Error::SymbolicReference { name: name.clone() }),
                Target::Peeled(current_id) => {
                    let changed = self.repo.reference(
                        self.name(),
                        id,
                        PreviousValue::MustExistAndMatch(Target::Peeled(current_id.to_owned())),
                        reflog_message,
                    )?;
                    *self = changed;
                }
            }
            Ok(())
        }
    }
}

///
pub mod delete {
    use gix_ref::transaction::{Change, PreviousValue, RefEdit, RefLog};

    use crate::Reference;

    impl<'repo> Reference<'repo> {
        /// Delete this reference or fail if it was changed since last observed.
        /// Note that this instance remains available in memory but probably shouldn't be used anymore.
        pub fn delete(&self) -> Result<(), crate::reference::edit::Error> {
            self.repo
                .edit_reference(RefEdit {
                    change: Change::Delete {
                        expected: PreviousValue::MustExistAndMatch(self.inner.target.clone()),
                        log: RefLog::AndReference,
                    },
                    name: self.inner.name.clone(),
                    deref: false,
                })
                .map(|_| ())
        }
    }
}