Skip to content

Commit

Permalink
cli: move resolve_destination_revs to mod.rs
Browse files Browse the repository at this point in the history
Summary: This is currently used by `new.rs`, `workspace.rs`, and `rebase.rs`,
so just move it into the parent module for cleanliness, but keep the
`pub(crate)` qualifier.

Signed-off-by: Austin Seipp <[email protected]>
Change-Id: I0ea12afd8107f95a37a91340820221a0
  • Loading branch information
thoughtpolice committed Nov 3, 2023
1 parent c0cab8d commit 2622663
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
23 changes: 21 additions & 2 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ use std::io::Write;
use std::{fmt, fs, io};

use clap::{Command, CommandFactory, FromArgMatches, Subcommand};
use indexmap::IndexSet;
use itertools::Itertools;
use jj_lib::commit::Commit;
use jj_lib::matchers::EverythingMatcher;
use jj_lib::merged_tree::MergedTree;
use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo::{ReadonlyRepo, Repo};
use jj_lib::rewrite::merge_commit_trees;
use jj_lib::settings::UserSettings;
use tracing::instrument;

use crate::cli_util::{
run_ui_editor, user_error, Args, CommandError, CommandHelper, WorkspaceCommandHelper,
resolve_multiple_nonempty_revsets_default_single, run_ui_editor, user_error, Args,
CommandError, CommandHelper, RevisionArg, WorkspaceCommandHelper,
};
use crate::diff_util::{self, DiffFormat};
use crate::formatter::PlainTextFormatter;
Expand Down Expand Up @@ -275,6 +277,23 @@ fn make_branch_term(branch_names: &[impl fmt::Display]) -> String {
}
}

/// Resolves revsets into revisions to rebase onto. These revisions don't have
/// to be rewriteable.
pub(crate) fn resolve_destination_revs(
workspace_command: &WorkspaceCommandHelper,
ui: &mut Ui,
revisions: &[RevisionArg],
) -> Result<IndexSet<Commit>, CommandError> {
let commits =
resolve_multiple_nonempty_revsets_default_single(workspace_command, ui, revisions)?;
let root_commit_id = workspace_command.repo().store().root_commit_id();
if commits.len() >= 2 && commits.iter().any(|c| c.id() == root_commit_id) {
Err(user_error("Cannot merge with root revision"))
} else {
Ok(commits)
}
}

pub fn default_app() -> Command {
Commands::augment_subcommands(Args::command())
}
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use tracing::instrument;
use crate::cli_util::{
self, short_commit_hash, user_error, CommandError, CommandHelper, RevisionArg,
};
use crate::commands::rebase::resolve_destination_revs;
use crate::ui::Ui;

/// Create a new, empty change and edit it in the working copy
Expand Down Expand Up @@ -76,7 +75,7 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.",
!args.revisions.is_empty(),
"expected a non-empty list from clap"
);
let target_commits = resolve_destination_revs(&workspace_command, ui, &args.revisions)?
let target_commits = super::resolve_destination_revs(&workspace_command, ui, &args.revisions)?
.into_iter()
.collect_vec();
let target_ids = target_commits.iter().map(|c| c.id().clone()).collect_vec();
Expand Down
19 changes: 1 addition & 18 deletions cli/src/commands/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets
));
}
let mut workspace_command = command.workspace_helper(ui)?;
let new_parents = resolve_destination_revs(&workspace_command, ui, &args.destination)?
let new_parents = super::resolve_destination_revs(&workspace_command, ui, &args.destination)?
.into_iter()
.collect_vec();
if let Some(rev_str) = &args.revision {
Expand Down Expand Up @@ -390,20 +390,3 @@ fn check_rebase_destinations(
}
Ok(())
}

/// Resolves revsets into revisions to rebase onto. These revisions don't have
/// to be rewriteable.
pub(crate) fn resolve_destination_revs(
workspace_command: &WorkspaceCommandHelper,
ui: &mut Ui,
revisions: &[RevisionArg],
) -> Result<IndexSet<Commit>, CommandError> {
let commits =
resolve_multiple_nonempty_revsets_default_single(workspace_command, ui, revisions)?;
let root_commit_id = workspace_command.repo().store().root_commit_id();
if commits.len() >= 2 && commits.iter().any(|c| c.id() == root_commit_id) {
Err(user_error("Cannot merge with root revision"))
} else {
Ok(commits)
}
}
10 changes: 3 additions & 7 deletions cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,9 @@ fn cmd_workspace_add(
vec![tx.repo().store().root_commit()]
}
} else {
crate::commands::rebase::resolve_destination_revs(
&old_workspace_command,
ui,
&args.revision,
)?
.into_iter()
.collect_vec()
super::resolve_destination_revs(&old_workspace_command, ui, &args.revision)?
.into_iter()
.collect_vec()
};

let tree = merge_commit_trees(tx.repo(), &parents)?;
Expand Down

0 comments on commit 2622663

Please sign in to comment.