From af04e3dd676220bda59a65b8c332eece9e156e69 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sat, 25 Nov 2023 21:10:54 -0800 Subject: [PATCH] TMP: Move function to a tmp location --- lib/src/rewrite.rs | 83 +++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/lib/src/rewrite.rs b/lib/src/rewrite.rs index 4321198a5f..86180068a9 100644 --- a/lib/src/rewrite.rs +++ b/lib/src/rewrite.rs @@ -216,6 +216,51 @@ pub fn back_out_commit( .write()?) } +/// Assumes that `parent_mapping` does not contain cycles +/// TODO: More details +pub fn new_parents_via_mapping( + old_ids: &[CommitId], + parent_mapping: &HashMap>, +) -> Vec { + let mut new_ids: Vec = old_ids.into(); + let mut iterations = 0; + loop { + let mut made_replacements = false; + // TODO(ilyagr): (Maybe?) optimize common case of replacements all + // being singletons + let previousy_new_ids = new_ids.split_off(0); + for id in previousy_new_ids.into_iter() { + match parent_mapping.get(&id) { + None => new_ids.push(id), + Some(replacements) => { + made_replacements = true; + assert!(!replacements.is_empty()); + new_ids.extend(replacements.iter().cloned()) + } + }; + } + if !made_replacements { + break; + } + iterations += 1; + assert!( + iterations <= parent_mapping.len(), + "cycle detected in the parent mapping" + ); + } + match new_ids.as_slice() { + // The first two cases are an optimization for the common case of commits with <=2 + // parents + [_singleton] => new_ids, + [a, b] if a != b => new_ids, + _ => { + // De-duplicate ids + let new_ids_set: IndexSet = new_ids.into_iter().collect(); + new_ids_set.into_iter().collect() + } + } +} + #[derive(Clone, Default, PartialEq, Eq, Debug)] pub enum EmptyBehaviour { /// Always keep empty commits @@ -383,43 +428,7 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> { /// Assumes that `parent_mapping` does not contain cycles /// TODO: More details fn new_parents(&self, old_ids: &[CommitId]) -> Vec { - let mut new_ids: Vec = old_ids.into(); - let mut iterations = 0; - loop { - let mut made_replacements = false; - // TODO(ilyagr): (Maybe?) optimize common case of replacements all - // being singletons - let previousy_new_ids = new_ids.split_off(0); - for id in previousy_new_ids.into_iter() { - match self.parent_mapping.get(&id) { - None => new_ids.push(id), - Some(replacements) => { - made_replacements = true; - assert!(!replacements.is_empty()); - new_ids.extend(replacements.iter().cloned()) - } - }; - } - if !made_replacements { - break; - } - iterations += 1; - assert!( - iterations <= self.parent_mapping.len(), - "cycle detected in the parent mapping" - ); - } - match new_ids.as_slice() { - // The first two cases are an optimization for the common case of commits with <=2 - // parents - [_singleton] => new_ids, - [a, b] if a != b => new_ids, - _ => { - // De-duplicate ids - let new_ids_set: IndexSet = new_ids.into_iter().collect(); - new_ids_set.into_iter().collect() - } - } + new_parents_via_mapping(old_ids, &self.parent_mapping) } fn ref_target_update(old_id: CommitId, new_ids: Vec) -> (RefTarget, RefTarget) {