cli: disallow to create new branch by "jj branch set"

Per discussion in https://github.com/martinvonz/jj/discussions/2555. I'm
okay with either way, but it's confusing if we had "branch create" and
"branch set" and both of these could create a new branch.
This commit is contained in:
Yuya Nishihara 2023-11-10 09:38:03 +09:00
parent aca2a3cf93
commit 0d21578846
3 changed files with 19 additions and 1 deletions

View File

@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* The `remote_branches()` revset no longer includes branches exported to the Git
repository (so called Git-tracking branches.)
* `jj branch set` no longer creates a new branch. Use `jj branch create`
instead.
### New features
* `jj workspace add` can now take _multiple_ `--revision` arguments, which will

View File

@ -123,7 +123,7 @@ pub struct BranchForgetArgs {
pub glob: Vec<StringPattern>,
}
/// Update a given branch to point to a certain commit.
/// Update an existing branch to point to a certain commit.
#[derive(clap::Args, Clone, Debug)]
pub struct BranchSetArgs {
/// The branch's target revision.
@ -298,6 +298,15 @@ fn cmd_branch_set(
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"), ui)?;
let repo = workspace_command.repo().as_ref();
let branch_names = &args.names;
for name in branch_names {
let old_target = repo.view().get_local_branch(name);
if old_target.is_absent() {
return Err(user_error_with_hint(
format!("No such branch: {name}"),
"Use `jj branch create` to create it.",
));
}
}
if !args.allow_backwards
&& !branch_names
.iter()

View File

@ -98,6 +98,12 @@ fn test_branch_move() {
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "set", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: No such branch: foo
Hint: Use `jj branch create` to create it.
"###);
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "create", "foo"]);
insta::assert_snapshot!(stderr, @"");