Skip to content

Commit

Permalink
cli: make set of immutable commits configurable
Browse files Browse the repository at this point in the history
This adds a new `revset-aliases.immutable_heads()s` config for
defining the set of immutable commits. The set is defined as the
configured revset, as well as its ancestors, and the root commit
commit (even if the configured set is empty).

This patch also adds enforcement of the config where we already had
checks preventing rewrite of the root commit. The working-copy commit
is implicitly assumed to be writable in most cases. Specifically, we
won't prevent amending the working copy even if the user includes it
in the config but we do prevent `jj edit @` in that case. That seems
good enough to me. Maybe we should emit a warning when the working
copy is in the set of immutable commits.

Maybe we should add support for something more like [Mercurial's
phases](https://wiki.mercurial-scm.org/Phases), which is propagated on
push and pull. There's already some affordance for that in the view
object's `public_heads` field. However, this is simpler, especially
since we can't propagate the phase to Git remotes, and seems like a
good start. Also, it lets you say that commits authored by other users
are immutable, for example.

For now, the functionality is in the CLI library. I'm not sure if we
want to move it into the library crate. I'm leaning towards letting
library users do whatever they want without being restricted by
immutable commits. I do think we should move the functionality into a
future `ui-lib` or `ui-util` crate. That crate would have most of the
functionality in the current `cli_util` module (but in a
non-CLI-specific form).
  • Loading branch information
martinvonz committed Sep 25, 2023
1 parent ab85b9e commit a6ef3f0
Showing 15 changed files with 276 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Support for the Watchman filesystem monitor is now bundled by default. Set
`core.fsmonitor = "watchman"` in your repo to enable.

* You can now configure the set of immutable commits via
`revset-aliases.immutable_heads()`. For example, set it to
`"remote_branches() | tags()"` to prevent rewriting those those. Their
ancestors are implicitly also immutable.

* `jj op log` now supports `--no-graph`.

* Templates now support an additional escape: `\0`. This will output a literal
28 changes: 20 additions & 8 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
@@ -1259,16 +1259,28 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
.map(|commit| commit.id().clone())
.collect(),
);
let immutable_revset =
RevsetExpression::commit(self.repo().store().root_commit_id().clone());
let invalid_revset = to_rewrite_revset.intersection(&immutable_revset);
let revset = self.evaluate_revset(invalid_revset)?;
let (params, immutable_heads_str) = self
.revset_aliases_map
.get_function("immutable_heads")
.unwrap();
if !params.is_empty() {
return Err(user_error(
r#"The `revset-aliases.immutable_heads()` function must be declared without arguments."#,
));
}
let immutable_heads_revset = self.parse_revset(immutable_heads_str, None)?;
let immutable_revset = immutable_heads_revset
.ancestors()
.union(&RevsetExpression::commit(
self.repo().store().root_commit_id().clone(),
));
let revset = self.evaluate_revset(to_rewrite_revset.intersection(&immutable_revset))?;
if let Some(commit) = revset.iter().commits(self.repo().store()).next() {
let commit = commit?;
return Err(user_error(format!(
"Cannot rewrite commit {}",
short_commit_hash(commit.id()),
)));
return Err(user_error_with_hint(
format!("Commit {} is immutable", short_commit_hash(commit.id()),),
"Configure the set of immutable commits via `revset-aliases.immutable_heads()`.",
));
}

Ok(())
14 changes: 12 additions & 2 deletions cli/src/config-schema.json
Original file line number Diff line number Diff line change
@@ -309,6 +309,13 @@
"revset-aliases": {
"type": "object",
"description": "Custom symbols/function aliases that can used in revset expressions",
"properties": {
"immutable_heads()": {
"type": "string",
"description": "Revisions to consider immutable. Ancestors of these are also considered immutable. The root commit is always considered immutable.",
"default": "trunk() | tags()"
}
},
"additionalProperties": {
"type": "string"
}
@@ -335,11 +342,14 @@
"description": "Parameters governing automatic capture of files into the working copy commit",
"properties": {
"max-new-file-size": {
"type": ["integer", "string"],
"type": [
"integer",
"string"
],
"description": "New files with a size in bytes above this threshold are not snapshotted, unless the threshold is 0",
"default": "1MiB"
}
}
}
}
}
}
2 changes: 2 additions & 0 deletions cli/src/config/revsets.toml
Original file line number Diff line number Diff line change
@@ -13,3 +13,5 @@ latest(
root()
)
'''

'immutable_heads()' = 'trunk() | tags()'
5 changes: 0 additions & 5 deletions cli/tests/test_abandon_command.rs
Original file line number Diff line number Diff line change
@@ -152,11 +152,6 @@ fn test_rebase_branch_with_merge() {
├─╯
◉ a b e??
"###);

let stderr = test_env.jj_cmd_failure(&repo_path, &["abandon", "root()"]);
insta::assert_snapshot!(stderr, @r###"
Error: Cannot rewrite commit 000000000000
"###);
}

fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
1 change: 1 addition & 0 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
@@ -449,6 +449,7 @@ fn test_branch_forget_deleted_or_nonexistent_branch() {
#[test]
fn test_branch_list_filtered_by_revset() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);

// Initialize remote refs
test_env.jj_cmd_success(test_env.env_root(), &["init", "remote", "--git"]);
11 changes: 0 additions & 11 deletions cli/tests/test_edit_command.rs
Original file line number Diff line number Diff line change
@@ -110,14 +110,3 @@ fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"commit_id.short() ++ " " ++ description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

#[test]
fn test_edit_root() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "root()"]);
insta::assert_snapshot!(stderr, @r###"
Error: Cannot rewrite commit 000000000000
"###);
}
2 changes: 2 additions & 0 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
@@ -382,6 +382,7 @@ fn create_trunk2_and_rebase_branches(test_env: &TestEnvironment, repo_path: &Pat
#[test]
fn test_git_fetch_all() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let source_git_repo_path = test_env.env_root().join("source");
let _git_repo = git2::Repository::init(source_git_repo_path.clone()).unwrap();

@@ -505,6 +506,7 @@ fn test_git_fetch_all() {
#[test]
fn test_git_fetch_some_of_many_branches() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let source_git_repo_path = test_env.env_root().join("source");
let _git_repo = git2::Repository::init(source_git_repo_path.clone()).unwrap();

3 changes: 3 additions & 0 deletions cli/tests/test_git_push.rs
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ fn test_git_push_nothing() {
#[test]
fn test_git_push_current_branch() {
let (test_env, workspace_root) = set_up();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
// Update some branches. `branch1` is not a current branch, but `branch2` and
// `my-branch` are.
test_env.jj_cmd_success(
@@ -105,6 +106,7 @@ fn test_git_push_current_branch() {
#[test]
fn test_git_push_parent_branch() {
let (test_env, workspace_root) = set_up();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
test_env.jj_cmd_success(&workspace_root, &["edit", "branch1"]);
test_env.jj_cmd_success(
&workspace_root,
@@ -151,6 +153,7 @@ fn test_git_push_matching_branch_unchanged() {
#[test]
fn test_git_push_other_remote_has_branch() {
let (test_env, workspace_root) = set_up();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
// Create another remote (but actually the same)
let other_remote_path = test_env
.env_root()
196 changes: 196 additions & 0 deletions cli/tests/test_immutable_commits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright 2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::common::TestEnvironment;

pub mod common;

#[test]
fn test_rewrite_immutable_generic() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file"), "a").unwrap();
test_env.jj_cmd_success(&repo_path, &["describe", "-m=a"]);
test_env.jj_cmd_success(&repo_path, &["new", "-m=b"]);
std::fs::write(repo_path.join("file"), "b").unwrap();
test_env.jj_cmd_success(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_success(&repo_path, &["new", "main-", "-m=c"]);
std::fs::write(repo_path.join("file"), "c").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["log"]);
insta::assert_snapshot!(stdout, @r###"
@ mzvwutvl test.user@example.com 2001-02-03 04:05:12.000 +07:00 78ebd449
│ c
│ ◉ kkmpptxz test.user@example.com 2001-02-03 04:05:10.000 +07:00 main c8d4c7ca
├─╯ b
◉ qpvuntsm test.user@example.com 2001-02-03 04:05:08.000 +07:00 46a8dc51
│ a
◉ zzzzzzzz root() 00000000
"###);

// Cannot rewrite a commit in the configured set
test_env.add_config(r#"revset-aliases."immutable_heads()" = "main""#);
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit c8d4c7ca95d0 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// Cannot rewrite an ancestor of the configured set
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "main-"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit 46a8dc5175be is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// Cannot rewrite the root commit even with an empty set of immutable commits
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "root()"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit 000000000000 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// Error if we redefine immutable_heads() with an argument
test_env.add_config(r#"revset-aliases."immutable_heads(foo)" = "none()""#);
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "root()"]);
insta::assert_snapshot!(stderr, @r###"
Error: The `revset-aliases.immutable_heads()` function must be declared without arguments.
"###);
}

#[test]
fn test_rewrite_immutable_commands() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file"), "a").unwrap();
test_env.jj_cmd_success(&repo_path, &["describe", "-m=a"]);
test_env.jj_cmd_success(&repo_path, &["new", "-m=b"]);
std::fs::write(repo_path.join("file"), "b").unwrap();
test_env.jj_cmd_success(&repo_path, &["new", "@-", "-m=c"]);
std::fs::write(repo_path.join("file"), "c").unwrap();
test_env.jj_cmd_success(&repo_path, &["new", "all:visible_heads()", "-m=merge"]);
test_env.jj_cmd_success(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_success(&repo_path, &["new", "description(b)"]);
test_env.add_config(r#"revset-aliases."immutable_heads()" = "main""#);
let stdout = test_env.jj_cmd_success(&repo_path, &["log"]);
insta::assert_snapshot!(stdout, @r###"
@ yqosqzyt test.user@example.com 2001-02-03 04:05:13.000 +07:00 3f89addf
│ (empty) (no description set)
│ ◉ mzvwutvl test.user@example.com 2001-02-03 04:05:11.000 +07:00 main d809c5d9 conflict
╭─┤ (empty) merge
◉ │ kkmpptxz test.user@example.com 2001-02-03 04:05:10.000 +07:00 c8d4c7ca
│ │ b
│ ◉ zsuskuln test.user@example.com 2001-02-03 04:05:11.000 +07:00 6e11f430
├─╯ c
◉ qpvuntsm test.user@example.com 2001-02-03 04:05:08.000 +07:00 46a8dc51
│ a
◉ zzzzzzzz root() 00000000
"###);

// abandon
let stderr = test_env.jj_cmd_failure(&repo_path, &["abandon", "main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// chmod
let stderr = test_env.jj_cmd_failure(&repo_path, &["chmod", "-r=main", "x", "file"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// describe
let stderr = test_env.jj_cmd_failure(&repo_path, &["describe", "main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// diffedit
let stderr = test_env.jj_cmd_failure(&repo_path, &["diffedit", "-r=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// edit
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// move --from
let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--from=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// move --to
let stderr = test_env.jj_cmd_failure(&repo_path, &["move", "--to=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// rebase -s
let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-s=main", "-d=@"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// rebase -b
let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-b=main", "-d=@"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit 6e11f430f297 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// rebase -r
let stderr = test_env.jj_cmd_failure(&repo_path, &["rebase", "-r=main", "-d=@"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// resolve
let stderr = test_env.jj_cmd_failure(&repo_path, &["resolve", "-r=description(merge)", "file"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// restore -c
let stderr = test_env.jj_cmd_failure(&repo_path, &["restore", "-c=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// restore --to
let stderr = test_env.jj_cmd_failure(&repo_path, &["restore", "--to=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// split
let stderr = test_env.jj_cmd_failure(&repo_path, &["split", "-r=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// squash
let stderr = test_env.jj_cmd_failure(&repo_path, &["squash", "-r=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
// unsquash
let stderr = test_env.jj_cmd_failure(&repo_path, &["unsquash", "-r=main"]);
insta::assert_snapshot!(stderr, @r###"
Error: Commit d809c5d93710 is immutable
Hint: Configure the set of immutable commits via `revset-aliases.immutable_heads()`.
"###);
}
Loading

0 comments on commit a6ef3f0

Please sign in to comment.