| 1 |
// Copyright 2024 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 |
use std::collections::HashMap; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCompleter; |
| 18 |
use futures::TryStreamExt as _; |
| 19 |
use indexmap::IndexSet; |
| 20 |
use itertools::Itertools as _; |
| 21 |
use jj_lib::backend::CommitId; |
| 22 |
use jj_lib::commit::Commit; |
| 23 |
use jj_lib::commit::CommitIteratorExt as _; |
| 24 |
use tracing::instrument; |
| 25 |
|
| 26 |
use crate::cli_util::CommandHelper; |
| 27 |
use crate::cli_util::RevisionArg; |
| 28 |
use crate::command_error::CommandError; |
| 29 |
use crate::complete; |
| 30 |
use crate::ui::Ui; |
| 31 |
|
| 32 |
/// Parallelize revisions by making them siblings |
| 33 |
/// |
| 34 |
/// Running `jj parallelize 1::2` will transform the history like this: |
| 35 |
/// ```text |
| 36 |
/// 3 |
| 37 |
/// | 3 |
| 38 |
/// 2 / \ |
| 39 |
/// | -> 1 2 |
| 40 |
/// 1 \ / |
| 41 |
/// | 0 |
| 42 |
/// 0 |
| 43 |
/// ``` |
| 44 |
/// |
| 45 |
/// The command effectively says "these revisions are actually independent", |
| 46 |
/// meaning that they should no longer be ancestors/descendants of each other. |
| 47 |
/// However, revisions outside the set that were previously ancestors of a |
| 48 |
/// revision in the set will remain ancestors of it. For example, revision 0 |
| 49 |
/// above remains an ancestor of both 1 and 2. Similarly, |
| 50 |
/// revisions outside the set that were previously descendants of a revision |
| 51 |
/// in the set will remain descendants of it. For example, revision 3 above |
| 52 |
/// remains a descendant of both 1 and 2. |
| 53 |
/// |
| 54 |
/// Therefore, `jj parallelize '1 | 3'` is a no-op. That's because 2, which is |
| 55 |
/// not in the target set, was a descendant of 1 before, so it remains a |
| 56 |
/// descendant, and it was an ancestor of 3 before, so it remains an ancestor. |
| 57 |
#[derive(clap::Args, Clone, Debug)] |
| 58 |
#[command(verbatim_doc_comment)] |
| 59 |
pub(crate) struct ParallelizeArgs { |
| 60 |
/// The revisions to parallelize [aliases: -r] |
| 61 |
#[arg(value_name = "REVSETS")] |
| 62 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 63 |
revisions_pos: Vec<RevisionArg>, |
| 64 |
|
| 65 |
#[arg(short = 'r', hide = true, value_name = "REVSETS")] |
| 66 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 67 |
revisions_opt: Vec<RevisionArg>, |
| 68 |
} |
| 69 |
|
| 70 |
#[instrument(skip_all)] |
| 71 |
pub(crate) async fn cmd_parallelize( |
| 72 |
ui: &mut Ui, |
| 73 |
command: &CommandHelper, |
| 74 |
args: &ParallelizeArgs, |
| 75 |
) -> Result<(), CommandError> { |
| 76 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 77 |
// The target commits are the commits being parallelized. They are ordered |
| 78 |
// here with children before parents. |
| 79 |
let target_commits: Vec<Commit> = workspace_command |
| 80 |
.parse_union_revsets(ui, &[&*args.revisions_pos, &*args.revisions_opt].concat())? |
| 81 |
.evaluate_to_commits()? |
| 82 |
.try_collect() |
| 83 |
.await?; |
| 84 |
|
| 85 |
// New parents for commits in the target set. Since commits in the set are now |
| 86 |
// supposed to be independent, they inherit the parent's non-target parents, |
| 87 |
// recursively. |
| 88 |
let mut new_target_parents: HashMap<CommitId, Vec<CommitId>> = HashMap::new(); |
| 89 |
let mut needs_rewrite = Vec::new(); |
| 90 |
for commit in target_commits.iter().rev() { |
| 91 |
let mut new_parents = vec![]; |
| 92 |
for old_parent in commit.parent_ids() { |
| 93 |
if let Some(grand_parents) = new_target_parents.get(old_parent) { |
| 94 |
new_parents.extend_from_slice(grand_parents); |
| 95 |
needs_rewrite.push(commit.id()); |
| 96 |
} else { |
| 97 |
new_parents.push(old_parent.clone()); |
| 98 |
} |
| 99 |
} |
| 100 |
new_target_parents.insert(commit.id().clone(), new_parents); |
| 101 |
} |
| 102 |
|
| 103 |
workspace_command.check_rewritable(needs_rewrite).await?; |
| 104 |
let mut tx = workspace_command.start_transaction(); |
| 105 |
|
| 106 |
// If a commit outside the target set has a commit in the target set as parent, |
| 107 |
// then - after the transformation - it should also have that commit's |
| 108 |
// parents as direct parents, if those commits are also in the target set. |
| 109 |
let mut new_child_parents: HashMap<CommitId, IndexSet<CommitId>> = HashMap::new(); |
| 110 |
for commit in target_commits.iter().rev() { |
| 111 |
let mut new_parents = IndexSet::new(); |
| 112 |
for old_parent in commit.parent_ids() { |
| 113 |
if let Some(parents) = new_child_parents.get(old_parent) { |
| 114 |
new_parents.extend(parents.iter().cloned()); |
| 115 |
} |
| 116 |
} |
| 117 |
new_parents.insert(commit.id().clone()); |
| 118 |
new_child_parents.insert(commit.id().clone(), new_parents); |
| 119 |
} |
| 120 |
|
| 121 |
tx.repo_mut() |
| 122 |
.transform_descendants( |
| 123 |
target_commits.iter().ids().cloned().collect_vec(), |
| 124 |
async |mut rewriter| { |
| 125 |
// Commits in the target set do not depend on each other but they still depend |
| 126 |
// on other parents |
| 127 |
if let Some(new_parents) = new_target_parents.get(rewriter.old_commit().id()) { |
| 128 |
rewriter.set_new_rewritten_parents(new_parents); |
| 129 |
} else if rewriter |
| 130 |
.old_commit() |
| 131 |
.parent_ids() |
| 132 |
.iter() |
| 133 |
.any(|id| new_child_parents.contains_key(id)) |
| 134 |
{ |
| 135 |
let mut new_parents = vec![]; |
| 136 |
for parent in rewriter.old_commit().parent_ids() { |
| 137 |
if let Some(parents) = new_child_parents.get(parent) { |
| 138 |
new_parents.extend(parents.iter().cloned()); |
| 139 |
} else { |
| 140 |
new_parents.push(parent.clone()); |
| 141 |
} |
| 142 |
} |
| 143 |
rewriter.set_new_rewritten_parents(&new_parents); |
| 144 |
} |
| 145 |
if rewriter.parents_changed() { |
| 146 |
let builder = rewriter.rebase().await?; |
| 147 |
builder.write().await?; |
| 148 |
} |
| 149 |
Ok(()) |
| 150 |
}, |
| 151 |
) |
| 152 |
.await?; |
| 153 |
|
| 154 |
tx.finish(ui, format!("parallelize {} commits", target_commits.len())) |
| 155 |
.await |
| 156 |
} |