Struct git_repository::Id
source · pub struct Id<'r> { /* private fields */ }
Expand description
An ObjectId with access to a repository.
Implementations§
source§impl<'repo> Id<'repo>
impl<'repo> Id<'repo>
An object id infused with Easy
.
sourcepub fn object(&self) -> Result<Object<'repo>, Error>
pub fn object(&self) -> Result<Object<'repo>, Error>
Find the Object
associated with this object id, and consider it an error if it doesn’t exist.
Note
There can only be one ObjectRef
per Easy
. To increase that limit, clone the Easy
.
Examples found in repository?
More examples
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
pub fn from_ids(
previous_id: &crate::Id<'old>,
new_id: &crate::Id<'new>,
) -> Result<Platform<'old, 'new>, init::Error> {
match previous_id
.object()
.and_then(|old| new_id.object().map(|new| (old, new)))
{
Ok((old, new)) => {
let algo = match new_id.repo.config.diff_algorithm() {
Ok(algo) => algo,
Err(err) => return Err(err.into()),
};
Ok(Platform { old, new, algo })
}
Err(err) => Err(err.into()),
}
}
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 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
pub fn peel_to_id_in_place(&mut self) -> Option<Result<crate::Id<'repo>, Error>> {
Some(match &mut self.kind {
Kind::Unborn(_name) => return None,
Kind::Detached {
peeled: Some(peeled), ..
} => Ok((*peeled).attach(self.repo)),
Kind::Detached { peeled: None, target } => {
match target
.attach(self.repo)
.object()
.map_err(Into::into)
.and_then(|obj| obj.peel_tags_to_end().map_err(Into::into))
.map(|peeled| peeled.id)
{
Ok(peeled) => {
self.kind = Kind::Detached {
peeled: Some(peeled),
target: *target,
};
Ok(peeled.attach(self.repo))
}
Err(err) => Err(err),
}
}
Kind::Symbolic(r) => {
let mut nr = r.clone().attach(self.repo);
let peeled = nr.peel_to_id_in_place().map_err(Into::into);
*r = nr.detach();
peeled
}
})
}
// TODO: tests
// TODO: something similar in `crate::Reference`
/// Follow the symbolic reference of this head until its target object and peel it by following tag objects until there is no
/// more object to follow, transform the id into a commit if possible and return that.
///
/// Returns an error if the head is unborn or if it doesn't point to a commit.
pub fn peel_to_commit_in_place(&mut self) -> Result<crate::Commit<'repo>, to_commit::Error> {
let id = self.peel_to_id_in_place().ok_or_else(|| to_commit::Error::Unborn {
name: self.referent_name().expect("unborn").to_owned(),
})??;
id.object()
.map_err(|err| to_commit::Error::Peel(Error::FindExistingObject(err)))
.and_then(|object| object.try_into_commit().map_err(Into::into))
}
/// Consume this instance and transform it into the final object that it points to, or `None` if the `HEAD`
/// reference is yet to be born.
pub fn into_fully_peeled_id(self) -> Option<Result<crate::Id<'repo>, Error>> {
Some(match self.kind {
Kind::Unborn(_name) => return None,
Kind::Detached {
peeled: Some(peeled), ..
} => Ok(peeled.attach(self.repo)),
Kind::Detached { peeled: None, target } => target
.attach(self.repo)
.object()
.map_err(Into::into)
.and_then(|obj| obj.peel_tags_to_end().map_err(Into::into))
.map(|obj| obj.id.attach(self.repo)),
Kind::Symbolic(r) => r.attach(self.repo).peel_to_id_in_place().map_err(Into::into),
})
}
48 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
pub fn main_worktree(
&mut self,
mut progress: impl crate::Progress,
should_interrupt: &AtomicBool,
) -> Result<(Repository, git_worktree::index::checkout::Outcome), Error> {
let repo = self
.repo
.as_ref()
.expect("still present as we never succeeded the worktree checkout yet");
let workdir = repo.work_dir().ok_or_else(|| Error::BareRepository {
git_dir: repo.git_dir().to_owned(),
})?;
let root_tree = match repo.head()?.peel_to_id_in_place().transpose()? {
Some(id) => id.object().expect("downloaded from remote").peel_to_tree()?.id,
None => {
return Ok((
self.repo.take().expect("still present"),
git_worktree::index::checkout::Outcome::default(),
))
}
};
let index = git_index::State::from_tree(&root_tree, |oid, buf| repo.objects.find_tree_iter(oid, buf).ok())
.map_err(|err| Error::IndexFromTree {
id: root_tree,
source: err,
})?;
let mut index = git_index::File::from_state(index, repo.index_path());
let mut opts = repo.config.checkout_options(repo.git_dir())?;
opts.destination_is_initially_empty = true;
let mut files = progress.add_child_with_id("checkout", *b"CLCF"); /* CLone Checkout Files */
let mut bytes = progress.add_child_with_id("writing", *b"CLCB") /* CLone Checkout Bytes */;
files.init(Some(index.entries().len()), crate::progress::count("files"));
bytes.init(None, crate::progress::bytes());
let start = std::time::Instant::now();
let outcome = git_worktree::index::checkout(
&mut index,
workdir,
{
let objects = repo.objects.clone().into_arc()?;
move |oid, buf| objects.find_blob(oid, buf)
},
&mut files,
&mut bytes,
should_interrupt,
opts,
)?;
files.show_throughput(start);
bytes.show_throughput(start);
index.write(Default::default())?;
Ok((self.repo.take().expect("still present"), outcome))
}
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 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 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 peel_until(&mut self, kind: PeelTo<'_>) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
match kind {
PeelTo::ValidObject => {
for obj in objs.iter() {
match repo.find_object(*obj) {
Ok(_) => {}
Err(err) => {
errors.push((*obj, err.into()));
}
};
}
}
PeelTo::ObjectKind(kind) => {
let peel = |obj| peel(repo, obj, kind);
for obj in objs.iter() {
match peel(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::Path(path) => {
let lookup_path = |obj: &ObjectId| {
let tree_id = peel(repo, obj, git_object::Kind::Tree)?;
if path.is_empty() {
return Ok(tree_id);
}
let tree = repo.find_object(tree_id)?.into_tree();
let entry =
tree.lookup_entry_by_path(git_path::from_bstr(path))?
.ok_or_else(|| Error::PathNotFound {
path: path.into(),
object: obj.attach(repo).shorten_or_id(),
tree: tree_id.attach(repo).shorten_or_id(),
})?;
Ok(entry.object_id())
};
for obj in objs.iter() {
match lookup_path(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::RecursiveTagObject => {
for oid in objs.iter() {
match oid.attach(repo).object().and_then(|obj| obj.peel_tags_to_end()) {
Ok(obj) => replacements.push((*oid, obj.id)),
Err(err) => errors.push((*oid, err.into())),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
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 shorten(&self) -> Result<Prefix, Error>
pub fn shorten(&self) -> Result<Prefix, Error>
Turn this object id into a shortened id with a length in hex as configured by core.abbrev
.
Examples found in repository?
More examples
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
pub fn peel_to_kind(mut self, kind: Kind) -> Result<Self, peel::to_kind::Error> {
loop {
match self.kind {
our_kind if kind == our_kind => {
return Ok(self);
}
Kind::Commit => {
let tree_id = self
.try_to_commit_ref_iter()
.expect("commit")
.tree_id()
.expect("valid commit");
let repo = self.repo;
drop(self);
self = repo.find_object(tree_id)?;
}
Kind::Tag => {
let target_id = self.to_tag_ref_iter().target_id().expect("valid tag");
let repo = self.repo;
drop(self);
self = repo.find_object(target_id)?;
}
Kind::Tree | Kind::Blob => {
return Err(peel::to_kind::Error::NotFound {
oid: self.id().shorten().unwrap_or_else(|_| self.id.into()),
actual: self.kind,
expected: kind,
})
}
}
}
}
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
pub(crate) fn ambiguous(candidates: HashSet<ObjectId>, prefix: git_hash::Prefix, repo: &Repository) -> Self {
#[derive(PartialOrd, Ord, Eq, PartialEq, Copy, Clone)]
enum Order {
Tag,
Commit,
Tree,
Blob,
Invalid,
}
let candidates = {
let mut c: Vec<_> = candidates
.into_iter()
.map(|oid| {
let obj = repo.find_object(oid);
let order = match &obj {
Err(_) => Order::Invalid,
Ok(obj) => match obj.kind {
git_object::Kind::Tag => Order::Tag,
git_object::Kind::Commit => Order::Commit,
git_object::Kind::Tree => Order::Tree,
git_object::Kind::Blob => Order::Blob,
},
};
(oid, obj, order)
})
.collect();
c.sort_by(|lhs, rhs| lhs.2.cmp(&rhs.2).then_with(|| lhs.0.cmp(&rhs.0)));
c
};
Error::AmbiguousPrefix {
prefix,
info: candidates
.into_iter()
.map(|(oid, find_result, _)| {
let info = match find_result {
Ok(obj) => match obj.kind {
git_object::Kind::Tree | git_object::Kind::Blob => CandidateInfo::Object { kind: obj.kind },
git_object::Kind::Tag => {
let tag = obj.to_tag_ref();
CandidateInfo::Tag { name: tag.name.into() }
}
git_object::Kind::Commit => {
use bstr::ByteSlice;
let commit = obj.to_commit_ref();
CandidateInfo::Commit {
date: commit.committer().time,
title: commit.message().title.trim().into(),
}
}
},
Err(err) => CandidateInfo::FindError { source: err },
};
(oid.attach(repo).shorten().unwrap_or_else(|_| oid.into()), info)
})
.collect(),
}
}
sourcepub fn shorten_or_id(&self) -> Prefix
pub fn shorten_or_id(&self) -> Prefix
Turn this object id into a shortened id with a length in hex as configured by core.abbrev
, or default
to a prefix which equals our id in the unlikely error case.
Examples found in repository?
More examples
146 147 148 149 150 151 152 153 154 155 156 157
fn require_object_kind(repo: &Repository, obj: &git_hash::oid, kind: git_object::Kind) -> Result<(), Error> {
let obj = repo.find_object(obj)?;
if obj.kind == kind {
Ok(())
} else {
Err(Error::ObjectKind {
actual: obj.kind,
expected: kind,
oid: obj.id.attach(repo).shorten_or_id(),
})
}
}
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 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 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 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 traverse(&mut self, kind: Traversal) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
for obj in objs.iter() {
match kind {
Traversal::NthParent(num) => {
match self.repo.find_object(*obj).map_err(Error::from).and_then(|obj| {
obj.try_into_commit().map_err(|err| {
let object::try_into::Error { actual, expected, id } = err;
Error::ObjectKind {
oid: id.attach(repo).shorten_or_id(),
actual,
expected,
}
})
}) {
Ok(commit) => match commit.parent_ids().nth(num.saturating_sub(1)) {
Some(id) => replacements.push((commit.id, id.detach())),
None => errors.push((
commit.id,
Error::ParentOutOfRange {
oid: commit.id().shorten_or_id(),
desired: num,
available: commit.parent_ids().count(),
},
)),
},
Err(err) => errors.push((*obj, err)),
}
}
Traversal::NthAncestor(num) => {
let id = obj.attach(repo);
match id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(num)
.filter_map(Result::ok)
.next()
{
Some(id) => replacements.push((*obj, id.detach())),
None => errors.push((
*obj,
Error::AncestorOutOfRange {
oid: id.shorten_or_id(),
desired: num,
available: id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(1)
.count(),
},
)),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
fn peel_until(&mut self, kind: PeelTo<'_>) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
match kind {
PeelTo::ValidObject => {
for obj in objs.iter() {
match repo.find_object(*obj) {
Ok(_) => {}
Err(err) => {
errors.push((*obj, err.into()));
}
};
}
}
PeelTo::ObjectKind(kind) => {
let peel = |obj| peel(repo, obj, kind);
for obj in objs.iter() {
match peel(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::Path(path) => {
let lookup_path = |obj: &ObjectId| {
let tree_id = peel(repo, obj, git_object::Kind::Tree)?;
if path.is_empty() {
return Ok(tree_id);
}
let tree = repo.find_object(tree_id)?.into_tree();
let entry =
tree.lookup_entry_by_path(git_path::from_bstr(path))?
.ok_or_else(|| Error::PathNotFound {
path: path.into(),
object: obj.attach(repo).shorten_or_id(),
tree: tree_id.attach(repo).shorten_or_id(),
})?;
Ok(entry.object_id())
};
for obj in objs.iter() {
match lookup_path(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::RecursiveTagObject => {
for oid in objs.iter() {
match oid.attach(repo).object().and_then(|obj| obj.peel_tags_to_end()) {
Ok(obj) => replacements.push((*oid, obj.id)),
Err(err) => errors.push((*oid, err.into())),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
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
}
},
}
}
source§impl<'repo> Id<'repo>
impl<'repo> Id<'repo>
sourcepub fn detach(self) -> ObjectId
pub fn detach(self) -> ObjectId
Turn this instance into its bare ObjectId.
Examples found in repository?
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
fn follow_refs_to_objects_if_needed(&mut self) -> Option<()> {
assert_eq!(self.refs.len(), self.objs.len());
let repo = self.repo;
for (r, obj) in self.refs.iter().zip(self.objs.iter_mut()) {
if let (_ref_opt @ Some(ref_), obj_opt @ None) = (r, obj) {
if let Some(id) = ref_.target.try_id().map(ToOwned::to_owned).or_else(|| {
ref_.clone()
.attach(repo)
.peel_to_id_in_place()
.ok()
.map(|id| id.detach())
}) {
obj_opt.get_or_insert_with(HashSet::default).insert(id);
};
};
}
Some(())
}
More examples
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
fn nth_checked_out_branch(&mut self, branch_no: usize) -> Option<()> {
self.unset_disambiguate_call();
fn prior_checkouts_iter<'a>(
platform: &'a mut git_ref::file::log::iter::Platform<'static, '_>,
) -> Result<impl Iterator<Item = (BString, ObjectId)> + 'a, Error> {
match platform.rev().ok().flatten() {
Some(log) => Ok(log.filter_map(Result::ok).filter_map(|line| {
line.message
.strip_prefix(b"checkout: moving from ")
.and_then(|from_to| from_to.find(" to ").map(|pos| &from_to[..pos]))
.map(|from_branch| (from_branch.into(), line.previous_oid))
})),
None => Err(Error::MissingRefLog {
reference: "HEAD".into(),
action: "search prior checked out branch",
}),
}
}
let head = match self.repo.head() {
Ok(head) => head,
Err(err) => {
self.err.push(err.into());
return None;
}
};
match prior_checkouts_iter(&mut head.log_iter()).map(|mut it| it.nth(branch_no.saturating_sub(1))) {
Ok(Some((ref_name, id))) => {
let id = match self.repo.find_reference(ref_name.as_bstr()) {
Ok(mut r) => {
let id = r.peel_to_id_in_place().map(|id| id.detach()).unwrap_or(id);
self.refs[self.idx] = Some(r.detach());
id
}
Err(_) => id,
};
self.objs[self.idx].get_or_insert_with(HashSet::default).insert(id);
Some(())
}
Ok(None) => {
self.err.push(Error::PriorCheckoutOutOfRange {
desired: branch_no,
available: prior_checkouts_iter(&mut head.log_iter())
.map(|it| it.count())
.unwrap_or(0),
});
None
}
Err(err) => {
self.err.push(err);
None
}
}
}
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 82 83 84 85 86 87 88 89
fn traverse(&mut self, kind: Traversal) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
for obj in objs.iter() {
match kind {
Traversal::NthParent(num) => {
match self.repo.find_object(*obj).map_err(Error::from).and_then(|obj| {
obj.try_into_commit().map_err(|err| {
let object::try_into::Error { actual, expected, id } = err;
Error::ObjectKind {
oid: id.attach(repo).shorten_or_id(),
actual,
expected,
}
})
}) {
Ok(commit) => match commit.parent_ids().nth(num.saturating_sub(1)) {
Some(id) => replacements.push((commit.id, id.detach())),
None => errors.push((
commit.id,
Error::ParentOutOfRange {
oid: commit.id().shorten_or_id(),
desired: num,
available: commit.parent_ids().count(),
},
)),
},
Err(err) => errors.push((*obj, err)),
}
}
Traversal::NthAncestor(num) => {
let id = obj.attach(repo);
match id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(num)
.filter_map(Result::ok)
.next()
{
Some(id) => replacements.push((*obj, id.detach())),
None => errors.push((
*obj,
Error::AncestorOutOfRange {
oid: id.shorten_or_id(),
desired: num,
available: id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(1)
.count(),
},
)),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
source§impl<'repo> Id<'repo>
impl<'repo> Id<'repo>
sourcepub fn ancestors(&self) -> Platform<'repo>
pub fn ancestors(&self) -> Platform<'repo>
Obtain a platform for traversing ancestors of this commit.
Note that unless error_on_missing_commit()
is enabled, which be default it is not,
one will always see an empty iteration even if this id is not a commit, instead of an error.
If this is undesirable, it’s best to check for the correct object type before creating an iterator.
Examples found in repository?
More examples
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 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 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 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 traverse(&mut self, kind: Traversal) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
for obj in objs.iter() {
match kind {
Traversal::NthParent(num) => {
match self.repo.find_object(*obj).map_err(Error::from).and_then(|obj| {
obj.try_into_commit().map_err(|err| {
let object::try_into::Error { actual, expected, id } = err;
Error::ObjectKind {
oid: id.attach(repo).shorten_or_id(),
actual,
expected,
}
})
}) {
Ok(commit) => match commit.parent_ids().nth(num.saturating_sub(1)) {
Some(id) => replacements.push((commit.id, id.detach())),
None => errors.push((
commit.id,
Error::ParentOutOfRange {
oid: commit.id().shorten_or_id(),
desired: num,
available: commit.parent_ids().count(),
},
)),
},
Err(err) => errors.push((*obj, err)),
}
}
Traversal::NthAncestor(num) => {
let id = obj.attach(repo);
match id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(num)
.filter_map(Result::ok)
.next()
{
Some(id) => replacements.push((*obj, id.detach())),
None => errors.push((
*obj,
Error::AncestorOutOfRange {
oid: id.shorten_or_id(),
desired: num,
available: id
.ancestors()
.first_parent_only()
.all()
.expect("cannot fail without sorting")
.skip(1)
.count(),
},
)),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
fn peel_until(&mut self, kind: PeelTo<'_>) -> Option<()> {
self.unset_disambiguate_call();
self.follow_refs_to_objects_if_needed()?;
let mut replacements = Replacements::default();
let mut errors = Vec::new();
let objs = self.objs[self.idx].as_mut()?;
let repo = self.repo;
match kind {
PeelTo::ValidObject => {
for obj in objs.iter() {
match repo.find_object(*obj) {
Ok(_) => {}
Err(err) => {
errors.push((*obj, err.into()));
}
};
}
}
PeelTo::ObjectKind(kind) => {
let peel = |obj| peel(repo, obj, kind);
for obj in objs.iter() {
match peel(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::Path(path) => {
let lookup_path = |obj: &ObjectId| {
let tree_id = peel(repo, obj, git_object::Kind::Tree)?;
if path.is_empty() {
return Ok(tree_id);
}
let tree = repo.find_object(tree_id)?.into_tree();
let entry =
tree.lookup_entry_by_path(git_path::from_bstr(path))?
.ok_or_else(|| Error::PathNotFound {
path: path.into(),
object: obj.attach(repo).shorten_or_id(),
tree: tree_id.attach(repo).shorten_or_id(),
})?;
Ok(entry.object_id())
};
for obj in objs.iter() {
match lookup_path(obj) {
Ok(replace) => replacements.push((*obj, replace)),
Err(err) => errors.push((*obj, err)),
}
}
}
PeelTo::RecursiveTagObject => {
for oid in objs.iter() {
match oid.attach(repo).object().and_then(|obj| obj.peel_tags_to_end()) {
Ok(obj) => replacements.push((*oid, obj.id)),
Err(err) => errors.push((*oid, err.into())),
}
}
}
}
handle_errors_and_replacements(&mut self.err, objs, errors, &mut replacements)
}
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
}
},
}
}
Methods from Deref<Target = oid>§
sourcepub fn first_byte(&self) -> u8
pub fn first_byte(&self) -> u8
The first byte of the hash, commonly used to partition a set of Id
s
sourcepub fn to_hex_with_len(&self, len: usize) -> HexDisplay<'_>
pub fn to_hex_with_len(&self, len: usize) -> HexDisplay<'_>
Return a type which can display itself in hexadecimal form with the len
amount of characters.
sourcepub fn hex_to_buf(&self, buf: &mut [u8]) -> usize
pub fn hex_to_buf(&self, buf: &mut [u8]) -> usize
Write ourselves to the out
in hexadecimal notation, returning the amount of written bytes.
Panics if the buffer isn’t big enough to hold twice as many bytes as the current binary size.
Trait Implementations§
source§impl<'repo> PartialEq<Id<'repo>> for Id<'repo>
impl<'repo> PartialEq<Id<'repo>> for Id<'repo>
source§impl<'repo> PartialEq<Id<'repo>> for ObjectId
impl<'repo> PartialEq<Id<'repo>> for ObjectId
source§impl<'repo> PartialEq<Object<'repo>> for Id<'repo>
impl<'repo> PartialEq<Object<'repo>> for Id<'repo>
source§impl<'repo> PartialEq<ObjectDetached> for Id<'repo>
impl<'repo> PartialEq<ObjectDetached> for Id<'repo>
source§fn eq(&self, other: &ObjectDetached) -> bool
fn eq(&self, other: &ObjectDetached) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'repo> PartialEq<ObjectId> for Id<'repo>
impl<'repo> PartialEq<ObjectId> for Id<'repo>
source§impl<'a> PartialOrd<Id<'a>> for Id<'a>
impl<'a> PartialOrd<Id<'a>> for Id<'a>
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 moreimpl<'r> Copy for Id<'r>
Auto Trait Implementations§
impl<'r> !RefUnwindSafe for Id<'r>
impl<'r> !Send for Id<'r>
impl<'r> !Sync for Id<'r>
impl<'r> Unpin for Id<'r>
impl<'r> !UnwindSafe for Id<'r>
Blanket Implementations§
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
]. Read more