From 0d215788462ec13a6771041470b322326de52b60 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 10 Nov 2023 09:38:03 +0900 Subject: [PATCH] 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. --- CHANGELOG.md | 3 +++ cli/src/commands/branch.rs | 11 ++++++++++- cli/tests/test_branch_command.rs | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 731575284..90379bef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index 0d9827e7e..77ea8ea29 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -123,7 +123,7 @@ pub struct BranchForgetArgs { pub glob: Vec, } -/// 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() diff --git a/cli/tests/test_branch_command.rs b/cli/tests/test_branch_command.rs index 283a347ed..3dddc37eb 100644 --- a/cli/tests/test_branch_command.rs +++ b/cli/tests/test_branch_command.rs @@ -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, @"");