Skip to content

Commit 3e2ff9a

Browse files
committed
feat: add Graph::insert_parents_with_lookup()
It's another way to insert parents, and tuned for the needs of merge-base.
1 parent 32706da commit 3e2ff9a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

gix-revwalk/src/graph/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,44 @@ impl<'find, T> Graph<'find, T> {
136136
Ok(())
137137
}
138138

139+
/// Insert the parents of commit named `id` to the graph and associate new parents with data
140+
/// as produced by `parent_data(parent_id, parent_info, maybe-existing-data &mut T) -> T`, which is always
141+
/// provided the full parent commit information.
142+
/// It will be provided either existing data, along with complete information about the parent,
143+
/// and produces new data even though it's only used in case the parent isn't stored in the graph yet.
144+
pub fn insert_parents_with_lookup<E>(
145+
&mut self,
146+
id: &gix_hash::oid,
147+
parent_data: &mut dyn FnMut(gix_hash::ObjectId, LazyCommit<'_>, Option<&mut T>) -> Result<T, E>,
148+
) -> Result<(), E>
149+
where
150+
E: From<gix_object::find::existing_iter::Error>
151+
+ From<gix_object::decode::Error>
152+
+ From<commit::iter_parents::Error>,
153+
{
154+
let commit = self.lookup(id).map_err(E::from)?;
155+
let parents: SmallVec<[_; 2]> = commit.iter_parents().collect();
156+
for parent_id in parents {
157+
let parent_id = parent_id.map_err(E::from)?;
158+
let parent = match try_lookup(&parent_id, &*self.find, self.cache.as_ref(), &mut self.parent_buf)
159+
.map_err(E::from)?
160+
{
161+
Some(p) => p,
162+
None => continue, // skip missing objects, this is due to shallow clones for instance.
163+
};
164+
165+
match self.map.entry(parent_id) {
166+
gix_hashtable::hash_map::Entry::Vacant(entry) => {
167+
entry.insert(parent_data(parent_id, parent, None)?);
168+
}
169+
gix_hashtable::hash_map::Entry::Occupied(mut entry) => {
170+
parent_data(parent_id, parent, Some(entry.get_mut()))?;
171+
}
172+
}
173+
}
174+
Ok(())
175+
}
176+
139177
/// Turn ourselves into the underlying graph structure, which is a mere mapping between object ids and their data.
140178
pub fn detach(self) -> IdMap<T> {
141179
self.map

0 commit comments

Comments
 (0)