/src/gitoxide/gix-ref/src/raw.rs
Line | Count | Source |
1 | | use gix_hash::ObjectId; |
2 | | |
3 | | use 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))] |
8 | | pub 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()`](crate::file::ReferenceExt::peel_to_id) was called or if this reference originated |
16 | | /// from a packed ref. |
17 | | pub peeled: Option<ObjectId>, |
18 | | } |
19 | | |
20 | | mod 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 | 0 | fn from(value: Reference) -> Self { |
31 | 0 | loose::Reference { |
32 | 0 | name: value.name, |
33 | 0 | target: value.target, |
34 | 0 | } |
35 | 0 | } Unexecuted instantiation: <gix_ref::store_impl::file::loose::Reference as core::convert::From<gix_ref::raw::Reference>>::from Unexecuted instantiation: <gix_ref::store_impl::file::loose::Reference as core::convert::From<gix_ref::raw::Reference>>::from |
36 | | } |
37 | | |
38 | | impl From<loose::Reference> for Reference { |
39 | 0 | fn from(value: loose::Reference) -> Self { |
40 | 0 | Reference { |
41 | 0 | name: value.name, |
42 | 0 | target: value.target, |
43 | 0 | peeled: None, |
44 | 0 | } |
45 | 0 | } Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::file::loose::Reference>>::from Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::file::loose::Reference>>::from |
46 | | } |
47 | | |
48 | | impl<'p> From<packed::Reference<'p>> for Reference { |
49 | 0 | fn from(value: packed::Reference<'p>) -> Self { |
50 | | Reference { |
51 | 0 | name: value.name.into(), |
52 | 0 | target: Target::Object(value.target()), |
53 | 0 | peeled: value |
54 | 0 | .object |
55 | 0 | .map(|hex| ObjectId::from_hex(hex).expect("parser validation")), Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::packed::Reference>>::from::{closure#0} Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::packed::Reference>>::from::{closure#0} |
56 | | } |
57 | 0 | } Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::packed::Reference>>::from Unexecuted instantiation: <gix_ref::raw::Reference as core::convert::From<gix_ref::store_impl::packed::Reference>>::from |
58 | | } |
59 | | } |
60 | | |
61 | | mod 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 | 0 | pub fn kind(&self) -> crate::Kind { |
69 | 0 | self.target.kind() |
70 | 0 | } Unexecuted instantiation: <gix_ref::raw::Reference>::kind Unexecuted instantiation: <gix_ref::raw::Reference>::kind |
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 | 0 | pub fn name_without_namespace(&self, namespace: &Namespace) -> Option<&FullNameRef> { |
76 | 0 | self.name |
77 | 0 | .0 |
78 | 0 | .as_bstr() |
79 | 0 | .strip_prefix(namespace.0.as_bytes()) |
80 | 0 | .map(|stripped| FullNameRef::new_unchecked(stripped.as_bstr())) Unexecuted instantiation: <gix_ref::raw::Reference>::name_without_namespace::{closure#0} Unexecuted instantiation: <gix_ref::raw::Reference>::name_without_namespace::{closure#0} |
81 | 0 | } Unexecuted instantiation: <gix_ref::raw::Reference>::name_without_namespace Unexecuted instantiation: <gix_ref::raw::Reference>::name_without_namespace |
82 | | |
83 | | /// Strip the given namespace from our name as well as the name, but not the reference we point to. |
84 | 0 | pub fn strip_namespace(&mut self, namespace: &Namespace) -> &mut Self { |
85 | 0 | self.name.strip_namespace(namespace); |
86 | 0 | if let Target::Symbolic(name) = &mut self.target { |
87 | 0 | name.strip_namespace(namespace); |
88 | 0 | } |
89 | 0 | self |
90 | 0 | } Unexecuted instantiation: <gix_ref::raw::Reference>::strip_namespace Unexecuted instantiation: <gix_ref::raw::Reference>::strip_namespace |
91 | | } |
92 | | } |
93 | | |
94 | | #[cfg(test)] |
95 | | mod tests { |
96 | | use gix_testtools::size_ok; |
97 | | |
98 | | use super::*; |
99 | | |
100 | | #[test] |
101 | | fn size_of_reference() { |
102 | | let actual = std::mem::size_of::<Reference>(); |
103 | | let expected = 80; |
104 | | assert!( |
105 | | size_ok(actual, expected), |
106 | | "let's not let it change size undetected: {actual} <~ {expected}" |
107 | | ); |
108 | | } |
109 | | } |