From 08d1809dc1cfcfcec9ecca5a0acc453d7c764033 Mon Sep 17 00:00:00 2001 From: Essien Ita Essien Date: Thu, 4 Jan 2024 14:55:15 +0000 Subject: [PATCH] Issue warning if renaming branch with a remote tracking branch. --- CHANGELOG.md | 3 +++ cli/src/commands/branch.rs | 14 +++++++++++++ cli/tests/test_branch_command.rs | 35 +++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9006ddb87..bbcc7012f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index d8a975f1c..cf4828488 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -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); @@ -363,6 +376,7 @@ fn cmd_branch_rename( make_branch_term(&[new_branch]), ), )?; + Ok(()) } diff --git a/cli/tests/test_branch_command.rs b/cli/tests/test_branch_command.rs index 7cd10cb15..29ede5e93 100644 --- a/cli/tests/test_branch_command.rs +++ b/cli/tests/test_branch_command.rs @@ -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: conflictfoo + 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###" + warning: Branch bremote has remote branches which will not be renamed "###); }