Mercurial > public > mercurial-scm > hg
comparison rust/rhg/src/commands/files.rs @ 46500:184e46550dc8
rhg: replace command structs with functions
The `Command` trait was not used in any generic context,
and the struct where nothing more than holders for values parsed from CLI
arguments to be available to a `run` method.
Differential Revision: https://phab.mercurial-scm.org/D9967
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 08 Feb 2021 20:33:04 +0100 |
parents | a6e4e4650bac |
children | 1ecaf09d9964 |
comparison
equal
deleted
inserted
replaced
46499:eace48b4a786 | 46500:184e46550dc8 |
---|---|
1 use crate::commands::Command; | |
2 use crate::error::CommandError; | 1 use crate::error::CommandError; |
3 use crate::ui::Ui; | 2 use crate::ui::Ui; |
3 use clap::ArgMatches; | |
4 use hg::config::Config; | 4 use hg::config::Config; |
5 use hg::operations::list_rev_tracked_files; | 5 use hg::operations::list_rev_tracked_files; |
6 use hg::operations::Dirstate; | 6 use hg::operations::Dirstate; |
7 use hg::repo::Repo; | 7 use hg::repo::Repo; |
8 use hg::utils::files::{get_bytes_from_path, relativize_path}; | 8 use hg::utils::files::{get_bytes_from_path, relativize_path}; |
12 List tracked files. | 12 List tracked files. |
13 | 13 |
14 Returns 0 on success. | 14 Returns 0 on success. |
15 "; | 15 "; |
16 | 16 |
17 pub struct FilesCommand<'a> { | 17 pub fn run( |
18 rev: Option<&'a str>, | 18 ui: &Ui, |
19 } | 19 config: &Config, |
20 args: &ArgMatches, | |
21 ) -> Result<(), CommandError> { | |
22 let rev = args.value_of("rev"); | |
20 | 23 |
21 impl<'a> FilesCommand<'a> { | 24 let repo = Repo::find(config)?; |
22 pub fn new(rev: Option<&'a str>) -> Self { | 25 if let Some(rev) = rev { |
23 FilesCommand { rev } | 26 let files = |
24 } | 27 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?; |
25 | 28 display_files(ui, &repo, files.iter()) |
26 fn display_files( | 29 } else { |
27 &self, | 30 let distate = Dirstate::new(&repo)?; |
28 ui: &Ui, | 31 let files = distate.tracked_files()?; |
29 repo: &Repo, | 32 display_files(ui, &repo, files) |
30 files: impl IntoIterator<Item = &'a HgPath>, | |
31 ) -> Result<(), CommandError> { | |
32 let cwd = hg::utils::current_dir()?; | |
33 let rooted_cwd = cwd | |
34 .strip_prefix(repo.working_directory_path()) | |
35 .expect("cwd was already checked within the repository"); | |
36 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd)); | |
37 | |
38 let mut stdout = ui.stdout_buffer(); | |
39 | |
40 for file in files { | |
41 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?; | |
42 stdout.write_all(b"\n")?; | |
43 } | |
44 stdout.flush()?; | |
45 Ok(()) | |
46 } | 33 } |
47 } | 34 } |
48 | 35 |
49 impl<'a> Command for FilesCommand<'a> { | 36 fn display_files<'a>( |
50 fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> { | 37 ui: &Ui, |
51 let repo = Repo::find(config)?; | 38 repo: &Repo, |
52 if let Some(rev) = self.rev { | 39 files: impl IntoIterator<Item = &'a HgPath>, |
53 let files = | 40 ) -> Result<(), CommandError> { |
54 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?; | 41 let cwd = hg::utils::current_dir()?; |
55 self.display_files(ui, &repo, files.iter()) | 42 let rooted_cwd = cwd |
56 } else { | 43 .strip_prefix(repo.working_directory_path()) |
57 let distate = Dirstate::new(&repo)?; | 44 .expect("cwd was already checked within the repository"); |
58 let files = distate.tracked_files()?; | 45 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd)); |
59 self.display_files(ui, &repo, files) | 46 |
60 } | 47 let mut stdout = ui.stdout_buffer(); |
48 | |
49 for file in files { | |
50 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?; | |
51 stdout.write_all(b"\n")?; | |
61 } | 52 } |
53 stdout.flush()?; | |
54 Ok(()) | |
62 } | 55 } |