Expand description
Denotes a ref target, equivalent to Kind
, but with mutable data.
Variants§
Peeled(ObjectId)
A ref that points to an object id
Symbolic(FullName)
A ref that points to another reference by its validated name, adding a level of indirection.
Note that this is an extension of gitoxide which will be helpful in logging all reference changes.
Implementations§
source§impl Target
impl Target
sourcepub fn kind(&self) -> Kind
pub fn kind(&self) -> Kind
Returns the kind of the target the ref is pointing to.
Examples found in repository?
More examples
src/store/file/raw_ext.rs (line 92)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn peel_to_id_in_place_packed<E: std::error::Error + Send + Sync + 'static>(
&mut self,
store: &file::Store,
mut find: impl FnMut(git_hash::ObjectId, &mut Vec<u8>) -> Result<Option<(git_object::Kind, &[u8])>, E>,
packed: Option<&packed::Buffer>,
) -> Result<ObjectId, peel::to_id::Error> {
match self.peeled {
Some(peeled) => {
self.target = Target::Peeled(peeled.to_owned());
Ok(peeled)
}
None => {
if self.target.kind() == crate::Kind::Symbolic {
let mut seen = BTreeSet::new();
let cursor = &mut *self;
while let Some(next) = cursor.follow_packed(store, packed) {
let next = next?;
if seen.contains(&next.name) {
return Err(peel::to_id::Error::Cycle {
start_absolute: store.reference_path(cursor.name.as_ref()),
});
}
*cursor = next;
seen.insert(cursor.name.clone());
const MAX_REF_DEPTH: usize = 5;
if seen.len() == MAX_REF_DEPTH {
return Err(peel::to_id::Error::DepthLimitExceeded {
max_depth: MAX_REF_DEPTH,
});
}
}
};
let mut buf = Vec::new();
let mut oid = self.target.try_id().expect("peeled ref").to_owned();
let peeled_id = loop {
let (kind, data) = find(oid, &mut buf)
.map_err(|err| Box::new(err) as Box<dyn std::error::Error + Send + Sync + 'static>)?
.ok_or_else(|| peel::to_id::Error::NotFound {
oid,
name: self.name.0.clone(),
})?;
match kind {
git_object::Kind::Tag => {
oid = git_object::TagRefIter::from_bytes(data).target_id().map_err(|_err| {
peel::to_id::Error::NotFound {
oid,
name: self.name.0.clone(),
}
})?;
}
_ => break oid,
};
};
self.peeled = Some(peeled_id);
self.target = Target::Peeled(peeled_id);
Ok(peeled_id)
}
}
}
sourcepub fn to_ref(&self) -> TargetRef<'_>
pub fn to_ref(&self) -> TargetRef<'_>
Interpret this owned Target as shared Target
Examples found in repository?
src/transaction/mod.rs (line 89)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
pub fn new_value(&self) -> Option<crate::TargetRef<'_>> {
match self {
Change::Update { new, .. } => new.to_ref().into(),
Change::Delete { .. } => None,
}
}
/// Return references to values that are in common between all variants and denote the previous observed value.
pub fn previous_value(&self) -> Option<crate::TargetRef<'_>> {
match self {
// TODO: use or-patterns once MRV is larger than 1.52 (and this is supported)
Change::Update {
expected: PreviousValue::MustExistAndMatch(previous),
..
}
| Change::Update {
expected: PreviousValue::ExistingMustMatch(previous),
..
}
| Change::Delete {
expected: PreviousValue::MustExistAndMatch(previous),
..
}
| Change::Delete {
expected: PreviousValue::ExistingMustMatch(previous),
..
} => previous,
_ => return None,
}
.to_ref()
.into()
}
sourcepub fn try_id(&self) -> Option<&oid>
pub fn try_id(&self) -> Option<&oid>
Interpret this target as object id which maybe None
if it is symbolic.
Examples found in repository?
src/store/file/raw_ext.rs (line 113)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn peel_to_id_in_place_packed<E: std::error::Error + Send + Sync + 'static>(
&mut self,
store: &file::Store,
mut find: impl FnMut(git_hash::ObjectId, &mut Vec<u8>) -> Result<Option<(git_object::Kind, &[u8])>, E>,
packed: Option<&packed::Buffer>,
) -> Result<ObjectId, peel::to_id::Error> {
match self.peeled {
Some(peeled) => {
self.target = Target::Peeled(peeled.to_owned());
Ok(peeled)
}
None => {
if self.target.kind() == crate::Kind::Symbolic {
let mut seen = BTreeSet::new();
let cursor = &mut *self;
while let Some(next) = cursor.follow_packed(store, packed) {
let next = next?;
if seen.contains(&next.name) {
return Err(peel::to_id::Error::Cycle {
start_absolute: store.reference_path(cursor.name.as_ref()),
});
}
*cursor = next;
seen.insert(cursor.name.clone());
const MAX_REF_DEPTH: usize = 5;
if seen.len() == MAX_REF_DEPTH {
return Err(peel::to_id::Error::DepthLimitExceeded {
max_depth: MAX_REF_DEPTH,
});
}
}
};
let mut buf = Vec::new();
let mut oid = self.target.try_id().expect("peeled ref").to_owned();
let peeled_id = loop {
let (kind, data) = find(oid, &mut buf)
.map_err(|err| Box::new(err) as Box<dyn std::error::Error + Send + Sync + 'static>)?
.ok_or_else(|| peel::to_id::Error::NotFound {
oid,
name: self.name.0.clone(),
})?;
match kind {
git_object::Kind::Tag => {
oid = git_object::TagRefIter::from_bytes(data).target_id().map_err(|_err| {
peel::to_id::Error::NotFound {
oid,
name: self.name.0.clone(),
}
})?;
}
_ => break oid,
};
};
self.peeled = Some(peeled_id);
self.target = Target::Peeled(peeled_id);
Ok(peeled_id)
}
}
}
sourcepub fn try_into_id(self) -> Result<ObjectId, Self>
pub fn try_into_id(self) -> Result<ObjectId, Self>
Return the contained object id if the target is peeled or itself if it is not.
sourcepub fn try_name(&self) -> Option<&FullNameRef>
pub fn try_name(&self) -> Option<&FullNameRef>
Interpret this target as name of the reference it points to which maybe None
if it an object id.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Target
impl<'de> Deserialize<'de> for Target
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Ord for Target
impl Ord for Target
source§impl PartialEq<Target> for Target
impl PartialEq<Target> for Target
source§impl<'a> PartialEq<TargetRef<'a>> for Target
impl<'a> PartialEq<TargetRef<'a>> for Target
source§impl PartialOrd<Target> for Target
impl PartialOrd<Target> for Target
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more