2024-02-29 20:44:49 +00:00
|
|
|
// Copyright 2024 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2024-05-03 16:27:55 +00:00
|
|
|
use std::any::Any;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2024-04-23 22:11:58 +00:00
|
|
|
use itertools::Itertools;
|
2024-02-29 20:44:49 +00:00
|
|
|
use jj_cli::cli_util::CliRunner;
|
2024-08-22 18:18:15 +00:00
|
|
|
use jj_cli::commit_templater::CommitTemplateBuildFnTable;
|
|
|
|
use jj_cli::commit_templater::CommitTemplateLanguage;
|
|
|
|
use jj_cli::commit_templater::CommitTemplateLanguageExtension;
|
2024-02-29 20:44:49 +00:00
|
|
|
use jj_cli::template_builder::TemplateLanguage;
|
2024-08-23 02:54:12 +00:00
|
|
|
use jj_cli::template_parser;
|
2024-08-22 18:18:15 +00:00
|
|
|
use jj_cli::template_parser::TemplateParseError;
|
2024-03-21 02:38:23 +00:00
|
|
|
use jj_cli::templater::TemplatePropertyExt as _;
|
2024-03-05 23:08:47 +00:00
|
|
|
use jj_lib::backend::CommitId;
|
2024-02-29 20:44:49 +00:00
|
|
|
use jj_lib::commit::Commit;
|
2024-03-05 23:08:47 +00:00
|
|
|
use jj_lib::extensions_map::ExtensionsMap;
|
2024-02-29 20:44:49 +00:00
|
|
|
use jj_lib::object_id::ObjectId;
|
2024-03-05 23:08:47 +00:00
|
|
|
use jj_lib::repo::Repo;
|
2024-08-22 18:18:15 +00:00
|
|
|
use jj_lib::revset::FunctionCallNode;
|
|
|
|
use jj_lib::revset::PartialSymbolResolver;
|
|
|
|
use jj_lib::revset::RevsetExpression;
|
|
|
|
use jj_lib::revset::RevsetFilterExtension;
|
|
|
|
use jj_lib::revset::RevsetFilterPredicate;
|
|
|
|
use jj_lib::revset::RevsetParseContext;
|
|
|
|
use jj_lib::revset::RevsetParseError;
|
|
|
|
use jj_lib::revset::RevsetResolutionError;
|
|
|
|
use jj_lib::revset::SymbolResolverExtension;
|
2024-03-05 23:08:47 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2024-02-29 20:44:49 +00:00
|
|
|
|
|
|
|
struct HexCounter;
|
|
|
|
|
2024-03-05 23:08:47 +00:00
|
|
|
fn num_digits_in_id(id: &CommitId) -> i64 {
|
2024-02-29 20:44:49 +00:00
|
|
|
let mut count = 0;
|
2024-03-05 23:08:47 +00:00
|
|
|
for ch in id.hex().chars() {
|
2024-02-29 20:44:49 +00:00
|
|
|
if ch.is_ascii_digit() {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 23:08:47 +00:00
|
|
|
count
|
2024-02-29 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 02:38:23 +00:00
|
|
|
fn num_char_in_id(commit: Commit, ch_match: char) -> i64 {
|
2024-02-29 20:44:49 +00:00
|
|
|
let mut count = 0;
|
|
|
|
for ch in commit.id().hex().chars() {
|
|
|
|
if ch == ch_match {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 02:38:23 +00:00
|
|
|
count
|
2024-02-29 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 17:02:29 +00:00
|
|
|
#[derive(Default)]
|
2024-03-05 23:08:47 +00:00
|
|
|
struct MostDigitsInId {
|
|
|
|
count: OnceCell<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MostDigitsInId {
|
|
|
|
fn count(&self, repo: &dyn Repo) -> i64 {
|
|
|
|
*self.count.get_or_init(|| {
|
|
|
|
RevsetExpression::all()
|
|
|
|
.evaluate_programmatic(repo)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|id| num_digits_in_id(&id))
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:11:58 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct TheDigitestResolver {
|
|
|
|
cache: MostDigitsInId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialSymbolResolver for TheDigitestResolver {
|
|
|
|
fn resolve_symbol(
|
|
|
|
&self,
|
|
|
|
repo: &dyn Repo,
|
|
|
|
symbol: &str,
|
|
|
|
) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> {
|
|
|
|
if symbol != "thedigitest" {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(
|
|
|
|
RevsetExpression::all()
|
|
|
|
.evaluate_programmatic(repo)
|
|
|
|
.map_err(|err| RevsetResolutionError::Other(err.into()))?
|
|
|
|
.iter()
|
|
|
|
.filter(|id| num_digits_in_id(id) == self.cache.count(repo))
|
|
|
|
.collect_vec(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TheDigitest;
|
|
|
|
|
|
|
|
impl SymbolResolverExtension for TheDigitest {
|
|
|
|
fn new_resolvers<'a>(&self, _repo: &'a dyn Repo) -> Vec<Box<dyn PartialSymbolResolver + 'a>> {
|
|
|
|
vec![Box::<TheDigitestResolver>::default()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 20:44:49 +00:00
|
|
|
impl CommitTemplateLanguageExtension for HexCounter {
|
|
|
|
fn build_fn_table<'repo>(&self) -> CommitTemplateBuildFnTable<'repo> {
|
2024-03-19 15:01:06 +00:00
|
|
|
type L<'repo> = CommitTemplateLanguage<'repo>;
|
2024-02-29 20:44:49 +00:00
|
|
|
let mut table = CommitTemplateBuildFnTable::empty();
|
2024-03-05 23:08:47 +00:00
|
|
|
table.commit_methods.insert(
|
|
|
|
"has_most_digits",
|
|
|
|
|language, _build_context, property, call| {
|
2024-05-21 03:27:04 +00:00
|
|
|
call.expect_no_arguments()?;
|
2024-03-05 23:08:47 +00:00
|
|
|
let most_digits = language
|
|
|
|
.cache_extension::<MostDigitsInId>()
|
|
|
|
.unwrap()
|
|
|
|
.count(language.repo());
|
2024-03-21 02:38:23 +00:00
|
|
|
Ok(L::wrap_boolean(property.map(move |commit| {
|
|
|
|
num_digits_in_id(commit.id()) == most_digits
|
|
|
|
})))
|
2024-03-05 23:08:47 +00:00
|
|
|
},
|
|
|
|
);
|
2024-02-29 20:44:49 +00:00
|
|
|
table.commit_methods.insert(
|
|
|
|
"num_digits_in_id",
|
2024-03-19 15:01:06 +00:00
|
|
|
|_language, _build_context, property, call| {
|
2024-05-21 03:27:04 +00:00
|
|
|
call.expect_no_arguments()?;
|
2024-03-21 02:38:23 +00:00
|
|
|
Ok(L::wrap_integer(
|
|
|
|
property.map(|commit| num_digits_in_id(commit.id())),
|
|
|
|
))
|
2024-02-29 20:44:49 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
table.commit_methods.insert(
|
|
|
|
"num_char_in_id",
|
2024-03-19 15:01:06 +00:00
|
|
|
|_language, _build_context, property, call| {
|
2024-05-21 03:27:04 +00:00
|
|
|
let [string_arg] = call.expect_exact_arguments()?;
|
2024-02-29 20:44:49 +00:00
|
|
|
let char_arg =
|
|
|
|
template_parser::expect_string_literal_with(string_arg, |string, span| {
|
|
|
|
let chars: Vec<_> = string.chars().collect();
|
|
|
|
match chars[..] {
|
|
|
|
[ch] => Ok(ch),
|
2024-03-29 11:35:36 +00:00
|
|
|
_ => Err(TemplateParseError::expression(
|
2024-02-29 20:44:49 +00:00
|
|
|
"Expected singular character argument",
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2024-03-21 02:38:23 +00:00
|
|
|
Ok(L::wrap_integer(
|
|
|
|
property.map(move |commit| num_char_in_id(commit, char_arg)),
|
|
|
|
))
|
2024-02-29 20:44:49 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
table
|
|
|
|
}
|
2024-03-05 23:08:47 +00:00
|
|
|
|
|
|
|
fn build_cache_extensions(&self, extensions: &mut ExtensionsMap) {
|
2024-04-25 17:02:29 +00:00
|
|
|
extensions.insert(MostDigitsInId::default());
|
2024-03-05 23:08:47 +00:00
|
|
|
}
|
2024-02-29 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 16:27:55 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct EvenDigitsFilter;
|
|
|
|
|
|
|
|
impl RevsetFilterExtension for EvenDigitsFilter {
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn matches_commit(&self, commit: &Commit) -> bool {
|
|
|
|
num_digits_in_id(commit.id()) % 2 == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn even_digits(
|
2024-05-26 10:30:02 +00:00
|
|
|
function: &FunctionCallNode,
|
2024-06-03 06:30:00 +00:00
|
|
|
_context: &RevsetParseContext,
|
2024-05-03 16:27:55 +00:00
|
|
|
) -> Result<Rc<RevsetExpression>, RevsetParseError> {
|
2024-05-26 10:30:02 +00:00
|
|
|
function.expect_no_arguments()?;
|
2024-05-03 16:27:55 +00:00
|
|
|
Ok(RevsetExpression::filter(RevsetFilterPredicate::Extension(
|
2024-06-04 08:50:14 +00:00
|
|
|
Rc::new(EvenDigitsFilter),
|
2024-05-03 16:27:55 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
2024-02-29 20:44:49 +00:00
|
|
|
fn main() -> std::process::ExitCode {
|
|
|
|
CliRunner::init()
|
2024-04-23 22:11:58 +00:00
|
|
|
.add_symbol_resolver_extension(Box::new(TheDigitest))
|
2024-05-03 16:27:55 +00:00
|
|
|
.add_revset_function_extension("even_digits", even_digits)
|
2024-04-11 18:58:38 +00:00
|
|
|
.add_commit_template_extension(Box::new(HexCounter))
|
2024-02-29 20:44:49 +00:00
|
|
|
.run()
|
|
|
|
}
|