| 1 |
//! The revset query language |
| 2 |
//! |
| 3 |
//! <https://www.mercurial-scm.org/repo/hg/help/revsets> |
| 4 |
|
| 5 |
use crate::errors::HgError; |
| 6 |
use crate::repo::Repo; |
| 7 |
use crate::revlog::NodePrefix; |
| 8 |
use crate::revlog::Revision; |
| 9 |
use crate::revlog::RevisionOrWdir; |
| 10 |
use crate::revlog::Revlog; |
| 11 |
use crate::revlog::RevlogError; |
| 12 |
use crate::revlog::NULL_REVISION; |
| 13 |
use crate::revlog::WORKING_DIRECTORY_HEX; |
| 14 |
use crate::Node; |
| 15 |
use crate::WORKING_DIRECTORY_REVISION; |
| 16 |
|
| 17 |
/// Resolve a query string into a single revision. |
| 18 |
/// |
| 19 |
/// Only some of the revset language is implemented yet. |
| 20 |
pub fn resolve_single( |
| 21 |
input: &str, |
| 22 |
repo: &Repo, |
| 23 |
) -> Result<RevisionOrWdir, RevlogError> { |
| 24 |
let changelog = repo.changelog()?; |
| 25 |
|
| 26 |
match input { |
| 27 |
"." => { |
| 28 |
let p1 = repo.dirstate_parents()?.p1; |
| 29 |
return Ok(changelog.revlog.rev_from_node(p1.into())?.into()); |
| 30 |
} |
| 31 |
"null" => return Ok(NULL_REVISION.into()), |
| 32 |
"wdir()" => return Ok(RevisionOrWdir::wdir()), |
| 33 |
_ => {} |
| 34 |
} |
| 35 |
|
| 36 |
match resolve(input, &changelog.revlog) { |
| 37 |
Err(RevlogError::InvalidRevision(revision)) => { |
| 38 |
// TODO: support for the rest of the language here. |
| 39 |
let msg = format!("cannot parse revset '{}'", revision); |
| 40 |
Err(HgError::unsupported(msg).into()) |
| 41 |
} |
| 42 |
result => result, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/// Resolve the small subset of the language suitable for revlogs other than |
| 47 |
/// the changelog, such as in `hg debugdata --manifest` CLI argument. |
| 48 |
/// |
| 49 |
/// * A non-negative decimal integer for a revision number, or |
| 50 |
/// * A hexadecimal string, for the unique node ID that starts with this prefix |
| 51 |
pub fn resolve_rev_number_or_hex_prefix( |
| 52 |
input: &str, |
| 53 |
revlog: &Revlog, |
| 54 |
) -> Result<Revision, RevlogError> { |
| 55 |
match resolve(input, revlog)?.exclude_wdir() { |
| 56 |
Some(rev) => Ok(rev), |
| 57 |
None => Err(RevlogError::WDirUnsupported), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
fn resolve( |
| 62 |
input: &str, |
| 63 |
revlog: &Revlog, |
| 64 |
) -> Result<RevisionOrWdir, RevlogError> { |
| 65 |
// The Python equivalent of this is part of `revsymbol` in |
| 66 |
// `mercurial/scmutil.py` |
| 67 |
if let Ok(integer) = input.parse::<i32>() { |
| 68 |
if integer.to_string() == input && integer >= 0 { |
| 69 |
if integer == WORKING_DIRECTORY_REVISION.0 { |
| 70 |
return Ok(RevisionOrWdir::wdir()); |
| 71 |
} |
| 72 |
if revlog.has_rev(integer.into()) { |
| 73 |
// This is fine because we've just checked that the revision is |
| 74 |
// valid for the given revlog. |
| 75 |
return Ok(Revision(integer).into()); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
if let Ok(prefix) = NodePrefix::from_hex(input) { |
| 80 |
let wdir_node = |
| 81 |
Node::from_hex(WORKING_DIRECTORY_HEX).expect("wdir hex is valid"); |
| 82 |
if prefix.is_prefix_of(&wdir_node) { |
| 83 |
return Ok(RevisionOrWdir::wdir()); |
| 84 |
} |
| 85 |
return Ok(revlog.rev_from_node(prefix)?.into()); |
| 86 |
} |
| 87 |
Err(RevlogError::InvalidRevision(input.to_string())) |
| 88 |
} |