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