comparison rust/rhg/src/commands/files.rs @ 46167:8a4914397d02

rust: introduce Repo and Vfs types for filesystem abstraction This is similar to the corresponding Python classes. Repo represents a repository and knows the path to the `.hg` directory, the `store` directory, and the working directory. Separating these will enable supporting the share extension. A Vfs is created from a Repo for one of these three directories. It has filesystem access APIs that take a relative std::path::Path as a parameter. Differential Revision: https://phab.mercurial-scm.org/D9596
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 14 Dec 2020 16:33:15 +0100
parents dca9cb99971c
children 3e2d539d0d1a
comparison
equal deleted inserted replaced
46166:c511fef30290 46167:8a4914397d02
1 use crate::commands::Command; 1 use crate::commands::Command;
2 use crate::error::{CommandError, CommandErrorKind}; 2 use crate::error::{CommandError, CommandErrorKind};
3 use crate::ui::utf8_to_local; 3 use crate::ui::utf8_to_local;
4 use crate::ui::Ui; 4 use crate::ui::Ui;
5 use hg::operations::find_root;
6 use hg::operations::{ 5 use hg::operations::{
7 list_rev_tracked_files, ListRevTrackedFilesError, 6 list_rev_tracked_files, ListRevTrackedFilesError,
8 ListRevTrackedFilesErrorKind, 7 ListRevTrackedFilesErrorKind,
9 }; 8 };
10 use hg::operations::{ 9 use hg::operations::{
11 Dirstate, ListDirstateTrackedFilesError, ListDirstateTrackedFilesErrorKind, 10 Dirstate, ListDirstateTrackedFilesError, ListDirstateTrackedFilesErrorKind,
12 }; 11 };
13 use hg::requirements; 12 use hg::repo::Repo;
14 use hg::utils::files::{get_bytes_from_path, relativize_path}; 13 use hg::utils::files::{get_bytes_from_path, relativize_path};
15 use hg::utils::hg_path::{HgPath, HgPathBuf}; 14 use hg::utils::hg_path::{HgPath, HgPathBuf};
16 use std::path::Path;
17 15
18 pub const HELP_TEXT: &str = " 16 pub const HELP_TEXT: &str = "
19 List tracked files. 17 List tracked files.
20 18
21 Returns 0 on success. 19 Returns 0 on success.
31 } 29 }
32 30
33 fn display_files( 31 fn display_files(
34 &self, 32 &self,
35 ui: &Ui, 33 ui: &Ui,
36 root: &Path, 34 repo: &Repo,
37 files: impl IntoIterator<Item = &'a HgPath>, 35 files: impl IntoIterator<Item = &'a HgPath>,
38 ) -> Result<(), CommandError> { 36 ) -> Result<(), CommandError> {
39 let cwd = std::env::current_dir() 37 let cwd = std::env::current_dir()
40 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?; 38 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
41 let rooted_cwd = cwd 39 let rooted_cwd = cwd
42 .strip_prefix(root) 40 .strip_prefix(repo.working_directory_path())
43 .expect("cwd was already checked within the repository"); 41 .expect("cwd was already checked within the repository");
44 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd)); 42 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
45 43
46 let mut stdout = ui.stdout_buffer(); 44 let mut stdout = ui.stdout_buffer();
47 45
54 } 52 }
55 } 53 }
56 54
57 impl<'a> Command for FilesCommand<'a> { 55 impl<'a> Command for FilesCommand<'a> {
58 fn run(&self, ui: &Ui) -> Result<(), CommandError> { 56 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
59 let root = find_root()?; 57 let repo = Repo::find()?;
60 requirements::check(&root)?; 58 repo.check_requirements()?;
61 if let Some(rev) = self.rev { 59 if let Some(rev) = self.rev {
62 let files = list_rev_tracked_files(&root, rev) 60 let files = list_rev_tracked_files(&repo, rev)
63 .map_err(|e| map_rev_error(rev, e))?; 61 .map_err(|e| map_rev_error(rev, e))?;
64 self.display_files(ui, &root, files.iter()) 62 self.display_files(ui, &repo, files.iter())
65 } else { 63 } else {
66 let distate = Dirstate::new(&root).map_err(map_dirstate_error)?; 64 let distate = Dirstate::new(&repo).map_err(map_dirstate_error)?;
67 let files = distate.tracked_files().map_err(map_dirstate_error)?; 65 let files = distate.tracked_files().map_err(map_dirstate_error)?;
68 self.display_files(ui, &root, files) 66 self.display_files(ui, &repo, files)
69 } 67 }
70 } 68 }
71 } 69 }
72 70
73 /// Convert `ListRevTrackedFilesErrorKind` to `CommandError` 71 /// Convert `ListRevTrackedFilesErrorKind` to `CommandError`