gix_ref/
raw.rs

1use gix_hash::ObjectId;
2
3use crate::{FullName, Target};
4
5/// A fully owned backend agnostic reference
6#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Reference {
9    /// The path to uniquely identify this ref within its store.
10    pub name: FullName,
11    /// The target of the reference, either a symbolic reference by full name or a possibly intermediate object by its id.
12    pub target: Target,
13    /// The fully peeled object to which this reference ultimately points to after following all symbolic refs and all annotated
14    /// tags. Only guaranteed to be set after
15    /// [`Reference::peel_to_id_in_place()`](crate::file::ReferenceExt) was called or if this reference originated
16    /// from a packed ref.
17    pub peeled: Option<ObjectId>,
18}
19
20mod convert {
21    use gix_hash::ObjectId;
22
23    use crate::{
24        raw::Reference,
25        store_impl::{file::loose, packed},
26        Target,
27    };
28
29    impl From<Reference> for loose::Reference {
30        fn from(value: Reference) -> Self {
31            loose::Reference {
32                name: value.name,
33                target: value.target,
34            }
35        }
36    }
37
38    impl From<loose::Reference> for Reference {
39        fn from(value: loose::Reference) -> Self {
40            Reference {
41                name: value.name,
42                target: value.target,
43                peeled: None,
44            }
45        }
46    }
47
48    impl<'p> From<packed::Reference<'p>> for Reference {
49        fn from(value: packed::Reference<'p>) -> Self {
50            Reference {
51                name: value.name.into(),
52                target: Target::Object(value.target()),
53                peeled: value
54                    .object
55                    .map(|hex| ObjectId::from_hex(hex).expect("parser validation")),
56            }
57        }
58    }
59}
60
61mod access {
62    use gix_object::bstr::ByteSlice;
63
64    use crate::{raw::Reference, FullNameRef, Namespace, Target};
65
66    impl Reference {
67        /// Returns the kind of reference based on its target
68        pub fn kind(&self) -> crate::Kind {
69            self.target.kind()
70        }
71
72        /// Return the full validated name of the reference, with the given namespace stripped if possible.
73        ///
74        /// If the reference name wasn't prefixed with `namespace`, `None` is returned instead.
75        pub fn name_without_namespace(&self, namespace: &Namespace) -> Option<&FullNameRef> {
76            self.name
77                .0
78                .as_bstr()
79                .strip_prefix(namespace.0.as_bytes())
80                .map(|stripped| FullNameRef::new_unchecked(stripped.as_bstr()))
81        }
82
83        /// Strip the given namespace from our name as well as the name, but not the reference we point to.
84        pub fn strip_namespace(&mut self, namespace: &Namespace) -> &mut Self {
85            self.name.strip_namespace(namespace);
86            if let Target::Symbolic(name) = &mut self.target {
87                name.strip_namespace(namespace);
88            }
89            self
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use gix_testtools::size_ok;
98
99    #[test]
100    fn size_of_reference() {
101        let actual = std::mem::size_of::<Reference>();
102        let expected = 80;
103        assert!(
104            size_ok(actual, expected),
105            "let's not let it change size undetected: {actual} <~ {expected}"
106        );
107    }
108}