diff rust/rhg/src/commands/files.rs @ 45535:72b7d58d6e35

hg-core: simplify `list_tracked_files` operation Use directly `ListDirstateTrackedFiles` rather than having an operation builder. Differential Revision: https://phab.mercurial-scm.org/D9013
author Antoine Cezar <antoine.cezar@octobus.net>
date Wed, 09 Sep 2020 12:12:11 +0200
parents ed95ccc94333
children 2f8227a12592
line wrap: on
line diff
--- 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(),
+                ))
+            }
+        },
+    }
+}