Show More
@@ -1,289 +1,327 b'' | |||
|
1 | 1 | use crate::errors::HgError; |
|
2 | use crate::revlog::Revision; | |
|
3 | 2 | use crate::revlog::{Node, NodePrefix}; |
|
3 | use crate::revlog::{Revision, NULL_REVISION}; | |
|
4 | 4 | use crate::revlog::{Revlog, RevlogEntry, RevlogError}; |
|
5 | 5 | use crate::utils::hg_path::HgPath; |
|
6 | 6 | use crate::vfs::Vfs; |
|
7 | 7 | use itertools::Itertools; |
|
8 | 8 | use std::ascii::escape_default; |
|
9 | 9 | use std::borrow::Cow; |
|
10 | 10 | use std::fmt::{Debug, Formatter}; |
|
11 | 11 | |
|
12 | 12 | /// A specialized `Revlog` to work with changelog data format. |
|
13 | 13 | pub struct Changelog { |
|
14 | 14 | /// The generic `revlog` format. |
|
15 | 15 | pub(crate) revlog: Revlog, |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | impl Changelog { |
|
19 | 19 | /// Open the `changelog` of a repository given by its root. |
|
20 | 20 | pub fn open(store_vfs: &Vfs, use_nodemap: bool) -> Result<Self, HgError> { |
|
21 | 21 | let revlog = |
|
22 | 22 | Revlog::open(store_vfs, "00changelog.i", None, use_nodemap)?; |
|
23 | 23 | Ok(Self { revlog }) |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | /// Return the `ChangelogRevisionData` for the given node ID. |
|
27 | 27 | pub fn data_for_node( |
|
28 | 28 | &self, |
|
29 | 29 | node: NodePrefix, |
|
30 | 30 | ) -> Result<ChangelogRevisionData, RevlogError> { |
|
31 | 31 | let rev = self.revlog.rev_from_node(node)?; |
|
32 | 32 | self.data_for_rev(rev) |
|
33 | 33 | } |
|
34 | 34 | |
|
35 |
/// Return the |
|
|
35 | /// Return the [`ChangelogEntry`] for the given revision number. | |
|
36 | 36 | pub fn entry_for_rev( |
|
37 | 37 | &self, |
|
38 | 38 | rev: Revision, |
|
39 |
) -> Result< |
|
|
40 | self.revlog.get_entry(rev) | |
|
39 | ) -> Result<ChangelogEntry, RevlogError> { | |
|
40 | let revlog_entry = self.revlog.get_entry(rev)?; | |
|
41 | Ok(ChangelogEntry { revlog_entry }) | |
|
41 | 42 | } |
|
42 | 43 | |
|
43 | 44 | /// Return the [`ChangelogRevisionData`] for the given revision number. |
|
45 | /// | |
|
46 | /// This is a useful shortcut in case the caller does not need the | |
|
47 | /// generic revlog information (parents, hashes etc). Otherwise | |
|
48 | /// consider taking a [`ChangelogEntry`] with | |
|
49 | /// [entry_for_rev](`Self::entry_for_rev`) and doing everything from there. | |
|
44 | 50 | pub fn data_for_rev( |
|
45 | 51 | &self, |
|
46 | 52 | rev: Revision, |
|
47 | 53 | ) -> Result<ChangelogRevisionData, RevlogError> { |
|
48 | let bytes = self.revlog.get_rev_data(rev)?; | |
|
49 | if bytes.is_empty() { | |
|
50 | Ok(ChangelogRevisionData::null()) | |
|
51 | } else { | |
|
52 | Ok(ChangelogRevisionData::new(bytes).map_err(|err| { | |
|
53 | RevlogError::Other(HgError::CorruptedRepository(format!( | |
|
54 | "Invalid changelog data for revision {}: {:?}", | |
|
55 | rev, err | |
|
56 | ))) | |
|
57 | })?) | |
|
54 | if rev == NULL_REVISION { | |
|
55 | return Ok(ChangelogRevisionData::null()); | |
|
58 | 56 | } |
|
57 | self.entry_for_rev(rev)?.data() | |
|
59 | 58 | } |
|
60 | 59 | |
|
61 | 60 | pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> { |
|
62 | 61 | self.revlog.node_from_rev(rev) |
|
63 | 62 | } |
|
64 | 63 | |
|
65 | 64 | pub fn rev_from_node( |
|
66 | 65 | &self, |
|
67 | 66 | node: NodePrefix, |
|
68 | 67 | ) -> Result<Revision, RevlogError> { |
|
69 | 68 | self.revlog.rev_from_node(node) |
|
70 | 69 | } |
|
71 | 70 | } |
|
72 | 71 | |
|
72 | /// A specialized `RevlogEntry` for `changelog` data format | |
|
73 | /// | |
|
74 | /// This is a `RevlogEntry` with the added semantics that the associated | |
|
75 | /// data should meet the requirements for `changelog`, materialized by | |
|
76 | /// the fact that `data()` constructs a `ChangelogRevisionData`. | |
|
77 | /// In case that promise would be broken, the `data` method returns an error. | |
|
78 | #[derive(Clone)] | |
|
79 | pub struct ChangelogEntry<'changelog> { | |
|
80 | /// Same data, as a generic `RevlogEntry`. | |
|
81 | pub(crate) revlog_entry: RevlogEntry<'changelog>, | |
|
82 | } | |
|
83 | ||
|
84 | impl<'changelog> ChangelogEntry<'changelog> { | |
|
85 | pub fn data<'a>( | |
|
86 | &'a self, | |
|
87 | ) -> Result<ChangelogRevisionData<'changelog>, RevlogError> { | |
|
88 | let bytes = self.revlog_entry.data()?; | |
|
89 | if bytes.is_empty() { | |
|
90 | Ok(ChangelogRevisionData::null()) | |
|
91 | } else { | |
|
92 | Ok(ChangelogRevisionData::new(bytes).map_err(|err| { | |
|
93 | RevlogError::Other(HgError::CorruptedRepository(format!( | |
|
94 | "Invalid changelog data for revision {}: {:?}", | |
|
95 | self.revlog_entry.revision(), | |
|
96 | err | |
|
97 | ))) | |
|
98 | })?) | |
|
99 | } | |
|
100 | } | |
|
101 | ||
|
102 | /// Obtain a reference to the underlying `RevlogEntry`. | |
|
103 | /// | |
|
104 | /// This allows the caller to access the information that is common | |
|
105 | /// to all revlog entries: revision number, node id, parent revisions etc. | |
|
106 | pub fn as_revlog_entry(&self) -> &RevlogEntry { | |
|
107 | &self.revlog_entry | |
|
108 | } | |
|
109 | } | |
|
110 | ||
|
73 | 111 | /// `Changelog` entry which knows how to interpret the `changelog` data bytes. |
|
74 | 112 | #[derive(PartialEq)] |
|
75 | 113 | pub struct ChangelogRevisionData<'changelog> { |
|
76 | 114 | /// The data bytes of the `changelog` entry. |
|
77 | 115 | bytes: Cow<'changelog, [u8]>, |
|
78 | 116 | /// The end offset for the hex manifest (not including the newline) |
|
79 | 117 | manifest_end: usize, |
|
80 | 118 | /// The end offset for the user+email (not including the newline) |
|
81 | 119 | user_end: usize, |
|
82 | 120 | /// The end offset for the timestamp+timezone+extras (not including the |
|
83 | 121 | /// newline) |
|
84 | 122 | timestamp_end: usize, |
|
85 | 123 | /// The end offset for the file list (not including the newline) |
|
86 | 124 | files_end: usize, |
|
87 | 125 | } |
|
88 | 126 | |
|
89 | 127 | impl<'changelog> ChangelogRevisionData<'changelog> { |
|
90 | 128 | fn new(bytes: Cow<'changelog, [u8]>) -> Result<Self, HgError> { |
|
91 | 129 | let mut line_iter = bytes.split(|b| b == &b'\n'); |
|
92 | 130 | let manifest_end = line_iter |
|
93 | 131 | .next() |
|
94 | 132 | .expect("Empty iterator from split()?") |
|
95 | 133 | .len(); |
|
96 | 134 | let user_slice = line_iter.next().ok_or_else(|| { |
|
97 | 135 | HgError::corrupted("Changeset data truncated after manifest line") |
|
98 | 136 | })?; |
|
99 | 137 | let user_end = manifest_end + 1 + user_slice.len(); |
|
100 | 138 | let timestamp_slice = line_iter.next().ok_or_else(|| { |
|
101 | 139 | HgError::corrupted("Changeset data truncated after user line") |
|
102 | 140 | })?; |
|
103 | 141 | let timestamp_end = user_end + 1 + timestamp_slice.len(); |
|
104 | 142 | let mut files_end = timestamp_end + 1; |
|
105 | 143 | loop { |
|
106 | 144 | let line = line_iter.next().ok_or_else(|| { |
|
107 | 145 | HgError::corrupted("Changeset data truncated in files list") |
|
108 | 146 | })?; |
|
109 | 147 | if line.is_empty() { |
|
110 | 148 | if files_end == bytes.len() { |
|
111 | 149 | // The list of files ended with a single newline (there |
|
112 | 150 | // should be two) |
|
113 | 151 | return Err(HgError::corrupted( |
|
114 | 152 | "Changeset data truncated after files list", |
|
115 | 153 | )); |
|
116 | 154 | } |
|
117 | 155 | files_end -= 1; |
|
118 | 156 | break; |
|
119 | 157 | } |
|
120 | 158 | files_end += line.len() + 1; |
|
121 | 159 | } |
|
122 | 160 | |
|
123 | 161 | Ok(Self { |
|
124 | 162 | bytes, |
|
125 | 163 | manifest_end, |
|
126 | 164 | user_end, |
|
127 | 165 | timestamp_end, |
|
128 | 166 | files_end, |
|
129 | 167 | }) |
|
130 | 168 | } |
|
131 | 169 | |
|
132 | 170 | fn null() -> Self { |
|
133 | 171 | Self::new(Cow::Borrowed( |
|
134 | 172 | b"0000000000000000000000000000000000000000\n\n0 0\n\n", |
|
135 | 173 | )) |
|
136 | 174 | .unwrap() |
|
137 | 175 | } |
|
138 | 176 | |
|
139 | 177 | /// Return an iterator over the lines of the entry. |
|
140 | 178 | pub fn lines(&self) -> impl Iterator<Item = &[u8]> { |
|
141 | 179 | self.bytes.split(|b| b == &b'\n') |
|
142 | 180 | } |
|
143 | 181 | |
|
144 | 182 | /// Return the node id of the `manifest` referenced by this `changelog` |
|
145 | 183 | /// entry. |
|
146 | 184 | pub fn manifest_node(&self) -> Result<Node, HgError> { |
|
147 | 185 | let manifest_node_hex = &self.bytes[..self.manifest_end]; |
|
148 | 186 | Node::from_hex_for_repo(manifest_node_hex) |
|
149 | 187 | } |
|
150 | 188 | |
|
151 | 189 | /// The full user string (usually a name followed by an email enclosed in |
|
152 | 190 | /// angle brackets) |
|
153 | 191 | pub fn user(&self) -> &[u8] { |
|
154 | 192 | &self.bytes[self.manifest_end + 1..self.user_end] |
|
155 | 193 | } |
|
156 | 194 | |
|
157 | 195 | /// The full timestamp line (timestamp in seconds, offset in seconds, and |
|
158 | 196 | /// possibly extras) |
|
159 | 197 | // TODO: We should expose this in a more useful way |
|
160 | 198 | pub fn timestamp_line(&self) -> &[u8] { |
|
161 | 199 | &self.bytes[self.user_end + 1..self.timestamp_end] |
|
162 | 200 | } |
|
163 | 201 | |
|
164 | 202 | /// The files changed in this revision. |
|
165 | 203 | pub fn files(&self) -> impl Iterator<Item = &HgPath> { |
|
166 | 204 | self.bytes[self.timestamp_end + 1..self.files_end] |
|
167 | 205 | .split(|b| b == &b'\n') |
|
168 | 206 | .map(HgPath::new) |
|
169 | 207 | } |
|
170 | 208 | |
|
171 | 209 | /// The change description. |
|
172 | 210 | pub fn description(&self) -> &[u8] { |
|
173 | 211 | &self.bytes[self.files_end + 2..] |
|
174 | 212 | } |
|
175 | 213 | } |
|
176 | 214 | |
|
177 | 215 | impl Debug for ChangelogRevisionData<'_> { |
|
178 | 216 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
|
179 | 217 | f.debug_struct("ChangelogRevisionData") |
|
180 | 218 | .field("bytes", &debug_bytes(&self.bytes)) |
|
181 | 219 | .field("manifest", &debug_bytes(&self.bytes[..self.manifest_end])) |
|
182 | 220 | .field( |
|
183 | 221 | "user", |
|
184 | 222 | &debug_bytes( |
|
185 | 223 | &self.bytes[self.manifest_end + 1..self.user_end], |
|
186 | 224 | ), |
|
187 | 225 | ) |
|
188 | 226 | .field( |
|
189 | 227 | "timestamp", |
|
190 | 228 | &debug_bytes( |
|
191 | 229 | &self.bytes[self.user_end + 1..self.timestamp_end], |
|
192 | 230 | ), |
|
193 | 231 | ) |
|
194 | 232 | .field( |
|
195 | 233 | "files", |
|
196 | 234 | &debug_bytes( |
|
197 | 235 | &self.bytes[self.timestamp_end + 1..self.files_end], |
|
198 | 236 | ), |
|
199 | 237 | ) |
|
200 | 238 | .field( |
|
201 | 239 | "description", |
|
202 | 240 | &debug_bytes(&self.bytes[self.files_end + 2..]), |
|
203 | 241 | ) |
|
204 | 242 | .finish() |
|
205 | 243 | } |
|
206 | 244 | } |
|
207 | 245 | |
|
208 | 246 | fn debug_bytes(bytes: &[u8]) -> String { |
|
209 | 247 | String::from_utf8_lossy( |
|
210 | 248 | &bytes.iter().flat_map(|b| escape_default(*b)).collect_vec(), |
|
211 | 249 | ) |
|
212 | 250 | .to_string() |
|
213 | 251 | } |
|
214 | 252 | |
|
215 | 253 | #[cfg(test)] |
|
216 | 254 | mod tests { |
|
217 | 255 | use super::*; |
|
218 | 256 | use crate::vfs::Vfs; |
|
219 | 257 | use crate::NULL_REVISION; |
|
220 | 258 | use pretty_assertions::assert_eq; |
|
221 | 259 | |
|
222 | 260 | #[test] |
|
223 | 261 | fn test_create_changelogrevisiondata_invalid() { |
|
224 | 262 | // Completely empty |
|
225 | 263 | assert!(ChangelogRevisionData::new(Cow::Borrowed(b"abcd")).is_err()); |
|
226 | 264 | // No newline after manifest |
|
227 | 265 | assert!(ChangelogRevisionData::new(Cow::Borrowed(b"abcd")).is_err()); |
|
228 | 266 | // No newline after user |
|
229 | 267 | assert!(ChangelogRevisionData::new(Cow::Borrowed(b"abcd\n")).is_err()); |
|
230 | 268 | // No newline after timestamp |
|
231 | 269 | assert!( |
|
232 | 270 | ChangelogRevisionData::new(Cow::Borrowed(b"abcd\n\n0 0")).is_err() |
|
233 | 271 | ); |
|
234 | 272 | // Missing newline after files |
|
235 | 273 | assert!(ChangelogRevisionData::new(Cow::Borrowed( |
|
236 | 274 | b"abcd\n\n0 0\nfile1\nfile2" |
|
237 | 275 | )) |
|
238 | 276 | .is_err(),); |
|
239 | 277 | // Only one newline after files |
|
240 | 278 | assert!(ChangelogRevisionData::new(Cow::Borrowed( |
|
241 | 279 | b"abcd\n\n0 0\nfile1\nfile2\n" |
|
242 | 280 | )) |
|
243 | 281 | .is_err(),); |
|
244 | 282 | } |
|
245 | 283 | |
|
246 | 284 | #[test] |
|
247 | 285 | fn test_create_changelogrevisiondata() { |
|
248 | 286 | let data = ChangelogRevisionData::new(Cow::Borrowed( |
|
249 | 287 | b"0123456789abcdef0123456789abcdef01234567 |
|
250 | 288 | Some One <someone@example.com> |
|
251 | 289 | 0 0 |
|
252 | 290 | file1 |
|
253 | 291 | file2 |
|
254 | 292 | |
|
255 | 293 | some |
|
256 | 294 | commit |
|
257 | 295 | message", |
|
258 | 296 | )) |
|
259 | 297 | .unwrap(); |
|
260 | 298 | assert_eq!( |
|
261 | 299 | data.manifest_node().unwrap(), |
|
262 | 300 | Node::from_hex("0123456789abcdef0123456789abcdef01234567") |
|
263 | 301 | .unwrap() |
|
264 | 302 | ); |
|
265 | 303 | assert_eq!(data.user(), b"Some One <someone@example.com>"); |
|
266 | 304 | assert_eq!(data.timestamp_line(), b"0 0"); |
|
267 | 305 | assert_eq!( |
|
268 | 306 | data.files().collect_vec(), |
|
269 | 307 | vec![HgPath::new("file1"), HgPath::new("file2")] |
|
270 | 308 | ); |
|
271 | 309 | assert_eq!(data.description(), b"some\ncommit\nmessage"); |
|
272 | 310 | } |
|
273 | 311 | |
|
274 | 312 | #[test] |
|
275 | 313 | fn test_data_from_rev_null() -> Result<(), RevlogError> { |
|
276 | 314 | // an empty revlog will be enough for this case |
|
277 | 315 | let temp = tempfile::tempdir().unwrap(); |
|
278 | 316 | let vfs = Vfs { base: temp.path() }; |
|
279 | 317 | std::fs::write(temp.path().join("foo.i"), b"").unwrap(); |
|
280 | 318 | let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap(); |
|
281 | 319 | |
|
282 | 320 | let changelog = Changelog { revlog }; |
|
283 | 321 | assert_eq!( |
|
284 | 322 | changelog.data_for_rev(NULL_REVISION)?, |
|
285 | 323 | ChangelogRevisionData::null() |
|
286 | 324 | ); |
|
287 | 325 | Ok(()) |
|
288 | 326 | } |
|
289 | 327 | } |
General Comments 0
You need to be logged in to leave comments.
Login now