Skip to content

Commit

Permalink
op_walk: simplify arguments passed to high-level "opset" query functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Jan 1, 2024
1 parent 26b5f38 commit c9b5815
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
16 changes: 3 additions & 13 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,8 @@ impl CommandHelper {
},
)
} else {
let operation = op_walk::resolve_op_for_load(
repo_loader.op_store(),
repo_loader.op_heads_store(),
&self.global_args.at_operation,
)?;
let operation =
op_walk::resolve_op_for_load(repo_loader, &self.global_args.at_operation)?;
Ok(operation)
}
}
Expand Down Expand Up @@ -975,14 +972,7 @@ impl WorkspaceCommandHelper {
}

pub fn resolve_single_op(&self, op_str: &str) -> Result<Operation, OpsetEvaluationError> {
// When resolving the "@" operation in a `ReadonlyRepo`, we resolve it to the
// operation the repo was loaded at.
op_walk::resolve_single_op(
self.repo().op_store(),
self.repo().op_heads_store(),
|| Ok(self.repo().operation().clone()),
op_str,
)
op_walk::resolve_op_with_repo(self.repo(), op_str)
}

/// Resolve a revset to a single revision. Return an error if the revset is
Expand Down
6 changes: 1 addition & 5 deletions cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,7 @@ fn cmd_debug_operation(
// even if e.g. the view object is broken.
let workspace = command.load_workspace()?;
let repo_loader = workspace.repo_loader();
let op = op_walk::resolve_op_for_load(
repo_loader.op_store(),
repo_loader.op_heads_store(),
&args.operation,
)?;
let op = op_walk::resolve_op_for_load(repo_loader, &args.operation)?;
if args.display == DebugOperationDisplay::Id {
writeln!(ui.stdout(), "{}", op.id().hex())?;
return Ok(());
Expand Down
29 changes: 22 additions & 7 deletions lib/src/op_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::backend::ObjectId as _;
use crate::op_heads_store::{OpHeadResolutionError, OpHeadsStore};
use crate::op_store::{OpStore, OpStoreError, OpStoreResult, OperationId};
use crate::operation::Operation;
use crate::repo::{ReadonlyRepo, Repo as _, RepoLoader};
use crate::{dag_walk, op_heads_store};

/// Error that may occur during evaluation of operation set expression.
Expand Down Expand Up @@ -66,23 +67,37 @@ pub enum OpsetResolutionError {

/// Resolves operation set expression without loading a repo.
pub fn resolve_op_for_load(
op_store: &Arc<dyn OpStore>,
op_heads_store: &Arc<dyn OpHeadsStore>,
repo_loader: &RepoLoader,
op_str: &str,
) -> Result<Operation, OpsetEvaluationError> {
let op_store = repo_loader.op_store();
let op_heads_store = repo_loader.op_heads_store().as_ref();
let get_current_op = || {
op_heads_store::resolve_op_heads(op_heads_store.as_ref(), op_store, |_| {
op_heads_store::resolve_op_heads(op_heads_store, op_store, |_| {
Err(OpsetResolutionError::MultipleOperations("@".to_owned()).into())
})
};
resolve_single_op(op_store, op_heads_store, get_current_op, op_str)
}

/// Resolves operation set expression against the loaded repo.
///
/// The "@" symbol will be resolved to the operation the repo was loaded at.
pub fn resolve_op_with_repo(
repo: &ReadonlyRepo,
op_str: &str,
) -> Result<Operation, OpsetEvaluationError> {
let op_store = repo.op_store();
let op_heads_store = repo.op_heads_store().as_ref();
let get_current_op = || Ok(repo.operation().clone());
resolve_single_op(op_store, op_heads_store, get_current_op, op_str)
}

/// Resolves operation set expression with the given "@" symbol resolution
/// callback.
pub fn resolve_single_op(
fn resolve_single_op(
op_store: &Arc<dyn OpStore>,
op_heads_store: &Arc<dyn OpHeadsStore>,
op_heads_store: &dyn OpHeadsStore,
get_current_op: impl FnOnce() -> Result<Operation, OpsetEvaluationError>,
op_str: &str,
) -> Result<Operation, OpsetEvaluationError> {
Expand All @@ -108,7 +123,7 @@ pub fn resolve_single_op(

fn resolve_single_op_from_store(
op_store: &Arc<dyn OpStore>,
op_heads_store: &Arc<dyn OpHeadsStore>,
op_heads_store: &dyn OpHeadsStore,
op_str: &str,
) -> Result<Operation, OpsetEvaluationError> {
if op_str.is_empty() || !op_str.as_bytes().iter().all(|b| b.is_ascii_hexdigit()) {
Expand Down Expand Up @@ -145,7 +160,7 @@ fn resolve_single_op_from_store(

fn find_all_operations(
op_store: &Arc<dyn OpStore>,
op_heads_store: &Arc<dyn OpHeadsStore>,
op_heads_store: &dyn OpHeadsStore,
) -> Result<Vec<Operation>, OpStoreError> {
let mut visited = HashSet::new();
let mut work: VecDeque<_> = op_heads_store.get_op_heads().into_iter().collect();
Expand Down

0 comments on commit c9b5815

Please sign in to comment.