manifest.rs
84 lines
| 2.8 KiB
| application/rls-services+xml
|
RustLexer
|
r48777 | use crate::errors::HgError; | ||
|
r46782 | use crate::repo::Repo; | ||
|
r46104 | use crate::revlog::revlog::{Revlog, RevlogError}; | ||
use crate::revlog::Revision; | ||||
|
r48778 | use crate::revlog::{Node, NodePrefix}; | ||
|
r46104 | use crate::utils::hg_path::HgPath; | ||
/// A specialized `Revlog` to work with `manifest` data format. | ||||
|
r48771 | pub struct Manifestlog { | ||
|
r46104 | /// The generic `revlog` format. | ||
revlog: Revlog, | ||||
} | ||||
|
r48771 | impl Manifestlog { | ||
|
r46104 | /// Open the `manifest` of a repository given by its root. | ||
|
r48777 | pub fn open(repo: &Repo) -> Result<Self, HgError> { | ||
|
r46782 | let revlog = Revlog::open(repo, "00manifest.i", None)?; | ||
|
r46104 | Ok(Self { revlog }) | ||
} | ||||
/// Return the `ManifestEntry` of a given node id. | ||||
|
r48771 | pub fn get_node(&self, node: NodePrefix) -> Result<Manifest, RevlogError> { | ||
|
r46104 | let rev = self.revlog.get_node_rev(node)?; | ||
self.get_rev(rev) | ||||
} | ||||
/// Return the `ManifestEntry` of a given node revision. | ||||
|
r48771 | pub fn get_rev(&self, rev: Revision) -> Result<Manifest, RevlogError> { | ||
|
r46104 | let bytes = self.revlog.get_rev_data(rev)?; | ||
|
r48771 | Ok(Manifest { bytes }) | ||
|
r46104 | } | ||
} | ||||
|
r48771 | /// `Manifestlog` entry which knows how to interpret the `manifest` data bytes. | ||
|
r46104 | #[derive(Debug)] | ||
|
r48771 | pub struct Manifest { | ||
|
r46104 | bytes: Vec<u8>, | ||
} | ||||
|
r48771 | impl Manifest { | ||
|
r46104 | /// Return an iterator over the lines of the entry. | ||
pub fn lines(&self) -> impl Iterator<Item = &[u8]> { | ||||
self.bytes | ||||
.split(|b| b == &b'\n') | ||||
.filter(|line| !line.is_empty()) | ||||
} | ||||
/// Return an iterator over the files of the entry. | ||||
pub fn files(&self) -> impl Iterator<Item = &HgPath> { | ||||
self.lines().filter(|line| !line.is_empty()).map(|line| { | ||||
let pos = line | ||||
.iter() | ||||
.position(|x| x == &b'\0') | ||||
.expect("manifest line should contain \\0"); | ||||
HgPath::new(&line[..pos]) | ||||
}) | ||||
} | ||||
|
r46111 | |||
/// Return an iterator over the files of the entry. | ||||
pub fn files_with_nodes(&self) -> impl Iterator<Item = (&HgPath, &[u8])> { | ||||
self.lines().filter(|line| !line.is_empty()).map(|line| { | ||||
let pos = line | ||||
.iter() | ||||
.position(|x| x == &b'\0') | ||||
.expect("manifest line should contain \\0"); | ||||
let hash_start = pos + 1; | ||||
let hash_end = hash_start + 40; | ||||
(HgPath::new(&line[..pos]), &line[hash_start..hash_end]) | ||||
}) | ||||
} | ||||
|
r48778 | |||
/// If the given path is in this manifest, return its filelog node ID | ||||
pub fn find_file(&self, path: &HgPath) -> Result<Option<Node>, HgError> { | ||||
// TODO: use binary search instead of linear scan. This may involve | ||||
// building (and caching) an index of the byte indicex of each manifest | ||||
// line. | ||||
for (manifest_path, node) in self.files_with_nodes() { | ||||
if manifest_path == path { | ||||
return Ok(Some(Node::from_hex_for_repo(node)?)); | ||||
} | ||||
} | ||||
Ok(None) | ||||
} | ||||
|
r46104 | } | ||