From 6af13ea89bcc5fb143063c764a7936ba288273a2 Mon Sep 17 00:00:00 2001 From: Antoine Cezar Date: Sun, 29 Oct 2023 18:43:37 +0100 Subject: [PATCH] commands: move new code to new.rs --- cli/src/commands/mod.rs | 166 +++--------------------------------- cli/src/commands/new.rs | 184 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+), 156 deletions(-) create mode 100644 cli/src/commands/new.rs diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index b29107bcd..de21d634e 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -33,6 +33,7 @@ mod git; mod init; mod interdiff; mod log; +mod new; mod operation; use std::collections::{BTreeMap, HashSet}; @@ -114,9 +115,9 @@ enum Commands { /// /// This is the same as `jj new`, except that it requires at least two /// arguments. - Merge(NewArgs), + Merge(new::NewArgs), Move(MoveArgs), - New(NewArgs), + New(new::NewArgs), Next(NextArgs), Obslog(ObslogArgs), #[command(subcommand)] @@ -212,37 +213,6 @@ struct ObslogArgs { diff_format: DiffFormatArgs, } -/// Create a new, empty change and edit it in the working copy -/// -/// Note that you can create a merge commit by specifying multiple revisions as -/// argument. For example, `jj new main @` will create a new commit with the -/// `main` branch and the working copy as parents. -/// -/// For more information, see -/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md. -#[derive(clap::Args, Clone, Debug)] -#[command(group(ArgGroup::new("order").args(&["insert_after", "insert_before"])))] -struct NewArgs { - /// Parent(s) of the new change - #[arg(default_value = "@")] - revisions: Vec, - /// Ignored (but lets you pass `-r` for consistency with other commands) - #[arg(short = 'r', hide = true)] - unused_revision: bool, - /// The change description to use - #[arg(long = "message", short, value_name = "MESSAGE")] - message_paragraphs: Vec, - /// Deprecated. Please prefix the revset with `all:` instead. - #[arg(long, short = 'L', hide = true)] - allow_large_revsets: bool, - /// Insert the new change between the target commit(s) and their children - #[arg(long, short = 'A', visible_alias = "after")] - insert_after: bool, - /// Insert the new change between the target commit(s) and their parents - #[arg(long, short = 'B', visible_alias = "before")] - insert_before: bool, -} - /// Move the current working copy commit to the next child revision in the /// repository. /// @@ -1242,126 +1212,6 @@ fn resolve_destination_revs( } } -#[instrument(skip_all)] -fn cmd_new(ui: &mut Ui, command: &CommandHelper, args: &NewArgs) -> Result<(), CommandError> { - if args.allow_large_revsets { - return Err(user_error( - "--allow-large-revsets has been deprecated. -Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.", - )); - } - let mut workspace_command = command.workspace_helper(ui)?; - assert!( - !args.revisions.is_empty(), - "expected a non-empty list from clap" - ); - let target_commits = resolve_destination_revs(&workspace_command, ui, &args.revisions)? - .into_iter() - .collect_vec(); - let target_ids = target_commits.iter().map(|c| c.id().clone()).collect_vec(); - let mut tx = workspace_command.start_transaction("new empty commit"); - let mut num_rebased = 0; - let new_commit; - if args.insert_before { - // Instead of having the new commit as a child of the changes given on the - // command line, add it between the changes' parents and the changes. - // The parents of the new commit will be the parents of the target commits - // which are not descendants of other target commits. - let root_commit = tx.repo().store().root_commit(); - if target_ids.contains(root_commit.id()) { - return Err(user_error("Cannot insert a commit before the root commit")); - } - let new_children = RevsetExpression::commits(target_ids.clone()); - let new_parents = new_children.parents(); - if let Some(commit_id) = new_children - .dag_range_to(&new_parents) - .resolve(tx.repo())? - .evaluate(tx.repo())? - .iter() - .next() - { - return Err(user_error(format!( - "Refusing to create a loop: commit {} would be both an ancestor and a descendant \ - of the new commit", - short_commit_hash(&commit_id), - ))); - } - let mut new_parents_commits: Vec = new_parents - .resolve(tx.repo())? - .evaluate(tx.repo())? - .iter() - .commits(tx.repo().store()) - .try_collect()?; - // The git backend does not support creating merge commits involving the root - // commit. - if new_parents_commits.len() > 1 { - new_parents_commits.retain(|c| c != &root_commit); - } - let merged_tree = merge_commit_trees(tx.repo(), &new_parents_commits)?; - let new_parents_commit_id = new_parents_commits.iter().map(|c| c.id().clone()).collect(); - new_commit = tx - .mut_repo() - .new_commit(command.settings(), new_parents_commit_id, merged_tree.id()) - .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) - .write()?; - num_rebased = target_ids.len(); - for child_commit in target_commits { - rebase_commit( - command.settings(), - tx.mut_repo(), - &child_commit, - &[new_commit.clone()], - )?; - } - } else { - let merged_tree = merge_commit_trees(tx.repo(), &target_commits)?; - new_commit = tx - .mut_repo() - .new_commit(command.settings(), target_ids.clone(), merged_tree.id()) - .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) - .write()?; - if args.insert_after { - // Each child of the targets will be rebased: its set of parents will be updated - // so that the targets are replaced by the new commit. - let old_parents = RevsetExpression::commits(target_ids); - // Exclude children that are ancestors of the new commit - let to_rebase = old_parents.children().minus(&old_parents.ancestors()); - let commits_to_rebase: Vec = to_rebase - .resolve(tx.base_repo().as_ref())? - .evaluate(tx.base_repo().as_ref())? - .iter() - .commits(tx.base_repo().store()) - .try_collect()?; - num_rebased = commits_to_rebase.len(); - for child_commit in commits_to_rebase { - let commit_parents = - RevsetExpression::commits(child_commit.parent_ids().to_owned()); - let new_parents = commit_parents.minus(&old_parents); - let mut new_parent_commits: Vec = new_parents - .resolve(tx.base_repo().as_ref())? - .evaluate(tx.base_repo().as_ref())? - .iter() - .commits(tx.base_repo().store()) - .try_collect()?; - new_parent_commits.push(new_commit.clone()); - rebase_commit( - command.settings(), - tx.mut_repo(), - &child_commit, - &new_parent_commits, - )?; - } - } - } - num_rebased += tx.mut_repo().rebase_descendants(command.settings())?; - if num_rebased > 0 { - writeln!(ui.stderr(), "Rebased {num_rebased} descendant commits")?; - } - tx.edit(&new_commit).unwrap(); - tx.finish(ui)?; - Ok(()) -} - fn cmd_next(ui: &mut Ui, command: &CommandHelper, args: &NextArgs) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; let edit = args.edit; @@ -2163,13 +2013,17 @@ don't make any changes, then the operation will be aborted. } #[instrument(skip_all)] -fn cmd_merge(ui: &mut Ui, command: &CommandHelper, args: &NewArgs) -> Result<(), CommandError> { +fn cmd_merge( + ui: &mut Ui, + command: &CommandHelper, + args: &new::NewArgs, +) -> Result<(), CommandError> { if args.revisions.len() < 2 { return Err(CommandError::CliError(String::from( "Merge requires at least two revisions", ))); } - cmd_new(ui, command, args) + new::cmd_new(ui, command, args) } // TODO: Move to run.rs @@ -2769,7 +2623,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Commands::Edit(sub_args) => edit::cmd_edit(ui, command_helper, sub_args), Commands::Next(sub_args) => cmd_next(ui, command_helper, sub_args), Commands::Prev(sub_args) => cmd_prev(ui, command_helper, sub_args), - Commands::New(sub_args) => cmd_new(ui, command_helper, sub_args), + Commands::New(sub_args) => new::cmd_new(ui, command_helper, sub_args), Commands::Move(sub_args) => cmd_move(ui, command_helper, sub_args), Commands::Squash(sub_args) => cmd_squash(ui, command_helper, sub_args), Commands::Unsquash(sub_args) => cmd_unsquash(ui, command_helper, sub_args), diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs new file mode 100644 index 000000000..ab72eac4f --- /dev/null +++ b/cli/src/commands/new.rs @@ -0,0 +1,184 @@ +// Copyright 2020 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 clap::ArgGroup; +use itertools::Itertools; +use jj_lib::commit::Commit; +use jj_lib::repo::Repo; +use jj_lib::revset::{RevsetExpression, RevsetIteratorExt}; +use jj_lib::rewrite::{merge_commit_trees, rebase_commit}; +use tracing::instrument; + +use crate::cli_util::{ + self, short_commit_hash, user_error, CommandError, CommandHelper, RevisionArg, +}; +use crate::commands::resolve_destination_revs; +use crate::ui::Ui; + +/// Create a new, empty change and edit it in the working copy +/// +/// Note that you can create a merge commit by specifying multiple revisions as +/// argument. For example, `jj new main @` will create a new commit with the +/// `main` branch and the working copy as parents. +/// +/// For more information, see +/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md. +#[derive(clap::Args, Clone, Debug)] +#[command(group(ArgGroup::new("order").args(&["insert_after", "insert_before"])))] +pub(crate) struct NewArgs { + /// Parent(s) of the new change + #[arg(default_value = "@")] + pub(crate) revisions: Vec, + /// Ignored (but lets you pass `-r` for consistency with other commands) + #[arg(short = 'r', hide = true)] + unused_revision: bool, + /// The change description to use + #[arg(long = "message", short, value_name = "MESSAGE")] + message_paragraphs: Vec, + /// Deprecated. Please prefix the revset with `all:` instead. + #[arg(long, short = 'L', hide = true)] + allow_large_revsets: bool, + /// Insert the new change between the target commit(s) and their children + #[arg(long, short = 'A', visible_alias = "after")] + insert_after: bool, + /// Insert the new change between the target commit(s) and their parents + #[arg(long, short = 'B', visible_alias = "before")] + insert_before: bool, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_new( + ui: &mut Ui, + command: &CommandHelper, + args: &NewArgs, +) -> Result<(), CommandError> { + if args.allow_large_revsets { + return Err(user_error( + "--allow-large-revsets has been deprecated. +Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.", + )); + } + let mut workspace_command = command.workspace_helper(ui)?; + assert!( + !args.revisions.is_empty(), + "expected a non-empty list from clap" + ); + let target_commits = resolve_destination_revs(&workspace_command, ui, &args.revisions)? + .into_iter() + .collect_vec(); + let target_ids = target_commits.iter().map(|c| c.id().clone()).collect_vec(); + let mut tx = workspace_command.start_transaction("new empty commit"); + let mut num_rebased = 0; + let new_commit; + if args.insert_before { + // Instead of having the new commit as a child of the changes given on the + // command line, add it between the changes' parents and the changes. + // The parents of the new commit will be the parents of the target commits + // which are not descendants of other target commits. + let root_commit = tx.repo().store().root_commit(); + if target_ids.contains(root_commit.id()) { + return Err(user_error("Cannot insert a commit before the root commit")); + } + let new_children = RevsetExpression::commits(target_ids.clone()); + let new_parents = new_children.parents(); + if let Some(commit_id) = new_children + .dag_range_to(&new_parents) + .resolve(tx.repo())? + .evaluate(tx.repo())? + .iter() + .next() + { + return Err(user_error(format!( + "Refusing to create a loop: commit {} would be both an ancestor and a descendant \ + of the new commit", + short_commit_hash(&commit_id), + ))); + } + let mut new_parents_commits: Vec = new_parents + .resolve(tx.repo())? + .evaluate(tx.repo())? + .iter() + .commits(tx.repo().store()) + .try_collect()?; + // The git backend does not support creating merge commits involving the root + // commit. + if new_parents_commits.len() > 1 { + new_parents_commits.retain(|c| c != &root_commit); + } + let merged_tree = merge_commit_trees(tx.repo(), &new_parents_commits)?; + let new_parents_commit_id = new_parents_commits.iter().map(|c| c.id().clone()).collect(); + new_commit = tx + .mut_repo() + .new_commit(command.settings(), new_parents_commit_id, merged_tree.id()) + .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) + .write()?; + num_rebased = target_ids.len(); + for child_commit in target_commits { + rebase_commit( + command.settings(), + tx.mut_repo(), + &child_commit, + &[new_commit.clone()], + )?; + } + } else { + let merged_tree = merge_commit_trees(tx.repo(), &target_commits)?; + new_commit = tx + .mut_repo() + .new_commit(command.settings(), target_ids.clone(), merged_tree.id()) + .set_description(cli_util::join_message_paragraphs(&args.message_paragraphs)) + .write()?; + if args.insert_after { + // Each child of the targets will be rebased: its set of parents will be updated + // so that the targets are replaced by the new commit. + let old_parents = RevsetExpression::commits(target_ids); + // Exclude children that are ancestors of the new commit + let to_rebase = old_parents.children().minus(&old_parents.ancestors()); + let commits_to_rebase: Vec = to_rebase + .resolve(tx.base_repo().as_ref())? + .evaluate(tx.base_repo().as_ref())? + .iter() + .commits(tx.base_repo().store()) + .try_collect()?; + num_rebased = commits_to_rebase.len(); + for child_commit in commits_to_rebase { + let commit_parents = + RevsetExpression::commits(child_commit.parent_ids().to_owned()); + let new_parents = commit_parents.minus(&old_parents); + let mut new_parent_commits: Vec = new_parents + .resolve(tx.base_repo().as_ref())? + .evaluate(tx.base_repo().as_ref())? + .iter() + .commits(tx.base_repo().store()) + .try_collect()?; + new_parent_commits.push(new_commit.clone()); + rebase_commit( + command.settings(), + tx.mut_repo(), + &child_commit, + &new_parent_commits, + )?; + } + } + } + num_rebased += tx.mut_repo().rebase_descendants(command.settings())?; + if num_rebased > 0 { + writeln!(ui.stderr(), "Rebased {num_rebased} descendant commits")?; + } + tx.edit(&new_commit).unwrap(); + tx.finish(ui)?; + Ok(()) +}