-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4de3f5
commit 7c8b87c
Showing
7 changed files
with
568 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
// Copyright 2024 The Jujutsu Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::io::Write; | ||
use std::rc::Rc; | ||
|
||
use indexmap::IndexSet; | ||
use itertools::Itertools; | ||
use jj_lib::backend::CommitId; | ||
use jj_lib::commit::Commit; | ||
use jj_lib::repo::Repo; | ||
use jj_lib::revset::{RevsetExpression, RevsetIteratorExt}; | ||
use tracing::instrument; | ||
|
||
use crate::cli_util::{short_commit_hash, CommandHelper, RevisionArg}; | ||
use crate::command_error::{user_error, CommandError}; | ||
use crate::commands::rebase::rebase_descendants; | ||
use crate::ui::Ui; | ||
|
||
/// Parallelize revisions by making them siblings | ||
/// | ||
/// The set of target commits being parallelized must have a single head and | ||
/// root commit (not to be confused with the repo root), otherwise the command | ||
/// will fail. Each of the target commits is rebased onto the parents of the | ||
/// root commit. The children of the head commit are rebased onto the target | ||
/// commits. | ||
/// | ||
/// Example: | ||
/// ```text | ||
/// Running jj parallelize 1::2 will transform the graph like this: | ||
/// 3 | ||
/// | 3 | ||
/// 2 / \ | ||
/// | -> 1 2 | ||
/// 1 \ / | ||
/// | 0 | ||
/// 0 | ||
/// ``` | ||
#[derive(clap::Args, Clone, Debug)] | ||
#[command(verbatim_doc_comment)] | ||
pub(crate) struct ParallelizeArgs { | ||
/// Revisions to parallelize | ||
revisions: Vec<RevisionArg>, | ||
} | ||
|
||
#[instrument(skip_all)] | ||
pub(crate) fn cmd_parallelize( | ||
ui: &mut Ui, | ||
command: &CommandHelper, | ||
args: &ParallelizeArgs, | ||
) -> Result<(), CommandError> { | ||
let mut workspace_command = command.workspace_helper(ui)?; | ||
// The target commits are the commits being parallelized. Ordered with parents | ||
// before children. | ||
let target_commits: Vec<CommitId> = workspace_command | ||
.parse_union_revsets(&args.revisions)? | ||
.evaluate_to_commit_ids()? | ||
.collect(); | ||
if target_commits.len() < 2 { | ||
writeln!(ui.status(), "Nothing changed.")?; | ||
return Ok(()); | ||
} | ||
workspace_command.check_rewritable(target_commits.iter())?; | ||
let mut tx = workspace_command.start_transaction(); | ||
let target_revset = RevsetExpression::commits(target_commits.clone()); | ||
let target_head = get_head(&target_revset, tx.repo())?; | ||
let target_root = get_root(&target_revset, tx.repo())?; | ||
let connected_length = RevsetExpression::commits(vec![target_head, target_root.clone()]) | ||
.connected() | ||
.evaluate_programmatic(tx.repo())? | ||
.iter() | ||
.count(); | ||
if connected_length != target_commits.len() { | ||
return Err(user_error( | ||
"Cannot parallelize since the target revisions are not connected.", | ||
)); | ||
} | ||
|
||
// Rebase the non-target children of each target commit onto its new | ||
// parents. A child which had a target commit as an ancestor before | ||
// parallelize ran will have the target commit as a parent afterward. | ||
for (i, target_commit) in target_commits.iter().enumerate() { | ||
// These parents are shared by all children of the target commit and | ||
// include the target commit itself plus any of its ancestors which are | ||
// being parallelized. The commits in `target_commits` form a linear | ||
// chain ordered with parents before children. | ||
let common_parents: IndexSet<CommitId> = target_commits[i..].iter().cloned().collect(); | ||
// Children of the target commit, excluding other target commits. | ||
let children: Vec<Commit> = RevsetExpression::commit(target_commit.clone()) | ||
.children() | ||
.minus(&target_revset) | ||
.evaluate_programmatic(tx.repo())? | ||
.iter() | ||
.commits(tx.repo().store()) | ||
.try_collect()?; | ||
for child in children { | ||
let mut new_parents = common_parents.clone(); | ||
new_parents.extend(child.parent_ids().iter().cloned()); | ||
let parents: Vec<Commit> = new_parents | ||
.iter() | ||
.map(|c| tx.repo().store().get_commit(c)) | ||
.try_collect()?; | ||
rebase_descendants( | ||
&mut tx, | ||
command.settings(), | ||
&parents, | ||
&[child], | ||
Default::default(), | ||
)?; | ||
} | ||
} | ||
|
||
// Rebase the target commits onto the parents of the root commit. | ||
let new_parents = tx.repo().store().get_commit(&target_root)?.parents(); | ||
// We need to reverse the iterator so that when we rebase the target commits | ||
// they will appear in the same relative order in `jj log` that they were in | ||
// before being parallelized. | ||
let target_commits: Vec<Commit> = target_commits | ||
.iter() | ||
// Children before parents. | ||
.rev() | ||
.map(|c| tx.repo().store().get_commit(c)) | ||
.try_collect()?; | ||
rebase_descendants( | ||
&mut tx, | ||
command.settings(), | ||
&new_parents, | ||
&target_commits, | ||
Default::default(), | ||
)?; | ||
|
||
tx.finish(ui, format!("parallelized {} commits", target_commits.len())) | ||
} | ||
|
||
// Returns the head of the target revset or an error if the revset has multiple | ||
// heads. | ||
fn get_head( | ||
target_revset: &Rc<RevsetExpression>, | ||
repo: &dyn Repo, | ||
) -> Result<CommitId, CommandError> { | ||
let mut target_heads: Vec<CommitId> = target_revset | ||
.heads() | ||
.evaluate_programmatic(repo)? | ||
.iter() | ||
.collect(); | ||
if target_heads.len() != 1 { | ||
let heads = target_heads.iter().map(short_commit_hash).join(", "); | ||
return Err(user_error(format!( | ||
"Cannot parallelize a set of revisions with multiple heads. Heads: {heads}" | ||
))); | ||
} | ||
Ok(target_heads.pop().unwrap()) | ||
} | ||
|
||
// Returns the root of the target revset or an error if the revset has multiple | ||
// roots. | ||
fn get_root( | ||
target_revset: &Rc<RevsetExpression>, | ||
repo: &dyn Repo, | ||
) -> Result<CommitId, CommandError> { | ||
let mut target_roots: Vec<CommitId> = target_revset | ||
.roots() | ||
.evaluate_programmatic(repo)? | ||
.iter() | ||
.collect(); | ||
if target_roots.len() != 1 { | ||
let roots = target_roots.iter().map(short_commit_hash).join(", "); | ||
return Err(user_error(format!( | ||
"Cannot parallelize a set of revisions with multiple roots. Roots: {roots}" | ||
))); | ||
} | ||
Ok(target_roots.pop().unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.