Skip to content

Commit 88a4a82

Browse files
committed
cli: add better error message when immutable_heads() cannot be resolved
1 parent b799848 commit 88a4a82

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

cli/src/cli_util.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,18 @@ impl WorkspaceCommandHelper {
965965
.map(|commit| commit.id().clone())
966966
.collect(),
967967
);
968-
let immutable = revset_util::parse_immutable_expression(&self.revset_parse_context())?;
968+
let immutable = revset_util::parse_immutable_expression(&self.revset_parse_context())
969+
.map_err(|e| {
970+
config_error_with_message("Invalid `revset-aliases.immutable_heads()`", e)
971+
})?;
969972
let mut expression = self.attach_revset_evaluator(immutable)?;
970973
expression.intersect_with(&to_rewrite_revset);
971-
if let Some(commit_id) = expression.evaluate_to_commit_ids()?.next() {
974+
975+
let mut commit_id_iter = expression.evaluate_to_commit_ids().map_err(|e| {
976+
config_error_with_message("Invalid `revset-aliases.immutable_heads()`", e)
977+
})?;
978+
979+
if let Some(commit_id) = commit_id_iter.next() {
972980
let error = if &commit_id == self.repo().store().root_commit_id() {
973981
user_error(format!(
974982
"The root commit {} is immutable",

cli/tests/test_immutable_commits.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ fn test_rewrite_immutable_generic() {
5656
insta::assert_snapshot!(stderr, @r###"
5757
Error: The root commit 000000000000 is immutable
5858
"###);
59+
60+
// Error mutating the repo if immutable_heads() uses a ref that can't be
61+
// resolved
62+
test_env.add_config(r#"revset-aliases."immutable_heads()" = "branch_that_does_not_exist""#);
63+
let stderr = test_env.jj_cmd_failure(&repo_path, &["new", "main"]);
64+
insta::assert_snapshot!(stderr, @r###"
65+
Config error: Invalid `revset-aliases.immutable_heads()`
66+
Caused by: Revision "branch_that_does_not_exist" doesn't exist
67+
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
68+
"###);
69+
70+
// Mutating the repo works if ref is wrapped in present()
71+
test_env.add_config(
72+
r#"revset-aliases."immutable_heads()" = "present(branch_that_does_not_exist)""#,
73+
);
74+
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["new", "main"]);
75+
insta::assert_snapshot!(stdout, @r###"
76+
"###);
77+
insta::assert_snapshot!(stderr, @r###"
78+
Working copy now at: kpqxywon dbce15b4 (empty) (no description set)
79+
Parent commit : kkmpptxz c8d4c7ca main | b
80+
Added 0 files, modified 1 files, removed 0 files
81+
"###);
82+
5983
// Error if we redefine immutable_heads() with an argument
6084
test_env.add_config(r#"revset-aliases."immutable_heads(foo)" = "none()""#);
6185
let stderr = test_env.jj_cmd_failure(&repo_path, &["edit", "root()"]);

docs/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ You will probably also want to make the `gh-pages` branch immutable (and thereby
114114
hidden from the default `jj log` output) by running the following in your repo:
115115

116116
```shell
117-
jj config set --repo "revset-aliases.immutable_heads()" "main@origin | gh-pages@origin"
117+
jj config set --repo "revset-aliases.immutable_heads()" 'remote_branches(exact:"main") | remote_branches(exact:"gh-pages")'
118118
```
119119

120120
### Summary

0 commit comments

Comments
 (0)