diff --git a/cli/src/commands/git/push.rs b/cli/src/commands/git/push.rs index 9dfffa61b8..39e49d5f9d 100644 --- a/cli/src/commands/git/push.rs +++ b/cli/src/commands/git/push.rs @@ -92,6 +92,9 @@ pub struct GitPushArgs { /// Allow pushing commits with empty descriptions #[arg(long)] allow_empty_description: bool, + /// Allow pushing commits that are private + #[arg(long)] + allow_private: bool, /// Push branches pointing to these commits (can be repeated) #[arg(long, short)] revisions: Vec, @@ -322,7 +325,7 @@ pub fn cmd_git_push( if commit.has_conflict()? { reasons.push("it has conflicts"); } - if is_private.as_ref().map_or(false, |f| f(commit.id())) { + if !args.allow_private && is_private.as_ref().map_or(false, |f| f(commit.id())) { reasons.push("it is private"); } if !reasons.is_empty() { diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 66ab5779ae..788bfdc8d9 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -943,6 +943,7 @@ Before the command actually moves, creates, or deletes a remote branch, it makes Only tracked branches can be successfully deleted on the remote. A warning will be printed if any untracked branches on the remote correspond to missing local branches. * `--allow-empty-description` — Allow pushing commits with empty descriptions +* `--allow-private` — Allow pushing commits that are private * `-r`, `--revisions ` — Push branches pointing to these commits (can be repeated) * `-c`, `--change ` — Push this commit by creating a branch based on its change ID (can be repeated) * `--dry-run` — Only display what will change on the remote diff --git a/cli/tests/test_private_commits.rs b/cli/tests/test_private_commits.rs index 38c6d23962..138d5f1842 100644 --- a/cli/tests/test_private_commits.rs +++ b/cli/tests/test_private_commits.rs @@ -96,6 +96,34 @@ fn test_git_private_commits_block_pushing() { "###); } +#[test] +fn test_git_private_commits_can_be_overridden() { + let (test_env, workspace_root) = set_up(); + + test_env.jj_cmd_ok(&workspace_root, &["new", "main", "-m=private 1"]); + test_env.jj_cmd_ok(&workspace_root, &["branch", "set", "main"]); + + // Will not push when a pushed commit is contained in git.private-commits + test_env.add_config(r#"git.private-commits = "description(glob:'private*')""#); + let stderr = test_env.jj_cmd_failure(&workspace_root, &["git", "push", "--all"]); + insta::assert_snapshot!(stderr, @r###" + Error: Won't push commit aa3058ff8663 since it is private + "###); + + // May push when the commit is removed from git.private-commits + let (_, stderr) = test_env.jj_cmd_ok( + &workspace_root, + &["git", "push", "--all", "--allow-private"], + ); + insta::assert_snapshot!(stderr, @r###" + Branch changes to push to origin: + Move forward branch main from 7eb97bf230ad to aa3058ff8663 + Warning: The working-copy commit in workspace 'default' became immutable, so a new commit has been created on top of it. + Working copy now at: znkkpsqq 2e1adf47 (empty) (no description set) + Parent commit : yqosqzyt aa3058ff main | (empty) private 1 + "###); +} + #[test] fn test_git_private_commits_are_not_checked_if_immutable() { let (test_env, workspace_root) = set_up();