Patchwork D8778: rhg: add a `Files` `Command` to prepare the `rhg files` subcommand

login
register
mail settings
Submitter phabricator
Date July 21, 2020, 2:07 p.m.
Message ID <differential-rev-PHID-DREV-nviyrbukt25rlxjb7x2t-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46822/
State Superseded
Headers show

Comments

phabricator - July 21, 2020, 2:07 p.m.
acezar created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/rhg/src/commands.rs
  rust/rhg/src/commands/files.rs
  rust/rhg/src/error.rs

CHANGE DETAILS




To: acezar, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs
--- a/rust/rhg/src/error.rs
+++ b/rust/rhg/src/error.rs
@@ -15,6 +15,8 @@ 
     StdoutError,
     /// The standard error stream cannot be written to
     StderrError,
+    /// The command aborted
+    Abort(Option<Vec<u8>>),
 }
 
 impl CommandErrorKind {
@@ -24,6 +26,7 @@ 
             CommandErrorKind::CurrentDirNotFound(_) => exitcode::ABORT,
             CommandErrorKind::StdoutError => exitcode::ABORT,
             CommandErrorKind::StderrError => exitcode::ABORT,
+            CommandErrorKind::Abort(_) => exitcode::ABORT,
         }
     }
 
@@ -50,6 +53,7 @@ 
                 ]
                 .concat(),
             ),
+            CommandErrorKind::Abort(message) => message.to_owned(),
             _ => None,
         }
     }
diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs
new file mode 100644
--- /dev/null
+++ b/rust/rhg/src/commands/files.rs
@@ -0,0 +1,68 @@ 
+use crate::commands::Command;
+use crate::error::{CommandError, CommandErrorKind};
+use crate::ui::Ui;
+use hg::operations::Operation;
+use hg::operations::{FindRootError, FindRootErrorKind};
+use hg::operations::{
+    ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
+};
+use hg::utils::files::get_bytes_from_path;
+use hg::utils::hg_path::HgPathBuf;
+use std::path::PathBuf;
+
+pub const HELP_TEXT: &str = "
+List tracked files.
+
+Returns 0 on success.
+";
+
+pub struct FilesCommand {
+    ui: Ui,
+}
+
+impl FilesCommand {
+    pub fn new(ui: Ui) -> Self {
+        FilesCommand { ui }
+    }
+}
+
+impl Command for FilesCommand {
+    fn run(&self) -> Result<(), CommandError> {
+        let files =
+            ListTrackedFiles::new()
+                .run()
+                .map_err(|err| match err.kind {
+                    ListTrackedFilesErrorKind::FindRootError(err) => {
+                        match err.kind {
+                            FindRootErrorKind::RootNotFound(path) => {
+                                CommandErrorKind::RootNotFound(path)
+                            }
+                            FindRootErrorKind::GetCurrentDirError(e) => {
+                                CommandErrorKind::CurrentDirNotFound(e)
+                            }
+                        }
+                    }
+                    ListTrackedFilesErrorKind::IoError(e) => {
+                        CommandErrorKind::Abort(Some(
+                            [b"abort: ", e.to_string().as_bytes(), b"\n"]
+                                .concat()
+                                .to_vec(),
+                        ))
+                    }
+                    ListTrackedFilesErrorKind::ParseError(_) => {
+                        CommandErrorKind::Abort(Some(
+                            // TODO find a better error message
+                            b"abort: parse error\n".to_vec(),
+                        ))
+                    }
+                })?;
+
+        for file in files {
+            let bytes = file.as_bytes();
+
+            // TODO use formating macro
+            self.ui.write_stdout(&[bytes, b"\n"].concat())?;
+        }
+        Ok(())
+    }
+}
diff --git a/rust/rhg/src/commands.rs b/rust/rhg/src/commands.rs
--- a/rust/rhg/src/commands.rs
+++ b/rust/rhg/src/commands.rs
@@ -1,3 +1,4 @@ 
+pub mod files;
 pub mod root;
 use crate::error::CommandError;