Struct git_repository::reference::iter::Platform
source · pub struct Platform<'r> { /* private fields */ }
Expand description
A platform to create iterators over references.
Implementations§
source§impl<'r> Platform<'r>
impl<'r> Platform<'r>
sourcepub fn all(&self) -> Result<Iter<'_>, Error>
pub fn all(&self) -> Result<Iter<'_>, Error>
Return an iterator over all references in the repository.
Even broken or otherwise unparsible or inaccessible references are returned and have to be handled by the caller on a case by case basis.
Examples found in repository?
src/commit.rs (line 76)
70 71 72 73 74 75 76 77 78 79 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 139 140
fn names(&self, repo: &Repository) -> Result<HashMap<ObjectId, Cow<'static, BStr>>, Error> {
let platform = repo.references()?;
Ok(match self {
SelectRef::AllTags | SelectRef::AllRefs => {
let mut refs: Vec<_> = match self {
SelectRef::AllRefs => platform.all()?,
SelectRef::AllTags => platform.tags()?,
_ => unreachable!(),
}
.filter_map(Result::ok)
.filter_map(|mut r: crate::Reference<'_>| {
let target_id = r.target().try_id().map(ToOwned::to_owned);
let peeled_id = r.peel_to_id_in_place().ok()?;
let (prio, tag_time) = match target_id {
Some(target_id) if peeled_id != *target_id => {
let tag = repo.find_object(target_id).ok()?.try_into_tag().ok()?;
(1, tag.tagger().ok()??.time.seconds_since_unix_epoch)
}
_ => (0, 0),
};
(
peeled_id.inner,
prio,
tag_time,
Cow::from(r.inner.name.shorten().to_owned()),
)
.into()
})
.collect();
// By priority, then by time ascending, then lexicographically.
// More recent entries overwrite older ones due to collection into hashmap.
refs.sort_by(
|(_a_peeled_id, a_prio, a_time, a_name), (_b_peeled_id, b_prio, b_time, b_name)| {
a_prio
.cmp(b_prio)
.then_with(|| a_time.cmp(b_time))
.then_with(|| b_name.cmp(a_name))
},
);
refs.into_iter().map(|(a, _, _, b)| (a, b)).collect()
}
SelectRef::AnnotatedTags => {
let mut peeled_commits_and_tag_date: Vec<_> = platform
.tags()?
.filter_map(Result::ok)
.filter_map(|r: crate::Reference<'_>| {
// TODO: we assume direct refs for tags, which is the common case, but it doesn't have to be
// so rather follow symrefs till the first object and then peel tags after the first object was found.
let tag = r.try_id()?.object().ok()?.try_into_tag().ok()?;
let tag_time = tag
.tagger()
.ok()
.and_then(|s| s.map(|s| s.time.seconds_since_unix_epoch))
.unwrap_or(0);
let commit_id = tag.target_id().ok()?.object().ok()?.try_into_commit().ok()?.id;
Some((commit_id, tag_time, Cow::<BStr>::from(r.name().shorten().to_owned())))
})
.collect();
// Sort by time ascending, then lexicographically.
// More recent entries overwrite older ones due to collection into hashmap.
peeled_commits_and_tag_date.sort_by(|(_a_id, a_time, a_name), (_b_id, b_time, b_name)| {
a_time.cmp(b_time).then_with(|| b_name.cmp(a_name))
});
peeled_commits_and_tag_date
.into_iter()
.map(|(a, _, c)| (a, c))
.collect()
}
})
}
More examples
src/revision/spec/parse/delegate/navigate.rs (line 230)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
fn find(&mut self, regex: &BStr, negated: bool) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
#[cfg(not(feature = "regex"))]
let matches = |message: &BStr| -> bool { message.contains_str(regex) ^ negated };
#[cfg(feature = "regex")]
let matches = match regex::bytes::Regex::new(regex.to_str_lossy().as_ref()) {
Ok(compiled) => {
let needs_regex = regex::escape(compiled.as_str()) != regex;
move |message: &BStr| -> bool {
if needs_regex {
compiled.is_match(message) ^ negated
} else {
message.contains_str(regex) ^ negated
}
}
}
Err(err) => {
self.err.push(err.into());
return None;
}
};
match self.objs[self.idx].as_mut() {
Some(objs) => {
let repo = self.repo;
let mut errors = Vec::new();
let mut replacements = Replacements::default();
for oid in objs.iter() {
match oid
.attach(repo)
.ancestors()
.sorting(Sorting::ByCommitTimeNewestFirst)
.all()
{
Ok(iter) => {
let mut matched = false;
let mut count = 0;
let commits = iter.map(|res| {
res.map_err(Error::from).and_then(|commit_id| {
commit_id.object().map_err(Error::from).map(|obj| obj.into_commit())
})
});
for commit in commits {
count += 1;
match commit {
Ok(commit) => {
if matches(commit.message_raw_sloppy()) {
replacements.push((*oid, commit.id));
matched = true;
break;
}
}
Err(err) => errors.push((*oid, err)),
}
}
if !matched {
errors.push((
*oid,
Error::NoRegexMatch {
regex: regex.into(),
commits_searched: count,
oid: oid.attach(repo).shorten_or_id(),
},
))
}
}
Err(err) => errors.push((*oid, err.into())),
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
None => match self.repo.references() {
Ok(references) => match references.all() {
Ok(references) => {
match self
.repo
.rev_walk(
references
.peeled()
.filter_map(Result::ok)
.filter(|r| {
r.id()
.object()
.ok()
.map(|obj| obj.kind == git_object::Kind::Commit)
.unwrap_or(false)
})
.filter_map(|r| r.detach().peeled),
)
.sorting(Sorting::ByCommitTimeNewestFirst)
.all()
{
Ok(iter) => {
let mut matched = false;
let mut count = 0;
let commits = iter.map(|res| {
res.map_err(Error::from).and_then(|commit_id| {
commit_id.object().map_err(Error::from).map(|obj| obj.into_commit())
})
});
for commit in commits {
count += 1;
match commit {
Ok(commit) => {
if matches(commit.message_raw_sloppy()) {
self.objs[self.idx]
.get_or_insert_with(HashSet::default)
.insert(commit.id);
matched = true;
break;
}
}
Err(err) => self.err.push(err),
}
}
if matched {
Some(())
} else {
self.err.push(Error::NoRegexMatchAllRefs {
regex: regex.into(),
commits_searched: count,
});
None
}
}
Err(err) => {
self.err.push(err.into());
None
}
}
}
Err(err) => {
self.err.push(err.into());
None
}
},
Err(err) => {
self.err.push(err.into());
None
}
},
}
}
sourcepub fn prefixed(&self, prefix: impl AsRef<Path>) -> Result<Iter<'_>, Error>
pub fn prefixed(&self, prefix: impl AsRef<Path>) -> Result<Iter<'_>, Error>
Return an iterator over all references that match the given prefix
.
These are of the form refs/heads
or refs/remotes/origin
, and must not contain relative paths components like .
or ..
.
Return an iterator over all references that are tags.
They are all prefixed with refs/tags
.
Examples found in repository?
src/commit.rs (line 77)
70 71 72 73 74 75 76 77 78 79 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 139 140
fn names(&self, repo: &Repository) -> Result<HashMap<ObjectId, Cow<'static, BStr>>, Error> {
let platform = repo.references()?;
Ok(match self {
SelectRef::AllTags | SelectRef::AllRefs => {
let mut refs: Vec<_> = match self {
SelectRef::AllRefs => platform.all()?,
SelectRef::AllTags => platform.tags()?,
_ => unreachable!(),
}
.filter_map(Result::ok)
.filter_map(|mut r: crate::Reference<'_>| {
let target_id = r.target().try_id().map(ToOwned::to_owned);
let peeled_id = r.peel_to_id_in_place().ok()?;
let (prio, tag_time) = match target_id {
Some(target_id) if peeled_id != *target_id => {
let tag = repo.find_object(target_id).ok()?.try_into_tag().ok()?;
(1, tag.tagger().ok()??.time.seconds_since_unix_epoch)
}
_ => (0, 0),
};
(
peeled_id.inner,
prio,
tag_time,
Cow::from(r.inner.name.shorten().to_owned()),
)
.into()
})
.collect();
// By priority, then by time ascending, then lexicographically.
// More recent entries overwrite older ones due to collection into hashmap.
refs.sort_by(
|(_a_peeled_id, a_prio, a_time, a_name), (_b_peeled_id, b_prio, b_time, b_name)| {
a_prio
.cmp(b_prio)
.then_with(|| a_time.cmp(b_time))
.then_with(|| b_name.cmp(a_name))
},
);
refs.into_iter().map(|(a, _, _, b)| (a, b)).collect()
}
SelectRef::AnnotatedTags => {
let mut peeled_commits_and_tag_date: Vec<_> = platform
.tags()?
.filter_map(Result::ok)
.filter_map(|r: crate::Reference<'_>| {
// TODO: we assume direct refs for tags, which is the common case, but it doesn't have to be
// so rather follow symrefs till the first object and then peel tags after the first object was found.
let tag = r.try_id()?.object().ok()?.try_into_tag().ok()?;
let tag_time = tag
.tagger()
.ok()
.and_then(|s| s.map(|s| s.time.seconds_since_unix_epoch))
.unwrap_or(0);
let commit_id = tag.target_id().ok()?.object().ok()?.try_into_commit().ok()?.id;
Some((commit_id, tag_time, Cow::<BStr>::from(r.name().shorten().to_owned())))
})
.collect();
// Sort by time ascending, then lexicographically.
// More recent entries overwrite older ones due to collection into hashmap.
peeled_commits_and_tag_date.sort_by(|(_a_id, a_time, a_name), (_b_id, b_time, b_name)| {
a_time.cmp(b_time).then_with(|| b_name.cmp(a_name))
});
peeled_commits_and_tag_date
.into_iter()
.map(|(a, _, c)| (a, c))
.collect()
}
})
}
sourcepub fn local_branches(&self) -> Result<Iter<'_>, Error>
pub fn local_branches(&self) -> Result<Iter<'_>, Error>
Return an iterator over all local branches.
They are all prefixed with refs/heads
.
sourcepub fn remote_branches(&self) -> Result<Iter<'_>, Error>
pub fn remote_branches(&self) -> Result<Iter<'_>, Error>
Return an iterator over all remote branches.
They are all prefixed with refs/remotes
.