mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-30 16:10:23 +00:00
49e9003c4e
Some checks are pending
binaries / Build binary artifacts (linux-aarch64-gnu, ubuntu-24.04, aarch64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-aarch64-musl, ubuntu-24.04, aarch64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-gnu, ubuntu-24.04, x86_64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-musl, ubuntu-24.04, x86_64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (macos-aarch64, macos-14, aarch64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (macos-x86_64, macos-13, x86_64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (win-x86_64, windows-2022, x86_64-pc-windows-msvc) (push) Waiting to run
nix / flake check (macos-14) (push) Waiting to run
nix / flake check (ubuntu-latest) (push) Waiting to run
build / build (, macos-13) (push) Waiting to run
build / build (, macos-14) (push) Waiting to run
build / build (, ubuntu-latest) (push) Waiting to run
build / build (, windows-latest) (push) Waiting to run
build / build (--all-features, ubuntu-latest) (push) Waiting to run
build / Build jj-lib without Git support (push) Waiting to run
build / Check protos (push) Waiting to run
build / Check formatting (push) Waiting to run
build / Check that MkDocs can build the docs (push) Waiting to run
build / Check that MkDocs can build the docs with Poetry 1.8 (push) Waiting to run
build / cargo-deny (advisories) (push) Waiting to run
build / cargo-deny (bans licenses sources) (push) Waiting to run
build / Clippy check (push) Waiting to run
Codespell / Codespell (push) Waiting to run
website / prerelease-docs-build-deploy (ubuntu-latest) (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
Custom backends may rely on networking or other unreliable implementations to support revsets, this change allows them to return errors cleanly instead of panicking. For simplicity, only the public-facing Revset and RevsetGraph types are changed in this commit; the internal revset engine remains mostly unchanged and error-free since it cannot generally produce errors.
207 lines
6.4 KiB
Rust
207 lines
6.4 KiB
Rust
// 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.
|
|
|
|
use std::any::Any;
|
|
use std::rc::Rc;
|
|
|
|
use itertools::Itertools;
|
|
use jj_cli::cli_util::CliRunner;
|
|
use jj_cli::commit_templater::CommitTemplateBuildFnTable;
|
|
use jj_cli::commit_templater::CommitTemplateLanguage;
|
|
use jj_cli::commit_templater::CommitTemplateLanguageExtension;
|
|
use jj_cli::template_builder::TemplateLanguage;
|
|
use jj_cli::template_parser;
|
|
use jj_cli::template_parser::TemplateParseError;
|
|
use jj_cli::templater::TemplatePropertyExt as _;
|
|
use jj_lib::backend::CommitId;
|
|
use jj_lib::commit::Commit;
|
|
use jj_lib::extensions_map::ExtensionsMap;
|
|
use jj_lib::object_id::ObjectId;
|
|
use jj_lib::repo::Repo;
|
|
use jj_lib::revset::FunctionCallNode;
|
|
use jj_lib::revset::PartialSymbolResolver;
|
|
use jj_lib::revset::RevsetDiagnostics;
|
|
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;
|
|
use once_cell::sync::OnceCell;
|
|
|
|
struct HexCounter;
|
|
|
|
fn num_digits_in_id(id: &CommitId) -> i64 {
|
|
let mut count = 0;
|
|
for ch in id.hex().chars() {
|
|
if ch.is_ascii_digit() {
|
|
count += 1;
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
fn num_char_in_id(commit: Commit, ch_match: char) -> i64 {
|
|
let mut count = 0;
|
|
for ch in commit.id().hex().chars() {
|
|
if ch == ch_match {
|
|
count += 1;
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
#[derive(Default)]
|
|
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(Result::unwrap)
|
|
.map(|id| num_digits_in_id(&id))
|
|
.max()
|
|
.unwrap_or(0)
|
|
})
|
|
}
|
|
}
|
|
|
|
#[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()
|
|
.map(Result::unwrap)
|
|
.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()]
|
|
}
|
|
}
|
|
|
|
impl CommitTemplateLanguageExtension for HexCounter {
|
|
fn build_fn_table<'repo>(&self) -> CommitTemplateBuildFnTable<'repo> {
|
|
type L<'repo> = CommitTemplateLanguage<'repo>;
|
|
let mut table = CommitTemplateBuildFnTable::empty();
|
|
table.commit_methods.insert(
|
|
"has_most_digits",
|
|
|language, _diagnostics, _build_context, property, call| {
|
|
call.expect_no_arguments()?;
|
|
let most_digits = language
|
|
.cache_extension::<MostDigitsInId>()
|
|
.unwrap()
|
|
.count(language.repo());
|
|
Ok(L::wrap_boolean(property.map(move |commit| {
|
|
num_digits_in_id(commit.id()) == most_digits
|
|
})))
|
|
},
|
|
);
|
|
table.commit_methods.insert(
|
|
"num_digits_in_id",
|
|
|_language, _diagnostics, _build_context, property, call| {
|
|
call.expect_no_arguments()?;
|
|
Ok(L::wrap_integer(
|
|
property.map(|commit| num_digits_in_id(commit.id())),
|
|
))
|
|
},
|
|
);
|
|
table.commit_methods.insert(
|
|
"num_char_in_id",
|
|
|_language, _diagnostics, _build_context, property, call| {
|
|
let [string_arg] = call.expect_exact_arguments()?;
|
|
let char_arg =
|
|
template_parser::expect_string_literal_with(string_arg, |string, span| {
|
|
let chars: Vec<_> = string.chars().collect();
|
|
match chars[..] {
|
|
[ch] => Ok(ch),
|
|
_ => Err(TemplateParseError::expression(
|
|
"Expected singular character argument",
|
|
span,
|
|
)),
|
|
}
|
|
})?;
|
|
|
|
Ok(L::wrap_integer(
|
|
property.map(move |commit| num_char_in_id(commit, char_arg)),
|
|
))
|
|
},
|
|
);
|
|
|
|
table
|
|
}
|
|
|
|
fn build_cache_extensions(&self, extensions: &mut ExtensionsMap) {
|
|
extensions.insert(MostDigitsInId::default());
|
|
}
|
|
}
|
|
|
|
#[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(
|
|
_diagnostics: &mut RevsetDiagnostics,
|
|
function: &FunctionCallNode,
|
|
_context: &RevsetParseContext,
|
|
) -> Result<Rc<RevsetExpression>, RevsetParseError> {
|
|
function.expect_no_arguments()?;
|
|
Ok(RevsetExpression::filter(RevsetFilterPredicate::Extension(
|
|
Rc::new(EvenDigitsFilter),
|
|
)))
|
|
}
|
|
|
|
fn main() -> std::process::ExitCode {
|
|
CliRunner::init()
|
|
.add_symbol_resolver_extension(Box::new(TheDigitest))
|
|
.add_revset_function_extension("even_digits", even_digits)
|
|
.add_commit_template_extension(Box::new(HexCounter))
|
|
.run()
|
|
}
|