-
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
4c5db0a
commit 0e5e146
Showing
5 changed files
with
427 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// 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 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 usage: | ||
/// ```text | ||
/// jj log | ||
/// ◉ 3 | ||
/// ◉ 2 | ||
/// ◉ 1 | ||
/// ◉ | ||
/// jj parallelize 1::2 | ||
/// jj log | ||
/// ◉ 3 | ||
/// ├─╮ | ||
/// │ ◉ 2 | ||
/// ◉ │ 1 | ||
/// ├─╯ | ||
/// ◉ | ||
/// ``` | ||
#[derive(clap::Args, Clone, Debug)] | ||
#[command(verbatim_doc_comment)] | ||
pub(crate) struct ParallelizeArgs { | ||
// #[arg(long, short)] | ||
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)?; | ||
// Parse and validate the input revset(s). | ||
// Find the parents of each commit. The parent(s) which are ancestors of | ||
// each revision are the new parents of the input revisions. | ||
// Find the children of each commit. The children which are descendants | ||
// of each revision are the new children of the input revisions. | ||
// If one or both of these sets cannot be found, then parallelize cannot | ||
// proceed. | ||
// let target_commits = workspace_command.parse_union_revsets(&args.revisions)?. | ||
let mut tx = workspace_command.start_transaction(); | ||
let target_revset = tx | ||
.base_workspace_helper() | ||
.parse_union_revsets(&args.revisions)?; | ||
let target_commits: Vec<Commit> = tx | ||
.base_workspace_helper() | ||
.evaluate_revset(target_revset.clone())? | ||
.iter() | ||
// We want parents before children, so we need to reverse the order. | ||
.reversed() | ||
.commits(tx.base_repo().store()) | ||
.try_collect::<_, Vec<Commit>, _>()?; | ||
if target_commits.len() < 2 { | ||
return Ok(writeln!(ui.stderr(), "Nothing to do.")?); | ||
} | ||
let target_heads: Vec<CommitId> = tx | ||
.base_workspace_helper() | ||
.evaluate_revset(target_revset.clone().heads())? | ||
.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}" | ||
))); | ||
} | ||
let target_roots: Vec<CommitId> = tx | ||
.base_workspace_helper() | ||
.evaluate_revset(target_revset.roots())? | ||
.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}" | ||
))); | ||
} | ||
|
||
// Rebase the children of the head commit onto the target commits. | ||
let new_children: Vec<Commit> = RevsetExpression::commit(target_heads[0].clone()) | ||
.children() | ||
.evaluate_programmatic(tx.base_repo().as_ref())? | ||
.iter() | ||
.commits(tx.base_repo().store()) | ||
.try_collect()?; | ||
for child in new_children { | ||
// The ordering here is intentional. We iterate over the target commits first so | ||
// that when `jj log` prints the graph they will be printed in their original | ||
// order. After that, we add any existing parents which aren't being | ||
// parallelized. | ||
let new_parents_for_child: Vec<Commit> = target_commits | ||
.iter() | ||
.map(|c| c.id().clone()) | ||
.chain(child.parent_ids().iter().cloned()) | ||
.collect::<IndexSet<CommitId>>() | ||
.iter() | ||
.map(|c| tx.mut_repo().store().get_commit(c)) | ||
.collect::<Result<Vec<Commit>, _>>()?; | ||
rebase_descendants( | ||
&mut tx, | ||
command.settings(), | ||
&new_parents_for_child, | ||
&[child], | ||
Default::default(), | ||
)?; | ||
} | ||
|
||
// Rebase the target commits onto the parents of the root commit. | ||
let new_parents: Vec<Commit> = RevsetExpression::commit(target_roots[0].clone()) | ||
.parents() | ||
.evaluate_programmatic(tx.base_repo().as_ref())? | ||
.iter() | ||
.commits(tx.base_repo().store()) | ||
.try_collect()?; | ||
rebase_descendants( | ||
&mut tx, | ||
command.settings(), | ||
&new_parents, | ||
&target_commits, | ||
Default::default(), | ||
)?; | ||
|
||
tx.finish( | ||
ui, | ||
format!("Parallelized {} commits.", target_commits.len()), | ||
) | ||
} |
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.