Skip to content

Commit

Permalink
commands: move resolve code to resolve.rs
Browse files Browse the repository at this point in the history
The print_conflicted_paths function could belong either
to `resolve.rs` or `status.rs`; I put it into the former for now.

Cc #2465, #2457
  • Loading branch information
ilyagr committed Oct 29, 2023
1 parent 4279589 commit 743962a
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 197 deletions.
204 changes: 7 additions & 197 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ mod edit;
mod files;
mod git;
mod operation;
mod resolve;

use std::collections::{BTreeMap, HashSet};
use std::collections::HashSet;
use std::fmt::Debug;
use std::io::{BufRead, Seek, SeekFrom, Write};
use std::path::Path;
Expand All @@ -43,12 +44,12 @@ use clap::parser::ValueSource;
use clap::{ArgGroup, Command, CommandFactory, FromArgMatches, Subcommand};
use indexmap::IndexSet;
use itertools::Itertools;
use jj_lib::backend::{CommitId, ObjectId, TreeValue};
use jj_lib::backend::{CommitId, ObjectId};
use jj_lib::commit::Commit;
use jj_lib::dag_walk::topo_order_reverse;
use jj_lib::git_backend::GitBackend;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::merge::{Merge, MergedTreeValue};
use jj_lib::merge::Merge;
use jj_lib::merged_tree::{MergedTree, MergedTreeBuilder};
use jj_lib::op_store::WorkspaceId;
use jj_lib::repo::{ReadonlyRepo, Repo};
Expand Down Expand Up @@ -125,7 +126,7 @@ enum Commands {
Operation(operation::OperationCommands),
Prev(PrevArgs),
Rebase(RebaseArgs),
Resolve(ResolveArgs),
Resolve(resolve::ResolveArgs),
Restore(RestoreArgs),
#[command(hide = true)]
// TODO: Flesh out.
Expand Down Expand Up @@ -470,42 +471,6 @@ struct UnsquashArgs {
interactive: bool,
}

/// Resolve a conflicted file with an external merge tool
///
/// Only conflicts that can be resolved with a 3-way merge are supported. See
/// docs for merge tool configuration instructions.
///
/// Note that conflicts can also be resolved without using this command. You may
/// edit the conflict markers in the conflicted file directly with a text
/// editor.
// TODOs:
// - `jj resolve --editor` to resolve a conflict in the default text editor. Should work for
// conflicts with 3+ adds. Useful to resolve conflicts in a commit other than the current one.
// - A way to help split commits with conflicts that are too complicated (more than two sides)
// into commits with simpler conflicts. In case of a tree with many merges, we could for example
// point to existing commits with simpler conflicts where resolving those conflicts would help
// simplify the present one.
#[derive(clap::Args, Clone, Debug)]
struct ResolveArgs {
#[arg(long, short, default_value = "@")]
revision: String,
/// Instead of resolving one conflict, list all the conflicts
// TODO: Also have a `--summary` option. `--list` currently acts like
// `diff --summary`, but should be more verbose.
#[arg(long, short)]
list: bool,
/// Do not print the list of remaining conflicts (if any) after resolving a
/// conflict
#[arg(long, short, conflicts_with = "list")]
quiet: bool,
/// Restrict to these paths when searching for a conflict to resolve. We
/// will attempt to resolve the first conflict we can find. You can use
/// the `--list` argument to find paths to use here.
// TODO: Find the conflict we can resolve even if it's not the first one.
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}

/// Restore paths from another revision
///
/// That means that the paths get the same content in the destination (`--to`)
Expand Down Expand Up @@ -1088,7 +1053,7 @@ fn cmd_status(
formatter.labeled("conflict"),
"There are unresolved conflicts at these paths:"
)?;
print_conflicted_paths(&conflicts, formatter, &workspace_command)?
resolve::print_conflicted_paths(&conflicts, formatter, &workspace_command)?
}

formatter.write_str("Working copy : ")?;
Expand Down Expand Up @@ -2128,161 +2093,6 @@ aborted.
Ok(())
}

#[instrument(skip_all)]
fn cmd_resolve(
ui: &mut Ui,
command: &CommandHelper,
args: &ResolveArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let matcher = workspace_command.matcher_from_values(&args.paths)?;
let commit = workspace_command.resolve_single_rev(&args.revision, ui)?;
let tree = commit.tree()?;
let conflicts = tree
.conflicts()
.filter(|path| matcher.matches(&path.0))
.collect_vec();
if conflicts.is_empty() {
return Err(CommandError::CliError(format!(
"No conflicts found {}",
if args.paths.is_empty() {
"at this revision"
} else {
"at the given path(s)"
}
)));
}
if args.list {
return print_conflicted_paths(
&conflicts,
ui.stdout_formatter().as_mut(),
&workspace_command,
);
};

let (repo_path, _) = conflicts.get(0).unwrap();
workspace_command.check_rewritable([&commit])?;
let mut tx = workspace_command.start_transaction(&format!(
"Resolve conflicts in commit {}",
commit.id().hex()
));
let new_tree_id = tx.run_mergetool(ui, &tree, repo_path)?;
let new_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.set_tree_id(new_tree_id)
.write()?;
tx.finish(ui)?;

if !args.quiet {
let new_tree = new_commit.tree()?;
let new_conflicts = new_tree.conflicts().collect_vec();
if !new_conflicts.is_empty() {
writeln!(
ui.stderr(),
"After this operation, some files at this revision still have conflicts:"
)?;
print_conflicted_paths(
&new_conflicts,
ui.stderr_formatter().as_mut(),
&workspace_command,
)?;
}
};
Ok(())
}

#[instrument(skip_all)]
fn print_conflicted_paths(
conflicts: &[(RepoPath, MergedTreeValue)],
formatter: &mut dyn Formatter,
workspace_command: &WorkspaceCommandHelper,
) -> Result<(), CommandError> {
let formatted_paths = conflicts
.iter()
.map(|(path, _conflict)| workspace_command.format_file_path(path))
.collect_vec();
let max_path_len = formatted_paths.iter().map(|p| p.len()).max().unwrap_or(0);
let formatted_paths = formatted_paths
.into_iter()
.map(|p| format!("{:width$}", p, width = max_path_len.min(32) + 3));

for ((_, conflict), formatted_path) in std::iter::zip(conflicts.iter(), formatted_paths) {
let sides = conflict.num_sides();
let n_adds = conflict.adds().iter().flatten().count();
let deletions = sides - n_adds;

let mut seen_objects = BTreeMap::new(); // Sort for consistency and easier testing
if deletions > 0 {
seen_objects.insert(
format!(
// Starting with a number sorts this first
"{deletions} deletion{}",
if deletions > 1 { "s" } else { "" }
),
"normal", // Deletions don't interfere with `jj resolve` or diff display
);
}
// TODO: We might decide it's OK for `jj resolve` to ignore special files in the
// `removes` of a conflict (see e.g. https://github.com/martinvonz/jj/pull/978). In
// that case, `conflict.removes` should be removed below.
for term in itertools::chain(conflict.removes().iter(), conflict.adds().iter()).flatten() {
seen_objects.insert(
match term {
TreeValue::File {
executable: false, ..
} => continue,
TreeValue::File {
executable: true, ..
} => "an executable",
TreeValue::Symlink(_) => "a symlink",
TreeValue::Tree(_) => "a directory",
TreeValue::GitSubmodule(_) => "a git submodule",
TreeValue::Conflict(_) => "another conflict (you found a bug!)",
}
.to_string(),
"difficult",
);
}

write!(formatter, "{formatted_path} ",)?;
formatter.with_label("conflict_description", |formatter| {
let print_pair = |formatter: &mut dyn Formatter, (text, label): &(String, &str)| {
formatter.with_label(label, |fmt| fmt.write_str(text))
};
print_pair(
formatter,
&(
format!("{sides}-sided"),
if sides > 2 { "difficult" } else { "normal" },
),
)?;
formatter.write_str(" conflict")?;

if !seen_objects.is_empty() {
formatter.write_str(" including ")?;
let seen_objects = seen_objects.into_iter().collect_vec();
match &seen_objects[..] {
[] => unreachable!(),
[only] => print_pair(formatter, only)?,
[first, middle @ .., last] => {
print_pair(formatter, first)?;
for pair in middle {
formatter.write_str(", ")?;
print_pair(formatter, pair)?;
}
formatter.write_str(" and ")?;
print_pair(formatter, last)?;
}
};
}
Ok(())
})?;
writeln!(formatter)?;
}
Ok(())
}

#[instrument(skip_all)]
fn cmd_restore(
ui: &mut Ui,
Expand Down Expand Up @@ -3130,7 +2940,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co
Commands::Merge(sub_args) => cmd_merge(ui, command_helper, sub_args),
Commands::Rebase(sub_args) => cmd_rebase(ui, command_helper, sub_args),
Commands::Backout(sub_args) => backout::cmd_backout(ui, command_helper, sub_args),
Commands::Resolve(sub_args) => cmd_resolve(ui, command_helper, sub_args),
Commands::Resolve(sub_args) => resolve::cmd_resolve(ui, command_helper, sub_args),
Commands::Branch(sub_args) => branch::cmd_branch(ui, command_helper, sub_args),
Commands::Undo(sub_args) => operation::cmd_op_undo(ui, command_helper, sub_args),
Commands::Operation(sub_args) => operation::cmd_operation(ui, command_helper, sub_args),
Expand Down
Loading

0 comments on commit 743962a

Please sign in to comment.