cli: show hint if e.g. checkout revset resolved to multiple revisions

As suggested at
https://github.com/martinvonz/jj/issues/878#issuecomment-1345466602
This commit is contained in:
Yuya Nishihara 2022-12-15 20:37:14 +09:00
parent 6889d64ffc
commit 9158c96202
2 changed files with 63 additions and 3 deletions

View file

@ -615,9 +615,20 @@ impl WorkspaceCommandHelper {
(None, _) => Err(user_error(format!(
"Revset \"{revision_str}\" didn't resolve to any revisions"
))),
(Some(_), Some(_)) => Err(user_error(format!(
"Revset \"{revision_str}\" resolved to more than one revision"
))),
(Some(commit0), Some(commit1)) => {
let mut iter = [commit0, commit1].into_iter().chain(iter);
let commits = iter.by_ref().take(5).collect::<Result<Vec<_>, _>>()?;
let elided = iter.next().is_some();
let hint = format!(
"The revset resolved to these revisions:\n{commits}{ellipsis}",
commits = commits.iter().map(short_commit_description).join("\n"),
ellipsis = elided.then(|| "\n...").unwrap_or_default()
);
Err(user_error_with_hint(
format!("Revset \"{revision_str}\" resolved to more than one revision"),
hint,
))
}
}
}

View file

@ -50,6 +50,55 @@ fn test_checkout() {
"###);
}
#[test]
fn test_checkout_not_single_rev() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "first"]);
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "second"]);
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "third"]);
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "fourth"]);
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "fifth"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["checkout", "root..@"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "root..@" resolved to more than one revision
Hint: The revset resolved to these revisions:
eb01ec3263be ()
17b6965c1349 (fifth)
f8381c76d1d3 (fourth)
ca3820f77363 (third)
91043abe9d03 (second)
...
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["checkout", "root..@-"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "root..@-" resolved to more than one revision
Hint: The revset resolved to these revisions:
17b6965c1349 (fifth)
f8381c76d1d3 (fourth)
ca3820f77363 (third)
91043abe9d03 (second)
85a1e2839620 (first)
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["checkout", "@-|@--"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "@-|@--" resolved to more than one revision
Hint: The revset resolved to these revisions:
17b6965c1349 (fifth)
f8381c76d1d3 (fourth)
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["checkout", "none()"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "none()" didn't resolve to any revisions
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id " " description"#])
}