Struct git_refspec::RefSpecRef
source · pub struct RefSpecRef<'a> { /* private fields */ }
Expand description
A refspec with references to the memory it was parsed from.
Implementations§
source§impl<'a> RefSpecRef<'a>
impl<'a> RefSpecRef<'a>
Access
sourcepub fn source(&self) -> Option<&BStr>
pub fn source(&self) -> Option<&BStr>
Return the left-hand side of the spec, typically the source. It takes many different forms so don’t rely on this being a ref name.
It’s not present in case of deletions.
Examples found in repository?
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn prefix(&self) -> Option<&BStr> {
if self.mode == Mode::Negative {
return None;
}
let source = match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
}?;
if source == "HEAD" {
return source.into();
}
let suffix = source.strip_prefix(b"refs/")?;
let slash_pos = suffix.find_byte(b'/')?;
let prefix = source[..="refs/".len() + slash_pos].as_bstr();
(!prefix.contains(&b'*')).then(|| prefix)
}
/// As opposed to [`prefix()`][Self::prefix], if the latter is `None` it will expand to all possible prefixes and place them in `out`.
///
/// Note that only the `source` side is considered.
pub fn expand_prefixes(&self, out: &mut Vec<BString>) {
match self.prefix() {
Some(prefix) => out.push(prefix.into()),
None => {
let source = match match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
} {
Some(source) => source,
None => return,
};
if let Some(rest) = source.strip_prefix(b"refs/") {
if !rest.contains(&b'/') {
out.push(source.into());
}
return;
} else if git_hash::ObjectId::from_hex(source).is_ok() {
return;
}
expand_partial_name(source, |expanded| {
out.push(expanded.into());
None::<()>
});
}
}
}
sourcepub fn destination(&self) -> Option<&BStr>
pub fn destination(&self) -> Option<&BStr>
Return the right-hand side of the spec, typically the destination. It takes many different forms so don’t rely on this being a ref name.
It’s not present in case of source-only specs.
Examples found in repository?
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn prefix(&self) -> Option<&BStr> {
if self.mode == Mode::Negative {
return None;
}
let source = match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
}?;
if source == "HEAD" {
return source.into();
}
let suffix = source.strip_prefix(b"refs/")?;
let slash_pos = suffix.find_byte(b'/')?;
let prefix = source[..="refs/".len() + slash_pos].as_bstr();
(!prefix.contains(&b'*')).then(|| prefix)
}
/// As opposed to [`prefix()`][Self::prefix], if the latter is `None` it will expand to all possible prefixes and place them in `out`.
///
/// Note that only the `source` side is considered.
pub fn expand_prefixes(&self, out: &mut Vec<BString>) {
match self.prefix() {
Some(prefix) => out.push(prefix.into()),
None => {
let source = match match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
} {
Some(source) => source,
None => return,
};
if let Some(rest) = source.strip_prefix(b"refs/") {
if !rest.contains(&b'/') {
out.push(source.into());
}
return;
} else if git_hash::ObjectId::from_hex(source).is_ok() {
return;
}
expand_partial_name(source, |expanded| {
out.push(expanded.into());
None::<()>
});
}
}
}
sourcepub fn remote(&self) -> Option<&BStr>
pub fn remote(&self) -> Option<&BStr>
Always returns the remote side, whose actual side in the refspec depends on how it was parsed.
sourcepub fn local(&self) -> Option<&BStr>
pub fn local(&self) -> Option<&BStr>
Always returns the local side, whose actual side in the refspec depends on how it was parsed.
sourcepub fn prefix(&self) -> Option<&BStr>
pub fn prefix(&self) -> Option<&BStr>
Derive the prefix from the source
side of this spec if this is a fetch spec,
or the destination
side if it is a push spec, if it is possible to do so without ambiguity.
This means it starts with refs/
. Note that it won’t contain more than two components, like refs/heads/
Examples found in repository?
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn expand_prefixes(&self, out: &mut Vec<BString>) {
match self.prefix() {
Some(prefix) => out.push(prefix.into()),
None => {
let source = match match self.op {
Operation::Fetch => self.source(),
Operation::Push => self.destination(),
} {
Some(source) => source,
None => return,
};
if let Some(rest) = source.strip_prefix(b"refs/") {
if !rest.contains(&b'/') {
out.push(source.into());
}
return;
} else if git_hash::ObjectId::from_hex(source).is_ok() {
return;
}
expand_partial_name(source, |expanded| {
out.push(expanded.into());
None::<()>
});
}
}
}
sourcepub fn expand_prefixes(&self, out: &mut Vec<BString>)
pub fn expand_prefixes(&self, out: &mut Vec<BString>)
As opposed to prefix()
, if the latter is None
it will expand to all possible prefixes and place them in out
.
Note that only the source
side is considered.
sourcepub fn instruction(&self) -> Instruction<'a>
pub fn instruction(&self) -> Instruction<'a>
Transform the state of the refspec into an instruction making clear what to do with it.
Examples found in repository?
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 76 77 78 79 80 81
fn hash<H: Hasher>(&self, state: &mut H) {
self.instruction().hash(state)
}
}
impl PartialEq for RefSpec {
fn eq(&self, other: &Self) -> bool {
self.to_ref().eq(&other.to_ref())
}
}
impl PartialEq for RefSpecRef<'_> {
fn eq(&self, other: &Self) -> bool {
self.instruction().eq(&other.instruction())
}
}
impl PartialOrd for RefSpecRef<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.instruction().partial_cmp(&other.instruction())
}
}
impl PartialOrd for RefSpec {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.to_ref().partial_cmp(&other.to_ref())
}
}
impl Ord for RefSpecRef<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.instruction().cmp(&other.instruction())
}
More examples
source§impl RefSpecRef<'_>
impl RefSpecRef<'_>
Conversion
sourcepub fn to_owned(&self) -> RefSpec
pub fn to_owned(&self) -> RefSpec
Convert this ref into a standalone, owned copy.
Examples found in repository?
More examples
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
pub fn validated(mut self) -> Result<(Self, Vec<Fix>), Error> {
let mut sources_by_destinations = BTreeMap::new();
for (dst, (spec_index, src)) in self
.mappings
.iter()
.filter_map(|m| m.rhs.as_ref().map(|dst| (dst.as_ref(), (m.spec_index, &m.lhs))))
{
let sources = sources_by_destinations.entry(dst).or_insert_with(Vec::new);
if !sources.iter().any(|(_, lhs)| lhs == &src) {
sources.push((spec_index, src))
}
}
let mut issues = Vec::new();
for (dst, conflicting_sources) in sources_by_destinations.into_iter().filter(|(_, v)| v.len() > 1) {
issues.push(Issue::Conflict {
destination_full_ref_name: dst.to_owned(),
specs: conflicting_sources
.iter()
.map(|(spec_idx, _)| self.group.specs[*spec_idx].to_bstring())
.collect(),
sources: conflicting_sources.into_iter().map(|(_, src)| src.to_owned()).collect(),
})
}
if !issues.is_empty() {
Err(Error { issues })
} else {
let mut fixed = Vec::new();
let group = &self.group;
self.mappings.retain(|m| match m.rhs.as_ref() {
Some(dst) => {
if dst.starts_with(b"refs/") || dst.as_ref() == "HEAD" {
true
} else {
fixed.push(Fix::MappingWithPartialDestinationRemoved {
name: dst.as_ref().to_owned(),
spec: group.specs[m.spec_index].to_owned(),
});
false
}
}
None => true,
});
Ok((self, fixed))
}
}
source§impl RefSpecRef<'_>
impl RefSpecRef<'_>
sourcepub fn to_bstring(&self) -> BString
pub fn to_bstring(&self) -> BString
Reproduce ourselves in parseable form.
Examples found in repository?
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
pub fn validated(mut self) -> Result<(Self, Vec<Fix>), Error> {
let mut sources_by_destinations = BTreeMap::new();
for (dst, (spec_index, src)) in self
.mappings
.iter()
.filter_map(|m| m.rhs.as_ref().map(|dst| (dst.as_ref(), (m.spec_index, &m.lhs))))
{
let sources = sources_by_destinations.entry(dst).or_insert_with(Vec::new);
if !sources.iter().any(|(_, lhs)| lhs == &src) {
sources.push((spec_index, src))
}
}
let mut issues = Vec::new();
for (dst, conflicting_sources) in sources_by_destinations.into_iter().filter(|(_, v)| v.len() > 1) {
issues.push(Issue::Conflict {
destination_full_ref_name: dst.to_owned(),
specs: conflicting_sources
.iter()
.map(|(spec_idx, _)| self.group.specs[*spec_idx].to_bstring())
.collect(),
sources: conflicting_sources.into_iter().map(|(_, src)| src.to_owned()).collect(),
})
}
if !issues.is_empty() {
Err(Error { issues })
} else {
let mut fixed = Vec::new();
let group = &self.group;
self.mappings.retain(|m| match m.rhs.as_ref() {
Some(dst) => {
if dst.starts_with(b"refs/") || dst.as_ref() == "HEAD" {
true
} else {
fixed.push(Fix::MappingWithPartialDestinationRemoved {
name: dst.as_ref().to_owned(),
spec: group.specs[m.spec_index].to_owned(),
});
false
}
}
None => true,
});
Ok((self, fixed))
}
}
Trait Implementations§
source§impl<'a> Clone for RefSpecRef<'a>
impl<'a> Clone for RefSpecRef<'a>
source§fn clone(&self) -> RefSpecRef<'a>
fn clone(&self) -> RefSpecRef<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for RefSpecRef<'a>
impl<'a> Debug for RefSpecRef<'a>
source§impl From<RefSpecRef<'_>> for RefSpec
impl From<RefSpecRef<'_>> for RefSpec
source§fn from(v: RefSpecRef<'_>) -> Self
fn from(v: RefSpecRef<'_>) -> Self
source§impl Hash for RefSpecRef<'_>
impl Hash for RefSpecRef<'_>
source§impl Ord for RefSpecRef<'_>
impl Ord for RefSpecRef<'_>
source§impl PartialEq<RefSpecRef<'_>> for RefSpecRef<'_>
impl PartialEq<RefSpecRef<'_>> for RefSpecRef<'_>
source§impl PartialOrd<RefSpecRef<'_>> for RefSpecRef<'_>
impl PartialOrd<RefSpecRef<'_>> for RefSpecRef<'_>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more