diff --git a/cli/src/commands/diffedit.rs b/cli/src/commands/diffedit.rs new file mode 100644 index 000000000..a86152661 --- /dev/null +++ b/cli/src/commands/diffedit.rs @@ -0,0 +1,116 @@ +// 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 jj_lib::backend::ObjectId; +use jj_lib::matchers::EverythingMatcher; +use jj_lib::rewrite::merge_commit_trees; +use tracing::instrument; + +use crate::cli_util::{CommandError, CommandHelper, RevisionArg}; +use crate::ui::Ui; + +/// Touch up the content changes in a revision with a diff editor +/// +/// With the `-r` option, which is the default, starts a diff editor (`meld` by +/// default) on the changes in the revision. +/// +/// With the `--from` and/or `--to` options, starts a diff editor comparing the +/// "from" revision to the "to" revision. +/// +/// Edit the right side of the diff until it looks the way you want. Once you +/// close the editor, the revision specified with `-r` or `--to` will be +/// updated. Descendants will be rebased on top as usual, which may result in +/// conflicts. +/// +/// See `jj restore` if you want to move entire files from one revision to +/// another. See `jj squash -i` or `jj unsquash -i` if you instead want to move +/// changes into or out of the parent revision. +#[derive(clap::Args, Clone, Debug)] +pub(crate) struct DiffeditArgs { + /// The revision to touch up. Defaults to @ if neither --to nor --from are + /// specified. + #[arg(long, short)] + revision: Option, + /// Show changes from this revision. Defaults to @ if --to is specified. + #[arg(long, conflicts_with = "revision")] + from: Option, + /// Edit changes in this revision. Defaults to @ if --from is specified. + #[arg(long, conflicts_with = "revision")] + to: Option, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_diffedit( + ui: &mut Ui, + command: &CommandHelper, + args: &DiffeditArgs, +) -> Result<(), CommandError> { + let mut workspace_command = command.workspace_helper(ui)?; + + let (target_commit, base_commits, diff_description); + if args.from.is_some() || args.to.is_some() { + target_commit = + workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?; + base_commits = + vec![workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?]; + diff_description = format!( + "The diff initially shows the commit's changes relative to:\n{}", + workspace_command.format_commit_summary(&base_commits[0]) + ); + } else { + target_commit = + workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?; + base_commits = target_commit.parents(); + diff_description = "The diff initially shows the commit's changes.".to_string(); + }; + workspace_command.check_rewritable([&target_commit])?; + + let mut tx = + workspace_command.start_transaction(&format!("edit commit {}", target_commit.id().hex())); + let instructions = format!( + "\ +You are editing changes in: {} + +{diff_description} + +Adjust the right side until it shows the contents you want. If you +don't make any changes, then the operation will be aborted.", + tx.format_commit_summary(&target_commit), + ); + let base_tree = merge_commit_trees(tx.repo(), base_commits.as_slice())?; + let tree = target_commit.tree()?; + let tree_id = tx.edit_diff(ui, &base_tree, &tree, &EverythingMatcher, &instructions)?; + if tree_id == *target_commit.tree_id() { + writeln!(ui.stderr(), "Nothing changed.")?; + } else { + let mut_repo = tx.mut_repo(); + let new_commit = mut_repo + .rewrite_commit(command.settings(), &target_commit) + .set_tree_id(tree_id) + .write()?; + // rebase_descendants early; otherwise `new_commit` would always have + // a conflicted change id at this point. + let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; + write!(ui.stderr(), "Created ")?; + tx.write_commit_summary(ui.stderr_formatter().as_mut(), &new_commit)?; + writeln!(ui.stderr())?; + if num_rebased > 0 { + writeln!(ui.stderr(), "Rebased {num_rebased} descendant commits")?; + } + tx.finish(ui)?; + } + Ok(()) +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 5ae8ef44f..8fc838a29 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -25,6 +25,7 @@ mod config; mod debug; mod describe; mod diff; +mod diffedit; mod git; mod operation; @@ -93,7 +94,7 @@ enum Commands { Debug(debug::DebugCommands), Describe(describe::DescribeArgs), Diff(diff::DiffArgs), - Diffedit(DiffeditArgs), + Diffedit(diffedit::DiffeditArgs), Duplicate(DuplicateArgs), Edit(EditArgs), Files(FilesArgs), @@ -608,36 +609,6 @@ struct RunArgs { revisions: Vec, } -/// Touch up the content changes in a revision with a diff editor -/// -/// With the `-r` option, which is the default, starts a diff editor (`meld` by -/// default) on the changes in the revision. -/// -/// With the `--from` and/or `--to` options, starts a diff editor comparing the -/// "from" revision to the "to" revision. -/// -/// Edit the right side of the diff until it looks the way you want. Once you -/// close the editor, the revision specified with `-r` or `--to` will be -/// updated. Descendants will be rebased on top as usual, which may result in -/// conflicts. -/// -/// See `jj restore` if you want to move entire files from one revision to -/// another. See `jj squash -i` or `jj unsquash -i` if you instead want to move -/// changes into or out of the parent revision. -#[derive(clap::Args, Clone, Debug)] -struct DiffeditArgs { - /// The revision to touch up. Defaults to @ if neither --to nor --from are - /// specified. - #[arg(long, short)] - revision: Option, - /// Show changes from this revision. Defaults to @ if --to is specified. - #[arg(long, conflicts_with = "revision")] - from: Option, - /// Edit changes in this revision. Defaults to @ if --from is specified. - #[arg(long, conflicts_with = "revision")] - to: Option, -} - /// Split a revision in two /// /// Starts a diff editor (`meld` by default) on the changes in the revision. @@ -2505,69 +2476,6 @@ fn cmd_restore( Ok(()) } -#[instrument(skip_all)] -fn cmd_diffedit( - ui: &mut Ui, - command: &CommandHelper, - args: &DiffeditArgs, -) -> Result<(), CommandError> { - let mut workspace_command = command.workspace_helper(ui)?; - - let (target_commit, base_commits, diff_description); - if args.from.is_some() || args.to.is_some() { - target_commit = - workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"), ui)?; - base_commits = - vec![workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"), ui)?]; - diff_description = format!( - "The diff initially shows the commit's changes relative to:\n{}", - workspace_command.format_commit_summary(&base_commits[0]) - ); - } else { - target_commit = - workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?; - base_commits = target_commit.parents(); - diff_description = "The diff initially shows the commit's changes.".to_string(); - }; - workspace_command.check_rewritable([&target_commit])?; - - let mut tx = - workspace_command.start_transaction(&format!("edit commit {}", target_commit.id().hex())); - let instructions = format!( - "\ -You are editing changes in: {} - -{diff_description} - -Adjust the right side until it shows the contents you want. If you -don't make any changes, then the operation will be aborted.", - tx.format_commit_summary(&target_commit), - ); - let base_tree = merge_commit_trees(tx.repo(), base_commits.as_slice())?; - let tree = target_commit.tree()?; - let tree_id = tx.edit_diff(ui, &base_tree, &tree, &EverythingMatcher, &instructions)?; - if tree_id == *target_commit.tree_id() { - writeln!(ui.stderr(), "Nothing changed.")?; - } else { - let mut_repo = tx.mut_repo(); - let new_commit = mut_repo - .rewrite_commit(command.settings(), &target_commit) - .set_tree_id(tree_id) - .write()?; - // rebase_descendants early; otherwise `new_commit` would always have - // a conflicted change id at this point. - let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; - write!(ui.stderr(), "Created ")?; - tx.write_commit_summary(ui.stderr_formatter().as_mut(), &new_commit)?; - writeln!(ui.stderr())?; - if num_rebased > 0 { - writeln!(ui.stderr(), "Rebased {num_rebased} descendant commits")?; - } - tx.finish(ui)?; - } - Ok(()) -} - fn description_template_for_commit( ui: &Ui, settings: &UserSettings, @@ -3347,7 +3255,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Commands::Unsquash(sub_args) => cmd_unsquash(ui, command_helper, sub_args), Commands::Restore(sub_args) => cmd_restore(ui, command_helper, sub_args), Commands::Run(sub_args) => cmd_run(ui, command_helper, sub_args), - Commands::Diffedit(sub_args) => cmd_diffedit(ui, command_helper, sub_args), + Commands::Diffedit(sub_args) => diffedit::cmd_diffedit(ui, command_helper, sub_args), Commands::Split(sub_args) => cmd_split(ui, command_helper, sub_args), Commands::Merge(sub_args) => cmd_merge(ui, command_helper, sub_args), Commands::Rebase(sub_args) => cmd_rebase(ui, command_helper, sub_args),