cli: fix jj squash on empty commit

Fixes #787
If `jj squash` is run on an empty commit, it fails with "Error: No changes selected"

With this change such squash command will behave like `jj abandon`.
This commit is contained in:
Aleksandr Mikhailov 2023-01-24 10:21:02 +01:00 committed by Aleksandr Mikhailov
parent 5421f5e92d
commit e7077909c1
2 changed files with 27 additions and 1 deletions

View file

@ -2136,7 +2136,7 @@ from the source will be moved into the parent.
args.interactive, args.interactive,
matcher.as_ref(), matcher.as_ref(),
)?; )?;
if &new_parent_tree_id == parent.tree_id() { if args.interactive && &new_parent_tree_id == parent.tree_id() {
return Err(user_error("No changes selected")); return Err(user_error("No changes selected"));
} }
// Abandon the child if the parent now has all the content from the child // Abandon the child if the parent now has all the content from the child

View file

@ -311,6 +311,32 @@ fn test_squash_description() {
"###); "###);
} }
#[test]
fn test_squash_empty() {
let mut 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");
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "parent"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["squash"]);
insta::assert_snapshot!(stdout, @r###"
Working copy now at: e45abe2cd9a9 (no description set)
"###);
insta::assert_snapshot!(get_description(&test_env, &repo_path, "@-"), @r###"
parent
"###);
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "child"]);
test_env.set_up_fake_editor();
test_env.jj_cmd_success(&repo_path, &["squash"]);
insta::assert_snapshot!(get_description(&test_env, &repo_path, "@-"), @r###"
parent
child
"###);
}
fn get_description(test_env: &TestEnvironment, repo_path: &Path, rev: &str) -> String { fn get_description(test_env: &TestEnvironment, repo_path: &Path, rev: &str) -> String {
test_env.jj_cmd_success( test_env.jj_cmd_success(
repo_path, repo_path,