| 1 |
// Copyright 2020 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::io::Write as _; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use clap_complete::ArgValueCompleter; |
| 19 |
use jj_lib::merge::Diff; |
| 20 |
use jj_lib::object_id::ObjectId as _; |
| 21 |
use jj_lib::rewrite::merge_commit_trees; |
| 22 |
use tracing::instrument; |
| 23 |
|
| 24 |
use crate::cli_util::CommandHelper; |
| 25 |
use crate::cli_util::RevisionArg; |
| 26 |
use crate::cli_util::print_unmatched_explicit_paths; |
| 27 |
use crate::command_error::CommandError; |
| 28 |
use crate::complete; |
| 29 |
use crate::ui::Ui; |
| 30 |
|
| 31 |
/// Touch up the content changes in a revision with a diff editor |
| 32 |
/// |
| 33 |
/// With the `-r` option, starts a [diff editor] on the changes in the revision. |
| 34 |
/// |
| 35 |
/// With the `--from` and/or `--to` options, starts a [diff editor] comparing |
| 36 |
/// the "from" revision to the "to" revision. |
| 37 |
/// |
| 38 |
/// [diff editor]: |
| 39 |
/// https://docs.jj-vcs.dev/latest/config/#editing-diffs |
| 40 |
/// |
| 41 |
/// Edit the right side of the diff until it looks the way you want. Once you |
| 42 |
/// close the editor, the revision specified with `-r` or `--to` will be |
| 43 |
/// updated. Unless `--restore-descendants` is used, descendants will be |
| 44 |
/// rebased on top as usual, which may result in conflicts. |
| 45 |
/// |
| 46 |
/// See `jj restore` if you want to move entire files from one revision to |
| 47 |
/// another. For moving changes between revisions, see `jj squash -i`. |
| 48 |
#[derive(clap::Args, Clone, Debug)] |
| 49 |
pub(crate) struct DiffeditArgs { |
| 50 |
/// The revision to touch up |
| 51 |
/// |
| 52 |
/// Defaults to @ if neither --to nor --from are specified. |
| 53 |
#[arg(long, short, value_name = "REVSET")] |
| 54 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 55 |
revision: Option<RevisionArg>, |
| 56 |
|
| 57 |
/// Show changes from this revision |
| 58 |
/// |
| 59 |
/// Defaults to @ if --to is specified. |
| 60 |
#[arg(long, short, conflicts_with = "revision", value_name = "REVSET")] |
| 61 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_all))] |
| 62 |
from: Option<RevisionArg>, |
| 63 |
|
| 64 |
/// Edit changes in this revision |
| 65 |
/// |
| 66 |
/// Defaults to @ if --from is specified. |
| 67 |
#[arg(long, short, conflicts_with = "revision", value_name = "REVSET")] |
| 68 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))] |
| 69 |
to: Option<RevisionArg>, |
| 70 |
|
| 71 |
/// Edit only these paths (unmatched paths will remain unchanged) |
| 72 |
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)] |
| 73 |
#[arg(add = ArgValueCompleter::new(complete::modified_revision_or_range_files))] |
| 74 |
paths: Vec<String>, |
| 75 |
|
| 76 |
/// Specify diff editor to be used |
| 77 |
#[arg(long, value_name = "NAME")] |
| 78 |
#[arg(add = ArgValueCandidates::new(complete::diff_editors))] |
| 79 |
tool: Option<String>, |
| 80 |
|
| 81 |
/// Preserve the content (not the diff) when rebasing descendants |
| 82 |
/// |
| 83 |
/// When rebasing a descendant on top of the rewritten revision, its diff |
| 84 |
/// compared to its parent(s) is normally preserved, i.e. the same way that |
| 85 |
/// descendants are always rebased. This flag makes it so the content/state |
| 86 |
/// is preserved instead of preserving the diff. |
| 87 |
#[arg(long)] |
| 88 |
restore_descendants: bool, |
| 89 |
} |
| 90 |
|
| 91 |
#[instrument(skip_all)] |
| 92 |
pub(crate) async fn cmd_diffedit( |
| 93 |
ui: &mut Ui, |
| 94 |
command: &CommandHelper, |
| 95 |
args: &DiffeditArgs, |
| 96 |
) -> Result<(), CommandError> { |
| 97 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 98 |
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?; |
| 99 |
let matcher = fileset_expression.to_matcher(); |
| 100 |
|
| 101 |
let (target_commit, base_commits, diff_description); |
| 102 |
if args.from.is_some() || args.to.is_some() { |
| 103 |
target_commit = workspace_command |
| 104 |
.resolve_single_rev(ui, args.to.as_ref().unwrap_or(&RevisionArg::AT)) |
| 105 |
.await?; |
| 106 |
base_commits = vec![ |
| 107 |
workspace_command |
| 108 |
.resolve_single_rev(ui, args.from.as_ref().unwrap_or(&RevisionArg::AT)) |
| 109 |
.await?, |
| 110 |
]; |
| 111 |
diff_description = format!( |
| 112 |
"The diff initially shows the commit's changes relative to:\n{}", |
| 113 |
workspace_command.format_commit_summary(&base_commits[0]) |
| 114 |
); |
| 115 |
} else { |
| 116 |
target_commit = workspace_command |
| 117 |
.resolve_single_rev(ui, args.revision.as_ref().unwrap_or(&RevisionArg::AT)) |
| 118 |
.await?; |
| 119 |
base_commits = target_commit.parents().await?; |
| 120 |
diff_description = "The diff initially shows the commit's changes.".to_string(); |
| 121 |
} |
| 122 |
workspace_command |
| 123 |
.check_rewritable([target_commit.id()]) |
| 124 |
.await?; |
| 125 |
|
| 126 |
let diff_editor = workspace_command.diff_editor(ui, args.tool.as_deref())?; |
| 127 |
let mut tx = workspace_command.start_transaction(); |
| 128 |
let format_instructions = || { |
| 129 |
format!( |
| 130 |
"\ |
| 131 |
You are editing changes in: {} |
| 132 |
|
| 133 |
{diff_description} |
| 134 |
|
| 135 |
Adjust the right side until it shows the contents you want. If you |
| 136 |
don't make any changes, then the operation will be aborted.", |
| 137 |
tx.format_commit_summary(&target_commit), |
| 138 |
) |
| 139 |
}; |
| 140 |
let base_tree = merge_commit_trees(tx.repo(), base_commits.as_slice()).await?; |
| 141 |
let tree = target_commit.tree(); |
| 142 |
let edited_tree = diff_editor |
| 143 |
.edit(Diff::new(&base_tree, &tree), &matcher, format_instructions) |
| 144 |
.await?; |
| 145 |
if edited_tree.tree_ids() == target_commit.tree_ids() { |
| 146 |
writeln!(ui.status(), "Nothing changed.")?; |
| 147 |
} else { |
| 148 |
tx.repo_mut() |
| 149 |
.rewrite_commit(&target_commit) |
| 150 |
.set_tree(edited_tree) |
| 151 |
.write() |
| 152 |
.await?; |
| 153 |
// rebase_descendants early; otherwise `new_commit` would always have |
| 154 |
// a conflicted change id at this point. |
| 155 |
let (num_rebased, extra_msg) = if args.restore_descendants { |
| 156 |
( |
| 157 |
tx.repo_mut().reparent_descendants().await?, |
| 158 |
" (while preserving their content)", |
| 159 |
) |
| 160 |
} else { |
| 161 |
(tx.repo_mut().rebase_descendants().await?, "") |
| 162 |
}; |
| 163 |
if let Some(mut formatter) = ui.status_formatter() |
| 164 |
&& num_rebased > 0 |
| 165 |
{ |
| 166 |
writeln!( |
| 167 |
formatter, |
| 168 |
"Rebased {num_rebased} descendant commits{extra_msg}." |
| 169 |
)?; |
| 170 |
} |
| 171 |
tx.finish(ui, format!("edit commit {}", target_commit.id().hex())) |
| 172 |
.await?; |
| 173 |
} |
| 174 |
print_unmatched_explicit_paths( |
| 175 |
ui, |
| 176 |
&workspace_command, |
| 177 |
&fileset_expression, |
| 178 |
[&base_tree, &tree], |
| 179 |
) |
| 180 |
.await?; |
| 181 |
Ok(()) |
| 182 |
} |