revset: extract CommitId resolution to function

I'm going to merge unresolved variants as RevsetExpression::CommitRef(_).
This prepares for the change.
This commit is contained in:
Yuya Nishihara 2023-04-05 21:55:30 +09:00
parent 0d991bfa4a
commit fc65b00020

View file

@ -1614,6 +1614,71 @@ pub fn resolve_symbol(
} }
} }
fn resolve_commit_ref(
repo: &dyn Repo,
expression: &RevsetExpression,
workspace_ctx: Option<&RevsetWorkspaceContext>,
) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> {
match expression {
RevsetExpression::Symbol(symbol) => {
let commit_ids =
resolve_symbol(repo, symbol, workspace_ctx.map(|ctx| ctx.workspace_id))?;
Ok(Some(commit_ids))
}
RevsetExpression::Branches(needle) => {
let mut commit_ids = vec![];
for (branch_name, branch_target) in repo.view().branches() {
if !branch_name.contains(needle) {
continue;
}
if let Some(local_target) = &branch_target.local_target {
commit_ids.extend(local_target.adds());
}
}
Ok(Some(commit_ids))
}
RevsetExpression::RemoteBranches {
branch_needle,
remote_needle,
} => {
let mut commit_ids = vec![];
for (branch_name, branch_target) in repo.view().branches() {
if !branch_name.contains(branch_needle) {
continue;
}
for (remote_name, remote_target) in branch_target.remote_targets.iter() {
if remote_name.contains(remote_needle) {
commit_ids.extend(remote_target.adds());
}
}
}
Ok(Some(commit_ids))
}
RevsetExpression::Tags => {
let mut commit_ids = vec![];
for ref_target in repo.view().tags().values() {
commit_ids.extend(ref_target.adds());
}
Ok(Some(commit_ids))
}
RevsetExpression::GitRefs => {
let mut commit_ids = vec![];
for ref_target in repo.view().git_refs().values() {
commit_ids.extend(ref_target.adds());
}
Ok(Some(commit_ids))
}
RevsetExpression::GitHead => {
let mut commit_ids = vec![];
if let Some(ref_target) = repo.view().git_head() {
commit_ids.extend(ref_target.adds());
}
Ok(Some(commit_ids))
}
_ => Ok(None),
}
}
// TODO: Maybe return a new type (RevsetParameters?) instead of // TODO: Maybe return a new type (RevsetParameters?) instead of
// RevsetExpression. Then pass that to evaluate(), so it's clear which variants // RevsetExpression. Then pass that to evaluate(), so it's clear which variants
// are allowed. // are allowed.
@ -1639,64 +1704,7 @@ pub fn resolve_symbols(
_ => Ok(None), _ => Ok(None),
}, },
|expression| { |expression| {
Ok(match expression.as_ref() { Ok(resolve_commit_ref(repo, expression, workspace_ctx)?.map(RevsetExpression::commits))
RevsetExpression::Symbol(symbol) => {
let commit_ids =
resolve_symbol(repo, symbol, workspace_ctx.map(|ctx| ctx.workspace_id))?;
Some(RevsetExpression::commits(commit_ids))
}
RevsetExpression::Branches(needle) => {
let mut commit_ids = vec![];
for (branch_name, branch_target) in repo.view().branches() {
if !branch_name.contains(needle) {
continue;
}
if let Some(local_target) = &branch_target.local_target {
commit_ids.extend(local_target.adds());
}
}
Some(RevsetExpression::commits(commit_ids))
}
RevsetExpression::RemoteBranches {
branch_needle,
remote_needle,
} => {
let mut commit_ids = vec![];
for (branch_name, branch_target) in repo.view().branches() {
if !branch_name.contains(branch_needle) {
continue;
}
for (remote_name, remote_target) in branch_target.remote_targets.iter() {
if remote_name.contains(remote_needle) {
commit_ids.extend(remote_target.adds());
}
}
}
Some(RevsetExpression::commits(commit_ids))
}
RevsetExpression::Tags => {
let mut commit_ids = vec![];
for ref_target in repo.view().tags().values() {
commit_ids.extend(ref_target.adds());
}
Some(RevsetExpression::commits(commit_ids))
}
RevsetExpression::GitRefs => {
let mut commit_ids = vec![];
for ref_target in repo.view().git_refs().values() {
commit_ids.extend(ref_target.adds());
}
Some(RevsetExpression::commits(commit_ids))
}
RevsetExpression::GitHead => {
let mut commit_ids = vec![];
if let Some(ref_target) = repo.view().git_head() {
commit_ids.extend(ref_target.adds());
}
Some(RevsetExpression::commits(commit_ids))
}
_ => None,
})
}, },
)? )?
.unwrap_or(expression)) .unwrap_or(expression))