Skip to content

Commit

Permalink
cmd: prohibit creating branches at the root commit
Browse files Browse the repository at this point in the history
Such branches lead to confusing errors on git expoert or push.
  • Loading branch information
ilyagr committed Apr 4, 2023
1 parent 31f7c80 commit 233dda1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 233dda1

Please sign in to comment.