Skip to content

Commit

Permalink
lib: add support for filtering by topics to revsets
Browse files Browse the repository at this point in the history
  • Loading branch information
noahmayr committed May 4, 2024
1 parent d0bdcc4 commit acd0a0a
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub enum RevsetCommitRef {
remote_pattern: StringPattern,
},
Tags,
Topics(StringPattern),
GitRefs,
GitHead,
}
Expand Down Expand Up @@ -452,6 +453,12 @@ impl RevsetExpression {
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::Tags))
}

pub fn topics(pattern: StringPattern) -> Rc<RevsetExpression> {
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::Topics(
pattern,
)))
}

pub fn git_refs() -> Rc<RevsetExpression> {
Rc::new(RevsetExpression::CommitRef(RevsetCommitRef::GitRefs))
}
Expand Down Expand Up @@ -1271,6 +1278,15 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
expect_no_arguments(name, arguments_pair)?;
Ok(RevsetExpression::tags())
});
map.insert("topics", |name, arguments_pair, state| {
let ([], [opt_arg]) = expect_arguments(name, arguments_pair)?;
let pattern = if let Some(arg) = opt_arg {
parse_function_argument_to_string_pattern(name, arg, state)?
} else {
StringPattern::everything()
};
Ok(RevsetExpression::topics(pattern))
});
map.insert("git_refs", |name, arguments_pair, _state| {
expect_no_arguments(name, arguments_pair)?;
Ok(RevsetExpression::git_refs())
Expand Down Expand Up @@ -2111,6 +2127,21 @@ impl PartialSymbolResolver for TagResolver {
}
}

struct TopicResolver;

impl PartialSymbolResolver for TopicResolver {
fn resolve_symbol(
&self,
repo: &dyn Repo,
symbol: &str,
) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> {
Ok(repo
.view()
.get_topic_commits(symbol)
.map(|set| set.iter().cloned().collect_vec()))
}
}

struct BranchResolver;

impl PartialSymbolResolver for BranchResolver {
Expand Down Expand Up @@ -2146,8 +2177,12 @@ impl PartialSymbolResolver for GitRefResolver {
}
}

const DEFAULT_RESOLVERS: &[&'static dyn PartialSymbolResolver] =
&[&TagResolver, &BranchResolver, &GitRefResolver];
const DEFAULT_RESOLVERS: &[&'static dyn PartialSymbolResolver] = &[
&TagResolver,
&BranchResolver,
&TopicResolver,
&GitRefResolver,
];

#[derive(Default)]
struct CommitPrefixResolver<'a> {
Expand Down Expand Up @@ -2331,6 +2366,15 @@ fn resolve_commit_ref(
}
Ok(commit_ids)
}
RevsetCommitRef::Topics(pattern) => {
let commit_ids = repo
.view()
.topics_matching(pattern)
.flat_map(|(_, ids)| ids)
.cloned()
.collect();
Ok(commit_ids)
}
RevsetCommitRef::GitRefs => {
let mut commit_ids = vec![];
for ref_target in repo.view().git_refs().values() {
Expand Down

0 comments on commit acd0a0a

Please sign in to comment.