Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: prohibit creating branches at the root commit #1479

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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