jj_lib/
rewrite.rs

1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![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/// Merges `commits` and tries to resolve any conflicts recursively.
50#[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/// Merges `commits` without attempting to resolve file conflicts.
60#[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
89/// Restore matching paths from the source into the destination.
90pub 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        // Optimization for a common case
97        Ok(source.id())
98    } else {
99        // TODO: We should be able to not traverse deeper in the diff if the matcher
100        // matches an entire subtree.
101        let mut tree_builder = MergedTreeBuilder::new(destination.id().clone());
102        async {
103            // TODO: handle copy tracking
104            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
130/// Helps rewrite a commit.
131pub 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    /// Create a new instance.
139    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    /// Returns the `MutableRepo`.
152    pub fn mut_repo(&mut self) -> &mut MutableRepo {
153        self.mut_repo
154    }
155
156    /// The commit we're rewriting.
157    pub fn old_commit(&self) -> &Commit {
158        &self.old_commit
159    }
160
161    /// Get the old commit's intended new parents.
162    pub fn new_parents(&self) -> &[CommitId] {
163        &self.new_parents
164    }
165
166    /// Set the old commit's intended new parents.
167    pub fn set_new_parents(&mut self, new_parents: Vec<CommitId>) {
168        self.new_parents = new_parents;
169    }
170
171    /// Set the old commit's intended new parents to be the rewritten versions
172    /// of the given parents.
173    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    /// Update the intended new parents by replacing any occurrence of
178    /// `old_parent` by `new_parents`.
179    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    /// Checks if the intended new parents are different from the old commit's
193    /// parents.
194    pub fn parents_changed(&self) -> bool {
195        self.new_parents != self.old_commit.parent_ids()
196    }
197
198    /// If a merge commit would end up with one parent being an ancestor of the
199    /// other, then filter out the ancestor.
200    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    /// Records the old commit as abandoned with the new parents.
212    ///
213    /// This is equivalent to `reparent(settings).abandon()`, but is cheaper.
214    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    /// Rebase the old commit onto the new parents. Returns a `CommitBuilder`
222    /// for the new commit. Returns `None` if the commit was abandoned.
223    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                // Optimization: was_empty is only used for newly empty, but when the
245                // parents haven't changed it can't be newly empty.
246                true,
247                // Optimization: Skip merging.
248                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        // Ensure we don't abandon commits with multiple parents (merge commits), even
260        // if they're empty.
261        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    /// Rebase the old commit onto the new parents. Returns a `CommitBuilder`
282    /// for the new commit.
283    pub fn rebase(self) -> BackendResult<CommitBuilder<'repo>> {
284        let builder = self.rebase_with_empty_behavior(EmptyBehaviour::Keep)?;
285        Ok(builder.unwrap())
286    }
287
288    /// Rewrite the old commit onto the new parents without changing its
289    /// contents. Returns a `CommitBuilder` for the new commit.
290    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 specified, don't create commit where one parent is an ancestor of another.
307    if options.simplify_ancestor_merge {
308        // TODO: BackendError is not the right error here because
309        // the error does not come from `Backend`, but `Index`.
310        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
331/// Moves changes from `sources` to the `destination` parent, returns new tree.
332pub 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    /// Always keep empty commits
355    #[default]
356    Keep,
357    /// Skips commits that would be empty after the rebase, but that were not
358    /// originally empty.
359    /// Will never skip merge commits with multiple non-empty parents.
360    AbandonNewlyEmpty,
361    /// Skips all empty commits, including ones that were empty before the
362    /// rebase.
363    /// Will never skip merge commits with multiple non-empty parents.
364    AbandonAllEmpty,
365}
366
367/// Controls the configuration of a rebase.
368// If we wanted to add a flag similar to `git rebase --ignore-date`, then this
369// makes it much easier by ensuring that the only changes required are to
370// change the RebaseOptions construction in the CLI, and changing the
371// rebase_commit function to actually use the flag, and ensure we don't need to
372// plumb it in.
373#[derive(Clone, Debug, Default)]
374pub struct RebaseOptions {
375    pub empty: EmptyBehaviour,
376    pub rewrite_refs: RewriteRefsOptions,
377    /// If a merge commit would end up with one parent being an ancestor of the
378    /// other, then filter out the ancestor.
379    pub simplify_ancestor_merge: bool,
380}
381
382/// Configuration for [`MutableRepo::update_rewritten_references()`].
383#[derive(Clone, Debug, Default)]
384pub struct RewriteRefsOptions {
385    /// Whether or not delete bookmarks pointing to the abandoned commits.
386    ///
387    /// If false, bookmarks will be moved to the parents of the abandoned
388    /// commit.
389    pub delete_abandoned_bookmarks: bool,
390}
391
392#[derive(Default)]
393pub struct MoveCommitsStats {
394    /// The number of commits in the target set which were rebased.
395    pub num_rebased_targets: u32,
396    /// The number of descendant commits which were rebased.
397    pub num_rebased_descendants: u32,
398    /// The number of commits for which rebase was skipped, due to the commit
399    /// already being in place.
400    pub num_skipped_rebases: u32,
401    /// The number of commits which were abandoned.
402    pub num_abandoned: u32,
403}
404
405#[derive(Clone, Debug)]
406pub enum MoveCommitsTarget {
407    /// The commits to be moved. Commits should be mutable and in reverse
408    /// topological order.
409    Commits(Vec<Commit>),
410    /// The root commits to be moved, along with all their descendants.
411    Roots(Vec<Commit>),
412}
413
414/// Moves `target_commits` from their current location to a new location in the
415/// graph.
416///
417/// Commits in `target` are rebased onto the new parents given by
418/// `new_parent_ids`, while the `new_children` commits are rebased onto the
419/// heads of the commits in `targets`. This assumes that commits in `target` and
420/// `new_children` can be rewritten, and there will be no cycles in the
421/// resulting graph. Commits in `target` should be in reverse topological order.
422pub 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            // We don't have to compute the internal parents for the connected target set,
481            // since the connected target set is the same as the target set.
482            connected_target_commits_internal_parents = HashMap::new();
483            target_roots = roots.iter().ids().cloned().collect();
484        }
485    }
486
487    // If a commit outside the target set has a commit in the target set as a
488    // parent, then - after the transformation - it should have that commit's
489    // ancestors which are not in the target set as parents.
490    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    // If the new parents include a commit in the target set, replace it with the
504    // commit's ancestors which are outside the set.
505    // e.g. `jj rebase -r A --before A`
506    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    // If the new children include a commit in the target set, replace it with the
518    // commit's descendants which are outside the set.
519    // e.g. `jj rebase -r A --after A`
520    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        // For all commits in the target set, compute its transitive descendant commits
538        // which are outside of the target set by up to 1 generation.
539        let mut target_commit_external_descendants: HashMap<CommitId, IndexSet<Commit>> =
540            HashMap::new();
541        // Iterate through all descendants of the target set, going through children
542        // before parents.
543        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    // Compute the parents of the new children, which will include the heads of the
588    // target set.
589    let new_children_parents: HashMap<_, _> = if !new_children.is_empty() {
590        // Compute the heads of the target set, which will be used as the parents of
591        // `new_children`.
592        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                    // Replace target commits with their parents outside the target set.
600                    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                    // If the original parents of the new children are the new parents of the
609                    // `target_heads`, replace them with the target heads since we are "inserting"
610                    // the target commits in between the new parents and the new children.
611                    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                // If not already present, add `target_heads` as parents of the new child
621                // commit.
622                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    // Compute the set of commits to visit, which includes the target commits, the
635    // new children commits (if any), and their descendants.
636    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            // New child of the rebased target commits.
647            if let Some(new_child_parents) = new_children_parents.get(commit_id) {
648                new_child_parents.clone()
649            }
650            // Commit is in the target set.
651            else if target_commit_ids.contains(commit_id) {
652                // If the commit is a root of the target set, it should be rebased onto the new destination.
653                if target_roots.contains(commit_id) {
654                    new_parent_ids.clone()
655                }
656                // Otherwise:
657                // 1. Keep parents which are within the target set.
658                // 2. Replace parents which are outside the target set but are part of the
659                //    connected target set with their ancestor commits which are in the target
660                //    set.
661                // 3. Keep other parents outside the target set if they are not descendants of the
662                //    new children of the target set.
663                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            // Commits outside the target set should have references to commits inside the set
680            // replaced.
681            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    // Always keep empty commits when rebasing descendants.
708    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    /// Map of original commit ID to newly duplicated commit.
756    pub duplicated_commits: IndexMap<CommitId, Commit>,
757    /// The number of descendant commits which were rebased onto the duplicated
758    /// commits.
759    pub num_rebased: u32,
760}
761
762/// Duplicates the given `target_commits` onto a new location in the graph.
763///
764/// The roots of `target_commits` are duplicated on top of the new
765/// `parent_commit_ids`, whilst other commits in `target_commits` are duplicated
766/// on top of the newly duplicated commits in the target set. If
767/// `children_commit_ids` is not empty, the `children_commit_ids` will be
768/// rebased onto the heads of the duplicated target commits.
769///
770/// If `target_descriptions` is not empty, it will be consulted to retrieve the
771/// new descriptions of the target commits, falling back to the original if
772/// the map does not contain an entry for a given commit.
773///
774/// This assumes that commits in `children_commit_ids` can be rewritten. There
775/// should also be no cycles in the resulting graph, i.e. `children_commit_ids`
776/// should not be ancestors of `parent_commit_ids`. Commits in `target_commits`
777/// should be in reverse topological order (children before parents).
778pub 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    // Commits in the target set should only have other commits in the set as
805    // parents, except the roots of the set, which persist their original
806    // parents.
807    // If a commit in the target set has a parent which is not in the set, but has
808    // an ancestor which is in the set, then the commit will have that ancestor
809    // as a parent instead.
810    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    // Compute the roots of `target_commits`.
818    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    // Compute the heads of the target set, which will be used as the parents of
825    // the children commits.
826    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    // Topological order ensures that any parents of the original commit are
833    // either not in `target_commits` or were already duplicated.
834    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                // Replace parent IDs with their new IDs if they were duplicated.
844                .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    // Replace the original commit IDs in `target_head_ids` with the duplicated
862    // commit IDs.
863    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    // Rebase new children onto the target heads.
873    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 the original parents of the new children are the new parents of
879                // `target_head_ids`, replace them with `target_head_ids` since we are
880                // "inserting" the target commits in between the new parents and the new
881                // children.
882                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            // If not already present, add `target_head_ids` as parents of the new child
889            // commit.
890            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
904/// Duplicates the given `target_commits` onto their original parents or other
905/// duplicated commits.
906///
907/// Commits in `target_commits` should be in reverse topological order (children
908/// before parents).
909///
910/// If `target_descriptions` is not empty, it will be consulted to retrieve the
911/// new descriptions of the target commits, falling back to the original if
912/// the map does not contain an entry for a given commit.
913pub 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    // Topological order ensures that any parents of the original commit are
925    // either not in `target_commits` or were already duplicated.
926    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
954/// Computes the internal parents of all commits in a connected commit graph,
955/// allowing only commits in the target set as parents.
956///
957/// The parents of each commit are identical to the ones found using a preorder
958/// DFS of the node's ancestors, starting from the node itself, and avoiding
959/// traversing an edge if the parent is in the target set. `graph_commits`
960/// should be in reverse topological order.
961fn 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        // The roots of the set will not have any parents found in `internal_parents`,
968        // and will be stored as an empty vector.
969        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
982/// Computes the heads of commits in the target set, given the list of
983/// `target_commit_ids` and a connected graph of commits.
984///
985/// `connected_target_commits` should be in reverse topological order (children
986/// before parents).
987fn 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    /// Returns true if the selection contains all changes in the commit.
1016    pub fn is_full_selection(&self) -> bool {
1017        &self.selected_tree.id() == self.commit.tree_id()
1018    }
1019
1020    /// Returns true if the selection matches the parent tree (contains no
1021    /// changes from the commit).
1022    ///
1023    /// Both `is_full_selection()` and `is_empty_selection()`
1024    /// can be true if the commit is itself empty.
1025    pub fn is_empty_selection(&self) -> bool {
1026        self.selected_tree.id() == self.parent_tree.id()
1027    }
1028}
1029
1030/// Resulting commit builder and stats to be returned by [`squash_commits()`].
1031#[must_use]
1032pub struct SquashedCommit<'repo> {
1033    /// New destination commit will be created by this builder.
1034    pub commit_builder: CommitBuilder<'repo>,
1035    /// List of abandoned source commits.
1036    pub abandoned_commits: Vec<Commit>,
1037}
1038
1039/// Squash `sources` into `destination` and return a [`SquashedCommit`] for the
1040/// resulting commit. Caller is responsible for setting the description and
1041/// finishing the commit.
1042pub 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            // Nothing selected from this commit. If it's abandoned (i.e. already empty), we
1057            // still include it so `jj squash` can be used for abandoning an empty commit in
1058            // the middle of a stack.
1059            continue;
1060        }
1061
1062        // TODO: Do we want to optimize the case of moving to the parent commit (`jj
1063        // squash -r`)? The source tree will be unchanged in that case.
1064        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            // Apply the reverse of the selected changes onto the source
1082            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        // If we're moving changes to a descendant, first rebase descendants onto the
1096        // rewritten sources. Otherwise it will likely already have the content
1097        // changes we're moving, so applying them will have no effect and the
1098        // changes will disappear.
1099        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    // Apply the selected changes onto the destination
1111    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}