forked from mirrors/jj
merge_tools: move run_mergetool() to MergeEditor::edit_file()
This makes sense because the editor doesn't interact with the transaction.
This commit is contained in:
parent
42f9ced423
commit
fc4aa36a80
3 changed files with 42 additions and 51 deletions
|
@ -1725,16 +1725,6 @@ impl WorkspaceCommandTransaction<'_> {
|
|||
self.tx.mut_repo().edit(workspace_id, commit)
|
||||
}
|
||||
|
||||
// TODO: inline this
|
||||
pub fn run_mergetool(
|
||||
&self,
|
||||
editor: &MergeEditor,
|
||||
tree: &MergedTree,
|
||||
repo_path: &RepoPath,
|
||||
) -> Result<MergedTreeId, CommandError> {
|
||||
Ok(merge_tools::run_mergetool(editor, tree, repo_path)?)
|
||||
}
|
||||
|
||||
// TODO: maybe capture parameters by diff_editor(), and inline this?
|
||||
pub fn edit_diff(
|
||||
&self,
|
||||
|
|
|
@ -103,7 +103,7 @@ pub(crate) fn cmd_resolve(
|
|||
workspace_command.format_file_path(repo_path)
|
||||
)?;
|
||||
let mut tx = workspace_command.start_transaction();
|
||||
let new_tree_id = tx.run_mergetool(&merge_editor, &tree, repo_path)?;
|
||||
let new_tree_id = merge_editor.edit_file(&tree, repo_path)?;
|
||||
let new_commit = tx
|
||||
.mut_repo()
|
||||
.rewrite_commit(command.settings(), &commit)
|
||||
|
|
|
@ -85,46 +85,6 @@ pub enum ConflictResolveError {
|
|||
Backend(#[from] jj_lib::backend::BackendError),
|
||||
}
|
||||
|
||||
pub fn run_mergetool(
|
||||
editor: &MergeEditor,
|
||||
tree: &MergedTree,
|
||||
repo_path: &RepoPath,
|
||||
) -> Result<MergedTreeId, ConflictResolveError> {
|
||||
let conflict = match tree.path_value(repo_path).into_resolved() {
|
||||
Err(conflict) => conflict,
|
||||
Ok(Some(_)) => return Err(ConflictResolveError::NotAConflict(repo_path.to_owned())),
|
||||
Ok(None) => return Err(ConflictResolveError::PathNotFound(repo_path.to_owned())),
|
||||
};
|
||||
let file_merge = conflict.to_file_merge().ok_or_else(|| {
|
||||
let mut summary_bytes: Vec<u8> = vec![];
|
||||
conflict
|
||||
.describe(&mut summary_bytes)
|
||||
.expect("Writing to an in-memory buffer should never fail");
|
||||
ConflictResolveError::NotNormalFiles(
|
||||
repo_path.to_owned(),
|
||||
String::from_utf8_lossy(summary_bytes.as_slice()).to_string(),
|
||||
)
|
||||
})?;
|
||||
// We only support conflicts with 2 sides (3-way conflicts)
|
||||
if file_merge.num_sides() > 2 {
|
||||
return Err(ConflictResolveError::ConflictTooComplicated {
|
||||
path: repo_path.to_owned(),
|
||||
sides: file_merge.num_sides(),
|
||||
});
|
||||
};
|
||||
let content = extract_as_single_hunk(&file_merge, tree.store(), repo_path).block_on();
|
||||
|
||||
match &editor.tool {
|
||||
MergeTool::Builtin => {
|
||||
let tree_id = edit_merge_builtin(tree, repo_path, content).map_err(Box::new)?;
|
||||
Ok(tree_id)
|
||||
}
|
||||
MergeTool::External(editor) => {
|
||||
external::run_mergetool_external(editor, file_merge, content, repo_path, conflict, tree)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn edit_diff(
|
||||
editor: &DiffEditor,
|
||||
left_tree: &MergedTree,
|
||||
|
@ -265,6 +225,47 @@ impl MergeEditor {
|
|||
}
|
||||
Ok(MergeEditor { tool })
|
||||
}
|
||||
|
||||
/// Starts a merge editor for the specified file.
|
||||
pub fn edit_file(
|
||||
&self,
|
||||
tree: &MergedTree,
|
||||
repo_path: &RepoPath,
|
||||
) -> Result<MergedTreeId, ConflictResolveError> {
|
||||
let conflict = match tree.path_value(repo_path).into_resolved() {
|
||||
Err(conflict) => conflict,
|
||||
Ok(Some(_)) => return Err(ConflictResolveError::NotAConflict(repo_path.to_owned())),
|
||||
Ok(None) => return Err(ConflictResolveError::PathNotFound(repo_path.to_owned())),
|
||||
};
|
||||
let file_merge = conflict.to_file_merge().ok_or_else(|| {
|
||||
let mut summary_bytes: Vec<u8> = vec![];
|
||||
conflict
|
||||
.describe(&mut summary_bytes)
|
||||
.expect("Writing to an in-memory buffer should never fail");
|
||||
ConflictResolveError::NotNormalFiles(
|
||||
repo_path.to_owned(),
|
||||
String::from_utf8_lossy(summary_bytes.as_slice()).to_string(),
|
||||
)
|
||||
})?;
|
||||
// We only support conflicts with 2 sides (3-way conflicts)
|
||||
if file_merge.num_sides() > 2 {
|
||||
return Err(ConflictResolveError::ConflictTooComplicated {
|
||||
path: repo_path.to_owned(),
|
||||
sides: file_merge.num_sides(),
|
||||
});
|
||||
};
|
||||
let content = extract_as_single_hunk(&file_merge, tree.store(), repo_path).block_on();
|
||||
|
||||
match &self.tool {
|
||||
MergeTool::Builtin => {
|
||||
let tree_id = edit_merge_builtin(tree, repo_path, content).map_err(Box::new)?;
|
||||
Ok(tree_id)
|
||||
}
|
||||
MergeTool::External(editor) => external::run_mergetool_external(
|
||||
editor, file_merge, content, repo_path, conflict, tree,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue