| 1 |
use std::collections::HashSet; |
| 2 |
|
| 3 |
use crate::errors::HgError; |
| 4 |
use crate::errors::HgResultExt; |
| 5 |
use crate::utils::strings::join_display; |
| 6 |
use crate::vfs::VfsImpl; |
| 7 |
|
| 8 |
fn parse(bytes: &[u8]) -> Result<HashSet<String>, HgError> { |
| 9 |
// The Python code reading this file uses `str.splitlines` |
| 10 |
// which looks for a number of line separators (even including a couple of |
| 11 |
// non-ASCII ones), but Python code writing it always uses `\n`. |
| 12 |
let lines = bytes.split(|&byte| byte == b'\n'); |
| 13 |
|
| 14 |
lines |
| 15 |
.filter(|line| !line.is_empty()) |
| 16 |
.map(|line| { |
| 17 |
// Python uses Unicode `str.isalnum` but feature names are all |
| 18 |
// ASCII |
| 19 |
if line[0].is_ascii_alphanumeric() && line.is_ascii() { |
| 20 |
Ok(String::from_utf8(line.into()).unwrap()) |
| 21 |
} else { |
| 22 |
Err(HgError::corrupted("parse error in 'requires' file")) |
| 23 |
} |
| 24 |
}) |
| 25 |
.collect() |
| 26 |
} |
| 27 |
|
| 28 |
pub(crate) fn load(hg_vfs: VfsImpl) -> Result<HashSet<String>, HgError> { |
| 29 |
parse(&hg_vfs.read("requires")?) |
| 30 |
} |
| 31 |
|
| 32 |
pub(crate) fn load_if_exists( |
| 33 |
hg_vfs: &VfsImpl, |
| 34 |
) -> Result<HashSet<String>, HgError> { |
| 35 |
if let Some(bytes) = hg_vfs.read("requires").io_not_found_as_none()? { |
| 36 |
parse(&bytes) |
| 37 |
} else { |
| 38 |
// Treat a missing file the same as an empty file. |
| 39 |
// From `mercurial/localrepo.py`: |
| 40 |
// > requires file contains a newline-delimited list of |
| 41 |
// > features/capabilities the opener (us) must have in order to use |
| 42 |
// > the repository. This file was introduced in Mercurial 0.9.2, |
| 43 |
// > which means very old repositories may not have one. We assume |
| 44 |
// > a missing file translates to no requirements. |
| 45 |
Ok(HashSet::new()) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub(crate) fn check(reqs: &HashSet<String>) -> Result<(), HgError> { |
| 50 |
let unknown: Vec<_> = reqs |
| 51 |
.iter() |
| 52 |
.map(String::as_str) |
| 53 |
.filter(|feature| { |
| 54 |
!REQUIRED.contains(feature) && !SUPPORTED.contains(feature) |
| 55 |
}) |
| 56 |
.collect(); |
| 57 |
if !unknown.is_empty() { |
| 58 |
return Err(HgError::unsupported(format!( |
| 59 |
"repository requires feature unknown to this Mercurial: {}", |
| 60 |
join_display(&unknown, ", ") |
| 61 |
))); |
| 62 |
} |
| 63 |
let missing: Vec<_> = |
| 64 |
REQUIRED.iter().filter(|&&feature| !reqs.contains(feature)).collect(); |
| 65 |
if !missing.is_empty() { |
| 66 |
return Err(HgError::unsupported(format!( |
| 67 |
"repository is missing feature required by this Mercurial: {}", |
| 68 |
join_display(&missing, ", ") |
| 69 |
))); |
| 70 |
} |
| 71 |
let has_fncache = reqs.contains(FNCACHE_REQUIREMENT); |
| 72 |
let has_fileindex = reqs.contains(FILEINDEX_V1_REQUIREMENT); |
| 73 |
if !(has_fncache ^ has_fileindex) { |
| 74 |
return Err(HgError::unsupported(format!( |
| 75 |
"bad requirements, need exactly one of {} or {}", |
| 76 |
FNCACHE_REQUIREMENT, FILEINDEX_V1_REQUIREMENT |
| 77 |
))); |
| 78 |
} |
| 79 |
let has_dotencode = reqs.contains(DOTENCODE_REQUIREMENT); |
| 80 |
let has_plainencode = reqs.contains(PLAIN_ENCODE_REQUIREMENT); |
| 81 |
if has_fncache && !(has_dotencode ^ has_plainencode) { |
| 82 |
return Err(HgError::unsupported(format!( |
| 83 |
"bad requirements, need exactly one of {} or {}", |
| 84 |
DOTENCODE_REQUIREMENT, PLAIN_ENCODE_REQUIREMENT |
| 85 |
))); |
| 86 |
} |
| 87 |
if has_fileindex && has_dotencode { |
| 88 |
return Err(HgError::unsupported(format!( |
| 89 |
"bad requirements, {} and {} are mutually exclusive", |
| 90 |
FILEINDEX_V1_REQUIREMENT, DOTENCODE_REQUIREMENT |
| 91 |
))); |
| 92 |
} |
| 93 |
Ok(()) |
| 94 |
} |
| 95 |
|
| 96 |
/// rhg does not support repositories that are *missing* any of these features |
| 97 |
const REQUIRED: &[&str] = &[REVLOGV1_REQUIREMENT, STORE_REQUIREMENT]; |
| 98 |
|
| 99 |
/// rhg supports repository with or without these |
| 100 |
const SUPPORTED: &[&str] = &[ |
| 101 |
GENERALDELTA_REQUIREMENT, |
| 102 |
SHARED_REQUIREMENT, |
| 103 |
SHARESAFE_REQUIREMENT, |
| 104 |
SPARSEREVLOG_REQUIREMENT, |
| 105 |
DELTA_INFO_REQUIREMENT, |
| 106 |
RELATIVE_SHARED_REQUIREMENT, |
| 107 |
REVLOG_COMPRESSION_ZSTD, |
| 108 |
DIRSTATE_V2_REQUIREMENT, |
| 109 |
DIRSTATE_TRACKED_HINT_V1, |
| 110 |
// As of this writing everything rhg does is read-only. |
| 111 |
// When it starts writing to the repository, it’ll need to either keep the |
| 112 |
// persistent nodemap up to date or remove this entry: |
| 113 |
NODEMAP_REQUIREMENT, |
| 114 |
// Not all commands support `sparse` and `narrow`. The commands that do |
| 115 |
// not should opt out by checking `has_sparse` and `has_narrow`. |
| 116 |
SPARSE_REQUIREMENT, |
| 117 |
NARROW_REQUIREMENT, |
| 118 |
// rhg doesn't care about bookmarks at all yet |
| 119 |
BOOKMARKS_IN_STORE_REQUIREMENT, |
| 120 |
FNCACHE_REQUIREMENT, |
| 121 |
FILEINDEX_V1_REQUIREMENT, |
| 122 |
PLAIN_ENCODE_REQUIREMENT, |
| 123 |
DOTENCODE_REQUIREMENT, |
| 124 |
]; |
| 125 |
|
| 126 |
// Copied from mercurial/requirements.py: |
| 127 |
|
| 128 |
pub const DIRSTATE_V2_REQUIREMENT: &str = "dirstate-v2"; |
| 129 |
pub const GENERALDELTA_REQUIREMENT: &str = "generaldelta"; |
| 130 |
pub const DOTENCODE_REQUIREMENT: &str = "dotencode"; |
| 131 |
pub const STORE_REQUIREMENT: &str = "store"; |
| 132 |
pub const FNCACHE_REQUIREMENT: &str = "fncache"; |
| 133 |
pub const FILEINDEX_V1_REQUIREMENT: &str = "fileindex-v1"; |
| 134 |
pub const PLAIN_ENCODE_REQUIREMENT: &str = |
| 135 |
"exp-very-fragile-and-unsafe-plain-store-encoding"; |
| 136 |
|
| 137 |
/// A repository that uses the tracked hint dirstate file |
| 138 |
#[allow(unused)] |
| 139 |
pub const DIRSTATE_TRACKED_HINT_V1: &str = "dirstate-tracked-key-v1"; |
| 140 |
|
| 141 |
/// When narrowing is finalized and no longer subject to format changes, |
| 142 |
/// we should move this to just "narrow" or similar. |
| 143 |
#[allow(unused)] |
| 144 |
pub const NARROW_REQUIREMENT: &str = "narrowhg-experimental"; |
| 145 |
|
| 146 |
/// Bookmarks must be stored in the `store` part of the repository and will be |
| 147 |
/// share accross shares |
| 148 |
#[allow(unused)] |
| 149 |
pub const BOOKMARKS_IN_STORE_REQUIREMENT: &str = "bookmarksinstore"; |
| 150 |
|
| 151 |
/// Enables sparse working directory usage |
| 152 |
#[allow(unused)] |
| 153 |
pub const SPARSE_REQUIREMENT: &str = "exp-sparse"; |
| 154 |
|
| 155 |
/// Enables the internal phase which is used to hide changesets instead |
| 156 |
/// of stripping them |
| 157 |
#[allow(unused)] |
| 158 |
pub const INTERNAL_PHASE_REQUIREMENT: &str = "internal-phase"; |
| 159 |
|
| 160 |
/// Stores manifest in Tree structure |
| 161 |
#[allow(unused)] |
| 162 |
pub const TREEMANIFEST_REQUIREMENT: &str = "treemanifest"; |
| 163 |
|
| 164 |
/// Whether to use the "RevlogNG" or V1 of the revlog format |
| 165 |
#[allow(unused)] |
| 166 |
pub const REVLOGV1_REQUIREMENT: &str = "revlogv1"; |
| 167 |
|
| 168 |
/// Increment the sub-version when the revlog v2 format changes to lock out old |
| 169 |
/// clients. |
| 170 |
#[allow(unused)] |
| 171 |
pub const REVLOGV2_REQUIREMENT: &str = "exp-revlogv2.1"; |
| 172 |
|
| 173 |
/// Increment the sub-version when the revlog v2 format changes to lock out old |
| 174 |
/// clients. |
| 175 |
#[allow(unused)] |
| 176 |
pub const CHANGELOGV2_REQUIREMENT: &str = "exp-changelog-v2"; |
| 177 |
|
| 178 |
/// A repository with the sparserevlog feature will have delta chains that |
| 179 |
/// can spread over a larger span. Sparse reading cuts these large spans into |
| 180 |
/// pieces, so that each piece isn't too big. |
| 181 |
/// Without the sparserevlog capability, reading from the repository could use |
| 182 |
/// huge amounts of memory, because the whole span would be read at once, |
| 183 |
/// including all the intermediate revisions that aren't pertinent for the |
| 184 |
/// chain. This is why once a repository has enabled sparse-read, it becomes |
| 185 |
/// required. |
| 186 |
#[allow(unused)] |
| 187 |
pub const SPARSEREVLOG_REQUIREMENT: &str = "sparserevlog"; |
| 188 |
|
| 189 |
/// Revlog stores extra information about delta |
| 190 |
#[allow(unused)] |
| 191 |
pub const DELTA_INFO_REQUIREMENT: &str = "delta-info-revlog"; |
| 192 |
|
| 193 |
/// A repository with the the copies-sidedata-changeset requirement will store |
| 194 |
/// copies related information in changeset's sidedata. |
| 195 |
#[allow(unused)] |
| 196 |
pub const COPIESSDC_REQUIREMENT: &str = "exp-copies-sidedata-changeset"; |
| 197 |
|
| 198 |
/// The repository use persistent nodemap for the changelog and the manifest. |
| 199 |
#[allow(unused)] |
| 200 |
pub const NODEMAP_REQUIREMENT: &str = "persistent-nodemap"; |
| 201 |
|
| 202 |
/// Denotes that the current repository is a share |
| 203 |
#[allow(unused)] |
| 204 |
pub const SHARED_REQUIREMENT: &str = "shared"; |
| 205 |
|
| 206 |
/// Denotes that current repository is a share and the shared source path is |
| 207 |
/// relative to the current repository root path |
| 208 |
#[allow(unused)] |
| 209 |
pub const RELATIVE_SHARED_REQUIREMENT: &str = "relshared"; |
| 210 |
|
| 211 |
/// A repository with share implemented safely. The repository has different |
| 212 |
/// store and working copy requirements i.e. both `.hg/requires` and |
| 213 |
/// `.hg/store/requires` are present. |
| 214 |
#[allow(unused)] |
| 215 |
pub const SHARESAFE_REQUIREMENT: &str = "share-safe"; |
| 216 |
|
| 217 |
/// A repository that use zstd compression inside its revlog |
| 218 |
#[allow(unused)] |
| 219 |
pub const REVLOG_COMPRESSION_ZSTD: &str = "revlog-compression-zstd"; |
| 220 |
|
| 221 |
#[cfg(test)] |
| 222 |
mod tests { |
| 223 |
use super::*; |
| 224 |
|
| 225 |
fn create_reqs(reqs: &[&str]) -> HashSet<String> { |
| 226 |
let mut set = create_reqs_no_defaults(reqs); |
| 227 |
for req in REQUIRED { |
| 228 |
set.insert(req.to_string()); |
| 229 |
} |
| 230 |
set |
| 231 |
} |
| 232 |
|
| 233 |
fn create_reqs_no_defaults(reqs: &[&str]) -> HashSet<String> { |
| 234 |
let mut set = HashSet::new(); |
| 235 |
for &req in reqs { |
| 236 |
set.insert(req.to_string()); |
| 237 |
} |
| 238 |
set |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn test_check() { |
| 243 |
// minimum reqs |
| 244 |
assert!(check(&create_reqs(&[ |
| 245 |
FNCACHE_REQUIREMENT, |
| 246 |
PLAIN_ENCODE_REQUIREMENT |
| 247 |
])) |
| 248 |
.is_ok()); |
| 249 |
assert!(check(&create_reqs(&[ |
| 250 |
FNCACHE_REQUIREMENT, |
| 251 |
DOTENCODE_REQUIREMENT |
| 252 |
])) |
| 253 |
.is_ok()); |
| 254 |
assert!(check(&create_reqs(&[ |
| 255 |
FILEINDEX_V1_REQUIREMENT, |
| 256 |
PLAIN_ENCODE_REQUIREMENT |
| 257 |
])) |
| 258 |
.is_ok()); |
| 259 |
assert!(check(&create_reqs(&[FILEINDEX_V1_REQUIREMENT])).is_ok()); |
| 260 |
// all supported reqs |
| 261 |
let mut reqs = create_reqs(SUPPORTED); |
| 262 |
reqs.remove(FILEINDEX_V1_REQUIREMENT); |
| 263 |
reqs.remove(PLAIN_ENCODE_REQUIREMENT); |
| 264 |
assert!(check(&reqs).is_ok()); |
| 265 |
|
| 266 |
// no mutually exclusive reqs |
| 267 |
assert!(check(&create_reqs(&[ |
| 268 |
FNCACHE_REQUIREMENT, |
| 269 |
DOTENCODE_REQUIREMENT, |
| 270 |
PLAIN_ENCODE_REQUIREMENT |
| 271 |
])) |
| 272 |
.is_err()); |
| 273 |
// no reqs is invalid |
| 274 |
assert!(check(&create_reqs_no_defaults(&[])).is_err()); |
| 275 |
// missing from mutually exclusive pairs |
| 276 |
assert!(check(&create_reqs(&[])).is_err()); |
| 277 |
// no reqs + mutually exclusive item is also wrong |
| 278 |
assert!( |
| 279 |
check(&create_reqs_no_defaults(&[DOTENCODE_REQUIREMENT])).is_err() |
| 280 |
); |
| 281 |
|
| 282 |
// no unknown req |
| 283 |
assert!(check(&create_reqs(&[ |
| 284 |
DOTENCODE_REQUIREMENT, |
| 285 |
"thisdoesnotexist" |
| 286 |
])) |
| 287 |
.is_err()); |
| 288 |
} |
| 289 |
} |