rewrite: add CommitRewriter::record_abandoned_commit()

We already have two uses for this function and I think we're soon
going to have more.

The function record the old commit as abandoned with the new parents,
which is typically what you want. We could record it as abandoned with
the old parents instead but then we'd have to do an extra iteration to
find the parents when rebasing any children. It would also be
confusing if
`rewriter.set_parents(new_parents).record_abandoned_commit()` didn't
respect the new parents.
This commit is contained in:
Martin von Zweigbergk 2024-04-28 10:40:12 -07:00 committed by Martin von Zweigbergk
parent 8c2c319f77
commit dbf2a98903
2 changed files with 10 additions and 9 deletions

View file

@ -184,6 +184,14 @@ impl<'repo> CommitRewriter<'repo> {
self.new_parents.retain(|parent| head_set.contains(parent));
}
/// Records the old commit as abandoned with the new parents.
pub fn abandon(self) {
let old_commit_id = self.old_commit.id().clone();
let new_parents = self.new_parents;
self.mut_repo
.record_abandoned_commit_with_parents(old_commit_id, new_parents);
}
/// Rebase the old commit onto the new parents. Returns a `CommitBuilder`
/// for the new commit. Returns `None` if the commit was abandoned.
pub fn rebase_with_empty_behavior(
@ -235,10 +243,7 @@ impl<'repo> CommitRewriter<'repo> {
EmptyBehaviour::AbandonAllEmpty => *parent.tree_id() == new_tree_id,
};
if should_abandon {
self.mut_repo.record_abandoned_commit_with_parents(
self.old_commit.id().clone(),
std::iter::once(parent.id().clone()),
);
self.abandon();
return Ok(None);
}
}

View file

@ -50,11 +50,7 @@ fn test_transform_descendants_sync() {
.transform_descendants(&settings, vec![commit_b.id().clone()], |mut rewriter| {
rewriter.replace_parent(commit_a.id(), [commit_g.id()]);
if *rewriter.old_commit() == commit_c {
let old_id = rewriter.old_commit().id().clone();
let new_parent_ids = rewriter.new_parents().to_vec();
rewriter
.mut_repo()
.record_abandoned_commit_with_parents(old_id, new_parent_ids);
rewriter.abandon();
} else {
let old_commit_id = rewriter.old_commit().id().clone();
let new_commit = rewriter.rebase(&settings)?.write()?;