| 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 clap_complete::ArgValueCandidates; |
| 16 |
use clap_complete::ArgValueCompleter; |
| 17 |
use itertools::Itertools as _; |
| 18 |
use jj_lib::object_id::ObjectId as _; |
| 19 |
use tracing::instrument; |
| 20 |
|
| 21 |
use crate::cli_util::CommandHelper; |
| 22 |
use crate::cli_util::RevisionArg; |
| 23 |
use crate::cli_util::print_conflicted_paths; |
| 24 |
use crate::cli_util::print_unmatched_explicit_paths; |
| 25 |
use crate::command_error::CommandError; |
| 26 |
use crate::command_error::cli_error; |
| 27 |
use crate::complete; |
| 28 |
use crate::formatter::FormatterExt as _; |
| 29 |
use crate::ui::Ui; |
| 30 |
|
| 31 |
/// Resolve conflicted files with an external merge tool |
| 32 |
/// |
| 33 |
/// Only conflicts that can be resolved with a 3-way merge are supported. See |
| 34 |
/// docs for merge tool configuration instructions. External merge tools will be |
| 35 |
/// invoked for each conflicted file one-by-one until all conflicts are |
| 36 |
/// resolved. To stop resolving conflicts, exit the merge tool without making |
| 37 |
/// any changes. |
| 38 |
/// |
| 39 |
/// Note that conflicts can also be resolved without using this command. You may |
| 40 |
/// edit the conflict markers in the conflicted file directly with a text |
| 41 |
/// editor. |
| 42 |
// TODOs: |
| 43 |
// - `jj resolve --editor` to resolve a conflict in the default text editor. Should work for |
| 44 |
// conflicts with 3+ adds. Useful to resolve conflicts in a commit other than the current one. |
| 45 |
// - A way to help split commits with conflicts that are too complicated (more than two sides) |
| 46 |
// into commits with simpler conflicts. In case of a tree with many merges, we could for example |
| 47 |
// point to existing commits with simpler conflicts where resolving those conflicts would help |
| 48 |
// simplify the present one. |
| 49 |
#[derive(clap::Args, Clone, Debug)] |
| 50 |
pub(crate) struct ResolveArgs { |
| 51 |
#[arg(long, short, default_value = "@", value_name = "REVSET")] |
| 52 |
#[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable_conflicts))] |
| 53 |
revision: RevisionArg, |
| 54 |
|
| 55 |
/// Instead of resolving conflicts, list all the conflicts |
| 56 |
// TODO: Also have a `--summary` option. `--list` currently acts like |
| 57 |
// `diff --summary`, but should be more verbose. |
| 58 |
#[arg(long, short)] |
| 59 |
list: bool, |
| 60 |
|
| 61 |
/// Specify 3-way merge tool to be used |
| 62 |
/// |
| 63 |
/// The built-in merge tools `:ours` and `:theirs` can be used to choose |
| 64 |
/// side #1 and side #2 of the conflict respectively. |
| 65 |
#[arg(long, conflicts_with = "list", value_name = "NAME")] |
| 66 |
#[arg(add = ArgValueCandidates::new(complete::merge_editors))] |
| 67 |
tool: Option<String>, |
| 68 |
|
| 69 |
/// Only resolve conflicts in these paths. You can use the `--list` argument |
| 70 |
/// to find paths to use here. |
| 71 |
#[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)] |
| 72 |
#[arg(add = ArgValueCompleter::new(complete::revision_conflicted_files))] |
| 73 |
paths: Vec<String>, |
| 74 |
} |
| 75 |
|
| 76 |
#[instrument(skip_all)] |
| 77 |
pub(crate) async fn cmd_resolve( |
| 78 |
ui: &mut Ui, |
| 79 |
command: &CommandHelper, |
| 80 |
args: &ResolveArgs, |
| 81 |
) -> Result<(), CommandError> { |
| 82 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 83 |
let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?; |
| 84 |
let matcher = fileset_expression.to_matcher(); |
| 85 |
let commit = workspace_command |
| 86 |
.resolve_single_rev(ui, &args.revision) |
| 87 |
.await?; |
| 88 |
let tree = commit.tree(); |
| 89 |
let conflicts = tree.conflicts_matching(&matcher).collect_vec(); |
| 90 |
|
| 91 |
print_unmatched_explicit_paths(ui, &workspace_command, &fileset_expression, [&tree]).await?; |
| 92 |
|
| 93 |
if conflicts.is_empty() { |
| 94 |
return Err(cli_error(if args.paths.is_empty() { |
| 95 |
"No conflicts found at this revision" |
| 96 |
} else { |
| 97 |
"No conflicts found at the given path(s)" |
| 98 |
})); |
| 99 |
} |
| 100 |
if args.list { |
| 101 |
return print_conflicted_paths( |
| 102 |
conflicts, |
| 103 |
ui.stdout_formatter().as_mut(), |
| 104 |
&workspace_command, |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
let repo_paths = conflicts |
| 109 |
.iter() |
| 110 |
.map(|(path, _)| path.as_ref()) |
| 111 |
.collect_vec(); |
| 112 |
workspace_command.check_rewritable([commit.id()]).await?; |
| 113 |
let merge_editor = workspace_command.merge_editor(ui, args.tool.as_deref())?; |
| 114 |
let mut tx = workspace_command.start_transaction(); |
| 115 |
let (new_tree, partial_resolution_error) = |
| 116 |
merge_editor.edit_files(ui, &tree, &repo_paths).await?; |
| 117 |
let new_commit = tx |
| 118 |
.repo_mut() |
| 119 |
.rewrite_commit(&commit) |
| 120 |
.set_tree(new_tree) |
| 121 |
.write() |
| 122 |
.await?; |
| 123 |
tx.finish( |
| 124 |
ui, |
| 125 |
format!("Resolve conflicts in commit {}", commit.id().hex()), |
| 126 |
) |
| 127 |
.await?; |
| 128 |
|
| 129 |
// Print conflicts that are still present after resolution if the workspace |
| 130 |
// working copy is not at the commit. Otherwise, the conflicting paths will |
| 131 |
// be printed by the `tx.finish()` instead. |
| 132 |
if workspace_command.get_wc_commit_id() != Some(new_commit.id()) |
| 133 |
&& let Some(mut formatter) = ui.status_formatter() |
| 134 |
&& new_commit.has_conflict() |
| 135 |
{ |
| 136 |
let new_tree = new_commit.tree(); |
| 137 |
let new_conflicts = new_tree.conflicts().collect_vec(); |
| 138 |
writeln!( |
| 139 |
formatter.labeled("warning").with_heading("Warning: "), |
| 140 |
"After this operation, some files at this revision still have conflicts:" |
| 141 |
)?; |
| 142 |
print_conflicted_paths(new_conflicts, formatter.as_mut(), &workspace_command)?; |
| 143 |
} |
| 144 |
|
| 145 |
if let Some(err) = partial_resolution_error { |
| 146 |
return Err(err.into()); |
| 147 |
} |
| 148 |
Ok(()) |
| 149 |
} |