rust: Add Repo::manifest(revision)
authorSimon Sapin <simon.sapin@octobus.net>
Mon, 13 Sep 2021 13:45:10 +0200
changeset 47960 cfb6e6699b25
parent 47959 21d25e9ee58e
child 47961 4d2a5ca060e3
rust: Add Repo::manifest(revision) This deduplicates some common code. Differential Revision: https://phab.mercurial-scm.org/D11407
rust/hg-core/src/operations/cat.rs
rust/hg-core/src/operations/list_tracked_files.rs
rust/hg-core/src/repo.rs
--- a/rust/hg-core/src/operations/cat.rs	Mon Sep 13 13:29:55 2021 +0200
+++ b/rust/hg-core/src/operations/cat.rs	Mon Sep 13 13:45:10 2021 +0200
@@ -40,20 +40,16 @@
     files: &'a [HgPathBuf],
 ) -> Result<CatOutput, RevlogError> {
     let rev = crate::revset::resolve_single(revset, repo)?;
-    let changelog = repo.changelog()?;
-    let manifest = repo.manifestlog()?;
-    let changelog_entry = changelog.get_rev(rev)?;
-    let node = *changelog
+    let manifest = repo.manifest(rev)?;
+    let node = *repo
+        .changelog()?
         .node_from_rev(rev)
-        .expect("should succeed when changelog.get_rev did");
-    let manifest_node =
-        Node::from_hex_for_repo(&changelog_entry.manifest_node()?)?;
-    let manifest_entry = manifest.get_node(manifest_node.into())?;
+        .expect("should succeed when repo.manifest did");
     let mut bytes = vec![];
     let mut matched = vec![false; files.len()];
     let mut found_any = false;
 
-    for (manifest_file, node_bytes) in manifest_entry.files_with_nodes() {
+    for (manifest_file, node_bytes) in manifest.files_with_nodes() {
         for (cat_file, is_matched) in files.iter().zip(&mut matched) {
             if cat_file.as_bytes() == manifest_file.as_bytes() {
                 *is_matched = true;
--- a/rust/hg-core/src/operations/list_tracked_files.rs	Mon Sep 13 13:29:55 2021 +0200
+++ b/rust/hg-core/src/operations/list_tracked_files.rs	Mon Sep 13 13:45:10 2021 +0200
@@ -10,7 +10,6 @@
 use crate::errors::HgError;
 use crate::repo::Repo;
 use crate::revlog::manifest::Manifest;
-use crate::revlog::node::Node;
 use crate::revlog::revlog::RevlogError;
 use crate::utils::hg_path::HgPath;
 use crate::DirstateError;
@@ -71,13 +70,7 @@
     revset: &str,
 ) -> Result<FilesForRev, RevlogError> {
     let rev = crate::revset::resolve_single(revset, repo)?;
-    let changelog = repo.changelog()?;
-    let manifest = repo.manifestlog()?;
-    let changelog_entry = changelog.get_rev(rev)?;
-    let manifest_node =
-        Node::from_hex_for_repo(&changelog_entry.manifest_node()?)?;
-    let manifest_entry = manifest.get_node(manifest_node.into())?;
-    Ok(FilesForRev(manifest_entry))
+    Ok(FilesForRev(repo.manifest(rev)?))
 }
 
 pub struct FilesForRev(Manifest);
--- a/rust/hg-core/src/repo.rs	Mon Sep 13 13:29:55 2021 +0200
+++ b/rust/hg-core/src/repo.rs	Mon Sep 13 13:45:10 2021 +0200
@@ -5,14 +5,14 @@
 use crate::dirstate_tree::owning::OwningDirstateMap;
 use crate::errors::HgError;
 use crate::errors::HgResultExt;
-use crate::exit_codes;
-use crate::manifest::Manifestlog;
+use crate::manifest::{Manifest, Manifestlog};
 use crate::requirements;
 use crate::revlog::revlog::RevlogError;
 use crate::utils::files::get_path_from_bytes;
 use crate::utils::SliceExt;
 use crate::vfs::{is_dir, is_file, Vfs};
-use crate::DirstateError;
+use crate::{exit_codes, Node};
+use crate::{DirstateError, Revision};
 use std::cell::{Cell, Ref, RefCell, RefMut};
 use std::collections::HashSet;
 use std::path::{Path, PathBuf};
@@ -333,6 +333,19 @@
     pub fn manifestlog_mut(&self) -> Result<RefMut<Manifestlog>, RevlogError> {
         self.manifestlog.get_mut_or_init(self)
     }
+
+    /// Returns the manifest of the given revision
+    pub fn manifest(
+        &self,
+        revision: Revision,
+    ) -> Result<Manifest, RevlogError> {
+        let changelog = self.changelog()?;
+        let manifest = self.manifestlog()?;
+        let changelog_entry = changelog.get_rev(revision)?;
+        let manifest_node =
+            Node::from_hex_for_repo(&changelog_entry.manifest_node()?)?;
+        manifest.get_node(manifest_node.into())
+    }
 }
 
 /// Lazily-initialized component of `Repo` with interior mutability