Skip to content

Commit

Permalink
cli: extract evaluation part of resolve_single_rev_with_hint_about_al…
Browse files Browse the repository at this point in the history
…l_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.
  • Loading branch information
yuja committed Apr 1, 2024
1 parent b64a947 commit f11d429
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
28 changes: 7 additions & 21 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 29 additions & 1 deletion cli/src/revset_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit f11d429

Please sign in to comment.