diff --git a/src/commands.rs b/src/commands.rs index 5033c21fc..44312d272 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4364,6 +4364,18 @@ fn cmd_git_push( BranchPushAction::LocalConflicted => {} BranchPushAction::RemoteConflicted => {} BranchPushAction::Update(update) => { + if let Some(new_target) = &update.new_target { + let new_target_commit = repo.store().get_commit(new_target)?; + // TODO: Should we also skip branches that have open commits as ancestors? + if new_target_commit.is_open() { + writeln!( + ui, + "Skipping branch '{}' since it points to an open commit.", + branch_name + )?; + continue; + } + } branch_updates.insert(branch_name, update); } } diff --git a/tests/test_git_push.rs b/tests/test_git_push.rs index 6f8aed1d7..f936e4942 100644 --- a/tests/test_git_push.rs +++ b/tests/test_git_push.rs @@ -37,6 +37,29 @@ fn test_git_push() { insta::assert_snapshot!(get_stdout_string(&assert), @"Nothing changed. "); + // When pushing everything, won't push an open commit even if there's a branch + // on it + test_env + .jj_cmd(&workspace_root, &["branch", "my-branch"]) + .assert() + .success(); + let assert = test_env + .jj_cmd(&workspace_root, &["git", "push"]) + .assert() + .success(); + insta::assert_snapshot!(get_stdout_string(&assert), @r###" + Skipping branch 'my-branch' since it points to an open commit. + Nothing changed. + "###); + + // When pushing a specific branch, won't push it if it points to an open commit + let assert = test_env + .jj_cmd(&workspace_root, &["git", "push", "--branch", "my-branch"]) + .assert() + .failure(); + insta::assert_snapshot!(get_stdout_string(&assert), @"Error: Won't push open commit +"); + // Try pushing a conflict std::fs::write(workspace_root.join("file"), "first").unwrap(); test_env @@ -65,6 +88,6 @@ fn test_git_push() { .jj_cmd(&workspace_root, &["git", "push"]) .assert() .failure(); - insta::assert_snapshot!(get_stdout_string(&assert), @"Error: Won't push commit 56e09a8ca383 since it has conflicts + insta::assert_snapshot!(get_stdout_string(&assert), @"Error: Won't push commit 28b5642cb786 since it has conflicts "); }