Skip to content

Commit

Permalink
Issue warning if renaming branch with a remote tracking branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
essiene committed Jan 7, 2024
1 parent 0a005a6 commit 02d886e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* New `jj op abandon` command is added to clean up the operation history. If GC
is implemented, Git refs and commit objects can be compacted.

* `jj branch rename` will now warn if the renamed branch has a remote branch, since
those will have to be manually renamed outside of `jj`.

### Fixed bugs


Expand Down
14 changes: 14 additions & 0 deletions cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ fn cmd_branch_rename(
return Err(user_error(format!("Branch already exists: {new_branch}")));
}

if view
.remote_branches_matching(
&StringPattern::exact(old_branch),
&StringPattern::everything(),
)
.any(|(_, remote_ref)| remote_ref.is_tracking())
{
writeln!(
ui.warning(),
"warning: Branch {old_branch} has remote branches which will not be renamed"
)?;
}

let mut tx = workspace_command.start_transaction();
tx.mut_repo()
.set_local_branch_target(new_branch, ref_target);
Expand All @@ -363,6 +376,7 @@ fn cmd_branch_rename(
make_branch_term(&[new_branch]),
),
)?;

Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions cli/tests/snapshots/test_branch_command__.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: cli/tests/test_branch_command.rs
expression: "\"moo\""
---
moo
35 changes: 28 additions & 7 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,41 @@ fn test_branch_rename() {
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", "rename", "foo", "bar"]);
// Set up remote
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(
&repo_path,
&["git", "remote", "add", "origin", "../git-repo"],
);

let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "rename", "bnoexist", "blocal"]);
insta::assert_snapshot!(stderr, @r###"
Error: No such branch: foo
Error: No such branch: bnoexist
"###);

test_env.jj_cmd_ok(&repo_path, &["branch", "create", "foo"]);
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "rename", "foo", "bar"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=commit-0"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "blocal"]);
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "rename", "blocal", "blocal1"]);
insta::assert_snapshot!(stderr, @"");

test_env.jj_cmd_ok(&repo_path, &["new"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "conflictfoo"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "rename", "bar", "conflictfoo"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=commit-1"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "bexist"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "rename", "blocal1", "bexist"]);
insta::assert_snapshot!(stderr, @r###"
Error: Branch already exists: bexist
"###);

test_env.jj_cmd_ok(&repo_path, &["new"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=commit-2"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "bremote"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push", "-b=bremote"]);
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "rename", "bremote", "bremote2"]);
insta::assert_snapshot!(stderr, @r###"
Error: Branch already exists: conflictfoo
warning: Branch bremote has remote branches which will not be renamed
"###);
}

Expand Down

0 comments on commit 02d886e

Please sign in to comment.