Skip to content

Commit

Permalink
cli: squash: inline move_diff() to address "too many arguments" linte…
Browse files Browse the repository at this point in the history
…r warning
  • Loading branch information
yuja committed Jan 2, 2025
1 parent 3cdd571 commit 5adeb63
Showing 1 changed file with 44 additions and 69 deletions.
113 changes: 44 additions & 69 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::rewrite;
use jj_lib::rewrite::CommitToSquash;
use jj_lib::settings::UserSettings;
use tracing::instrument;

use crate::cli_util::CommandHelper;
Expand Down Expand Up @@ -157,82 +156,30 @@ pub(crate) fn cmd_squash(
.to_matcher();
let diff_selector =
workspace_command.diff_selector(ui, args.tool.as_deref(), args.interactive)?;
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());
move_diff(
ui,
&mut tx,
command.settings(),
&sources,
&destination,
matcher.as_ref(),
&diff_selector,
SquashedDescription::from_args(args),
args.revision.is_none() && args.from.is_empty() && args.into.is_none(),
&args.paths,
args.keep_emptied,
)?;
tx.finish(ui, tx_description)?;
Ok(())
}

enum SquashedDescription {
// Use this exact description.
Exact(String),
// Use the destination's description and discard the descriptions of the
// source revisions.
UseDestination,
// Combine the descriptions of the source and destination revisions.
Combine,
}

impl SquashedDescription {
fn from_args(args: &SquashArgs) -> Self {
// These options are incompatible and Clap is configured to prevent this.
assert!(args.message_paragraphs.is_empty() || !args.use_destination_message);

if !args.message_paragraphs.is_empty() {
let desc = join_message_paragraphs(&args.message_paragraphs);
SquashedDescription::Exact(desc)
} else if args.use_destination_message {
SquashedDescription::UseDestination
} else {
SquashedDescription::Combine
}
}
}

#[allow(clippy::too_many_arguments)]
fn move_diff(
ui: &mut Ui,
tx: &mut WorkspaceCommandTransaction,
settings: &UserSettings,
sources: &[Commit],
destination: &Commit,
matcher: &dyn Matcher,
diff_selector: &DiffSelector,
description: SquashedDescription,
no_rev_arg: bool,
path_arg: &[String],
keep_emptied: bool,
) -> Result<(), CommandError> {
tx.base_workspace_helper()
.check_rewritable(sources.iter().chain(std::iter::once(destination)).ids())?;

let source_commits = select_diff(tx, sources, destination, matcher, diff_selector)?;

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,
destination,
keep_emptied,
&destination,
args.keep_emptied,
|abandoned_commits| match description {
SquashedDescription::Exact(description) => Ok(description),
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, settings)
combine_messages(
&repo_path,
&abandoned_commits,
&destination,
command.settings(),
)
}
},
)? {
Expand All @@ -241,7 +188,9 @@ fn move_diff(
return Err(user_error("No changes selected"));
}

if let [only_path] = path_arg {
if let [only_path] = &*args.paths {
let no_rev_arg =
args.revision.is_none() && args.from.is_empty() && args.into.is_none();
if no_rev_arg
&& tx
.base_workspace_helper()
Expand All @@ -255,10 +204,36 @@ fn move_diff(
)?;
}
}
}
rewrite::SquashResult::NewCommit(_) => {}
}
tx.finish(ui, tx_description)?;
Ok(())
}

Ok(())
enum SquashedDescription {
// Use this exact description.
Exact(String),
// Use the destination's description and discard the descriptions of the
// source revisions.
UseDestination,
// Combine the descriptions of the source and destination revisions.
Combine,
}

impl SquashedDescription {
fn from_args(args: &SquashArgs) -> Self {
// These options are incompatible and Clap is configured to prevent this.
assert!(args.message_paragraphs.is_empty() || !args.use_destination_message);

if !args.message_paragraphs.is_empty() {
let desc = join_message_paragraphs(&args.message_paragraphs);
SquashedDescription::Exact(desc)
} else if args.use_destination_message {
SquashedDescription::UseDestination
} else {
SquashedDescription::Combine
}
rewrite::SquashResult::NewCommit(_) => Ok(()),
}
}

Expand Down

0 comments on commit 5adeb63

Please sign in to comment.