mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-20 03:20:08 +00:00
revset: Add optional argument n to ancestors() in revset language
This commit is contained in:
parent
6ea10906a8
commit
0946934ca6
4 changed files with 34 additions and 4 deletions
|
@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### New features
|
### New features
|
||||||
|
|
||||||
|
* The `ancestors()` revset function now takes an optional `depth` argument
|
||||||
|
to limit the depth of the ancestor set. For example, use `jj log -r
|
||||||
|
'ancestors(@, 5)` to view the last 5 commits.
|
||||||
|
|
||||||
### Fixed bugs
|
### Fixed bugs
|
||||||
|
|
||||||
## [0.9.0] - 2023-09-06
|
## [0.9.0] - 2023-09-06
|
||||||
|
|
|
@ -79,7 +79,9 @@ revsets (expressions) as arguments.
|
||||||
|
|
||||||
* `parents(x)`: Same as `x-`.
|
* `parents(x)`: Same as `x-`.
|
||||||
* `children(x)`: Same as `x+`.
|
* `children(x)`: Same as `x+`.
|
||||||
* `ancestors(x)`: Same as `:x`.
|
* `ancestors(x[, depth])`: `ancestors(x)` is the same as `::x`.
|
||||||
|
`ancestors(x, depth)` returns the ancestors of `x` limited to the given
|
||||||
|
`depth`.
|
||||||
* `descendants(x)`: Same as `x::`.
|
* `descendants(x)`: Same as `x::`.
|
||||||
* `connected(x)`: Same as `x::x`. Useful when `x` includes several commits.
|
* `connected(x)`: Same as `x::x`. Useful when `x` includes several commits.
|
||||||
* `all()`: All visible commits in the repo.
|
* `all()`: All visible commits in the repo.
|
||||||
|
|
|
@ -1171,9 +1171,15 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
|
||||||
Ok(expression.children())
|
Ok(expression.children())
|
||||||
});
|
});
|
||||||
map.insert("ancestors", |name, arguments_pair, state| {
|
map.insert("ancestors", |name, arguments_pair, state| {
|
||||||
let arg = expect_one_argument(name, arguments_pair)?;
|
let ([heads_arg], [depth_opt_arg]) = expect_arguments(name, arguments_pair)?;
|
||||||
let expression = parse_expression_rule(arg.into_inner(), state)?;
|
let heads = parse_expression_rule(heads_arg.into_inner(), state)?;
|
||||||
Ok(expression.ancestors())
|
let generation = if let Some(depth_arg) = depth_opt_arg {
|
||||||
|
let depth = parse_function_argument_as_literal("integer", name, depth_arg, state)?;
|
||||||
|
0..depth
|
||||||
|
} else {
|
||||||
|
GENERATION_RANGE_FULL
|
||||||
|
};
|
||||||
|
Ok(heads.ancestors_range(generation))
|
||||||
});
|
});
|
||||||
map.insert("descendants", |name, arguments_pair, state| {
|
map.insert("descendants", |name, arguments_pair, state| {
|
||||||
let arg = expect_one_argument(name, arguments_pair)?;
|
let arg = expect_one_argument(name, arguments_pair)?;
|
||||||
|
|
|
@ -1205,6 +1205,24 @@ fn test_evaluate_expression_ancestors(use_git: bool) {
|
||||||
root_commit.id().clone(),
|
root_commit.id().clone(),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Can find last n ancestors of a commit
|
||||||
|
assert_eq!(
|
||||||
|
resolve_commit_ids(mut_repo, &format!("ancestors({}, 0)", commit2.id().hex())),
|
||||||
|
vec![]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resolve_commit_ids(mut_repo, &format!("ancestors({}, 1)", commit3.id().hex())),
|
||||||
|
vec![commit3.id().clone()]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resolve_commit_ids(mut_repo, &format!("ancestors({}, 3)", commit3.id().hex())),
|
||||||
|
vec![
|
||||||
|
commit3.id().clone(),
|
||||||
|
commit2.id().clone(),
|
||||||
|
commit1.id().clone(),
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_case(false ; "local backend")]
|
#[test_case(false ; "local backend")]
|
||||||
|
|
Loading…
Reference in a new issue