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§

Access

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?
src/spec.rs (line 134)
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::<()>
                });
            }
        }
    }

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?
src/spec.rs (line 135)
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::<()>
                });
            }
        }
    }

Always returns the remote side, whose actual side in the refspec depends on how it was parsed.

Always returns the local side, whose actual side in the refspec depends on how it was parsed.

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?
src/spec.rs (line 150)
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::<()>
                });
            }
        }
    }

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.

Transform the state of the refspec into an instruction making clear what to do with it.

Examples found in repository?
src/spec.rs (line 50)
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
Hide additional examples
src/write.rs (line 18)
17
18
19
    pub fn write_to(&self, out: impl std::io::Write) -> std::io::Result<()> {
        self.instruction().write_to(out)
    }

Conversion

Convert this ref into a standalone, owned copy.

Examples found in repository?
src/spec.rs (line 38)
37
38
39
        fn from(v: RefSpecRef<'_>) -> Self {
            v.to_owned()
        }
More examples
Hide additional examples
src/match_group/validate.rs (line 129)
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))
        }
    }

Reproduce ourselves in parseable form.

Examples found in repository?
src/match_group/validate.rs (line 112)
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))
        }
    }

Serialize ourselves in a parseable format to out.

Examples found in repository?
src/write.rs (line 12)
10
11
12
13
14
    pub fn to_bstring(&self) -> BString {
        let mut buf = Vec::with_capacity(128);
        self.write_to(&mut buf).expect("no io error");
        buf.into()
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Checks if this value is equivalent to the given key. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.