Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Added

- (#508) Added `exactly(<revset>, n)` revset function to allow assertions on the number of commits within a set.

### Changed

- (#512) Fixed so that the setting for `--color` is now respected.
Expand Down
19 changes: 19 additions & 0 deletions git-branchless/src/revset/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ lazy_static! {
("committer.name", &fn_committer_name),
("committer.email", &fn_committer_email),
("committer.date", &fn_committer_date),
("exactly", &fn_exactly),
];
functions.iter().cloned().collect()
};
Expand Down Expand Up @@ -377,3 +378,21 @@ fn fn_committer_date(ctx: &mut Context, name: &str, args: &[Expr]) -> EvalResult
}),
)
}

fn fn_exactly(ctx: &mut Context, name: &str, args: &[Expr]) -> EvalResult {
let (lhs, expected_len) = eval_number_rhs(ctx, name, args)?;
let actual_len: usize = lhs
.count()
.wrap_err("Counting commit set")
.map_err(EvalError::OtherError)?;

if actual_len == expected_len {
Ok(lhs)
} else {
Err(EvalError::UnexpectedSetLength {
expr: format!("{}", args[0]),
expected_len,
actual_len,
})
}
}
69 changes: 67 additions & 2 deletions git-branchless/src/revset/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use once_cell::unsync::OnceCell;
use thiserror::Error;

use lib::core::dag::{CommitSet, Dag};
use lib::core::formatting::Pluralize;
use lib::git::{Repo, ResolvedReferenceInfo};
use tracing::instrument;

Expand Down Expand Up @@ -104,8 +105,19 @@ pub enum EvalError {
actual_arity: usize,
},

#[error("expected {expr} to evaluate to 1 element, but got {actual_len}")]
ExpectedSingletonSet { expr: String, actual_len: usize },
#[error(
"expected '{expr}' to evaluate to {}, but got {actual_len}",
Pluralize {
determiner: None,
amount: *expected_len,
unit: ("element", "elements"),
}
)]
UnexpectedSetLength {
expr: String,
expected_len: usize,
actual_len: usize,
},

#[error("not an integer: {from}")]
ParseInt {
Expand Down Expand Up @@ -708,6 +720,59 @@ mod tests {
"###);
}

{
let expr = Expr::FunctionCall(
Cow::Borrowed("exactly"),
vec![
Expr::FunctionCall(Cow::Borrowed("stack"), vec![]),
Expr::Name(Cow::Borrowed("3")),
],
);
insta::assert_debug_snapshot!(eval_and_sort(&effects, &repo, &mut dag, &expr), @r###"
Ok(
[
Commit {
inner: Commit {
id: 848121cb21bf9af8b064c91bc8930bd16d624a22,
summary: "create test5.txt",
},
},
Commit {
inner: Commit {
id: f0abf649939928fe5475179fd84e738d3d3725dc,
summary: "create test6.txt",
},
},
Commit {
inner: Commit {
id: ba07500a4adc661dc06a748d200ef92120e1b355,
summary: "create test7.txt",
},
},
],
)
"###);
}

{
let expr = Expr::FunctionCall(
Cow::Borrowed("exactly"),
vec![
Expr::FunctionCall(Cow::Borrowed("stack"), vec![]),
Expr::Name(Cow::Borrowed("2")),
],
);
insta::assert_debug_snapshot!(eval_and_sort(&effects, &repo, &mut dag, &expr), @r###"
Err(
UnexpectedSetLength {
expr: "stack()",
expected_len: 2,
actual_len: 3,
},
)
"###);
}

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion git-branchless/tests/command/test_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn test_query_eval_error() -> eyre::Result<()> {
},
)?;
insta::assert_snapshot!(stderr, @r###"
Evaluation error for expression 'foo()': no function with the name 'foo' could be found; these functions are available: all, ancestors, ancestors.nth, author.date, author.email, author.name, branches, children, committer.date, committer.email, committer.name, descendants, difference, draft, heads, intersection, message, none, not, only, parents, parents.nth, paths.changed, range, roots, stack, union
Evaluation error for expression 'foo()': no function with the name 'foo' could be found; these functions are available: all, ancestors, ancestors.nth, author.date, author.email, author.name, branches, children, committer.date, committer.email, committer.name, descendants, difference, draft, exactly, heads, intersection, message, none, not, only, parents, parents.nth, paths.changed, range, roots, stack, union
"###);
insta::assert_snapshot!(stdout, @"");
}
Expand Down