##// END OF EJS Templates
rust: add `UncheckedRevision` type...
Raphaël Gomès -
r51867:c950fdba default
parent child Browse files
Show More
@@ -1,829 +1,849 b''
1 // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net>
1 // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net>
2 // and Mercurial contributors
2 // and Mercurial contributors
3 //
3 //
4 // This software may be used and distributed according to the terms of the
4 // This software may be used and distributed according to the terms of the
5 // GNU General Public License version 2 or any later version.
5 // GNU General Public License version 2 or any later version.
6 //! Mercurial concepts for handling revision history
6 //! Mercurial concepts for handling revision history
7
7
8 pub mod node;
8 pub mod node;
9 pub mod nodemap;
9 pub mod nodemap;
10 mod nodemap_docket;
10 mod nodemap_docket;
11 pub mod path_encode;
11 pub mod path_encode;
12 pub use node::{FromHexError, Node, NodePrefix};
12 pub use node::{FromHexError, Node, NodePrefix};
13 pub mod changelog;
13 pub mod changelog;
14 pub mod filelog;
14 pub mod filelog;
15 pub mod index;
15 pub mod index;
16 pub mod manifest;
16 pub mod manifest;
17 pub mod patch;
17 pub mod patch;
18
18
19 use std::borrow::Cow;
19 use std::borrow::Cow;
20 use std::io::Read;
20 use std::io::Read;
21 use std::ops::Deref;
21 use std::ops::Deref;
22 use std::path::Path;
22 use std::path::Path;
23
23
24 use flate2::read::ZlibDecoder;
24 use flate2::read::ZlibDecoder;
25 use sha1::{Digest, Sha1};
25 use sha1::{Digest, Sha1};
26 use std::cell::RefCell;
26 use std::cell::RefCell;
27 use zstd;
27 use zstd;
28
28
29 use self::node::{NODE_BYTES_LENGTH, NULL_NODE};
29 use self::node::{NODE_BYTES_LENGTH, NULL_NODE};
30 use self::nodemap_docket::NodeMapDocket;
30 use self::nodemap_docket::NodeMapDocket;
31 use super::index::Index;
31 use super::index::Index;
32 use super::nodemap::{NodeMap, NodeMapError};
32 use super::nodemap::{NodeMap, NodeMapError};
33 use crate::errors::HgError;
33 use crate::errors::HgError;
34 use crate::vfs::Vfs;
34 use crate::vfs::Vfs;
35
35
36 /// Mercurial revision numbers
36 /// Mercurial revision numbers
37 ///
37 ///
38 /// As noted in revlog.c, revision numbers are actually encoded in
38 /// As noted in revlog.c, revision numbers are actually encoded in
39 /// 4 bytes, and are liberally converted to ints, whence the i32
39 /// 4 bytes, and are liberally converted to ints, whence the i32
40 pub type Revision = i32;
40 pub type Revision = i32;
41
41
42 /// Unchecked Mercurial revision numbers.
43 ///
44 /// Values of this type have no guarantee of being a valid revision number
45 /// in any context. Use method `check_revision` to get a valid revision within
46 /// the appropriate index object.
47 ///
48 /// As noted in revlog.c, revision numbers are actually encoded in
49 /// 4 bytes, and are liberally converted to ints, whence the i32
50 pub type UncheckedRevision = i32;
51
42 /// Marker expressing the absence of a parent
52 /// Marker expressing the absence of a parent
43 ///
53 ///
44 /// Independently of the actual representation, `NULL_REVISION` is guaranteed
54 /// Independently of the actual representation, `NULL_REVISION` is guaranteed
45 /// to be smaller than all existing revisions.
55 /// to be smaller than all existing revisions.
46 pub const NULL_REVISION: Revision = -1;
56 pub const NULL_REVISION: Revision = -1;
47
57
48 /// Same as `mercurial.node.wdirrev`
58 /// Same as `mercurial.node.wdirrev`
49 ///
59 ///
50 /// This is also equal to `i32::max_value()`, but it's better to spell
60 /// This is also equal to `i32::max_value()`, but it's better to spell
51 /// it out explicitely, same as in `mercurial.node`
61 /// it out explicitely, same as in `mercurial.node`
52 #[allow(clippy::unreadable_literal)]
62 #[allow(clippy::unreadable_literal)]
53 pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
63 pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
54
64
55 pub const WORKING_DIRECTORY_HEX: &str =
65 pub const WORKING_DIRECTORY_HEX: &str =
56 "ffffffffffffffffffffffffffffffffffffffff";
66 "ffffffffffffffffffffffffffffffffffffffff";
57
67
58 /// The simplest expression of what we need of Mercurial DAGs.
68 /// The simplest expression of what we need of Mercurial DAGs.
59 pub trait Graph {
69 pub trait Graph {
60 /// Return the two parents of the given `Revision`.
70 /// Return the two parents of the given `Revision`.
61 ///
71 ///
62 /// Each of the parents can be independently `NULL_REVISION`
72 /// Each of the parents can be independently `NULL_REVISION`
63 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
73 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
64 }
74 }
65
75
66 #[derive(Clone, Debug, PartialEq)]
76 #[derive(Clone, Debug, PartialEq)]
67 pub enum GraphError {
77 pub enum GraphError {
68 ParentOutOfRange(Revision),
78 ParentOutOfRange(Revision),
69 WorkingDirectoryUnsupported,
79 WorkingDirectoryUnsupported,
70 }
80 }
71
81
72 /// The Mercurial Revlog Index
82 /// The Mercurial Revlog Index
73 ///
83 ///
74 /// This is currently limited to the minimal interface that is needed for
84 /// This is currently limited to the minimal interface that is needed for
75 /// the [`nodemap`](nodemap/index.html) module
85 /// the [`nodemap`](nodemap/index.html) module
76 pub trait RevlogIndex {
86 pub trait RevlogIndex {
77 /// Total number of Revisions referenced in this index
87 /// Total number of Revisions referenced in this index
78 fn len(&self) -> usize;
88 fn len(&self) -> usize;
79
89
80 fn is_empty(&self) -> bool {
90 fn is_empty(&self) -> bool {
81 self.len() == 0
91 self.len() == 0
82 }
92 }
83
93
84 /// Return a reference to the Node or `None` if rev is out of bounds
94 /// Return a reference to the Node or `None` if rev is out of bounds
85 ///
95 ///
86 /// `NULL_REVISION` is not considered to be out of bounds.
96 /// `NULL_REVISION` is not considered to be out of bounds.
87 fn node(&self, rev: Revision) -> Option<&Node>;
97 fn node(&self, rev: Revision) -> Option<&Node>;
98
99 /// Return a [`Revision`] if `rev` is a valid revision number for this
100 /// index
101 fn check_revision(&self, rev: UncheckedRevision) -> Option<Revision> {
102 if rev == NULL_REVISION || (rev >= 0 && (rev as usize) < self.len()) {
103 Some(rev)
104 } else {
105 None
106 }
107 }
88 }
108 }
89
109
90 const REVISION_FLAG_CENSORED: u16 = 1 << 15;
110 const REVISION_FLAG_CENSORED: u16 = 1 << 15;
91 const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14;
111 const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14;
92 const REVISION_FLAG_EXTSTORED: u16 = 1 << 13;
112 const REVISION_FLAG_EXTSTORED: u16 = 1 << 13;
93 const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12;
113 const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12;
94
114
95 // Keep this in sync with REVIDX_KNOWN_FLAGS in
115 // Keep this in sync with REVIDX_KNOWN_FLAGS in
96 // mercurial/revlogutils/flagutil.py
116 // mercurial/revlogutils/flagutil.py
97 const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED
117 const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED
98 | REVISION_FLAG_ELLIPSIS
118 | REVISION_FLAG_ELLIPSIS
99 | REVISION_FLAG_EXTSTORED
119 | REVISION_FLAG_EXTSTORED
100 | REVISION_FLAG_HASCOPIESINFO;
120 | REVISION_FLAG_HASCOPIESINFO;
101
121
102 const NULL_REVLOG_ENTRY_FLAGS: u16 = 0;
122 const NULL_REVLOG_ENTRY_FLAGS: u16 = 0;
103
123
104 #[derive(Debug, derive_more::From)]
124 #[derive(Debug, derive_more::From)]
105 pub enum RevlogError {
125 pub enum RevlogError {
106 InvalidRevision,
126 InvalidRevision,
107 /// Working directory is not supported
127 /// Working directory is not supported
108 WDirUnsupported,
128 WDirUnsupported,
109 /// Found more than one entry whose ID match the requested prefix
129 /// Found more than one entry whose ID match the requested prefix
110 AmbiguousPrefix,
130 AmbiguousPrefix,
111 #[from]
131 #[from]
112 Other(HgError),
132 Other(HgError),
113 }
133 }
114
134
115 impl From<NodeMapError> for RevlogError {
135 impl From<NodeMapError> for RevlogError {
116 fn from(error: NodeMapError) -> Self {
136 fn from(error: NodeMapError) -> Self {
117 match error {
137 match error {
118 NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix,
138 NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix,
119 NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted(
139 NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted(
120 format!("nodemap point to revision {} not in index", rev),
140 format!("nodemap point to revision {} not in index", rev),
121 ),
141 ),
122 }
142 }
123 }
143 }
124 }
144 }
125
145
126 fn corrupted<S: AsRef<str>>(context: S) -> HgError {
146 fn corrupted<S: AsRef<str>>(context: S) -> HgError {
127 HgError::corrupted(format!("corrupted revlog, {}", context.as_ref()))
147 HgError::corrupted(format!("corrupted revlog, {}", context.as_ref()))
128 }
148 }
129
149
130 impl RevlogError {
150 impl RevlogError {
131 fn corrupted<S: AsRef<str>>(context: S) -> Self {
151 fn corrupted<S: AsRef<str>>(context: S) -> Self {
132 RevlogError::Other(corrupted(context))
152 RevlogError::Other(corrupted(context))
133 }
153 }
134 }
154 }
135
155
136 /// Read only implementation of revlog.
156 /// Read only implementation of revlog.
137 pub struct Revlog {
157 pub struct Revlog {
138 /// When index and data are not interleaved: bytes of the revlog index.
158 /// When index and data are not interleaved: bytes of the revlog index.
139 /// When index and data are interleaved: bytes of the revlog index and
159 /// When index and data are interleaved: bytes of the revlog index and
140 /// data.
160 /// data.
141 index: Index,
161 index: Index,
142 /// When index and data are not interleaved: bytes of the revlog data
162 /// When index and data are not interleaved: bytes of the revlog data
143 data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>>,
163 data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>>,
144 /// When present on disk: the persistent nodemap for this revlog
164 /// When present on disk: the persistent nodemap for this revlog
145 nodemap: Option<nodemap::NodeTree>,
165 nodemap: Option<nodemap::NodeTree>,
146 }
166 }
147
167
148 impl Revlog {
168 impl Revlog {
149 /// Open a revlog index file.
169 /// Open a revlog index file.
150 ///
170 ///
151 /// It will also open the associated data file if index and data are not
171 /// It will also open the associated data file if index and data are not
152 /// interleaved.
172 /// interleaved.
153 pub fn open(
173 pub fn open(
154 store_vfs: &Vfs,
174 store_vfs: &Vfs,
155 index_path: impl AsRef<Path>,
175 index_path: impl AsRef<Path>,
156 data_path: Option<&Path>,
176 data_path: Option<&Path>,
157 use_nodemap: bool,
177 use_nodemap: bool,
158 ) -> Result<Self, HgError> {
178 ) -> Result<Self, HgError> {
159 let index_path = index_path.as_ref();
179 let index_path = index_path.as_ref();
160 let index = {
180 let index = {
161 match store_vfs.mmap_open_opt(&index_path)? {
181 match store_vfs.mmap_open_opt(&index_path)? {
162 None => Index::new(Box::new(vec![])),
182 None => Index::new(Box::new(vec![])),
163 Some(index_mmap) => {
183 Some(index_mmap) => {
164 let index = Index::new(Box::new(index_mmap))?;
184 let index = Index::new(Box::new(index_mmap))?;
165 Ok(index)
185 Ok(index)
166 }
186 }
167 }
187 }
168 }?;
188 }?;
169
189
170 let default_data_path = index_path.with_extension("d");
190 let default_data_path = index_path.with_extension("d");
171
191
172 // type annotation required
192 // type annotation required
173 // won't recognize Mmap as Deref<Target = [u8]>
193 // won't recognize Mmap as Deref<Target = [u8]>
174 let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> =
194 let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> =
175 if index.is_inline() {
195 if index.is_inline() {
176 None
196 None
177 } else {
197 } else {
178 let data_path = data_path.unwrap_or(&default_data_path);
198 let data_path = data_path.unwrap_or(&default_data_path);
179 let data_mmap = store_vfs.mmap_open(data_path)?;
199 let data_mmap = store_vfs.mmap_open(data_path)?;
180 Some(Box::new(data_mmap))
200 Some(Box::new(data_mmap))
181 };
201 };
182
202
183 let nodemap = if index.is_inline() || !use_nodemap {
203 let nodemap = if index.is_inline() || !use_nodemap {
184 None
204 None
185 } else {
205 } else {
186 NodeMapDocket::read_from_file(store_vfs, index_path)?.map(
206 NodeMapDocket::read_from_file(store_vfs, index_path)?.map(
187 |(docket, data)| {
207 |(docket, data)| {
188 nodemap::NodeTree::load_bytes(
208 nodemap::NodeTree::load_bytes(
189 Box::new(data),
209 Box::new(data),
190 docket.data_length,
210 docket.data_length,
191 )
211 )
192 },
212 },
193 )
213 )
194 };
214 };
195
215
196 Ok(Revlog {
216 Ok(Revlog {
197 index,
217 index,
198 data_bytes,
218 data_bytes,
199 nodemap,
219 nodemap,
200 })
220 })
201 }
221 }
202
222
203 /// Return number of entries of the `Revlog`.
223 /// Return number of entries of the `Revlog`.
204 pub fn len(&self) -> usize {
224 pub fn len(&self) -> usize {
205 self.index.len()
225 self.index.len()
206 }
226 }
207
227
208 /// Returns `true` if the `Revlog` has zero `entries`.
228 /// Returns `true` if the `Revlog` has zero `entries`.
209 pub fn is_empty(&self) -> bool {
229 pub fn is_empty(&self) -> bool {
210 self.index.is_empty()
230 self.index.is_empty()
211 }
231 }
212
232
213 /// Returns the node ID for the given revision number, if it exists in this
233 /// Returns the node ID for the given revision number, if it exists in this
214 /// revlog
234 /// revlog
215 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
235 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
216 if rev == NULL_REVISION {
236 if rev == NULL_REVISION {
217 return Some(&NULL_NODE);
237 return Some(&NULL_NODE);
218 }
238 }
219 Some(self.index.get_entry(rev)?.hash())
239 Some(self.index.get_entry(rev)?.hash())
220 }
240 }
221
241
222 /// Return the revision number for the given node ID, if it exists in this
242 /// Return the revision number for the given node ID, if it exists in this
223 /// revlog
243 /// revlog
224 pub fn rev_from_node(
244 pub fn rev_from_node(
225 &self,
245 &self,
226 node: NodePrefix,
246 node: NodePrefix,
227 ) -> Result<Revision, RevlogError> {
247 ) -> Result<Revision, RevlogError> {
228 let looked_up = if let Some(nodemap) = &self.nodemap {
248 let looked_up = if let Some(nodemap) = &self.nodemap {
229 nodemap
249 nodemap
230 .find_bin(&self.index, node)?
250 .find_bin(&self.index, node)?
231 .ok_or(RevlogError::InvalidRevision)
251 .ok_or(RevlogError::InvalidRevision)
232 } else {
252 } else {
233 self.rev_from_node_no_persistent_nodemap(node)
253 self.rev_from_node_no_persistent_nodemap(node)
234 };
254 };
235
255
236 if node.is_prefix_of(&NULL_NODE) {
256 if node.is_prefix_of(&NULL_NODE) {
237 return match looked_up {
257 return match looked_up {
238 Ok(_) => Err(RevlogError::AmbiguousPrefix),
258 Ok(_) => Err(RevlogError::AmbiguousPrefix),
239 Err(RevlogError::InvalidRevision) => Ok(NULL_REVISION),
259 Err(RevlogError::InvalidRevision) => Ok(NULL_REVISION),
240 res => res,
260 res => res,
241 };
261 };
242 };
262 };
243
263
244 looked_up
264 looked_up
245 }
265 }
246
266
247 /// Same as `rev_from_node`, without using a persistent nodemap
267 /// Same as `rev_from_node`, without using a persistent nodemap
248 ///
268 ///
249 /// This is used as fallback when a persistent nodemap is not present.
269 /// This is used as fallback when a persistent nodemap is not present.
250 /// This happens when the persistent-nodemap experimental feature is not
270 /// This happens when the persistent-nodemap experimental feature is not
251 /// enabled, or for small revlogs.
271 /// enabled, or for small revlogs.
252 fn rev_from_node_no_persistent_nodemap(
272 fn rev_from_node_no_persistent_nodemap(
253 &self,
273 &self,
254 node: NodePrefix,
274 node: NodePrefix,
255 ) -> Result<Revision, RevlogError> {
275 ) -> Result<Revision, RevlogError> {
256 // Linear scan of the revlog
276 // Linear scan of the revlog
257 // TODO: consider building a non-persistent nodemap in memory to
277 // TODO: consider building a non-persistent nodemap in memory to
258 // optimize these cases.
278 // optimize these cases.
259 let mut found_by_prefix = None;
279 let mut found_by_prefix = None;
260 for rev in (0..self.len() as Revision).rev() {
280 for rev in (0..self.len() as Revision).rev() {
261 let index_entry = self.index.get_entry(rev).ok_or_else(|| {
281 let index_entry = self.index.get_entry(rev).ok_or_else(|| {
262 HgError::corrupted(
282 HgError::corrupted(
263 "revlog references a revision not in the index",
283 "revlog references a revision not in the index",
264 )
284 )
265 })?;
285 })?;
266 if node == *index_entry.hash() {
286 if node == *index_entry.hash() {
267 return Ok(rev);
287 return Ok(rev);
268 }
288 }
269 if node.is_prefix_of(index_entry.hash()) {
289 if node.is_prefix_of(index_entry.hash()) {
270 if found_by_prefix.is_some() {
290 if found_by_prefix.is_some() {
271 return Err(RevlogError::AmbiguousPrefix);
291 return Err(RevlogError::AmbiguousPrefix);
272 }
292 }
273 found_by_prefix = Some(rev)
293 found_by_prefix = Some(rev)
274 }
294 }
275 }
295 }
276 found_by_prefix.ok_or(RevlogError::InvalidRevision)
296 found_by_prefix.ok_or(RevlogError::InvalidRevision)
277 }
297 }
278
298
279 /// Returns whether the given revision exists in this revlog.
299 /// Returns whether the given revision exists in this revlog.
280 pub fn has_rev(&self, rev: Revision) -> bool {
300 pub fn has_rev(&self, rev: Revision) -> bool {
281 self.index.get_entry(rev).is_some()
301 self.index.get_entry(rev).is_some()
282 }
302 }
283
303
284 /// Return the full data associated to a revision.
304 /// Return the full data associated to a revision.
285 ///
305 ///
286 /// All entries required to build the final data out of deltas will be
306 /// All entries required to build the final data out of deltas will be
287 /// retrieved as needed, and the deltas will be applied to the inital
307 /// retrieved as needed, and the deltas will be applied to the inital
288 /// snapshot to rebuild the final data.
308 /// snapshot to rebuild the final data.
289 pub fn get_rev_data(
309 pub fn get_rev_data(
290 &self,
310 &self,
291 rev: Revision,
311 rev: Revision,
292 ) -> Result<Cow<[u8]>, RevlogError> {
312 ) -> Result<Cow<[u8]>, RevlogError> {
293 if rev == NULL_REVISION {
313 if rev == NULL_REVISION {
294 return Ok(Cow::Borrowed(&[]));
314 return Ok(Cow::Borrowed(&[]));
295 };
315 };
296 Ok(self.get_entry(rev)?.data()?)
316 Ok(self.get_entry(rev)?.data()?)
297 }
317 }
298
318
299 /// Check the hash of some given data against the recorded hash.
319 /// Check the hash of some given data against the recorded hash.
300 pub fn check_hash(
320 pub fn check_hash(
301 &self,
321 &self,
302 p1: Revision,
322 p1: Revision,
303 p2: Revision,
323 p2: Revision,
304 expected: &[u8],
324 expected: &[u8],
305 data: &[u8],
325 data: &[u8],
306 ) -> bool {
326 ) -> bool {
307 let e1 = self.index.get_entry(p1);
327 let e1 = self.index.get_entry(p1);
308 let h1 = match e1 {
328 let h1 = match e1 {
309 Some(ref entry) => entry.hash(),
329 Some(ref entry) => entry.hash(),
310 None => &NULL_NODE,
330 None => &NULL_NODE,
311 };
331 };
312 let e2 = self.index.get_entry(p2);
332 let e2 = self.index.get_entry(p2);
313 let h2 = match e2 {
333 let h2 = match e2 {
314 Some(ref entry) => entry.hash(),
334 Some(ref entry) => entry.hash(),
315 None => &NULL_NODE,
335 None => &NULL_NODE,
316 };
336 };
317
337
318 hash(data, h1.as_bytes(), h2.as_bytes()) == expected
338 hash(data, h1.as_bytes(), h2.as_bytes()) == expected
319 }
339 }
320
340
321 /// Build the full data of a revision out its snapshot
341 /// Build the full data of a revision out its snapshot
322 /// and its deltas.
342 /// and its deltas.
323 fn build_data_from_deltas(
343 fn build_data_from_deltas(
324 snapshot: RevlogEntry,
344 snapshot: RevlogEntry,
325 deltas: &[RevlogEntry],
345 deltas: &[RevlogEntry],
326 ) -> Result<Vec<u8>, HgError> {
346 ) -> Result<Vec<u8>, HgError> {
327 let snapshot = snapshot.data_chunk()?;
347 let snapshot = snapshot.data_chunk()?;
328 let deltas = deltas
348 let deltas = deltas
329 .iter()
349 .iter()
330 .rev()
350 .rev()
331 .map(RevlogEntry::data_chunk)
351 .map(RevlogEntry::data_chunk)
332 .collect::<Result<Vec<_>, _>>()?;
352 .collect::<Result<Vec<_>, _>>()?;
333 let patches: Vec<_> =
353 let patches: Vec<_> =
334 deltas.iter().map(|d| patch::PatchList::new(d)).collect();
354 deltas.iter().map(|d| patch::PatchList::new(d)).collect();
335 let patch = patch::fold_patch_lists(&patches);
355 let patch = patch::fold_patch_lists(&patches);
336 Ok(patch.apply(&snapshot))
356 Ok(patch.apply(&snapshot))
337 }
357 }
338
358
339 /// Return the revlog data.
359 /// Return the revlog data.
340 fn data(&self) -> &[u8] {
360 fn data(&self) -> &[u8] {
341 match &self.data_bytes {
361 match &self.data_bytes {
342 Some(data_bytes) => data_bytes,
362 Some(data_bytes) => data_bytes,
343 None => panic!(
363 None => panic!(
344 "forgot to load the data or trying to access inline data"
364 "forgot to load the data or trying to access inline data"
345 ),
365 ),
346 }
366 }
347 }
367 }
348
368
349 pub fn make_null_entry(&self) -> RevlogEntry {
369 pub fn make_null_entry(&self) -> RevlogEntry {
350 RevlogEntry {
370 RevlogEntry {
351 revlog: self,
371 revlog: self,
352 rev: NULL_REVISION,
372 rev: NULL_REVISION,
353 bytes: b"",
373 bytes: b"",
354 compressed_len: 0,
374 compressed_len: 0,
355 uncompressed_len: 0,
375 uncompressed_len: 0,
356 base_rev_or_base_of_delta_chain: None,
376 base_rev_or_base_of_delta_chain: None,
357 p1: NULL_REVISION,
377 p1: NULL_REVISION,
358 p2: NULL_REVISION,
378 p2: NULL_REVISION,
359 flags: NULL_REVLOG_ENTRY_FLAGS,
379 flags: NULL_REVLOG_ENTRY_FLAGS,
360 hash: NULL_NODE,
380 hash: NULL_NODE,
361 }
381 }
362 }
382 }
363
383
364 /// Get an entry of the revlog.
384 /// Get an entry of the revlog.
365 pub fn get_entry(
385 pub fn get_entry(
366 &self,
386 &self,
367 rev: Revision,
387 rev: Revision,
368 ) -> Result<RevlogEntry, RevlogError> {
388 ) -> Result<RevlogEntry, RevlogError> {
369 if rev == NULL_REVISION {
389 if rev == NULL_REVISION {
370 return Ok(self.make_null_entry());
390 return Ok(self.make_null_entry());
371 }
391 }
372 let index_entry = self
392 let index_entry = self
373 .index
393 .index
374 .get_entry(rev)
394 .get_entry(rev)
375 .ok_or(RevlogError::InvalidRevision)?;
395 .ok_or(RevlogError::InvalidRevision)?;
376 let start = index_entry.offset();
396 let start = index_entry.offset();
377 let end = start + index_entry.compressed_len() as usize;
397 let end = start + index_entry.compressed_len() as usize;
378 let data = if self.index.is_inline() {
398 let data = if self.index.is_inline() {
379 self.index.data(start, end)
399 self.index.data(start, end)
380 } else {
400 } else {
381 &self.data()[start..end]
401 &self.data()[start..end]
382 };
402 };
383 let entry = RevlogEntry {
403 let entry = RevlogEntry {
384 revlog: self,
404 revlog: self,
385 rev,
405 rev,
386 bytes: data,
406 bytes: data,
387 compressed_len: index_entry.compressed_len(),
407 compressed_len: index_entry.compressed_len(),
388 uncompressed_len: index_entry.uncompressed_len(),
408 uncompressed_len: index_entry.uncompressed_len(),
389 base_rev_or_base_of_delta_chain: if index_entry
409 base_rev_or_base_of_delta_chain: if index_entry
390 .base_revision_or_base_of_delta_chain()
410 .base_revision_or_base_of_delta_chain()
391 == rev
411 == rev
392 {
412 {
393 None
413 None
394 } else {
414 } else {
395 Some(index_entry.base_revision_or_base_of_delta_chain())
415 Some(index_entry.base_revision_or_base_of_delta_chain())
396 },
416 },
397 p1: index_entry.p1(),
417 p1: index_entry.p1(),
398 p2: index_entry.p2(),
418 p2: index_entry.p2(),
399 flags: index_entry.flags(),
419 flags: index_entry.flags(),
400 hash: *index_entry.hash(),
420 hash: *index_entry.hash(),
401 };
421 };
402 Ok(entry)
422 Ok(entry)
403 }
423 }
404
424
405 /// when resolving internal references within revlog, any errors
425 /// when resolving internal references within revlog, any errors
406 /// should be reported as corruption, instead of e.g. "invalid revision"
426 /// should be reported as corruption, instead of e.g. "invalid revision"
407 fn get_entry_internal(
427 fn get_entry_internal(
408 &self,
428 &self,
409 rev: Revision,
429 rev: Revision,
410 ) -> Result<RevlogEntry, HgError> {
430 ) -> Result<RevlogEntry, HgError> {
411 self.get_entry(rev)
431 self.get_entry(rev)
412 .map_err(|_| corrupted(format!("revision {} out of range", rev)))
432 .map_err(|_| corrupted(format!("revision {} out of range", rev)))
413 }
433 }
414 }
434 }
415
435
416 /// The revlog entry's bytes and the necessary informations to extract
436 /// The revlog entry's bytes and the necessary informations to extract
417 /// the entry's data.
437 /// the entry's data.
418 #[derive(Clone)]
438 #[derive(Clone)]
419 pub struct RevlogEntry<'revlog> {
439 pub struct RevlogEntry<'revlog> {
420 revlog: &'revlog Revlog,
440 revlog: &'revlog Revlog,
421 rev: Revision,
441 rev: Revision,
422 bytes: &'revlog [u8],
442 bytes: &'revlog [u8],
423 compressed_len: u32,
443 compressed_len: u32,
424 uncompressed_len: i32,
444 uncompressed_len: i32,
425 base_rev_or_base_of_delta_chain: Option<Revision>,
445 base_rev_or_base_of_delta_chain: Option<Revision>,
426 p1: Revision,
446 p1: Revision,
427 p2: Revision,
447 p2: Revision,
428 flags: u16,
448 flags: u16,
429 hash: Node,
449 hash: Node,
430 }
450 }
431
451
432 thread_local! {
452 thread_local! {
433 // seems fine to [unwrap] here: this can only fail due to memory allocation
453 // seems fine to [unwrap] here: this can only fail due to memory allocation
434 // failing, and it's normal for that to cause panic.
454 // failing, and it's normal for that to cause panic.
435 static ZSTD_DECODER : RefCell<zstd::bulk::Decompressor<'static>> =
455 static ZSTD_DECODER : RefCell<zstd::bulk::Decompressor<'static>> =
436 RefCell::new(zstd::bulk::Decompressor::new().ok().unwrap());
456 RefCell::new(zstd::bulk::Decompressor::new().ok().unwrap());
437 }
457 }
438
458
439 fn zstd_decompress_to_buffer(
459 fn zstd_decompress_to_buffer(
440 bytes: &[u8],
460 bytes: &[u8],
441 buf: &mut Vec<u8>,
461 buf: &mut Vec<u8>,
442 ) -> Result<usize, std::io::Error> {
462 ) -> Result<usize, std::io::Error> {
443 ZSTD_DECODER
463 ZSTD_DECODER
444 .with(|decoder| decoder.borrow_mut().decompress_to_buffer(bytes, buf))
464 .with(|decoder| decoder.borrow_mut().decompress_to_buffer(bytes, buf))
445 }
465 }
446
466
447 impl<'revlog> RevlogEntry<'revlog> {
467 impl<'revlog> RevlogEntry<'revlog> {
448 pub fn revision(&self) -> Revision {
468 pub fn revision(&self) -> Revision {
449 self.rev
469 self.rev
450 }
470 }
451
471
452 pub fn node(&self) -> &Node {
472 pub fn node(&self) -> &Node {
453 &self.hash
473 &self.hash
454 }
474 }
455
475
456 pub fn uncompressed_len(&self) -> Option<u32> {
476 pub fn uncompressed_len(&self) -> Option<u32> {
457 u32::try_from(self.uncompressed_len).ok()
477 u32::try_from(self.uncompressed_len).ok()
458 }
478 }
459
479
460 pub fn has_p1(&self) -> bool {
480 pub fn has_p1(&self) -> bool {
461 self.p1 != NULL_REVISION
481 self.p1 != NULL_REVISION
462 }
482 }
463
483
464 pub fn p1_entry(
484 pub fn p1_entry(
465 &self,
485 &self,
466 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
486 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
467 if self.p1 == NULL_REVISION {
487 if self.p1 == NULL_REVISION {
468 Ok(None)
488 Ok(None)
469 } else {
489 } else {
470 Ok(Some(self.revlog.get_entry(self.p1)?))
490 Ok(Some(self.revlog.get_entry(self.p1)?))
471 }
491 }
472 }
492 }
473
493
474 pub fn p2_entry(
494 pub fn p2_entry(
475 &self,
495 &self,
476 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
496 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
477 if self.p2 == NULL_REVISION {
497 if self.p2 == NULL_REVISION {
478 Ok(None)
498 Ok(None)
479 } else {
499 } else {
480 Ok(Some(self.revlog.get_entry(self.p2)?))
500 Ok(Some(self.revlog.get_entry(self.p2)?))
481 }
501 }
482 }
502 }
483
503
484 pub fn p1(&self) -> Option<Revision> {
504 pub fn p1(&self) -> Option<Revision> {
485 if self.p1 == NULL_REVISION {
505 if self.p1 == NULL_REVISION {
486 None
506 None
487 } else {
507 } else {
488 Some(self.p1)
508 Some(self.p1)
489 }
509 }
490 }
510 }
491
511
492 pub fn p2(&self) -> Option<Revision> {
512 pub fn p2(&self) -> Option<Revision> {
493 if self.p2 == NULL_REVISION {
513 if self.p2 == NULL_REVISION {
494 None
514 None
495 } else {
515 } else {
496 Some(self.p2)
516 Some(self.p2)
497 }
517 }
498 }
518 }
499
519
500 pub fn is_censored(&self) -> bool {
520 pub fn is_censored(&self) -> bool {
501 (self.flags & REVISION_FLAG_CENSORED) != 0
521 (self.flags & REVISION_FLAG_CENSORED) != 0
502 }
522 }
503
523
504 pub fn has_length_affecting_flag_processor(&self) -> bool {
524 pub fn has_length_affecting_flag_processor(&self) -> bool {
505 // Relevant Python code: revlog.size()
525 // Relevant Python code: revlog.size()
506 // note: ELLIPSIS is known to not change the content
526 // note: ELLIPSIS is known to not change the content
507 (self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0
527 (self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0
508 }
528 }
509
529
510 /// The data for this entry, after resolving deltas if any.
530 /// The data for this entry, after resolving deltas if any.
511 pub fn rawdata(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
531 pub fn rawdata(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
512 let mut entry = self.clone();
532 let mut entry = self.clone();
513 let mut delta_chain = vec![];
533 let mut delta_chain = vec![];
514
534
515 // The meaning of `base_rev_or_base_of_delta_chain` depends on
535 // The meaning of `base_rev_or_base_of_delta_chain` depends on
516 // generaldelta. See the doc on `ENTRY_DELTA_BASE` in
536 // generaldelta. See the doc on `ENTRY_DELTA_BASE` in
517 // `mercurial/revlogutils/constants.py` and the code in
537 // `mercurial/revlogutils/constants.py` and the code in
518 // [_chaininfo] and in [index_deltachain].
538 // [_chaininfo] and in [index_deltachain].
519 let uses_generaldelta = self.revlog.index.uses_generaldelta();
539 let uses_generaldelta = self.revlog.index.uses_generaldelta();
520 while let Some(base_rev) = entry.base_rev_or_base_of_delta_chain {
540 while let Some(base_rev) = entry.base_rev_or_base_of_delta_chain {
521 let base_rev = if uses_generaldelta {
541 let base_rev = if uses_generaldelta {
522 base_rev
542 base_rev
523 } else {
543 } else {
524 entry.rev - 1
544 entry.rev - 1
525 };
545 };
526 delta_chain.push(entry);
546 delta_chain.push(entry);
527 entry = self.revlog.get_entry_internal(base_rev)?;
547 entry = self.revlog.get_entry_internal(base_rev)?;
528 }
548 }
529
549
530 let data = if delta_chain.is_empty() {
550 let data = if delta_chain.is_empty() {
531 entry.data_chunk()?
551 entry.data_chunk()?
532 } else {
552 } else {
533 Revlog::build_data_from_deltas(entry, &delta_chain)?.into()
553 Revlog::build_data_from_deltas(entry, &delta_chain)?.into()
534 };
554 };
535
555
536 Ok(data)
556 Ok(data)
537 }
557 }
538
558
539 fn check_data(
559 fn check_data(
540 &self,
560 &self,
541 data: Cow<'revlog, [u8]>,
561 data: Cow<'revlog, [u8]>,
542 ) -> Result<Cow<'revlog, [u8]>, HgError> {
562 ) -> Result<Cow<'revlog, [u8]>, HgError> {
543 if self.revlog.check_hash(
563 if self.revlog.check_hash(
544 self.p1,
564 self.p1,
545 self.p2,
565 self.p2,
546 self.hash.as_bytes(),
566 self.hash.as_bytes(),
547 &data,
567 &data,
548 ) {
568 ) {
549 Ok(data)
569 Ok(data)
550 } else {
570 } else {
551 if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 {
571 if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 {
552 return Err(HgError::unsupported(
572 return Err(HgError::unsupported(
553 "ellipsis revisions are not supported by rhg",
573 "ellipsis revisions are not supported by rhg",
554 ));
574 ));
555 }
575 }
556 Err(corrupted(format!(
576 Err(corrupted(format!(
557 "hash check failed for revision {}",
577 "hash check failed for revision {}",
558 self.rev
578 self.rev
559 )))
579 )))
560 }
580 }
561 }
581 }
562
582
563 pub fn data(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
583 pub fn data(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
564 let data = self.rawdata()?;
584 let data = self.rawdata()?;
565 if self.rev == NULL_REVISION {
585 if self.rev == NULL_REVISION {
566 return Ok(data);
586 return Ok(data);
567 }
587 }
568 if self.is_censored() {
588 if self.is_censored() {
569 return Err(HgError::CensoredNodeError);
589 return Err(HgError::CensoredNodeError);
570 }
590 }
571 self.check_data(data)
591 self.check_data(data)
572 }
592 }
573
593
574 /// Extract the data contained in the entry.
594 /// Extract the data contained in the entry.
575 /// This may be a delta. (See `is_delta`.)
595 /// This may be a delta. (See `is_delta`.)
576 fn data_chunk(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
596 fn data_chunk(&self) -> Result<Cow<'revlog, [u8]>, HgError> {
577 if self.bytes.is_empty() {
597 if self.bytes.is_empty() {
578 return Ok(Cow::Borrowed(&[]));
598 return Ok(Cow::Borrowed(&[]));
579 }
599 }
580 match self.bytes[0] {
600 match self.bytes[0] {
581 // Revision data is the entirety of the entry, including this
601 // Revision data is the entirety of the entry, including this
582 // header.
602 // header.
583 b'\0' => Ok(Cow::Borrowed(self.bytes)),
603 b'\0' => Ok(Cow::Borrowed(self.bytes)),
584 // Raw revision data follows.
604 // Raw revision data follows.
585 b'u' => Ok(Cow::Borrowed(&self.bytes[1..])),
605 b'u' => Ok(Cow::Borrowed(&self.bytes[1..])),
586 // zlib (RFC 1950) data.
606 // zlib (RFC 1950) data.
587 b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)),
607 b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)),
588 // zstd data.
608 // zstd data.
589 b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)),
609 b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)),
590 // A proper new format should have had a repo/store requirement.
610 // A proper new format should have had a repo/store requirement.
591 format_type => Err(corrupted(format!(
611 format_type => Err(corrupted(format!(
592 "unknown compression header '{}'",
612 "unknown compression header '{}'",
593 format_type
613 format_type
594 ))),
614 ))),
595 }
615 }
596 }
616 }
597
617
598 fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, HgError> {
618 fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, HgError> {
599 let mut decoder = ZlibDecoder::new(self.bytes);
619 let mut decoder = ZlibDecoder::new(self.bytes);
600 if self.is_delta() {
620 if self.is_delta() {
601 let mut buf = Vec::with_capacity(self.compressed_len as usize);
621 let mut buf = Vec::with_capacity(self.compressed_len as usize);
602 decoder
622 decoder
603 .read_to_end(&mut buf)
623 .read_to_end(&mut buf)
604 .map_err(|e| corrupted(e.to_string()))?;
624 .map_err(|e| corrupted(e.to_string()))?;
605 Ok(buf)
625 Ok(buf)
606 } else {
626 } else {
607 let cap = self.uncompressed_len.max(0) as usize;
627 let cap = self.uncompressed_len.max(0) as usize;
608 let mut buf = vec![0; cap];
628 let mut buf = vec![0; cap];
609 decoder
629 decoder
610 .read_exact(&mut buf)
630 .read_exact(&mut buf)
611 .map_err(|e| corrupted(e.to_string()))?;
631 .map_err(|e| corrupted(e.to_string()))?;
612 Ok(buf)
632 Ok(buf)
613 }
633 }
614 }
634 }
615
635
616 fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, HgError> {
636 fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, HgError> {
617 let cap = self.uncompressed_len.max(0) as usize;
637 let cap = self.uncompressed_len.max(0) as usize;
618 if self.is_delta() {
638 if self.is_delta() {
619 // [cap] is usually an over-estimate of the space needed because
639 // [cap] is usually an over-estimate of the space needed because
620 // it's the length of delta-decoded data, but we're interested
640 // it's the length of delta-decoded data, but we're interested
621 // in the size of the delta.
641 // in the size of the delta.
622 // This means we have to [shrink_to_fit] to avoid holding on
642 // This means we have to [shrink_to_fit] to avoid holding on
623 // to a large chunk of memory, but it also means we must have a
643 // to a large chunk of memory, but it also means we must have a
624 // fallback branch, for the case when the delta is longer than
644 // fallback branch, for the case when the delta is longer than
625 // the original data (surprisingly, this does happen in practice)
645 // the original data (surprisingly, this does happen in practice)
626 let mut buf = Vec::with_capacity(cap);
646 let mut buf = Vec::with_capacity(cap);
627 match zstd_decompress_to_buffer(self.bytes, &mut buf) {
647 match zstd_decompress_to_buffer(self.bytes, &mut buf) {
628 Ok(_) => buf.shrink_to_fit(),
648 Ok(_) => buf.shrink_to_fit(),
629 Err(_) => {
649 Err(_) => {
630 buf.clear();
650 buf.clear();
631 zstd::stream::copy_decode(self.bytes, &mut buf)
651 zstd::stream::copy_decode(self.bytes, &mut buf)
632 .map_err(|e| corrupted(e.to_string()))?;
652 .map_err(|e| corrupted(e.to_string()))?;
633 }
653 }
634 };
654 };
635 Ok(buf)
655 Ok(buf)
636 } else {
656 } else {
637 let mut buf = Vec::with_capacity(cap);
657 let mut buf = Vec::with_capacity(cap);
638 let len = zstd_decompress_to_buffer(self.bytes, &mut buf)
658 let len = zstd_decompress_to_buffer(self.bytes, &mut buf)
639 .map_err(|e| corrupted(e.to_string()))?;
659 .map_err(|e| corrupted(e.to_string()))?;
640 if len != self.uncompressed_len as usize {
660 if len != self.uncompressed_len as usize {
641 Err(corrupted("uncompressed length does not match"))
661 Err(corrupted("uncompressed length does not match"))
642 } else {
662 } else {
643 Ok(buf)
663 Ok(buf)
644 }
664 }
645 }
665 }
646 }
666 }
647
667
648 /// Tell if the entry is a snapshot or a delta
668 /// Tell if the entry is a snapshot or a delta
649 /// (influences on decompression).
669 /// (influences on decompression).
650 fn is_delta(&self) -> bool {
670 fn is_delta(&self) -> bool {
651 self.base_rev_or_base_of_delta_chain.is_some()
671 self.base_rev_or_base_of_delta_chain.is_some()
652 }
672 }
653 }
673 }
654
674
655 /// Calculate the hash of a revision given its data and its parents.
675 /// Calculate the hash of a revision given its data and its parents.
656 fn hash(
676 fn hash(
657 data: &[u8],
677 data: &[u8],
658 p1_hash: &[u8],
678 p1_hash: &[u8],
659 p2_hash: &[u8],
679 p2_hash: &[u8],
660 ) -> [u8; NODE_BYTES_LENGTH] {
680 ) -> [u8; NODE_BYTES_LENGTH] {
661 let mut hasher = Sha1::new();
681 let mut hasher = Sha1::new();
662 let (a, b) = (p1_hash, p2_hash);
682 let (a, b) = (p1_hash, p2_hash);
663 if a > b {
683 if a > b {
664 hasher.update(b);
684 hasher.update(b);
665 hasher.update(a);
685 hasher.update(a);
666 } else {
686 } else {
667 hasher.update(a);
687 hasher.update(a);
668 hasher.update(b);
688 hasher.update(b);
669 }
689 }
670 hasher.update(data);
690 hasher.update(data);
671 *hasher.finalize().as_ref()
691 *hasher.finalize().as_ref()
672 }
692 }
673
693
674 #[cfg(test)]
694 #[cfg(test)]
675 mod tests {
695 mod tests {
676 use super::*;
696 use super::*;
677 use crate::index::{IndexEntryBuilder, INDEX_ENTRY_SIZE};
697 use crate::index::{IndexEntryBuilder, INDEX_ENTRY_SIZE};
678 use itertools::Itertools;
698 use itertools::Itertools;
679
699
680 #[test]
700 #[test]
681 fn test_empty() {
701 fn test_empty() {
682 let temp = tempfile::tempdir().unwrap();
702 let temp = tempfile::tempdir().unwrap();
683 let vfs = Vfs { base: temp.path() };
703 let vfs = Vfs { base: temp.path() };
684 std::fs::write(temp.path().join("foo.i"), b"").unwrap();
704 std::fs::write(temp.path().join("foo.i"), b"").unwrap();
685 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
705 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
686 assert!(revlog.is_empty());
706 assert!(revlog.is_empty());
687 assert_eq!(revlog.len(), 0);
707 assert_eq!(revlog.len(), 0);
688 assert!(revlog.get_entry(0).is_err());
708 assert!(revlog.get_entry(0).is_err());
689 assert!(!revlog.has_rev(0));
709 assert!(!revlog.has_rev(0));
690 assert_eq!(
710 assert_eq!(
691 revlog.rev_from_node(NULL_NODE.into()).unwrap(),
711 revlog.rev_from_node(NULL_NODE.into()).unwrap(),
692 NULL_REVISION
712 NULL_REVISION
693 );
713 );
694 let null_entry = revlog.get_entry(NULL_REVISION).ok().unwrap();
714 let null_entry = revlog.get_entry(NULL_REVISION).ok().unwrap();
695 assert_eq!(null_entry.revision(), NULL_REVISION);
715 assert_eq!(null_entry.revision(), NULL_REVISION);
696 assert!(null_entry.data().unwrap().is_empty());
716 assert!(null_entry.data().unwrap().is_empty());
697 }
717 }
698
718
699 #[test]
719 #[test]
700 fn test_inline() {
720 fn test_inline() {
701 let temp = tempfile::tempdir().unwrap();
721 let temp = tempfile::tempdir().unwrap();
702 let vfs = Vfs { base: temp.path() };
722 let vfs = Vfs { base: temp.path() };
703 let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd")
723 let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd")
704 .unwrap();
724 .unwrap();
705 let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
725 let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
706 .unwrap();
726 .unwrap();
707 let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582")
727 let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582")
708 .unwrap();
728 .unwrap();
709 let entry0_bytes = IndexEntryBuilder::new()
729 let entry0_bytes = IndexEntryBuilder::new()
710 .is_first(true)
730 .is_first(true)
711 .with_version(1)
731 .with_version(1)
712 .with_inline(true)
732 .with_inline(true)
713 .with_offset(INDEX_ENTRY_SIZE)
733 .with_offset(INDEX_ENTRY_SIZE)
714 .with_node(node0)
734 .with_node(node0)
715 .build();
735 .build();
716 let entry1_bytes = IndexEntryBuilder::new()
736 let entry1_bytes = IndexEntryBuilder::new()
717 .with_offset(INDEX_ENTRY_SIZE)
737 .with_offset(INDEX_ENTRY_SIZE)
718 .with_node(node1)
738 .with_node(node1)
719 .build();
739 .build();
720 let entry2_bytes = IndexEntryBuilder::new()
740 let entry2_bytes = IndexEntryBuilder::new()
721 .with_offset(INDEX_ENTRY_SIZE)
741 .with_offset(INDEX_ENTRY_SIZE)
722 .with_p1(0)
742 .with_p1(0)
723 .with_p2(1)
743 .with_p2(1)
724 .with_node(node2)
744 .with_node(node2)
725 .build();
745 .build();
726 let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes]
746 let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes]
727 .into_iter()
747 .into_iter()
728 .flatten()
748 .flatten()
729 .collect_vec();
749 .collect_vec();
730 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
750 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
731 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
751 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
732
752
733 let entry0 = revlog.get_entry(0).ok().unwrap();
753 let entry0 = revlog.get_entry(0).ok().unwrap();
734 assert_eq!(entry0.revision(), 0);
754 assert_eq!(entry0.revision(), 0);
735 assert_eq!(*entry0.node(), node0);
755 assert_eq!(*entry0.node(), node0);
736 assert!(!entry0.has_p1());
756 assert!(!entry0.has_p1());
737 assert_eq!(entry0.p1(), None);
757 assert_eq!(entry0.p1(), None);
738 assert_eq!(entry0.p2(), None);
758 assert_eq!(entry0.p2(), None);
739 let p1_entry = entry0.p1_entry().unwrap();
759 let p1_entry = entry0.p1_entry().unwrap();
740 assert!(p1_entry.is_none());
760 assert!(p1_entry.is_none());
741 let p2_entry = entry0.p2_entry().unwrap();
761 let p2_entry = entry0.p2_entry().unwrap();
742 assert!(p2_entry.is_none());
762 assert!(p2_entry.is_none());
743
763
744 let entry1 = revlog.get_entry(1).ok().unwrap();
764 let entry1 = revlog.get_entry(1).ok().unwrap();
745 assert_eq!(entry1.revision(), 1);
765 assert_eq!(entry1.revision(), 1);
746 assert_eq!(*entry1.node(), node1);
766 assert_eq!(*entry1.node(), node1);
747 assert!(!entry1.has_p1());
767 assert!(!entry1.has_p1());
748 assert_eq!(entry1.p1(), None);
768 assert_eq!(entry1.p1(), None);
749 assert_eq!(entry1.p2(), None);
769 assert_eq!(entry1.p2(), None);
750 let p1_entry = entry1.p1_entry().unwrap();
770 let p1_entry = entry1.p1_entry().unwrap();
751 assert!(p1_entry.is_none());
771 assert!(p1_entry.is_none());
752 let p2_entry = entry1.p2_entry().unwrap();
772 let p2_entry = entry1.p2_entry().unwrap();
753 assert!(p2_entry.is_none());
773 assert!(p2_entry.is_none());
754
774
755 let entry2 = revlog.get_entry(2).ok().unwrap();
775 let entry2 = revlog.get_entry(2).ok().unwrap();
756 assert_eq!(entry2.revision(), 2);
776 assert_eq!(entry2.revision(), 2);
757 assert_eq!(*entry2.node(), node2);
777 assert_eq!(*entry2.node(), node2);
758 assert!(entry2.has_p1());
778 assert!(entry2.has_p1());
759 assert_eq!(entry2.p1(), Some(0));
779 assert_eq!(entry2.p1(), Some(0));
760 assert_eq!(entry2.p2(), Some(1));
780 assert_eq!(entry2.p2(), Some(1));
761 let p1_entry = entry2.p1_entry().unwrap();
781 let p1_entry = entry2.p1_entry().unwrap();
762 assert!(p1_entry.is_some());
782 assert!(p1_entry.is_some());
763 assert_eq!(p1_entry.unwrap().revision(), 0);
783 assert_eq!(p1_entry.unwrap().revision(), 0);
764 let p2_entry = entry2.p2_entry().unwrap();
784 let p2_entry = entry2.p2_entry().unwrap();
765 assert!(p2_entry.is_some());
785 assert!(p2_entry.is_some());
766 assert_eq!(p2_entry.unwrap().revision(), 1);
786 assert_eq!(p2_entry.unwrap().revision(), 1);
767 }
787 }
768
788
769 #[test]
789 #[test]
770 fn test_nodemap() {
790 fn test_nodemap() {
771 let temp = tempfile::tempdir().unwrap();
791 let temp = tempfile::tempdir().unwrap();
772 let vfs = Vfs { base: temp.path() };
792 let vfs = Vfs { base: temp.path() };
773
793
774 // building a revlog with a forced Node starting with zeros
794 // building a revlog with a forced Node starting with zeros
775 // This is a corruption, but it does not preclude using the nodemap
795 // This is a corruption, but it does not preclude using the nodemap
776 // if we don't try and access the data
796 // if we don't try and access the data
777 let node0 = Node::from_hex("00d2a3912a0b24502043eae84ee4b279c18b90dd")
797 let node0 = Node::from_hex("00d2a3912a0b24502043eae84ee4b279c18b90dd")
778 .unwrap();
798 .unwrap();
779 let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
799 let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
780 .unwrap();
800 .unwrap();
781 let entry0_bytes = IndexEntryBuilder::new()
801 let entry0_bytes = IndexEntryBuilder::new()
782 .is_first(true)
802 .is_first(true)
783 .with_version(1)
803 .with_version(1)
784 .with_inline(true)
804 .with_inline(true)
785 .with_offset(INDEX_ENTRY_SIZE)
805 .with_offset(INDEX_ENTRY_SIZE)
786 .with_node(node0)
806 .with_node(node0)
787 .build();
807 .build();
788 let entry1_bytes = IndexEntryBuilder::new()
808 let entry1_bytes = IndexEntryBuilder::new()
789 .with_offset(INDEX_ENTRY_SIZE)
809 .with_offset(INDEX_ENTRY_SIZE)
790 .with_node(node1)
810 .with_node(node1)
791 .build();
811 .build();
792 let contents = vec![entry0_bytes, entry1_bytes]
812 let contents = vec![entry0_bytes, entry1_bytes]
793 .into_iter()
813 .into_iter()
794 .flatten()
814 .flatten()
795 .collect_vec();
815 .collect_vec();
796 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
816 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
797 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
817 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
798
818
799 // accessing the data shows the corruption
819 // accessing the data shows the corruption
800 revlog.get_entry(0).unwrap().data().unwrap_err();
820 revlog.get_entry(0).unwrap().data().unwrap_err();
801
821
802 assert_eq!(revlog.rev_from_node(NULL_NODE.into()).unwrap(), -1);
822 assert_eq!(revlog.rev_from_node(NULL_NODE.into()).unwrap(), -1);
803 assert_eq!(revlog.rev_from_node(node0.into()).unwrap(), 0);
823 assert_eq!(revlog.rev_from_node(node0.into()).unwrap(), 0);
804 assert_eq!(revlog.rev_from_node(node1.into()).unwrap(), 1);
824 assert_eq!(revlog.rev_from_node(node1.into()).unwrap(), 1);
805 assert_eq!(
825 assert_eq!(
806 revlog
826 revlog
807 .rev_from_node(NodePrefix::from_hex("000").unwrap())
827 .rev_from_node(NodePrefix::from_hex("000").unwrap())
808 .unwrap(),
828 .unwrap(),
809 -1
829 -1
810 );
830 );
811 assert_eq!(
831 assert_eq!(
812 revlog
832 revlog
813 .rev_from_node(NodePrefix::from_hex("b00").unwrap())
833 .rev_from_node(NodePrefix::from_hex("b00").unwrap())
814 .unwrap(),
834 .unwrap(),
815 1
835 1
816 );
836 );
817 // RevlogError does not implement PartialEq
837 // RevlogError does not implement PartialEq
818 // (ultimately because io::Error does not)
838 // (ultimately because io::Error does not)
819 match revlog
839 match revlog
820 .rev_from_node(NodePrefix::from_hex("00").unwrap())
840 .rev_from_node(NodePrefix::from_hex("00").unwrap())
821 .expect_err("Expected to give AmbiguousPrefix error")
841 .expect_err("Expected to give AmbiguousPrefix error")
822 {
842 {
823 RevlogError::AmbiguousPrefix => (),
843 RevlogError::AmbiguousPrefix => (),
824 e => {
844 e => {
825 panic!("Got another error than AmbiguousPrefix: {:?}", e);
845 panic!("Got another error than AmbiguousPrefix: {:?}", e);
826 }
846 }
827 };
847 };
828 }
848 }
829 }
849 }
General Comments 0
You need to be logged in to leave comments. Login now