Skip to content

Commit

Permalink
rewrite: add CommitRewriter::record_abandoned_commit()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinvonz committed Apr 30, 2024
1 parent f543974 commit 1562276
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ 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 record_abandoned_commit(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(
Expand Down Expand Up @@ -235,10 +242,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.record_abandoned_commit();
return Ok(None);
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/tests/test_rewrite_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.record_abandoned_commit();
} else {
let old_commit_id = rewriter.old_commit().id().clone();
let new_commit = rewriter.rebase(&settings)?.write()?;
Expand Down

0 comments on commit 1562276

Please sign in to comment.