Skip to content

Commit

Permalink
cli: replace run_ui_editor() and edit_temp_file() with editor type
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Jan 3, 2025
1 parent e306f84 commit 641ec52
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 94 deletions.
20 changes: 0 additions & 20 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2836,26 +2836,6 @@ impl LogContentFormat {
}
}

pub fn run_ui_editor(settings: &UserSettings, edit_path: &Path) -> Result<(), CommandError> {
let editor = TextEditor::from_settings(settings)?;
editor.edit_file(edit_path)?;
Ok(())
}

pub fn edit_temp_file(
error_name: &str,
tempfile_suffix: &str,
dir: &Path,
content: &str,
settings: &UserSettings,
) -> Result<String, CommandError> {
let editor = TextEditor::from_settings(settings)?.with_temp_dir(dir);
let edited = editor
.edit_str(content, Some(tempfile_suffix))
.map_err(|err| err.with_name(error_name))?;
Ok(edited)
}

pub fn short_commit_hash(commit_id: &CommitId) -> String {
format!("{commit_id:.12}")
}
Expand Down
7 changes: 2 additions & 5 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub(crate) fn cmd_commit(
let advanceable_bookmarks = workspace_command.get_advanceable_bookmarks(commit.parent_ids())?;
let diff_selector =
workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?;
let text_editor = workspace_command.text_editor()?;
let mut tx = workspace_command.start_transaction();
let base_tree = commit.parent_tree(tx.repo())?;
let format_instructions = || {
Expand Down Expand Up @@ -138,11 +139,7 @@ new working-copy commit.
}
let temp_commit = commit_builder.write_hidden()?;
let template = description_template(ui, &tx, "", &temp_commit)?;
edit_description(
tx.base_workspace_helper().repo_path(),
&template,
command.settings(),
)?
edit_description(&text_editor, &template)?
};
commit_builder.set_description(description);
let new_commit = commit_builder.write(tx.repo_mut())?;
Expand Down
5 changes: 3 additions & 2 deletions cli/src/commands/config/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use tracing::instrument;

use super::ConfigLevelArgs;
use crate::cli_util::run_ui_editor;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::ui::Ui;
Expand All @@ -36,9 +35,11 @@ pub fn cmd_config_edit(
command: &CommandHelper,
args: &ConfigEditArgs,
) -> Result<(), CommandError> {
let editor = command.text_editor()?;
let file = args.level.edit_config_file(command)?;
if !file.path().exists() {
file.save()?;
}
run_ui_editor(command.settings(), file.path())
editor.edit_file(file.path())?;
Ok(())
}
10 changes: 3 additions & 7 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub(crate) fn cmd_describe(
return Ok(());
}
workspace_command.check_rewritable(commits.iter().ids())?;
let text_editor = workspace_command.text_editor()?;

let mut tx = workspace_command.start_transaction();
let tx_description = if commits.len() == 1 {
Expand Down Expand Up @@ -172,20 +173,15 @@ pub(crate) fn cmd_describe(

if let [(_, temp_commit)] = &*temp_commits {
let template = description_template(ui, &tx, "", temp_commit)?;
let description = edit_description(
tx.base_workspace_helper().repo_path(),
&template,
command.settings(),
)?;

let description = edit_description(&text_editor, &template)?;
vec![(&commits[0], description)]
} else {
let ParsedBulkEditMessage {
descriptions,
missing,
duplicates,
unexpected,
} = edit_multiple_descriptions(ui, &tx, &temp_commits, command.settings())?;
} = edit_multiple_descriptions(ui, &text_editor, &tx, &temp_commits)?;
if !missing.is_empty() {
return Err(user_error(format!(
"The description for the following commits were not found in the edited \
Expand Down
20 changes: 7 additions & 13 deletions cli/src/commands/sparse/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use std::path::Path;

use itertools::Itertools;
use jj_lib::repo_path::RepoPathBuf;
use jj_lib::settings::UserSettings;
use tracing::instrument;

use super::update_sparse_patterns_with;
use crate::cli_util::edit_temp_file;
use crate::cli_util::CommandHelper;
use crate::command_error::internal_error;
use crate::command_error::user_error_with_message;
use crate::command_error::CommandError;
use crate::description_util::TextEditor;
use crate::ui::Ui;

/// Start an editor to update the patterns that are present in the working copy
Expand All @@ -39,19 +38,18 @@ pub fn cmd_sparse_edit(
_args: &SparseEditArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let repo_path = workspace_command.repo_path().to_owned();
let editor = workspace_command.text_editor()?;
update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| {
let mut new_patterns = edit_sparse(&repo_path, old_patterns, command.settings())?;
let mut new_patterns = edit_sparse(&editor, old_patterns)?;
new_patterns.sort_unstable();
new_patterns.dedup();
Ok(new_patterns)
})
}

fn edit_sparse(
repo_path: &Path,
editor: &TextEditor,
sparse: &[RepoPathBuf],
settings: &UserSettings,
) -> Result<Vec<RepoPathBuf>, CommandError> {
let mut content = String::new();
for sparse_path in sparse {
Expand All @@ -66,13 +64,9 @@ fn edit_sparse(
writeln!(&mut content, "{path_string}").unwrap();
}

let content = edit_temp_file(
"sparse patterns",
".jjsparse",
repo_path,
&content,
settings,
)?;
let content = editor
.edit_str(content, Some(".jjsparse"))
.map_err(|err| err.with_name("sparse patterns"))?;

content
.lines()
Expand Down
13 changes: 3 additions & 10 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub(crate) fn cmd_split(
args.tool.as_deref(),
args.interactive || args.paths.is_empty(),
)?;
let text_editor = workspace_command.text_editor()?;
let mut tx = workspace_command.start_transaction();
let end_tree = commit.tree()?;
let base_tree = commit.parent_tree(tx.repo())?;
Expand Down Expand Up @@ -151,11 +152,7 @@ The remainder will be in the second commit.
"Enter a description for the first commit.",
&temp_commit,
)?;
let description = edit_description(
tx.base_workspace_helper().repo_path(),
&template,
command.settings(),
)?;
let description = edit_description(&text_editor, &template)?;
commit_builder.set_description(description);
commit_builder.write(tx.repo_mut())?
};
Expand Down Expand Up @@ -195,11 +192,7 @@ The remainder will be in the second commit.
"Enter a description for the second commit.",
&temp_commit,
)?;
edit_description(
tx.base_workspace_helper().repo_path(),
&template,
command.settings(),
)?
edit_description(&text_editor, &template)?
};
commit_builder.set_description(description);
commit_builder.write(tx.repo_mut())?
Expand Down
9 changes: 2 additions & 7 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ pub(crate) fn cmd_squash(
.to_matcher();
let diff_selector =
workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?;
let text_editor = workspace_command.text_editor()?;
let description = SquashedDescription::from_args(args);
workspace_command
.check_rewritable(sources.iter().chain(std::iter::once(&destination)).ids())?;

let mut tx = workspace_command.start_transaction();
let tx_description = format!("squash commits into {}", destination.id().hex());
let source_commits = select_diff(&tx, &sources, &destination, &matcher, &diff_selector)?;
let repo_path = tx.base_workspace_helper().repo_path().to_owned();
match rewrite::squash_commits(
tx.repo_mut(),
&source_commits,
Expand All @@ -175,12 +175,7 @@ pub(crate) fn cmd_squash(
SquashedDescription::UseDestination => Ok(destination.description().to_owned()),
SquashedDescription::Combine => {
let abandoned_commits = abandoned_commits.iter().map(|c| &c.commit).collect_vec();
combine_messages(
&repo_path,
&abandoned_commits,
&destination,
command.settings(),
)
combine_messages(&text_editor, &abandoned_commits, &destination)
}
},
)? {
Expand Down
8 changes: 2 additions & 6 deletions cli/src/commands/unsquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub(crate) fn cmd_unsquash(
} else {
None
};
let text_editor = workspace_command.text_editor()?;
let mut tx = workspace_command.start_transaction();
let parent_base_tree = parent.parent_tree(tx.repo())?;
let new_parent_tree_id;
Expand Down Expand Up @@ -116,12 +117,7 @@ aborted.
// case).
if new_parent_tree_id == parent_base_tree.id() {
tx.repo_mut().record_abandoned_commit(parent.id().clone());
let description = combine_messages(
tx.base_workspace_helper().repo_path(),
&[&parent],
&commit,
command.settings(),
)?;
let description = combine_messages(&text_editor, &[&parent], &commit)?;
// Commit the new child on top of the parent's parents.
tx.repo_mut()
.rewrite_commit(&commit)
Expand Down
34 changes: 10 additions & 24 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use jj_lib::file_util::PathError;
use jj_lib::settings::UserSettings;
use thiserror::Error;

use crate::cli_util::edit_temp_file;
use crate::cli_util::short_commit_hash;
use crate::cli_util::WorkspaceCommandTransaction;
use crate::command_error::CommandError;
Expand Down Expand Up @@ -155,34 +154,26 @@ where
text_util::complete_newline(description.trim_matches('\n'))
}

pub fn edit_description(
repo_path: &Path,
description: &str,
settings: &UserSettings,
) -> Result<String, CommandError> {
pub fn edit_description(editor: &TextEditor, description: &str) -> Result<String, CommandError> {
let description = format!(
r#"{description}
JJ: Lines starting with "JJ:" (like this one) will be removed.
"#
);

let description = edit_temp_file(
"description",
".jjdescription",
repo_path,
&description,
settings,
)?;
let description = editor
.edit_str(description, Some(".jjdescription"))
.map_err(|err| err.with_name("description"))?;

Ok(cleanup_description_lines(description.lines()))
}

/// Edits the descriptions of the given commits in a single editor session.
pub fn edit_multiple_descriptions(
ui: &Ui,
editor: &TextEditor,
tx: &WorkspaceCommandTransaction,
commits: &[(&CommitId, Commit)],
settings: &UserSettings,
) -> Result<ParsedBulkEditMessage<CommitId>, CommandError> {
let mut commits_map = IndexMap::new();
let mut bulk_message = String::new();
Expand All @@ -206,13 +197,9 @@ pub fn edit_multiple_descriptions(
}
bulk_message.push_str("JJ: Lines starting with \"JJ: \" (like this one) will be removed.\n");

let bulk_message = edit_temp_file(
"description",
".jjdescription",
tx.base_workspace_helper().repo_path(),
&bulk_message,
settings,
)?;
let bulk_message = editor
.edit_str(bulk_message, Some(".jjdescription"))
.map_err(|err| err.with_name("description"))?;

Ok(parse_bulk_edit_message(&bulk_message, &commits_map)?)
}
Expand Down Expand Up @@ -300,10 +287,9 @@ where
/// then that one is used. Otherwise we concatenate the messages and ask the
/// user to edit the result in their editor.
pub fn combine_messages(
repo_path: &Path,
editor: &TextEditor,
sources: &[&Commit],
destination: &Commit,
settings: &UserSettings,
) -> Result<String, CommandError> {
let non_empty = sources
.iter()
Expand All @@ -330,7 +316,7 @@ pub fn combine_messages(
combined.push_str("\nJJ: Description from source commit:\n");
combined.push_str(commit.description());
}
edit_description(repo_path, &combined, settings)
edit_description(editor, &combined)
}

/// Create a description from a list of paragraphs.
Expand Down

0 comments on commit 641ec52

Please sign in to comment.