From dbf2a98903abbeefe75a16399eb1378ea2f59345 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 28 Apr 2024 10:40:12 -0700 Subject: [PATCH] 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. --- lib/src/rewrite.rs | 13 +++++++++---- lib/tests/test_rewrite_transform.rs | 6 +----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/src/rewrite.rs b/lib/src/rewrite.rs index 6915cb5bd..9e4b1c5ec 100644 --- a/lib/src/rewrite.rs +++ b/lib/src/rewrite.rs @@ -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); } } diff --git a/lib/tests/test_rewrite_transform.rs b/lib/tests/test_rewrite_transform.rs index 58ebc7e76..87daa9775 100644 --- a/lib/tests/test_rewrite_transform.rs +++ b/lib/tests/test_rewrite_transform.rs @@ -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()?;