From 399110b1fcad52c603a5e2653a25ed61c4f25e38 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 24 Jul 2024 12:09:31 +0900 Subject: [PATCH] op_walk: allow to resolve operation expression from multiple heads I'll make "op abandon" work without merging op heads. --- cli/src/commands/operation/abandon.rs | 3 ++- lib/src/op_walk.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cli/src/commands/operation/abandon.rs b/cli/src/commands/operation/abandon.rs index 3ec22090d..5334835cb 100644 --- a/cli/src/commands/operation/abandon.rs +++ b/cli/src/commands/operation/abandon.rs @@ -57,7 +57,8 @@ pub fn cmd_op_abandon( return Err(cli_error("--at-op is not respected")); } let current_head_op = op_walk::resolve_op_for_load(repo_loader, "@")?; - let resolve_op = |op_str| op_walk::resolve_op_at(op_store, ¤t_head_op, op_str); + let resolve_op = + |op_str| op_walk::resolve_op_at(op_store, slice::from_ref(¤t_head_op), op_str); let (abandon_root_op, abandon_head_op) = if let Some((root_op_str, head_op_str)) = args.operation.split_once("..") { let root_op = if root_op_str.is_empty() { diff --git a/lib/src/op_walk.rs b/lib/src/op_walk.rs index 81d9590c1..a0cf1a65e 100644 --- a/lib/src/op_walk.rs +++ b/lib/src/op_walk.rs @@ -98,17 +98,25 @@ pub fn resolve_op_with_repo( repo: &ReadonlyRepo, op_str: &str, ) -> Result { - resolve_op_at(repo.op_store(), repo.operation(), op_str) + resolve_op_at(repo.op_store(), slice::from_ref(repo.operation()), op_str) } -/// Resolves operation set expression at the given head operation. +/// Resolves operation set expression at the given head operations. pub fn resolve_op_at( op_store: &Arc, - head_op: &Operation, + head_ops: &[Operation], op_str: &str, ) -> Result { - let get_current_op = || Ok(head_op.clone()); - let get_head_ops = || Ok(vec![head_op.clone()]); + let get_current_op = || match head_ops { + [head_op] => Ok(head_op.clone()), + [] => Err(OpsetResolutionError::EmptyOperations("@".to_owned()).into()), + _ => Err(OpsetResolutionError::MultipleOperations { + expr: "@".to_owned(), + candidates: head_ops.iter().map(|op| op.id().clone()).collect(), + } + .into()), + }; + let get_head_ops = || Ok(head_ops.to_vec()); resolve_single_op(op_store, get_current_op, get_head_ops, op_str) }