rust/rhg/src/commands/files.rs
changeset 45535 72b7d58d6e35
parent 45438 ed95ccc94333
child 45537 2f8227a12592
--- a/rust/rhg/src/commands/files.rs	Fri Sep 18 16:52:08 2020 +0200
+++ b/rust/rhg/src/commands/files.rs	Wed Sep 09 12:12:11 2020 +0200
@@ -1,7 +1,12 @@
 use crate::commands::Command;
 use crate::error::{CommandError, CommandErrorKind};
+use crate::ui::utf8_to_local;
 use crate::ui::Ui;
-use hg::operations::{ListTrackedFiles, ListTrackedFilesErrorKind};
+use hg::operations::FindRoot;
+use hg::operations::{
+    ListDirstateTrackedFiles, ListDirstateTrackedFilesError,
+    ListDirstateTrackedFilesErrorKind,
+};
 use hg::utils::files::{get_bytes_from_path, relativize_path};
 use hg::utils::hg_path::HgPathBuf;
 
@@ -21,27 +26,15 @@
 
 impl Command for FilesCommand {
     fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let operation_builder = ListTrackedFiles::new()?;
-        let operation = operation_builder.load().map_err(|err| {
-            CommandErrorKind::Abort(Some(
-                [b"abort: ", err.to_string().as_bytes(), b"\n"]
-                    .concat()
-                    .to_vec(),
-            ))
-        })?;
-        let files = operation.run().map_err(|err| match err.kind {
-            ListTrackedFilesErrorKind::ParseError(_) => {
-                CommandErrorKind::Abort(Some(
-                    // TODO find a better error message
-                    b"abort: parse error\n".to_vec(),
-                ))
-            }
-        })?;
+        let root = FindRoot::new().run()?;
+        let mut operation = ListDirstateTrackedFiles::new(&root)
+            .map_err(map_dirstate_error)?;
+        let files = operation.run().map_err(map_dirstate_error)?;
 
         let cwd = std::env::current_dir()
             .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
         let rooted_cwd = cwd
-            .strip_prefix(operation_builder.get_root())
+            .strip_prefix(&root)
             .expect("cwd was already checked within the repository");
         let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
 
@@ -52,7 +45,25 @@
             stdout.write_all(b"\n")?;
         }
         stdout.flush()?;
-
         Ok(())
     }
 }
+
+/// Convert operation errors to command errors
+fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError {
+    CommandError {
+        kind: match err.kind {
+            ListDirstateTrackedFilesErrorKind::IoError(err) => {
+                CommandErrorKind::Abort(Some(
+                    utf8_to_local(&format!("abort: {}\n", err)).into(),
+                ))
+            }
+            ListDirstateTrackedFilesErrorKind::ParseError(_) => {
+                CommandErrorKind::Abort(Some(
+                    // TODO find a better error message
+                    b"abort: parse error\n".to_vec(),
+                ))
+            }
+        },
+    }
+}