ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: hint for conflicted branches

This commit is contained in:
Willian Mori 2023-09-09 19:47:27 -03:00 committed by Willian Mori
parent a3ed43efc3
commit 15e2cc22ce
2 changed files with 55 additions and 7 deletions

View file

@ -51,9 +51,9 @@ use jj_lib::repo::{
};
use jj_lib::repo_path::{FsPathParseError, RepoPath};
use jj_lib::revset::{
DefaultSymbolResolver, Revset, RevsetAliasesMap, RevsetEvaluationError, RevsetExpression,
RevsetIteratorExt, RevsetParseContext, RevsetParseError, RevsetParseErrorKind,
RevsetResolutionError, RevsetWorkspaceContext,
DefaultSymbolResolver, Revset, RevsetAliasesMap, RevsetCommitRef, RevsetEvaluationError,
RevsetExpression, RevsetIteratorExt, RevsetParseContext, RevsetParseError,
RevsetParseErrorKind, RevsetResolutionError, RevsetWorkspaceContext,
};
use jj_lib::settings::{ConfigResultExt as _, UserSettings};
use jj_lib::transaction::Transaction;
@ -1005,7 +1005,7 @@ impl WorkspaceCommandHelper {
ui: &mut Ui,
) -> Result<Commit, CommandError> {
let revset_expression = self.parse_revset(revision_str, Some(ui))?;
let revset = self.evaluate_revset(revset_expression)?;
let revset = self.evaluate_revset(revset_expression.clone())?;
let mut iter = revset.iter().commits(self.repo().store()).fuse();
match (iter.next(), iter.next()) {
(Some(commit), None) => Ok(commit?),
@ -1021,10 +1021,23 @@ impl WorkspaceCommandHelper {
.map(|c| self.format_commit_summary(c))
.join("\n")
+ elided.then_some("\n...").unwrap_or_default();
let hint = format!(
r#"The revset "{revision_str}" resolved to these revisions:
let hint = if let RevsetExpression::CommitRef(RevsetCommitRef::Symbol(
branch_name,
)) = revset_expression.as_ref()
{
// Separate hint if there's a conflicted branch
format!(
r#"Branch {branch_name} resolved to multiple revisions because it's conflicted.
It resolved to these revisions:
{commits_summary}
Set which revision the branch points to with `jj branch set {branch_name} -r <REVISION>`."#,
)
} else {
format!(
r#"The revset "{revision_str}" resolved to these revisions:
{commits_summary}"#,
);
)
};
Err(user_error_with_hint(
format!(r#"Revset "{revision_str}" resolved to more than one revision"#),
hint,

View file

@ -100,6 +100,41 @@ fn test_checkout_not_single_rev() {
"###);
}
#[test]
fn test_checkout_conflicting_branches() {
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, &["describe", "-m", "one"]);
test_env.jj_cmd_success(&repo_path, &["new", "-m", "two", "@-"]);
test_env.jj_cmd_success(&repo_path, &["branch", "create", "foo"]);
test_env.jj_cmd_success(
&repo_path,
&[
"--at-op=@-",
"branch",
"create",
"foo",
"-r",
r#"description("one")"#,
],
);
// Trigger resolution of concurrent operations
test_env.jj_cmd_success(&repo_path, &["st"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["checkout", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revset "foo" resolved to more than one revision
Hint: Branch foo resolved to multiple revisions because it's conflicted.
It resolved to these revisions:
kkmpptxz 66c6502d foo?? | (empty) two
qpvuntsm a9330854 foo?? | (empty) one
Set which revision the branch points to with `jj branch set foo -r <REVISION>`.
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"commit_id ++ " " ++ description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])