From f87823a521a15ad56e3278d8f5c828f09cd3ac3c Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Sat, 14 Dec 2024 06:16:21 -0500 Subject: [PATCH] feat(revset): add ! as shortcut for "only child" eg `@!`, `abc123!`, `current(foo)!`, etc --- git-branchless-revset/src/grammar.lalrpop | 2 + git-branchless-revset/src/parser.rs | 69 +++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/git-branchless-revset/src/grammar.lalrpop b/git-branchless-revset/src/grammar.lalrpop index 1803d480d..40c0d8698 100644 --- a/git-branchless-revset/src/grammar.lalrpop +++ b/git-branchless-revset/src/grammar.lalrpop @@ -46,6 +46,8 @@ Expr4: Expr<'input> = { "~" => Expr::FunctionCall(Cow::Borrowed("ancestors.nth"), vec![lhs, Expr::Name(Cow::Borrowed("1"))]), "~" => Expr::FunctionCall(Cow::Borrowed("ancestors.nth"), vec![lhs, Expr::Name(rhs)]), + "!" => Expr::FunctionCall(Cow::Borrowed("sole"), vec![Expr::FunctionCall(Cow::Borrowed("children"), vec![lhs])]), + } diff --git a/git-branchless-revset/src/parser.rs b/git-branchless-revset/src/parser.rs index 86b65f0bd..8945140e7 100644 --- a/git-branchless-revset/src/parser.rs +++ b/git-branchless-revset/src/parser.rs @@ -667,4 +667,73 @@ mod tests { Ok(()) } + + #[test] + fn test_revset_child_operator() -> eyre::Result<()> { + insta::assert_debug_snapshot!(parse("foo!"), @r###" + Ok( + FunctionCall( + "sole", + [ + FunctionCall( + "children", + [ + Name( + "foo", + ), + ], + ), + ], + ), + ) + "###); + + insta::assert_debug_snapshot!(parse("@! + @!!"), @r###" + Ok( + FunctionCall( + "union", + [ + FunctionCall( + "sole", + [ + FunctionCall( + "children", + [ + Name( + "@", + ), + ], + ), + ], + ), + FunctionCall( + "sole", + [ + FunctionCall( + "children", + [ + FunctionCall( + "sole", + [ + FunctionCall( + "children", + [ + Name( + "@", + ), + ], + ), + ], + ), + ], + ), + ], + ), + ], + ), + ) + "###); + + Ok(()) + } }