cli: extract evaluation part of resolve_single_rev_with_hint_about_all_prefix()

I'm going to reorganize "single"/"default_single" revset functions in a way
that resolve_single_rev_with_hint_about_all_prefix() is inlined.
evaluate_revset_to_single_commit() could be a private method of
WorkspaceCommandHelper, but I want to minimize the code that has to be hosted
there.
This commit is contained in:
Yuya Nishihara 2024-03-31 14:51:28 +09:00
parent f04402e3c7
commit 0c4f4c8767
2 changed files with 36 additions and 22 deletions

View file

@ -782,27 +782,13 @@ impl WorkspaceCommandHelper {
revision_str: &str,
should_hint_about_all_prefix: bool,
) -> Result<Commit, CommandError> {
let revset_expression = self.parse_revset(revision_str)?;
let mut iter = revset_expression.evaluate_to_commits()?.fuse();
match (iter.next(), iter.next()) {
(Some(commit), None) => Ok(commit?),
(None, _) => Err(user_error(format!(
r#"Revset "{revision_str}" didn't resolve to any revisions"#
))),
(Some(commit0), Some(commit1)) => {
let mut iter = [commit0, commit1].into_iter().chain(iter);
let commits: Vec<_> = iter.by_ref().take(5).try_collect()?;
let elided = iter.next().is_some();
Err(revset_util::format_multiple_revisions_error(
revision_str,
revset_expression.expression(),
&commits,
elided,
&self.commit_summary_template(),
should_hint_about_all_prefix,
))
}
}
let expression = self.parse_revset(revision_str)?;
revset_util::evaluate_revset_to_single_commit(
revision_str,
&expression,
|| self.commit_summary_template(),
should_hint_about_all_prefix,
)
}
pub fn parse_revset(

View file

@ -186,7 +186,35 @@ pub fn parse_immutable_expression(
Ok(heads.union(&RevsetExpression::root()).ancestors())
}
pub(super) fn format_multiple_revisions_error(
pub(super) fn evaluate_revset_to_single_commit<'a>(
revision_str: &str,
expression: &RevsetExpressionEvaluator<'_>,
commit_summary_template: impl FnOnce() -> TemplateRenderer<'a, Commit>,
should_hint_about_all_prefix: bool,
) -> Result<Commit, CommandError> {
let mut iter = expression.evaluate_to_commits()?.fuse();
match (iter.next(), iter.next()) {
(Some(commit), None) => Ok(commit?),
(None, _) => Err(user_error(format!(
r#"Revset "{revision_str}" didn't resolve to any revisions"#
))),
(Some(commit0), Some(commit1)) => {
let mut iter = [commit0, commit1].into_iter().chain(iter);
let commits: Vec<_> = iter.by_ref().take(5).try_collect()?;
let elided = iter.next().is_some();
Err(format_multiple_revisions_error(
revision_str,
expression.expression(),
&commits,
elided,
&commit_summary_template(),
should_hint_about_all_prefix,
))
}
}
}
fn format_multiple_revisions_error(
revision_str: &str,
expression: &RevsetExpression,
commits: &[Commit],