From 3fbfd17182038aa1585093b922993d740d06308f Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 3 Sep 2023 14:00:25 -0700 Subject: [PATCH] cli: `jj duplicate` should refuse to duplicate only the root commit Once we add support for immutable commits, `jj duplicate` should be allowed to create duplicate of them. The reason it can't duplicate the root commit is that it would mean there would be multiple root commits, which would break the invariant that the single root commit is the only root commit (and the backends refuse to write a commit without parents). So let's have `jj duplicate` check specifically that the user doesn't try to duplicate the root commit instead. --- cli/src/commands/mod.rs | 8 +++++--- cli/tests/test_duplicate_command.rs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 9bde3b4e2..20e522377 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -2173,10 +2173,12 @@ fn cmd_duplicate( let mut workspace_command = command.workspace_helper(ui)?; let to_duplicate: IndexSet = resolve_multiple_nonempty_revsets(&args.revisions, &workspace_command, ui)?; - to_duplicate + if to_duplicate .iter() - .map(|commit| workspace_command.check_rewritable(commit)) - .try_collect()?; + .any(|commit| commit.id() == workspace_command.repo().store().root_commit_id()) + { + return Err(user_error("Cannot duplicate the root commit")); + } let mut duplicated_old_to_new: IndexMap = IndexMap::new(); let mut tx = workspace_command diff --git a/cli/tests/test_duplicate_command.rs b/cli/tests/test_duplicate_command.rs index e9b18d8c9..31740ef31 100644 --- a/cli/tests/test_duplicate_command.rs +++ b/cli/tests/test_duplicate_command.rs @@ -51,7 +51,7 @@ fn test_duplicate() { let stderr = test_env.jj_cmd_failure(&repo_path, &["duplicate", "root()"]); insta::assert_snapshot!(stderr, @r###" - Error: Cannot rewrite the root commit + Error: Cannot duplicate the root commit "###); let stdout = test_env.jj_cmd_success(&repo_path, &["duplicate", "a"]);