##// END OF EJS Templates
hg-core: add `files_with_nodes` to `Manifest`...
Antoine Cezar -
r46111:f2de24c2 default
parent child Browse files
Show More
@@ -1,60 +1,73 b''
1 1 use crate::revlog::revlog::{Revlog, RevlogError};
2 2 use crate::revlog::Revision;
3 3 use crate::utils::hg_path::HgPath;
4 4 use std::path::PathBuf;
5 5
6 6 /// A specialized `Revlog` to work with `manifest` data format.
7 7 pub struct Manifest {
8 8 /// The generic `revlog` format.
9 9 revlog: Revlog,
10 10 }
11 11
12 12 impl Manifest {
13 13 /// Open the `manifest` of a repository given by its root.
14 14 pub fn open(root: &PathBuf) -> Result<Self, RevlogError> {
15 15 let index_file = root.join(".hg/store/00manifest.i");
16 16 let revlog = Revlog::open(&index_file)?;
17 17 Ok(Self { revlog })
18 18 }
19 19
20 20 /// Return the `ManifestEntry` of a given node id.
21 21 pub fn get_node(&self, node: &[u8]) -> Result<ManifestEntry, RevlogError> {
22 22 let rev = self.revlog.get_node_rev(node)?;
23 23 self.get_rev(rev)
24 24 }
25 25
26 26 /// Return the `ManifestEntry` of a given node revision.
27 27 pub fn get_rev(
28 28 &self,
29 29 rev: Revision,
30 30 ) -> Result<ManifestEntry, RevlogError> {
31 31 let bytes = self.revlog.get_rev_data(rev)?;
32 32 Ok(ManifestEntry { bytes })
33 33 }
34 34 }
35 35
36 36 /// `Manifest` entry which knows how to interpret the `manifest` data bytes.
37 37 #[derive(Debug)]
38 38 pub struct ManifestEntry {
39 39 bytes: Vec<u8>,
40 40 }
41 41
42 42 impl ManifestEntry {
43 43 /// Return an iterator over the lines of the entry.
44 44 pub fn lines(&self) -> impl Iterator<Item = &[u8]> {
45 45 self.bytes
46 46 .split(|b| b == &b'\n')
47 47 .filter(|line| !line.is_empty())
48 48 }
49 49
50 50 /// Return an iterator over the files of the entry.
51 51 pub fn files(&self) -> impl Iterator<Item = &HgPath> {
52 52 self.lines().filter(|line| !line.is_empty()).map(|line| {
53 53 let pos = line
54 54 .iter()
55 55 .position(|x| x == &b'\0')
56 56 .expect("manifest line should contain \\0");
57 57 HgPath::new(&line[..pos])
58 58 })
59 59 }
60
61 /// Return an iterator over the files of the entry.
62 pub fn files_with_nodes(&self) -> impl Iterator<Item = (&HgPath, &[u8])> {
63 self.lines().filter(|line| !line.is_empty()).map(|line| {
64 let pos = line
65 .iter()
66 .position(|x| x == &b'\0')
67 .expect("manifest line should contain \\0");
68 let hash_start = pos + 1;
69 let hash_end = hash_start + 40;
70 (HgPath::new(&line[..pos]), &line[hash_start..hash_end])
71 })
72 }
60 73 }
General Comments 0
You need to be logged in to leave comments. Login now