cmd: prohibit creating branches at the root commit

Such branches lead to confusing errors on git expoert or push.
This commit is contained in:
Ilya Grigoriev 2023-04-03 19:02:12 -07:00
parent 31f7c806e2
commit 233dda1057
3 changed files with 16 additions and 0 deletions

View file

@ -86,6 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj workspace update-stale` now snapshots the working-copy changes before
updating to the new working-copy commit.
* It is no longer allowed to create branches at the root commit.
## [0.7.0] - 2023-02-16
### Breaking changes

View file

@ -136,6 +136,7 @@ fn cmd_branch_create(
let target_commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
workspace_command.check_rewritable(&target_commit)?;
let mut tx = workspace_command.start_transaction(&format!(
"create {} pointing to commit {}",
make_branch_term(&branch_names),
@ -168,6 +169,7 @@ fn cmd_branch_set(
let target_commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
workspace_command.check_rewritable(&target_commit)?;
if !args.allow_backwards
&& !branch_names.iter().all(|branch_name| {
is_fast_forward(

View file

@ -45,6 +45,18 @@ fn test_branch_multiple_names() {
"###);
}
#[test]
fn test_branch_forbidden_at_root() {
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");
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "create", "fred", "-r=root"]);
insta::assert_snapshot!(stderr, @r###"
Error: Cannot rewrite the root commit
"###);
}
#[test]
fn test_branch_empty_name() {
let test_env = TestEnvironment::default();