Skip to content

Commit

Permalink
rewrite: extract compute_commits_heads function
Browse files Browse the repository at this point in the history
This will be shared between `move_commits` and the new
`duplicate_commits` function to be added.
  • Loading branch information
bnjmnt4n committed Nov 15, 2024
1 parent 4316968 commit 0a7f6bd
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,21 +587,7 @@ pub fn move_commits(
let new_children_parents: HashMap<_, _> = if !new_children.is_empty() {
// Compute the heads of the target set, which will be used as the parents of
// `new_children`.
let mut target_heads: HashSet<CommitId> = HashSet::new();
for commit in connected_target_commits.iter().rev() {
target_heads.insert(commit.id().clone());
for old_parent in commit.parent_ids() {
target_heads.remove(old_parent);
}
}
let target_heads = connected_target_commits
.iter()
.rev()
.filter(|commit| {
target_heads.contains(commit.id()) && target_commit_ids.contains(commit.id())
})
.map(|commit| commit.id().clone())
.collect_vec();
let target_heads = compute_commits_heads(&target_commit_ids, &connected_target_commits);

new_children
.iter()
Expand Down Expand Up @@ -824,6 +810,32 @@ fn compute_internal_parents_within(
internal_parents
}

/// Computes the heads of commits in the target set, given the list of
/// `target_commit_ids` and a connected graph of commits.
///
/// `connected_target_commits` should be in reverse topological order (children
/// before parents).
fn compute_commits_heads(
target_commit_ids: &HashSet<CommitId>,
connected_target_commits: &[Commit],
) -> Vec<CommitId> {
let mut target_head_ids: HashSet<CommitId> = HashSet::new();
for commit in connected_target_commits.iter().rev() {
target_head_ids.insert(commit.id().clone());
for old_parent in commit.parent_ids() {
target_head_ids.remove(old_parent);
}
}
connected_target_commits
.iter()
.rev()
.filter(|commit| {
target_head_ids.contains(commit.id()) && target_commit_ids.contains(commit.id())
})
.map(|commit| commit.id().clone())
.collect_vec()
}

pub struct CommitToSquash {
pub commit: Commit,
pub selected_tree: MergedTree,
Expand Down

0 comments on commit 0a7f6bd

Please sign in to comment.