annotate rust/hg-core/src/requirements.rs @ 46443:43d63979a75e

rust: use HgError in RevlogError and Vfs Differential Revision: https://phab.mercurial-scm.org/D9897
author Simon Sapin <simon.sapin@octobus.net>
date Wed, 27 Jan 2021 14:45:25 +0100
parents 02d3bb972121
children d03b0601e0eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46443
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46442
diff changeset
1 use crate::errors::{HgError, HgResultExt};
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46090
diff changeset
2 use crate::repo::Repo;
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
3
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
4 fn parse(bytes: &[u8]) -> Result<Vec<String>, HgError> {
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
5 // The Python code reading this file uses `str.splitlines`
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
6 // which looks for a number of line separators (even including a couple of
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
7 // non-ASCII ones), but Python code writing it always uses `\n`.
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
8 let lines = bytes.split(|&byte| byte == b'\n');
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
9
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
10 lines
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
11 .filter(|line| !line.is_empty())
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
12 .map(|line| {
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
13 // Python uses Unicode `str.isalnum` but feature names are all
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
14 // ASCII
45938
f5d62f4d5327 rhg: check that .hg/requires is ASCII
Simon Sapin <simon-commits@exyr.org>
parents: 45937
diff changeset
15 if line[0].is_ascii_alphanumeric() && line.is_ascii() {
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
16 Ok(String::from_utf8(line.into()).unwrap())
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
17 } else {
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
18 Err(HgError::corrupted("parse error in 'requires' file"))
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
19 }
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
20 })
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
21 .collect()
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
22 }
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
23
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
24 pub fn load(repo: &Repo) -> Result<Vec<String>, HgError> {
46443
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46442
diff changeset
25 if let Some(bytes) =
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46442
diff changeset
26 repo.hg_vfs().read("requires").io_not_found_as_none()?
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
27 {
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
28 parse(&bytes)
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
29 } else {
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
30 // Treat a missing file the same as an empty file.
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
31 // From `mercurial/localrepo.py`:
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
32 // > requires file contains a newline-delimited list of
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
33 // > features/capabilities the opener (us) must have in order to use
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
34 // > the repository. This file was introduced in Mercurial 0.9.2,
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
35 // > which means very old repositories may not have one. We assume
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
36 // > a missing file translates to no requirements.
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
37 Ok(Vec::new())
45924
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
38 }
a2eda1ff22aa requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
diff changeset
39 }
45937
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
40
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
41 pub fn check(repo: &Repo) -> Result<(), HgError> {
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46090
diff changeset
42 for feature in load(repo)? {
45937
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
43 if !SUPPORTED.contains(&&*feature) {
46442
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
44 // TODO: collect and all unknown features and include them in the
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
45 // error message?
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
46 return Err(HgError::UnsupportedFeature(format!(
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
47 "repository requires feature unknown to this Mercurial: {}",
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
48 feature
02d3bb972121 rust: replace RequirementsError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
49 )));
45937
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
50 }
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
51 }
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
52 Ok(())
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
53 }
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
54
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
55 // TODO: set this to actually-supported features
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
56 const SUPPORTED: &[&str] = &[
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
57 "dotencode",
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
58 "fncache",
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
59 "generaldelta",
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
60 "revlogv1",
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
61 "sparserevlog",
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
62 "store",
46090
9eb07ab3f2d4 rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents: 45956
diff changeset
63 // As of this writing everything rhg does is read-only.
9eb07ab3f2d4 rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents: 45956
diff changeset
64 // When it starts writing to the repository, it’ll need to either keep the
9eb07ab3f2d4 rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents: 45956
diff changeset
65 // persistent nodemap up to date or remove this entry:
9eb07ab3f2d4 rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents: 45956
diff changeset
66 "persistent-nodemap",
45937
2ad2745e0be9 rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents: 45924
diff changeset
67 ];