Skip to content

Commit

Permalink
cli: add global --ignore-immutable
Browse files Browse the repository at this point in the history
Closes #3576
  • Loading branch information
martinvonz committed Apr 26, 2024
1 parent 2f45a48 commit 7fb6912
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,17 @@ impl WorkspaceCommandHelper {
&self,
commits: impl IntoIterator<Item = &'a CommitId>,
) -> 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())
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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 <AT_OPERATION>` — Operation to load the repo at
Default value: `@`
Expand Down
1 change: 1 addition & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ fn test_help() {
Global Options:
-R, --repository <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 <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
--debug Enable debug logging
--color <WHEN> When to colorize output (always, never, auto)
Expand Down
19 changes: 17 additions & 2 deletions cli/tests/test_immutable_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)""#,
Expand All @@ -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
Expand Down

0 comments on commit 7fb6912

Please sign in to comment.