From 8d283f9aa53ef57eb0c322fe9e27058b275861a5 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Mon, 3 Apr 2023 19:02:12 -0700 Subject: [PATCH] cmd: prohibit creating branches at the root commit Such branches lead to confusing errors on git expoert or push. --- CHANGELOG.md | 2 ++ src/commands/branch.rs | 2 ++ tests/test_branch_command.rs | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 591db0d97c..6d7297b943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,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 diff --git a/src/commands/branch.rs b/src/commands/branch.rs index fe0b74c5c8..069db3cf19 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -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( diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 4b61bc77dd..ee73a07372 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -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();