ok/jj
1
0
Fork 0
forked from mirrors/jj

revset: initialize with default prefix resolver

Since repo is passed as argument, we can define the default resolver as
a plain function.
This commit is contained in:
Yuya Nishihara 2023-05-12 16:11:45 +09:00
parent f58beca760
commit 8d56b199bc

View file

@ -1654,8 +1654,8 @@ pub type PrefixResolver<'a, T> = Box<dyn Fn(&dyn Repo, &HexPrefix) -> PrefixReso
pub struct DefaultSymbolResolver<'a> {
repo: &'a dyn Repo,
workspace_id: Option<&'a WorkspaceId>,
commit_id_resolver: Option<PrefixResolver<'a, CommitId>>,
change_id_resolver: Option<PrefixResolver<'a, Vec<CommitId>>>,
commit_id_resolver: PrefixResolver<'a, CommitId>,
change_id_resolver: PrefixResolver<'a, Vec<CommitId>>,
}
impl<'a> DefaultSymbolResolver<'a> {
@ -1663,8 +1663,8 @@ impl<'a> DefaultSymbolResolver<'a> {
DefaultSymbolResolver {
repo,
workspace_id,
commit_id_resolver: None,
change_id_resolver: None,
commit_id_resolver: Box::new(|repo, prefix| repo.index().resolve_prefix(prefix)),
change_id_resolver: Box::new(|repo, prefix| repo.resolve_change_id_prefix(prefix)),
}
}
@ -1672,7 +1672,7 @@ impl<'a> DefaultSymbolResolver<'a> {
mut self,
commit_id_resolver: PrefixResolver<'a, CommitId>,
) -> Self {
self.commit_id_resolver = Some(commit_id_resolver);
self.commit_id_resolver = commit_id_resolver;
self
}
@ -1680,7 +1680,7 @@ impl<'a> DefaultSymbolResolver<'a> {
mut self,
change_id_resolver: PrefixResolver<'a, Vec<CommitId>>,
) -> Self {
self.change_id_resolver = Some(change_id_resolver);
self.change_id_resolver = change_id_resolver;
self
}
}
@ -1727,12 +1727,7 @@ impl SymbolResolver for DefaultSymbolResolver<'_> {
// Try to resolve as a commit id.
if let Some(prefix) = HexPrefix::new(symbol) {
let prefix_resolution = if let Some(commit_id_resolver) = &self.commit_id_resolver {
commit_id_resolver(self.repo, &prefix)
} else {
self.repo.index().resolve_prefix(&prefix)
};
match prefix_resolution {
match (self.commit_id_resolver)(self.repo, &prefix) {
PrefixResolution::AmbiguousMatch => {
return Err(RevsetResolutionError::AmbiguousIdPrefix(symbol.to_owned()));
}
@ -1747,12 +1742,7 @@ impl SymbolResolver for DefaultSymbolResolver<'_> {
// Try to resolve as a change id.
if let Some(prefix) = to_forward_hex(symbol).as_deref().and_then(HexPrefix::new) {
let prefix_resolution = if let Some(change_id_resolver) = &self.change_id_resolver {
change_id_resolver(self.repo, &prefix)
} else {
self.repo.resolve_change_id_prefix(&prefix)
};
match prefix_resolution {
match (self.change_id_resolver)(self.repo, &prefix) {
PrefixResolution::AmbiguousMatch => {
return Err(RevsetResolutionError::AmbiguousIdPrefix(symbol.to_owned()));
}