From 7fb6912a8229b0a64f35b8b723fa58cabcdd7cec Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 25 Apr 2024 16:56:03 -0700 Subject: [PATCH] cli: add global `--ignore-immutable` Closes #3576 --- CHANGELOG.md | 2 ++ cli/src/cli_util.rs | 21 +++++++++++++++++++++ cli/tests/cli-reference@.md.snap | 4 ++++ cli/tests/test_global_opts.rs | 1 + cli/tests/test_immutable_commits.rs | 19 +++++++++++++++++-- 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df804949a5d..24cd70867ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * You can check whether Watchman fsmonitor is enabled or installed with the new `jj debug watchman status` command. +* A new global flag `--ignore-immutable` lets you rewrite immutable commits. + ### Fixed bugs * Revsets now support `\`-escapes in string literal. diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 4be76be21ce..12df8994b20 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -1065,6 +1065,17 @@ impl WorkspaceCommandHelper { &self, commits: impl IntoIterator, ) -> Result<(), CommandError> { + if self.global_args.ignore_immutable { + let root_id = self.repo().store().root_commit_id(); + return if commits.into_iter().contains(root_id) { + Err(user_error(format!( + "The root commit {} is immutable", + short_commit_hash(root_id), + ))) + } else { + Ok(()) + }; + } let to_rewrite_revset = RevsetExpression::commits(commits.into_iter().cloned().collect_vec()); let immutable = revset_util::parse_immutable_expression(&self.revset_parse_context()) @@ -2330,6 +2341,16 @@ pub struct GlobalArgs { /// implies `--ignore-working-copy`. #[arg(long, global = true)] pub ignore_working_copy: bool, + /// Don't prevent rewriting immutable commits + /// + /// By default, Jujutsu prevents rewriting commits in the configured set of + /// immutable commits. This option disables that check and lets you rewrite + /// any commit but the root commit. + /// + /// This option only affects the check. It does not affect the + /// `immutable_heads()` revset or the `immutable` template keyword. + #[arg(long, global = true)] + pub ignore_immutable: bool, /// Operation to load the repo at /// /// Operation to load the repo at. By default, Jujutsu loads the repo at the diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 8c5a2e61e45..76bc688160a 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -153,6 +153,10 @@ To get started, see the tutorial at https://github.com/martinvonz/jj/blob/main/d Possible values: `true`, `false` +* `--ignore-immutable` — Don't prevent rewriting immutable commits + + Possible values: `true`, `false` + * `--at-operation ` — Operation to load the repo at Default value: `@` diff --git a/cli/tests/test_global_opts.rs b/cli/tests/test_global_opts.rs index 9b55fd52178..0dd196f2640 100644 --- a/cli/tests/test_global_opts.rs +++ b/cli/tests/test_global_opts.rs @@ -592,6 +592,7 @@ fn test_help() { Global Options: -R, --repository Path to repository to operate on --ignore-working-copy Don't snapshot the working copy, and don't update it + --ignore-immutable Don't prevent rewriting immutable commits --at-operation Operation to load the repo at [default: @] [aliases: at-op] --debug Enable debug logging --color When to colorize output (always, never, auto) diff --git a/cli/tests/test_immutable_commits.rs b/cli/tests/test_immutable_commits.rs index 32d03cd8ead..aa6161e198d 100644 --- a/cli/tests/test_immutable_commits.rs +++ b/cli/tests/test_immutable_commits.rs @@ -67,6 +67,22 @@ fn test_rewrite_immutable_generic() { For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md. "###); + // Can use --ignore-immutable to override + test_env.add_config(r#"revset-aliases."immutable_heads()" = "main""#); + let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["--ignore-immutable", "edit", "main"]); + insta::assert_snapshot!(stdout, @r###" + "###); + insta::assert_snapshot!(stderr, @r###" + Working copy now at: kkmpptxz c8d4c7ca main | b + Parent commit : qpvuntsm 46a8dc51 a + Added 0 files, modified 1 files, removed 0 files + "###); + // ... but not the root commit + let stderr = test_env.jj_cmd_failure(&repo_path, &["--ignore-immutable", "edit", "root()"]); + insta::assert_snapshot!(stderr, @r###" + Error: The root commit 000000000000 is immutable + "###); + // Mutating the repo works if ref is wrapped in present() test_env.add_config( r#"revset-aliases."immutable_heads()" = "present(branch_that_does_not_exist)""#, @@ -75,9 +91,8 @@ fn test_rewrite_immutable_generic() { insta::assert_snapshot!(stdout, @r###" "###); insta::assert_snapshot!(stderr, @r###" - Working copy now at: kpqxywon dbce15b4 (empty) (no description set) + Working copy now at: wqnwkozp de8b93b4 (empty) (no description set) Parent commit : kkmpptxz c8d4c7ca main | b - Added 0 files, modified 1 files, removed 0 files "###); // Error if we redefine immutable_heads() with an argument