From 7cfd32bac198758caf32ce649176a1b7156422fd Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 8 Jan 2024 11:59:08 +0900 Subject: [PATCH] cli: disallow gc run from non-head operation It doesn't make sense to do gc from a non-head operation because that means either the head operation would be corrupted or the --at-op argument is ignored. --- cli/src/commands/util.rs | 5 +++++ cli/tests/test_util_command.rs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/cli/src/commands/util.rs b/cli/src/commands/util.rs index 43d68ebc1..357ed28ed 100644 --- a/cli/src/commands/util.rs +++ b/cli/src/commands/util.rs @@ -108,6 +108,11 @@ fn cmd_util_gc( command: &CommandHelper, _args: &UtilGcArgs, ) -> Result<(), CommandError> { + if command.global_args().at_operation != "@" { + return Err(user_error( + "Cannot garbage collect from a non-head operation", + )); + } let workspace_command = command.workspace_helper(ui)?; let store = workspace_command.repo().store(); store.gc().map_err(|err| user_error(err.to_string()))?; diff --git a/cli/tests/test_util_command.rs b/cli/tests/test_util_command.rs index ad57aa1dd..1509e57f7 100644 --- a/cli/tests/test_util_command.rs +++ b/cli/tests/test_util_command.rs @@ -37,3 +37,22 @@ fn test_util_config_schema() { "###) }); } + +#[test] +fn test_gc_args() { + let test_env = TestEnvironment::default(); + // Use the local backend because GitBackend::gc() depends on the git CLI. + test_env.jj_cmd_ok( + test_env.env_root(), + &["init", "repo", "--config-toml=ui.allow-init-native=true"], + ); + let repo_path = test_env.env_root().join("repo"); + + let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["util", "gc"]); + insta::assert_snapshot!(stderr, @""); + + let stderr = test_env.jj_cmd_failure(&repo_path, &["util", "gc", "--at-op=@-"]); + insta::assert_snapshot!(stderr, @r###" + Error: Cannot garbage collect from a non-head operation + "###); +}