Skip to content

Commit

Permalink
merge_tools: take diff editor instruction as Option<&str>
Browse files Browse the repository at this point in the history
This clarifies that the instructions text can be omitted. All callers appear to
pass non-empty instructions, though.
  • Loading branch information
yuja committed Mar 1, 2024
1 parent 1789edc commit ea1aa53
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ impl WorkspaceCommandTransaction<'_> {
left_tree: &MergedTree,
right_tree: &MergedTree,
matcher: &dyn Matcher,
instructions: &str,
instructions: Option<&str>,
) -> Result<MergedTreeId, CommandError> {
let base_ignores = self.helper.base_ignores()?;
let settings = &self.helper.settings;
Expand All @@ -1763,7 +1763,7 @@ impl WorkspaceCommandTransaction<'_> {
left_tree: &MergedTree,
right_tree: &MergedTree,
matcher: &dyn Matcher,
instructions: &str,
instructions: Option<&str>,
interactive: bool,
) -> Result<MergedTreeId, CommandError> {
if interactive {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ new working-copy commit.
&base_tree,
&commit.tree()?,
matcher.as_ref(),
&instructions,
Some(&instructions),
args.interactive,
)?;
let middle_tree = tx.repo().store().get_root_tree(&tree_id)?;
Expand Down
8 changes: 7 additions & 1 deletion cli/src/commands/diffedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ don't make any changes, then the operation will be aborted.",
);
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)?;
let tree_id = tx.edit_diff(
ui,
&base_tree,
&tree,
&EverythingMatcher,
Some(&instructions),
)?;
if tree_id == *target_commit.tree_id() {
writeln!(ui.stderr(), "Nothing changed.")?;
} else {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ from the source will be moved into the destination.
&parent_tree,
&source_tree,
matcher.as_ref(),
&instructions,
Some(&instructions),
args.interactive,
)?;
if args.interactive && new_parent_tree_id == parent_tree.id() {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ don't make any changes, then the operation will be aborted.
&base_tree,
&end_tree,
matcher.as_ref(),
&instructions,
Some(&instructions),
interactive,
)?;
if &selected_tree_id == commit.tree_id() && interactive {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ from the source will be moved into the parent.
&parent_tree,
&tree,
matcher.as_ref(),
&instructions,
Some(&instructions),
args.interactive,
)?;
if &new_parent_tree_id == parent.tree_id() {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/unsquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ aborted.
&parent_base_tree,
&parent_tree,
&EverythingMatcher,
&instructions,
Some(&instructions),
)?;
if new_parent_tree_id == parent_base_tree.id() {
return Err(user_error("No changes selected"));
Expand Down
15 changes: 9 additions & 6 deletions cli/src/merge_tools/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ pub fn edit_diff_external(
left_tree: &MergedTree,
right_tree: &MergedTree,
matcher: &dyn Matcher,
instructions: &str,
instructions: Option<&str>,
base_ignores: Arc<GitIgnoreFile>,
settings: &UserSettings,
) -> Result<MergedTreeId, DiffEditError> {
Expand All @@ -452,10 +452,13 @@ pub fn edit_diff_external(
let instructions_path_to_cleanup = output_wc_path.join("JJ-INSTRUCTIONS");
// In the unlikely event that the file already exists, then the user will simply
// not get any instructions.
let add_instructions = settings.diff_instructions()
&& !instructions.is_empty()
&& !instructions_path_to_cleanup.exists();
if add_instructions {
let add_instructions = if settings.diff_instructions() && !instructions_path_to_cleanup.exists()
{
instructions
} else {
None
};
if let Some(instructions) = add_instructions {
let mut output_instructions_file =
File::create(&instructions_path_to_cleanup).map_err(ExternalToolError::SetUpDir)?;
if diff_wc.right_working_copy_path() != output_wc_path {
Expand Down Expand Up @@ -513,7 +516,7 @@ diff editing in mind and be a little inaccurate.
exit_status,
}));
}
if add_instructions {
if add_instructions.is_some() {
std::fs::remove_file(instructions_path_to_cleanup).ok();
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/merge_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn edit_diff(
left_tree: &MergedTree,
right_tree: &MergedTree,
matcher: &dyn Matcher,
instructions: &str,
instructions: Option<&str>,
base_ignores: Arc<GitIgnoreFile>,
settings: &UserSettings,
) -> Result<MergedTreeId, DiffEditError> {
Expand Down

0 comments on commit ea1aa53

Please sign in to comment.