From e7077909c1ae24d8652afd360eabf6db5bc8a419 Mon Sep 17 00:00:00 2001 From: Aleksandr Mikhailov Date: Tue, 24 Jan 2023 10:21:02 +0100 Subject: [PATCH] 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`. --- src/commands/mod.rs | 2 +- tests/test_squash_command.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7c63cfe50..1fe60dbe5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2136,7 +2136,7 @@ from the source will be moved into the parent. args.interactive, 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")); } // Abandon the child if the parent now has all the content from the child diff --git a/tests/test_squash_command.rs b/tests/test_squash_command.rs index a07e3914e..891f39dfe 100644 --- a/tests/test_squash_command.rs +++ b/tests/test_squash_command.rs @@ -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 { test_env.jj_cmd_success( repo_path,