rewrite: remove return value from rebase_next()

`rebase_next()` returns an `Option<RebasedDescendant>`, but the only
way we use it is to decide whether to terminate the loop over
`to_visit`. Let's simplify by making the caller iterate over
`to_visit` instead.
This commit is contained in:
Martin von Zweigbergk 2024-01-28 12:45:49 -08:00 committed by Martin von Zweigbergk
parent 881d75e899
commit 9efa66e8c9

View file

@ -539,8 +539,7 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
Ok(())
}
fn rebase_next(&mut self) -> Result<Option<RebasedDescendant>, TreeMergeError> {
while let Some(old_commit) = self.to_visit.pop() {
fn rebase_one(&mut self, old_commit: Commit) -> Result<(), TreeMergeError> {
let old_commit_id = old_commit.id().clone();
if let Some(new_parent_ids) = self.parent_mapping.get(&old_commit_id).cloned() {
// This is a commit that had already been rebased before `self` was created
@ -548,13 +547,13 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
// to rebase it, but we still want to update branches pointing
// to the old commit.
self.update_references(old_commit_id, new_parent_ids, false)?;
continue;
return Ok(());
}
if let Some(divergent_ids) = self.divergent.get(&old_commit_id).cloned() {
// Leave divergent commits in place. Don't update `parent_mapping` since we
// don't want to rebase descendants either.
self.update_references(old_commit_id, divergent_ids, false)?;
continue;
return Ok(());
}
let old_parent_ids = old_commit.parent_ids();
let new_parent_ids = self.new_parents(old_parent_ids);
@ -563,10 +562,10 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
self.parent_mapping
.insert(old_commit_id.clone(), new_parent_ids.clone());
self.update_references(old_commit_id, new_parent_ids, true)?;
continue;
return Ok(());
} else if new_parent_ids == old_parent_ids {
// The commit is already in place.
continue;
return Ok(());
}
// Don't create commit where one parent is an ancestor of another.
@ -604,16 +603,13 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
vec![new_commit.id().clone()],
abandoned_old_commit,
)?;
return Ok(Some(RebasedDescendant {
old_commit,
new_commit,
}));
}
Ok(None)
Ok(())
}
pub fn rebase_all(&mut self) -> Result<(), TreeMergeError> {
while self.rebase_next()?.is_some() {}
while let Some(old_commit) = self.to_visit.pop() {
self.rebase_one(old_commit)?;
}
let mut view = self.mut_repo.view().store_view().clone();
for commit_id in &self.heads_to_remove {
view.head_ids.remove(commit_id);
@ -627,9 +623,3 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
Ok(())
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RebasedDescendant {
pub old_commit: Commit,
pub new_commit: Commit,
}