[Updated] D8863: hg-core: define a `ListTrackedFiles` `Operation`

acezar (Antoine Cezar) phabricator at mercurial-scm.org
Sat Aug 8 20:30:27 UTC 2020


Closed by commit rHG0f5286ccf82c: hg-core: define a `ListTrackedFiles` `Operation` (authored by acezar).
This revision was automatically updated to reflect the committed changes.

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D8863?vs=22247&id=22358#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D8863?vs=22247&id=22358

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8863/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8863

AFFECTED FILES
  rust/hg-core/src/operations/list_tracked_files.rs
  rust/hg-core/src/operations/mod.rs

CHANGE DETAILS

diff --git a/rust/hg-core/src/operations/mod.rs b/rust/hg-core/src/operations/mod.rs
--- a/rust/hg-core/src/operations/mod.rs
+++ b/rust/hg-core/src/operations/mod.rs
@@ -4,7 +4,11 @@
 
 mod dirstate_status;
 mod find_root;
+mod list_tracked_files;
 pub use find_root::{FindRoot, FindRootError, FindRootErrorKind};
+pub use list_tracked_files::{
+    ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
+};
 
 // TODO add an `Operation` trait when GAT have landed (rust #44265):
 // there is no way to currently define a trait which can both return
diff --git a/rust/hg-core/src/operations/list_tracked_files.rs b/rust/hg-core/src/operations/list_tracked_files.rs
new file mode 100644
--- /dev/null
+++ b/rust/hg-core/src/operations/list_tracked_files.rs
@@ -0,0 +1,85 @@
+// list_tracked_files.rs
+//
+// Copyright 2020 Antoine Cezar <antoine.cezar at octobus.net>
+//
+// This software may be used and distributed according to the terms of the
+// GNU General Public License version 2 or any later version.
+
+use super::find_root;
+use crate::dirstate::parsers::parse_dirstate;
+use crate::utils::hg_path::HgPath;
+use crate::{DirstateParseError, EntryState};
+use rayon::prelude::*;
+use std::convert::From;
+use std::fmt;
+use std::fs;
+use std::io;
+use std::path::PathBuf;
+
+/// Kind of error encoutered by ListTrackedFiles
+#[derive(Debug)]
+pub enum ListTrackedFilesErrorKind {
+    ParseError(DirstateParseError),
+}
+
+/// A ListTrackedFiles error
+#[derive(Debug)]
+pub struct ListTrackedFilesError {
+    /// Kind of error encoutered by ListTrackedFiles
+    pub kind: ListTrackedFilesErrorKind,
+}
+
+impl std::error::Error for ListTrackedFilesError {}
+
+impl fmt::Display for ListTrackedFilesError {
+    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        unimplemented!()
+    }
+}
+
+impl From<ListTrackedFilesErrorKind> for ListTrackedFilesError {
+    fn from(kind: ListTrackedFilesErrorKind) -> Self {
+        ListTrackedFilesError { kind }
+    }
+}
+
+/// List files under Mercurial control in the working directory
+pub struct ListTrackedFiles {
+    root: PathBuf,
+}
+
+impl ListTrackedFiles {
+    pub fn new() -> Result<Self, find_root::FindRootError> {
+        let root = find_root::FindRoot::new().run()?;
+        Ok(ListTrackedFiles { root })
+    }
+
+    /// Load the tracked files data from disk
+    pub fn load(&self) -> Result<ListDirstateTrackedFiles, io::Error> {
+        let dirstate = &self.root.join(".hg/dirstate");
+        let content = fs::read(&dirstate)?;
+        Ok(ListDirstateTrackedFiles { content })
+    }
+}
+
+/// List files under Mercurial control in the working directory
+/// by reading the dirstate
+pub struct ListDirstateTrackedFiles {
+    content: Vec<u8>,
+}
+
+impl ListDirstateTrackedFiles {
+    pub fn run(&self) -> Result<Vec<&HgPath>, ListTrackedFilesError> {
+        let (_, entries, _) = parse_dirstate(&self.content)
+            .map_err(ListTrackedFilesErrorKind::ParseError)?;
+        let mut files: Vec<&HgPath> = entries
+            .into_iter()
+            .filter_map(|(path, entry)| match entry.state {
+                EntryState::Removed => None,
+                _ => Some(path),
+            })
+            .collect();
+        files.par_sort_unstable();
+        Ok(files)
+    }
+}



To: acezar, #hg-reviewers, Alphare, indygreg
Cc: indygreg, mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20200808/2bdb7900/attachment-0002.html>


More information about the Mercurial-patches mailing list