Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/revset.rs @ 46433:4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
This new module will be the place to implement more of the revset language
when we do so.
Differential Revision: https://phab.mercurial-scm.org/D9873
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Tue, 26 Jan 2021 18:31:46 +0100 |
parents | |
children | df247f58ecee |
comparison
equal
deleted
inserted
replaced
46432:18a261b11b20 | 46433:4b381dbbf8b7 |
---|---|
1 //! The revset query language | |
2 //! | |
3 //! <https://www.mercurial-scm.org/repo/hg/help/revsets> | |
4 | |
5 use crate::repo::Repo; | |
6 use crate::revlog::changelog::Changelog; | |
7 use crate::revlog::revlog::{Revlog, RevlogError}; | |
8 use crate::revlog::NodePrefix; | |
9 use crate::revlog::{Revision, NULL_REVISION}; | |
10 | |
11 /// Resolve a query string into a single revision. | |
12 /// | |
13 /// Only some of the revset language is implemented yet. | |
14 pub fn resolve_single( | |
15 input: &str, | |
16 repo: &Repo, | |
17 ) -> Result<Revision, RevlogError> { | |
18 let changelog = Changelog::open(repo)?; | |
19 | |
20 match resolve_rev_number_or_hex_prefix(input, &changelog.revlog) { | |
21 Err(RevlogError::InvalidRevision) => {} // Try other syntax | |
22 result => return result, | |
23 } | |
24 | |
25 if input == "null" { | |
26 return Ok(NULL_REVISION); | |
27 } | |
28 | |
29 // TODO: support for the rest of the language here. | |
30 | |
31 Err(RevlogError::InvalidRevision) | |
32 } | |
33 | |
34 /// Resolve the small subset of the language suitable for revlogs other than | |
35 /// the changelog, such as in `hg debugdata --manifest` CLI argument. | |
36 /// | |
37 /// * A non-negative decimal integer for a revision number, or | |
38 /// * An hexadecimal string, for the unique node ID that starts with this | |
39 /// prefix | |
40 pub fn resolve_rev_number_or_hex_prefix( | |
41 input: &str, | |
42 revlog: &Revlog, | |
43 ) -> Result<Revision, RevlogError> { | |
44 if let Ok(integer) = input.parse::<i32>() { | |
45 if integer >= 0 && revlog.has_rev(integer) { | |
46 return Ok(integer); | |
47 } | |
48 } | |
49 if let Ok(prefix) = NodePrefix::from_hex(input) { | |
50 return revlog.get_node_rev(prefix); | |
51 } | |
52 Err(RevlogError::InvalidRevision) | |
53 } |