1#![allow(missing_docs)]
16
17use std::collections::HashMap;
18use std::collections::HashSet;
19use std::sync::Arc;
20
21use futures::StreamExt as _;
22use indexmap::IndexMap;
23use indexmap::IndexSet;
24use itertools::Itertools as _;
25use pollster::FutureExt as _;
26use tracing::instrument;
27
28use crate::backend::BackendError;
29use crate::backend::BackendResult;
30use crate::backend::CommitId;
31use crate::backend::MergedTreeId;
32use crate::commit::Commit;
33use crate::commit::CommitIteratorExt as _;
34use crate::commit_builder::CommitBuilder;
35use crate::index::Index;
36use crate::index::IndexError;
37use crate::matchers::Matcher;
38use crate::matchers::Visit;
39use crate::merged_tree::MergedTree;
40use crate::merged_tree::MergedTreeBuilder;
41use crate::merged_tree::TreeDiffEntry;
42use crate::repo::MutableRepo;
43use crate::repo::Repo;
44use crate::repo_path::RepoPath;
45use crate::revset::RevsetExpression;
46use crate::revset::RevsetIteratorExt as _;
47use crate::store::Store;
48
49#[instrument(skip(repo))]
51pub fn merge_commit_trees(repo: &dyn Repo, commits: &[Commit]) -> BackendResult<MergedTree> {
52 if let [commit] = commits {
53 commit.tree()
54 } else {
55 merge_commit_trees_no_resolve_without_repo(repo.store(), repo.index(), commits)?.resolve()
56 }
57}
58
59#[instrument(skip(index))]
61pub fn merge_commit_trees_no_resolve_without_repo(
62 store: &Arc<Store>,
63 index: &dyn Index,
64 commits: &[Commit],
65) -> BackendResult<MergedTree> {
66 if commits.is_empty() {
67 Ok(store.get_root_tree(&store.empty_merged_tree_id())?)
68 } else {
69 let mut new_tree = commits[0].tree()?;
70 let commit_ids = commits
71 .iter()
72 .map(|commit| commit.id().clone())
73 .collect_vec();
74 for (i, other_commit) in commits.iter().enumerate().skip(1) {
75 let ancestor_ids = index.common_ancestors(&commit_ids[0..i], &commit_ids[i..][..1]);
76 let ancestors: Vec<_> = ancestor_ids
77 .iter()
78 .map(|id| store.get_commit(id))
79 .try_collect()?;
80 let ancestor_tree =
81 merge_commit_trees_no_resolve_without_repo(store, index, &ancestors)?;
82 let other_tree = other_commit.tree()?;
83 new_tree = new_tree.merge_no_resolve(&ancestor_tree, &other_tree);
84 }
85 Ok(new_tree)
86 }
87}
88
89pub fn restore_tree(
91 source: &MergedTree,
92 destination: &MergedTree,
93 matcher: &dyn Matcher,
94) -> BackendResult<MergedTreeId> {
95 if matcher.visit(RepoPath::root()) == Visit::AllRecursively {
96 Ok(source.id())
98 } else {
99 let mut tree_builder = MergedTreeBuilder::new(destination.id().clone());
102 async {
103 let mut diff_stream = source.diff_stream(destination, matcher);
105 while let Some(TreeDiffEntry {
106 path: repo_path,
107 values,
108 }) = diff_stream.next().await
109 {
110 let (source_value, _destination_value) = values?;
111 tree_builder.set_or_remove(repo_path, source_value);
112 }
113 Ok::<(), BackendError>(())
114 }
115 .block_on()?;
116 tree_builder.write_tree(destination.store())
117 }
118}
119
120pub fn rebase_commit(
121 mut_repo: &mut MutableRepo,
122 old_commit: Commit,
123 new_parents: Vec<CommitId>,
124) -> BackendResult<Commit> {
125 let rewriter = CommitRewriter::new(mut_repo, old_commit, new_parents);
126 let builder = rewriter.rebase()?;
127 builder.write()
128}
129
130pub struct CommitRewriter<'repo> {
132 mut_repo: &'repo mut MutableRepo,
133 old_commit: Commit,
134 new_parents: Vec<CommitId>,
135}
136
137impl<'repo> CommitRewriter<'repo> {
138 pub fn new(
140 mut_repo: &'repo mut MutableRepo,
141 old_commit: Commit,
142 new_parents: Vec<CommitId>,
143 ) -> Self {
144 Self {
145 mut_repo,
146 old_commit,
147 new_parents,
148 }
149 }
150
151 pub fn mut_repo(&mut self) -> &mut MutableRepo {
153 self.mut_repo
154 }
155
156 pub fn old_commit(&self) -> &Commit {
158 &self.old_commit
159 }
160
161 pub fn new_parents(&self) -> &[CommitId] {
163 &self.new_parents
164 }
165
166 pub fn set_new_parents(&mut self, new_parents: Vec<CommitId>) {
168 self.new_parents = new_parents;
169 }
170
171 pub fn set_new_rewritten_parents(&mut self, unrewritten_parents: &[CommitId]) {
174 self.new_parents = self.mut_repo.new_parents(unrewritten_parents);
175 }
176
177 pub fn replace_parent<'a>(
180 &mut self,
181 old_parent: &CommitId,
182 new_parents: impl IntoIterator<Item = &'a CommitId>,
183 ) {
184 if let Some(i) = self.new_parents.iter().position(|p| p == old_parent) {
185 self.new_parents
186 .splice(i..i + 1, new_parents.into_iter().cloned());
187 let mut unique = HashSet::new();
188 self.new_parents.retain(|p| unique.insert(p.clone()));
189 }
190 }
191
192 pub fn parents_changed(&self) -> bool {
195 self.new_parents != self.old_commit.parent_ids()
196 }
197
198 pub fn simplify_ancestor_merge(&mut self) -> Result<(), IndexError> {
201 let head_set: HashSet<_> = self
202 .mut_repo
203 .index()
204 .heads(&mut self.new_parents.iter())?
205 .into_iter()
206 .collect();
207 self.new_parents.retain(|parent| head_set.contains(parent));
208 Ok(())
209 }
210
211 pub fn abandon(self) {
215 let old_commit_id = self.old_commit.id().clone();
216 let new_parents = self.new_parents;
217 self.mut_repo
218 .record_abandoned_commit_with_parents(old_commit_id, new_parents);
219 }
220
221 pub fn rebase_with_empty_behavior(
224 self,
225 empty: EmptyBehaviour,
226 ) -> BackendResult<Option<CommitBuilder<'repo>>> {
227 let old_parents: Vec<_> = self.old_commit.parents().try_collect()?;
228 let old_parent_trees = old_parents
229 .iter()
230 .map(|parent| parent.tree_id().clone())
231 .collect_vec();
232 let new_parents: Vec<_> = self
233 .new_parents
234 .iter()
235 .map(|new_parent_id| self.mut_repo.store().get_commit(new_parent_id))
236 .try_collect()?;
237 let new_parent_trees = new_parents
238 .iter()
239 .map(|parent| parent.tree_id().clone())
240 .collect_vec();
241
242 let (was_empty, new_tree_id) = if new_parent_trees == old_parent_trees {
243 (
244 true,
247 self.old_commit.tree_id().clone(),
249 )
250 } else {
251 let old_base_tree = merge_commit_trees(self.mut_repo, &old_parents)?;
252 let new_base_tree = merge_commit_trees(self.mut_repo, &new_parents)?;
253 let old_tree = self.old_commit.tree()?;
254 (
255 old_base_tree.id() == *self.old_commit.tree_id(),
256 new_base_tree.merge(&old_base_tree, &old_tree)?.id(),
257 )
258 };
259 if let [parent] = &new_parents[..] {
262 let should_abandon = match empty {
263 EmptyBehaviour::Keep => false,
264 EmptyBehaviour::AbandonNewlyEmpty => *parent.tree_id() == new_tree_id && !was_empty,
265 EmptyBehaviour::AbandonAllEmpty => *parent.tree_id() == new_tree_id,
266 };
267 if should_abandon {
268 self.abandon();
269 return Ok(None);
270 }
271 }
272
273 let builder = self
274 .mut_repo
275 .rewrite_commit(&self.old_commit)
276 .set_parents(self.new_parents)
277 .set_tree_id(new_tree_id);
278 Ok(Some(builder))
279 }
280
281 pub fn rebase(self) -> BackendResult<CommitBuilder<'repo>> {
284 let builder = self.rebase_with_empty_behavior(EmptyBehaviour::Keep)?;
285 Ok(builder.unwrap())
286 }
287
288 pub fn reparent(self) -> CommitBuilder<'repo> {
291 self.mut_repo
292 .rewrite_commit(&self.old_commit)
293 .set_parents(self.new_parents)
294 }
295}
296
297pub enum RebasedCommit {
298 Rewritten(Commit),
299 Abandoned { parent_id: CommitId },
300}
301
302pub fn rebase_commit_with_options(
303 mut rewriter: CommitRewriter<'_>,
304 options: &RebaseOptions,
305) -> BackendResult<RebasedCommit> {
306 if options.simplify_ancestor_merge {
308 rewriter
311 .simplify_ancestor_merge()
312 .map_err(|err| BackendError::Other(err.into()))?;
313 }
314
315 let single_parent = match &rewriter.new_parents[..] {
316 [parent_id] => Some(parent_id.clone()),
317 _ => None,
318 };
319 let new_parents_len = rewriter.new_parents.len();
320 if let Some(builder) = rewriter.rebase_with_empty_behavior(options.empty)? {
321 let new_commit = builder.write()?;
322 Ok(RebasedCommit::Rewritten(new_commit))
323 } else {
324 assert_eq!(new_parents_len, 1);
325 Ok(RebasedCommit::Abandoned {
326 parent_id: single_parent.unwrap(),
327 })
328 }
329}
330
331pub fn rebase_to_dest_parent(
333 repo: &dyn Repo,
334 sources: &[Commit],
335 destination: &Commit,
336) -> BackendResult<MergedTree> {
337 if let [source] = sources {
338 if source.parent_ids() == destination.parent_ids() {
339 return source.tree();
340 }
341 }
342 sources.iter().try_fold(
343 destination.parent_tree(repo)?,
344 |destination_tree, source| {
345 let source_parent_tree = source.parent_tree(repo)?;
346 let source_tree = source.tree()?;
347 destination_tree.merge(&source_parent_tree, &source_tree)
348 },
349 )
350}
351
352#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
353pub enum EmptyBehaviour {
354 #[default]
356 Keep,
357 AbandonNewlyEmpty,
361 AbandonAllEmpty,
365}
366
367#[derive(Clone, Debug, Default)]
374pub struct RebaseOptions {
375 pub empty: EmptyBehaviour,
376 pub rewrite_refs: RewriteRefsOptions,
377 pub simplify_ancestor_merge: bool,
380}
381
382#[derive(Clone, Debug, Default)]
384pub struct RewriteRefsOptions {
385 pub delete_abandoned_bookmarks: bool,
390}
391
392#[derive(Default)]
393pub struct MoveCommitsStats {
394 pub num_rebased_targets: u32,
396 pub num_rebased_descendants: u32,
398 pub num_skipped_rebases: u32,
401 pub num_abandoned: u32,
403}
404
405#[derive(Clone, Debug)]
406pub enum MoveCommitsTarget {
407 Commits(Vec<Commit>),
410 Roots(Vec<Commit>),
412}
413
414pub fn move_commits(
423 mut_repo: &mut MutableRepo,
424 new_parent_ids: &[CommitId],
425 new_children: &[Commit],
426 target: &MoveCommitsTarget,
427 options: &RebaseOptions,
428) -> BackendResult<MoveCommitsStats> {
429 let target_commits: Vec<Commit>;
430 let target_commit_ids: HashSet<_>;
431 let connected_target_commits: Vec<Commit>;
432 let connected_target_commits_internal_parents: HashMap<CommitId, Vec<CommitId>>;
433 let target_roots: HashSet<CommitId>;
434
435 match target {
436 MoveCommitsTarget::Commits(commits) => {
437 if commits.is_empty() {
438 return Ok(MoveCommitsStats::default());
439 }
440
441 target_commits = commits.clone();
442 target_commit_ids = target_commits.iter().ids().cloned().collect();
443
444 connected_target_commits =
445 RevsetExpression::commits(target_commits.iter().ids().cloned().collect_vec())
446 .connected()
447 .evaluate(mut_repo)
448 .map_err(|err| err.into_backend_error())?
449 .iter()
450 .commits(mut_repo.store())
451 .try_collect()
452 .map_err(|err| err.into_backend_error())?;
453 connected_target_commits_internal_parents =
454 compute_internal_parents_within(&target_commit_ids, &connected_target_commits);
455
456 target_roots = connected_target_commits_internal_parents
457 .iter()
458 .filter(|(commit_id, parents)| {
459 target_commit_ids.contains(commit_id) && parents.is_empty()
460 })
461 .map(|(commit_id, _)| commit_id.clone())
462 .collect();
463 }
464 MoveCommitsTarget::Roots(roots) => {
465 if roots.is_empty() {
466 return Ok(MoveCommitsStats::default());
467 }
468
469 target_commits = RevsetExpression::commits(roots.iter().ids().cloned().collect_vec())
470 .descendants()
471 .evaluate(mut_repo)
472 .map_err(|err| err.into_backend_error())?
473 .iter()
474 .commits(mut_repo.store())
475 .try_collect()
476 .map_err(|err| err.into_backend_error())?;
477 target_commit_ids = target_commits.iter().ids().cloned().collect();
478
479 connected_target_commits = target_commits.iter().cloned().collect_vec();
480 connected_target_commits_internal_parents = HashMap::new();
483 target_roots = roots.iter().ids().cloned().collect();
484 }
485 }
486
487 let mut target_commits_external_parents: HashMap<CommitId, IndexSet<CommitId>> = HashMap::new();
491 for commit in target_commits.iter().rev() {
492 let mut new_parents = IndexSet::new();
493 for old_parent in commit.parent_ids() {
494 if let Some(parents) = target_commits_external_parents.get(old_parent) {
495 new_parents.extend(parents.iter().cloned());
496 } else {
497 new_parents.insert(old_parent.clone());
498 }
499 }
500 target_commits_external_parents.insert(commit.id().clone(), new_parents);
501 }
502
503 let new_parent_ids: Vec<_> = new_parent_ids
507 .iter()
508 .flat_map(|parent_id| {
509 if let Some(parent_ids) = target_commits_external_parents.get(parent_id) {
510 parent_ids.iter().cloned().collect_vec()
511 } else {
512 vec![parent_id.clone()]
513 }
514 })
515 .collect();
516
517 let new_children: Vec<_> = if new_children
521 .iter()
522 .any(|child| target_commit_ids.contains(child.id()))
523 {
524 let target_commits_descendants: Vec<_> =
525 RevsetExpression::commits(target_commit_ids.iter().cloned().collect_vec())
526 .union(
527 &RevsetExpression::commits(target_commit_ids.iter().cloned().collect_vec())
528 .children(),
529 )
530 .evaluate(mut_repo)
531 .map_err(|err| err.into_backend_error())?
532 .iter()
533 .commits(mut_repo.store())
534 .try_collect()
535 .map_err(|err| err.into_backend_error())?;
536
537 let mut target_commit_external_descendants: HashMap<CommitId, IndexSet<Commit>> =
540 HashMap::new();
541 for commit in &target_commits_descendants {
544 if !target_commit_external_descendants.contains_key(commit.id()) {
545 let children = if target_commit_ids.contains(commit.id()) {
546 IndexSet::new()
547 } else {
548 IndexSet::from([commit.clone()])
549 };
550 target_commit_external_descendants.insert(commit.id().clone(), children);
551 }
552
553 let children = target_commit_external_descendants
554 .get(commit.id())
555 .unwrap()
556 .iter()
557 .cloned()
558 .collect_vec();
559 for parent_id in commit.parent_ids() {
560 if target_commit_ids.contains(parent_id) {
561 if let Some(target_children) =
562 target_commit_external_descendants.get_mut(parent_id)
563 {
564 target_children.extend(children.iter().cloned());
565 } else {
566 target_commit_external_descendants
567 .insert(parent_id.clone(), children.iter().cloned().collect());
568 }
569 };
570 }
571 }
572
573 new_children
574 .iter()
575 .flat_map(|child| {
576 if let Some(children) = target_commit_external_descendants.get(child.id()) {
577 children.iter().cloned().collect_vec()
578 } else {
579 vec![child.clone()]
580 }
581 })
582 .collect()
583 } else {
584 new_children.to_vec()
585 };
586
587 let new_children_parents: HashMap<_, _> = if !new_children.is_empty() {
590 let target_heads = compute_commits_heads(&target_commit_ids, &connected_target_commits);
593
594 new_children
595 .iter()
596 .map(|child_commit| {
597 let mut new_child_parent_ids = IndexSet::new();
598 for old_child_parent_id in child_commit.parent_ids() {
599 let old_child_parent_ids = if let Some(parents) =
601 target_commits_external_parents.get(old_child_parent_id)
602 {
603 parents.iter().collect_vec()
604 } else {
605 vec![old_child_parent_id]
606 };
607
608 for id in old_child_parent_ids {
612 if new_parent_ids.contains(id) {
613 new_child_parent_ids.extend(target_heads.clone());
614 } else {
615 new_child_parent_ids.insert(id.clone());
616 };
617 }
618 }
619
620 new_child_parent_ids.extend(target_heads.clone());
623
624 (
625 child_commit.id().clone(),
626 new_child_parent_ids.into_iter().collect_vec(),
627 )
628 })
629 .collect()
630 } else {
631 HashMap::new()
632 };
633
634 let mut roots = target_roots.iter().cloned().collect_vec();
637 roots.extend(new_children.iter().ids().cloned());
638
639 let descendants = mut_repo.find_descendants_for_rebase(roots.clone())?;
640 let commit_new_parents_map: HashMap<_, _> = descendants
641 .iter()
642 .map(|commit| {
643 let commit_id = commit.id();
644 let new_parent_ids =
645
646 if let Some(new_child_parents) = new_children_parents.get(commit_id) {
648 new_child_parents.clone()
649 }
650 else if target_commit_ids.contains(commit_id) {
652 if target_roots.contains(commit_id) {
654 new_parent_ids.clone()
655 }
656 else {
664 let mut new_parents = vec![];
665 for parent_id in commit.parent_ids() {
666 if target_commit_ids.contains(parent_id) {
667 new_parents.push(parent_id.clone());
668 } else if let Some(parents) =
669 connected_target_commits_internal_parents.get(parent_id) {
670 new_parents.extend(parents.iter().cloned());
671 } else if !new_children.iter().any(|new_child| {
672 mut_repo.index().is_ancestor(new_child.id(), parent_id) }) {
673 new_parents.push(parent_id.clone());
674 }
675 }
676 new_parents
677 }
678 }
679 else if commit
682 .parent_ids()
683 .iter()
684 .any(|id| target_commits_external_parents.contains_key(id))
685 {
686 let mut new_parents = vec![];
687 for parent in commit.parent_ids() {
688 if let Some(parents) = target_commits_external_parents.get(parent) {
689 new_parents.extend(parents.iter().cloned());
690 } else {
691 new_parents.push(parent.clone());
692 }
693 }
694 new_parents
695 } else {
696 commit.parent_ids().iter().cloned().collect_vec()
697 };
698 (commit.id().clone(), new_parent_ids)
699 })
700 .collect();
701
702 let mut num_rebased_targets = 0;
703 let mut num_rebased_descendants = 0;
704 let mut num_skipped_rebases = 0;
705 let mut num_abandoned = 0;
706
707 let rebase_descendant_options = &RebaseOptions {
709 empty: EmptyBehaviour::Keep,
710 rewrite_refs: options.rewrite_refs.clone(),
711 simplify_ancestor_merge: options.simplify_ancestor_merge,
712 };
713
714 mut_repo.transform_commits(
715 descendants,
716 &commit_new_parents_map,
717 &options.rewrite_refs,
718 |rewriter| {
719 let old_commit_id = rewriter.old_commit().id();
720 if rewriter.parents_changed() {
721 let is_target_commit = target_commit_ids.contains(old_commit_id);
722 let rebased_commit = rebase_commit_with_options(
723 rewriter,
724 if is_target_commit {
725 options
726 } else {
727 rebase_descendant_options
728 },
729 )?;
730 if let RebasedCommit::Abandoned { .. } = rebased_commit {
731 num_abandoned += 1;
732 } else if is_target_commit {
733 num_rebased_targets += 1;
734 } else {
735 num_rebased_descendants += 1;
736 }
737 } else {
738 num_skipped_rebases += 1;
739 }
740
741 Ok(())
742 },
743 )?;
744
745 Ok(MoveCommitsStats {
746 num_rebased_targets,
747 num_rebased_descendants,
748 num_skipped_rebases,
749 num_abandoned,
750 })
751}
752
753#[derive(Default)]
754pub struct DuplicateCommitsStats {
755 pub duplicated_commits: IndexMap<CommitId, Commit>,
757 pub num_rebased: u32,
760}
761
762pub fn duplicate_commits(
779 mut_repo: &mut MutableRepo,
780 target_commits: &[CommitId],
781 target_descriptions: &HashMap<CommitId, String>,
782 parent_commit_ids: &[CommitId],
783 children_commit_ids: &[CommitId],
784) -> BackendResult<DuplicateCommitsStats> {
785 if target_commits.is_empty() {
786 return Ok(DuplicateCommitsStats::default());
787 }
788
789 let mut duplicated_old_to_new: IndexMap<CommitId, Commit> = IndexMap::new();
790 let mut num_rebased = 0;
791
792 let target_commit_ids: HashSet<_> = target_commits.iter().cloned().collect();
793
794 let connected_target_commits: Vec<_> =
795 RevsetExpression::commits(target_commit_ids.iter().cloned().collect_vec())
796 .connected()
797 .evaluate(mut_repo)
798 .map_err(|err| err.into_backend_error())?
799 .iter()
800 .commits(mut_repo.store())
801 .try_collect()
802 .map_err(|err| err.into_backend_error())?;
803
804 let target_commits_internal_parents = {
811 let mut target_commits_internal_parents =
812 compute_internal_parents_within(&target_commit_ids, &connected_target_commits);
813 target_commits_internal_parents.retain(|id, _| target_commit_ids.contains(id));
814 target_commits_internal_parents
815 };
816
817 let target_root_ids: HashSet<_> = target_commits_internal_parents
819 .iter()
820 .filter(|(_, parents)| parents.is_empty())
821 .map(|(commit_id, _)| commit_id.clone())
822 .collect();
823
824 let target_head_ids = if !children_commit_ids.is_empty() {
827 compute_commits_heads(&target_commit_ids, &connected_target_commits)
828 } else {
829 vec![]
830 };
831
832 for original_commit_id in target_commits.iter().rev() {
835 let original_commit = mut_repo.store().get_commit(original_commit_id)?;
836 let new_parent_ids = if target_root_ids.contains(original_commit_id) {
837 parent_commit_ids.to_vec()
838 } else {
839 target_commits_internal_parents
840 .get(original_commit_id)
841 .unwrap()
842 .iter()
843 .map(|id| {
845 duplicated_old_to_new
846 .get(id)
847 .map_or(id, |commit| commit.id())
848 .clone()
849 })
850 .collect()
851 };
852 let mut new_commit_builder = CommitRewriter::new(mut_repo, original_commit, new_parent_ids)
853 .rebase()?
854 .generate_new_change_id();
855 if let Some(desc) = target_descriptions.get(original_commit_id) {
856 new_commit_builder = new_commit_builder.set_description(desc);
857 }
858 duplicated_old_to_new.insert(original_commit_id.clone(), new_commit_builder.write()?);
859 }
860
861 let target_head_ids = target_head_ids
864 .into_iter()
865 .map(|commit_id| {
866 duplicated_old_to_new
867 .get(&commit_id)
868 .map_or(commit_id, |commit| commit.id().clone())
869 })
870 .collect_vec();
871
872 let children_commit_ids_set: HashSet<CommitId> = children_commit_ids.iter().cloned().collect();
874 mut_repo.transform_descendants(children_commit_ids.to_vec(), |mut rewriter| {
875 if children_commit_ids_set.contains(rewriter.old_commit().id()) {
876 let mut child_new_parent_ids = IndexSet::new();
877 for old_parent_id in rewriter.old_commit().parent_ids() {
878 if parent_commit_ids.contains(old_parent_id) {
883 child_new_parent_ids.extend(target_head_ids.clone());
884 } else {
885 child_new_parent_ids.insert(old_parent_id.clone());
886 }
887 }
888 child_new_parent_ids.extend(target_head_ids.clone());
891 rewriter.set_new_parents(child_new_parent_ids.into_iter().collect());
892 }
893 num_rebased += 1;
894 rewriter.rebase()?.write()?;
895 Ok(())
896 })?;
897
898 Ok(DuplicateCommitsStats {
899 duplicated_commits: duplicated_old_to_new,
900 num_rebased,
901 })
902}
903
904pub fn duplicate_commits_onto_parents(
914 mut_repo: &mut MutableRepo,
915 target_commits: &[CommitId],
916 target_descriptions: &HashMap<CommitId, String>,
917) -> BackendResult<DuplicateCommitsStats> {
918 if target_commits.is_empty() {
919 return Ok(DuplicateCommitsStats::default());
920 }
921
922 let mut duplicated_old_to_new: IndexMap<CommitId, Commit> = IndexMap::new();
923
924 for original_commit_id in target_commits.iter().rev() {
927 let original_commit = mut_repo.store().get_commit(original_commit_id)?;
928 let new_parent_ids = original_commit
929 .parent_ids()
930 .iter()
931 .map(|id| {
932 duplicated_old_to_new
933 .get(id)
934 .map_or(id, |commit| commit.id())
935 .clone()
936 })
937 .collect();
938 let mut new_commit_builder = mut_repo
939 .rewrite_commit(&original_commit)
940 .generate_new_change_id()
941 .set_parents(new_parent_ids);
942 if let Some(desc) = target_descriptions.get(original_commit_id) {
943 new_commit_builder = new_commit_builder.set_description(desc);
944 }
945 duplicated_old_to_new.insert(original_commit_id.clone(), new_commit_builder.write()?);
946 }
947
948 Ok(DuplicateCommitsStats {
949 duplicated_commits: duplicated_old_to_new,
950 num_rebased: 0,
951 })
952}
953
954fn compute_internal_parents_within(
962 target_commit_ids: &HashSet<CommitId>,
963 graph_commits: &[Commit],
964) -> HashMap<CommitId, Vec<CommitId>> {
965 let mut internal_parents: HashMap<CommitId, Vec<CommitId>> = HashMap::new();
966 for commit in graph_commits.iter().rev() {
967 let mut new_parents = vec![];
970 for old_parent in commit.parent_ids() {
971 if target_commit_ids.contains(old_parent) {
972 new_parents.push(old_parent.clone());
973 } else if let Some(parents) = internal_parents.get(old_parent) {
974 new_parents.extend(parents.iter().cloned());
975 }
976 }
977 internal_parents.insert(commit.id().clone(), new_parents);
978 }
979 internal_parents
980}
981
982fn compute_commits_heads(
988 target_commit_ids: &HashSet<CommitId>,
989 connected_target_commits: &[Commit],
990) -> Vec<CommitId> {
991 let mut target_head_ids: HashSet<CommitId> = HashSet::new();
992 for commit in connected_target_commits.iter().rev() {
993 target_head_ids.insert(commit.id().clone());
994 for old_parent in commit.parent_ids() {
995 target_head_ids.remove(old_parent);
996 }
997 }
998 connected_target_commits
999 .iter()
1000 .rev()
1001 .filter(|commit| {
1002 target_head_ids.contains(commit.id()) && target_commit_ids.contains(commit.id())
1003 })
1004 .map(|commit| commit.id().clone())
1005 .collect_vec()
1006}
1007
1008pub struct CommitWithSelection {
1009 pub commit: Commit,
1010 pub selected_tree: MergedTree,
1011 pub parent_tree: MergedTree,
1012}
1013
1014impl CommitWithSelection {
1015 pub fn is_full_selection(&self) -> bool {
1017 &self.selected_tree.id() == self.commit.tree_id()
1018 }
1019
1020 pub fn is_empty_selection(&self) -> bool {
1026 self.selected_tree.id() == self.parent_tree.id()
1027 }
1028}
1029
1030#[must_use]
1032pub struct SquashedCommit<'repo> {
1033 pub commit_builder: CommitBuilder<'repo>,
1035 pub abandoned_commits: Vec<Commit>,
1037}
1038
1039pub fn squash_commits<'repo>(
1043 repo: &'repo mut MutableRepo,
1044 sources: &[CommitWithSelection],
1045 destination: &Commit,
1046 keep_emptied: bool,
1047) -> BackendResult<Option<SquashedCommit<'repo>>> {
1048 struct SourceCommit<'a> {
1049 commit: &'a CommitWithSelection,
1050 abandon: bool,
1051 }
1052 let mut source_commits = vec![];
1053 for source in sources {
1054 let abandon = !keep_emptied && source.is_full_selection();
1055 if !abandon && source.is_empty_selection() {
1056 continue;
1060 }
1061
1062 source_commits.push(SourceCommit {
1065 commit: source,
1066 abandon,
1067 });
1068 }
1069
1070 if source_commits.is_empty() {
1071 return Ok(None);
1072 }
1073
1074 let mut abandoned_commits = vec![];
1075 for source in &source_commits {
1076 if source.abandon {
1077 repo.record_abandoned_commit(&source.commit.commit);
1078 abandoned_commits.push(source.commit.commit.clone());
1079 } else {
1080 let source_tree = source.commit.commit.tree()?;
1081 let new_source_tree =
1083 source_tree.merge(&source.commit.selected_tree, &source.commit.parent_tree)?;
1084 repo.rewrite_commit(&source.commit.commit)
1085 .set_tree_id(new_source_tree.id().clone())
1086 .write()?;
1087 }
1088 }
1089
1090 let mut rewritten_destination = destination.clone();
1091 if sources.iter().any(|source| {
1092 repo.index()
1093 .is_ancestor(source.commit.id(), destination.id())
1094 }) {
1095 let options = RebaseOptions::default();
1100 repo.rebase_descendants_with_options(&options, |old_commit, rebased_commit| {
1101 if old_commit.id() != destination.id() {
1102 return;
1103 }
1104 rewritten_destination = match rebased_commit {
1105 RebasedCommit::Rewritten(commit) => commit,
1106 RebasedCommit::Abandoned { .. } => panic!("all commits should be kept"),
1107 };
1108 })?;
1109 }
1110 let mut destination_tree = rewritten_destination.tree()?;
1112 for source in &source_commits {
1113 destination_tree =
1114 destination_tree.merge(&source.commit.parent_tree, &source.commit.selected_tree)?;
1115 }
1116 let mut predecessors = vec![destination.id().clone()];
1117 predecessors.extend(
1118 source_commits
1119 .iter()
1120 .map(|source| source.commit.commit.id().clone()),
1121 );
1122
1123 let commit_builder = repo
1124 .rewrite_commit(&rewritten_destination)
1125 .set_tree_id(destination_tree.id().clone())
1126 .set_predecessors(predecessors);
1127 Ok(Some(SquashedCommit {
1128 commit_builder,
1129 abandoned_commits,
1130 }))
1131}