From 0a7f6bd8aa8560962388df6d1a9a68b18f670d64 Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Sun, 2 Jun 2024 02:39:22 +0800 Subject: [PATCH] rewrite: extract `compute_commits_heads` function This will be shared between `move_commits` and the new `duplicate_commits` function to be added. --- lib/src/rewrite.rs | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/src/rewrite.rs b/lib/src/rewrite.rs index 75932c4491..749713362e 100644 --- a/lib/src/rewrite.rs +++ b/lib/src/rewrite.rs @@ -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 = 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() @@ -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, + connected_target_commits: &[Commit], +) -> Vec { + let mut target_head_ids: HashSet = 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,