Skip to content

Commit

Permalink
revset: don't allow symbols in RevsetExpression::resolve()
Browse files Browse the repository at this point in the history
When creating `RevsetExpression` programmatically, I think we should
use commit ids instead of symbols in the expression. This commit adds
a check for that by using a `SymbolResolver` that always errors
out.
  • Loading branch information
martinvonz committed May 12, 2023
1 parent 99e9cd7 commit efd7433
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl RevsetExpression {
self: Rc<Self>,
repo: &dyn Repo,
) -> Result<ResolvedExpression, RevsetResolutionError> {
let symbol_resolver = DefaultSymbolResolver::new(repo, None);
let symbol_resolver = FailingSymbolResolver;
resolve_symbols(repo, self, &symbol_resolver)
.map(|expression| resolve_visibility(repo, &expression))
}
Expand Down Expand Up @@ -1669,6 +1669,18 @@ pub trait SymbolResolver {
fn resolve_symbol(&self, symbol: &str) -> Result<Vec<CommitId>, RevsetResolutionError>;
}

/// Fails on any attempt to resolve a symbol.
pub struct FailingSymbolResolver;

impl SymbolResolver for FailingSymbolResolver {
fn resolve_symbol(&self, symbol: &str) -> Result<Vec<CommitId>, RevsetResolutionError> {
Err(RevsetResolutionError::NoSuchRevision(format!(
"Won't resolve symbol {symbol:?}. When creating revsets programmatically, avoid using \
RevsetExpression::symbol(); use RevsetExpression::commits() instead."
)))
}
}

/// Resolves the "root" and "@" symbols, branches, remote branches, tags, git
/// refs, and full and abbreviated commit and change ids.
pub struct DefaultSymbolResolver<'a> {
Expand Down
11 changes: 8 additions & 3 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ fn revset_for_commits<'index>(
repo: &'index dyn Repo,
commits: &[&Commit],
) -> Box<dyn Revset<'index> + 'index> {
let symbol_resolver = DefaultSymbolResolver::new(repo, None);
RevsetExpression::commits(commits.iter().map(|commit| commit.id().clone()).collect())
.resolve(repo)
.resolve_user_expression(repo, &symbol_resolver)
.unwrap()
.evaluate(repo)
.unwrap()
Expand Down Expand Up @@ -173,8 +174,9 @@ fn test_resolve_symbol_commit_id() {

// Test present() suppresses only NoSuchRevision error
assert_eq!(resolve_commit_ids(repo.as_ref(), "present(foo)"), []);
let symbol_resolver = DefaultSymbolResolver::new(repo.as_ref(), None);
assert_matches!(
optimize(parse("present(04)", &RevsetAliasesMap::new(), None).unwrap()).resolve(repo.as_ref()),
optimize(parse("present(04)", &RevsetAliasesMap::new(), None).unwrap()).resolve_user_expression(repo.as_ref(), &symbol_resolver),
Err(RevsetResolutionError::AmbiguousIdPrefix(s)) if s == "04"
);
assert_eq!(
Expand Down Expand Up @@ -504,7 +506,10 @@ fn test_resolve_symbol_git_refs() {

fn resolve_commit_ids(repo: &dyn Repo, revset_str: &str) -> Vec<CommitId> {
let expression = optimize(parse(revset_str, &RevsetAliasesMap::new(), None).unwrap());
let expression = expression.resolve(repo).unwrap();
let symbol_resolver = DefaultSymbolResolver::new(repo, None);
let expression = expression
.resolve_user_expression(repo, &symbol_resolver)
.unwrap();
expression.evaluate(repo).unwrap().iter().collect()
}

Expand Down

0 comments on commit efd7433

Please sign in to comment.