diff --git a/cli/src/commands/operation/diff.rs b/cli/src/commands/operation/diff.rs index 596cbc6e2..461db2593 100644 --- a/cli/src/commands/operation/diff.rs +++ b/cli/src/commands/operation/diff.rs @@ -40,7 +40,6 @@ use crate::cli_util::short_change_hash; use crate::cli_util::short_operation_hash; use crate::cli_util::CommandHelper; use crate::cli_util::LogContentFormat; -use crate::command_error::user_error; use crate::command_error::CommandError; use crate::diff_util::DiffFormatArgs; use crate::diff_util::DiffRenderer; @@ -93,9 +92,6 @@ pub fn cmd_op_diff( } else { to_op = workspace_command.resolve_single_op(args.operation.as_deref().unwrap_or("@"))?; let to_op_parents: Vec<_> = to_op.parents().try_collect()?; - if to_op_parents.is_empty() { - return Err(user_error("Cannot diff operation with no parents")); - } from_op = repo_loader.merge_operations(command.settings(), to_op_parents, None)?; } let graph_style = GraphStyle::from_settings(command.settings())?; diff --git a/cli/src/commands/operation/show.rs b/cli/src/commands/operation/show.rs index c494099c6..70ca646f6 100644 --- a/cli/src/commands/operation/show.rs +++ b/cli/src/commands/operation/show.rs @@ -17,7 +17,6 @@ use itertools::Itertools; use super::diff::show_op_diff; use crate::cli_util::CommandHelper; use crate::cli_util::LogContentFormat; -use crate::command_error::user_error; use crate::command_error::CommandError; use crate::diff_util::DiffFormatArgs; use crate::graphlog::GraphStyle; @@ -55,9 +54,6 @@ pub fn cmd_op_show( let repo_loader = &repo.loader(); let op = workspace_command.resolve_single_op(&args.operation)?; let parents: Vec<_> = op.parents().try_collect()?; - if parents.is_empty() { - return Err(user_error("Cannot show the root operation")); - } let parent_op = repo_loader.merge_operations(command.settings(), parents, None)?; let parent_repo = repo_loader.load_at(&parent_op)?; let repo = repo_loader.load_at(&op)?; diff --git a/cli/tests/test_operations.rs b/cli/tests/test_operations.rs index ad4121421..8935c8dbb 100644 --- a/cli/tests/test_operations.rs +++ b/cli/tests/test_operations.rs @@ -1211,9 +1211,9 @@ fn test_op_show() { "###); // The root operation is empty. - let stderr = test_env.jj_cmd_failure(&repo_path, &["op", "show", "0000000"]); - insta::assert_snapshot!(&stderr, @r###" - Error: Cannot show the root operation + let stdout = test_env.jj_cmd_success(&repo_path, &["op", "show", "0000000"]); + insta::assert_snapshot!(&stdout, @r###" + 000000000000 root() "###); // Showing the latest operation. diff --git a/lib/src/repo.rs b/lib/src/repo.rs index bea1fa8be..037d49c77 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -753,8 +753,8 @@ impl RepoLoader { Arc::new(repo) } - /// Merges the given `operations` into a single operation. - /// Assumes that there is at least one operation. + /// Merges the given `operations` into a single operation. Returns the root + /// operation if the `operations` is empty. pub fn merge_operations( &self, settings: &UserSettings, @@ -763,7 +763,11 @@ impl RepoLoader { ) -> Result { let num_operations = operations.len(); let mut operations = operations.into_iter(); - let base_op = operations.next().unwrap(); + let Some(base_op) = operations.next() else { + let id = self.op_store.root_operation_id(); + let data = self.op_store.read_operation(id)?; + return Ok(Operation::new(self.op_store.clone(), id.clone(), data)); + }; let final_op = if num_operations > 1 { let base_repo = self.load_at(&base_op)?; let mut tx = base_repo.start_transaction(settings); @@ -789,6 +793,7 @@ impl RepoLoader { op_heads: Vec, user_settings: &UserSettings, ) -> Result { + assert!(!op_heads.is_empty()); self.merge_operations( user_settings, op_heads,