diff --git a/src/commands.rs b/src/commands.rs index 33bda8856..0ec28acdc 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4928,16 +4928,18 @@ fn cmd_git_push( ui.settings().push_branch_prefix(), commit.change_id().hex() ); - tx.mut_repo() - .set_local_branch(branch_name.clone(), RefTarget::Normal(commit.id().clone())); - if let Some(update) = - branch_updates_for_push(tx.mut_repo().as_repo_ref(), &args.remote, &branch_name)? - { + if tx.mut_repo().get_local_branch(&branch_name).is_none() { writeln!( ui, "Creating branch {} for revision {}", branch_name, change_str )?; + } + tx.mut_repo() + .set_local_branch(branch_name.clone(), RefTarget::Normal(commit.id().clone())); + if let Some(update) = + branch_updates_for_push(tx.mut_repo().as_repo_ref(), &args.remote, &branch_name)? + { branch_updates.insert(branch_name.clone(), update); } else { writeln!( diff --git a/tests/test_git_push.rs b/tests/test_git_push.rs index a0a92b370..0ebfa1297 100644 --- a/tests/test_git_push.rs +++ b/tests/test_git_push.rs @@ -14,7 +14,9 @@ use std::path::PathBuf; -use crate::common::TestEnvironment; +use regex::Regex; + +use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; pub mod common; @@ -60,9 +62,14 @@ fn test_git_push_open() { Error: Won't push open commit "###); // When pushing with `--change`, won't push if it points to an open commit - let stderr = - test_env.jj_cmd_failure(&workspace_root, &["git", "push", "--change", "my-branch"]); - insta::assert_snapshot!(stderr, @r###" + let assert = test_env + .jj_cmd(&workspace_root, &["git", "push", "--change", "my-branch"]) + .assert(); + let branch_pattern = Regex::new("push-[0-9a-f]+").unwrap(); + insta::assert_snapshot!(branch_pattern.replace(&get_stdout_string(&assert), ""), @r###" + Creating branch for revision my-branch + "###); + insta::assert_snapshot!(get_stderr_string(&assert), @r###" Error: Won't push open commit "###); }