Skip to content

Commit

Permalink
cli_util: Make resolve_base_revs use IndexSet
Browse files Browse the repository at this point in the history
This will make deduplication easier in the next commit.
The error message becomes slightly less informative, unfortunately.
  • Loading branch information
ilyagr committed Feb 5, 2023
1 parent 2c53946 commit e13eaab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
13 changes: 5 additions & 8 deletions src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,19 +1412,16 @@ pub fn resolve_multiple_nonempty_revsets(
pub fn resolve_base_revs(
workspace_command: &WorkspaceCommandHelper,
revisions: &[RevisionArg],
) -> Result<Vec<Commit>, CommandError> {
let mut commits = vec![];
) -> Result<IndexSet<Commit>, CommandError> {
let mut commits = IndexSet::new();
for revision_str in revisions {
let commit = workspace_command.resolve_single_rev(revision_str)?;
if let Some(i) = commits.iter().position(|c| c == &commit) {
let commit_hash = short_commit_hash(commit.id());
if !commits.insert(commit) {
return Err(user_error(format!(
r#"Revset "{}" and "{}" resolved to the same revision {}"#,
&revisions[i].0,
&revision_str.0,
short_commit_hash(commit.id()),
r#"More than one revset resolved to revision {commit_hash}"#,
)));
}
commits.push(commit);
}

let root_commit_id = workspace_command.repo().store().root_commit_id();
Expand Down
8 changes: 6 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,9 @@ fn cmd_new(ui: &mut Ui, command: &CommandHelper, args: &NewArgs) -> Result<(), C
!args.revisions.is_empty(),
"expected a non-empty list from clap"
);
let commits = resolve_base_revs(&workspace_command, &args.revisions)?;
let commits = resolve_base_revs(&workspace_command, &args.revisions)?
.into_iter()
.collect_vec();
let parent_ids = commits.iter().map(|c| c.id().clone()).collect();
let mut tx = workspace_command.start_transaction("new empty commit");
let merged_tree = merge_commit_trees(tx.base_repo().as_repo_ref(), &commits);
Expand Down Expand Up @@ -2685,7 +2687,9 @@ fn cmd_merge(ui: &mut Ui, command: &CommandHelper, args: &NewArgs) -> Result<(),

fn cmd_rebase(ui: &mut Ui, command: &CommandHelper, args: &RebaseArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let new_parents = resolve_base_revs(&workspace_command, &args.destination)?;
let new_parents = resolve_base_revs(&workspace_command, &args.destination)?
.into_iter()
.collect_vec();
if let Some(rev_str) = &args.revision {
rebase_revision(ui, command, &mut workspace_command, &new_parents, rev_str)?;
} else if let Some(source_str) = &args.source {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_new_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn test_new_merge() {
// merge with non-unique revisions
let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "@", "200e"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "@" and "200e" resolved to the same revision 200ed1a14c8a
Error: More than one revset resolved to revision 200ed1a14c8a
"###);

// merge with root
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rebase_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn test_rebase_multiple_destinations() {

let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r", "a", "-d", "b", "-d", "b"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "b" and "b" resolved to the same revision d370aee184ba
Error: More than one revset resolved to revision d370aee184ba
"###);

let stderr =
Expand Down

0 comments on commit e13eaab

Please sign in to comment.