comparison rust/rhg/src/commands/files.rs @ 46555:d8730ff51d5a

rhg: Add support for -R and --repository command-line arguments Differential Revision: https://phab.mercurial-scm.org/D9970
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 08 Feb 2021 21:37:30 +0100
parents 1ecaf09d9964
children 80840b651721
comparison
equal deleted inserted replaced
46554:95d37db31479 46555:d8730ff51d5a
6 use hg::operations::list_rev_tracked_files; 6 use hg::operations::list_rev_tracked_files;
7 use hg::operations::Dirstate; 7 use hg::operations::Dirstate;
8 use hg::repo::Repo; 8 use hg::repo::Repo;
9 use hg::utils::files::{get_bytes_from_path, relativize_path}; 9 use hg::utils::files::{get_bytes_from_path, relativize_path};
10 use hg::utils::hg_path::{HgPath, HgPathBuf}; 10 use hg::utils::hg_path::{HgPath, HgPathBuf};
11 use std::path::Path;
11 12
12 pub const HELP_TEXT: &str = " 13 pub const HELP_TEXT: &str = "
13 List tracked files. 14 List tracked files.
14 15
15 Returns 0 on success. 16 Returns 0 on success.
29 } 30 }
30 31
31 pub fn run( 32 pub fn run(
32 ui: &Ui, 33 ui: &Ui,
33 config: &Config, 34 config: &Config,
35 repo_path: Option<&Path>,
34 args: &ArgMatches, 36 args: &ArgMatches,
35 ) -> Result<(), CommandError> { 37 ) -> Result<(), CommandError> {
36 let rev = args.value_of("rev"); 38 let rev = args.value_of("rev");
37 39
38 let repo = Repo::find(config)?; 40 let repo = Repo::find(config, repo_path)?;
39 if let Some(rev) = rev { 41 if let Some(rev) = rev {
40 let files = 42 let files =
41 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?; 43 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?;
42 display_files(ui, &repo, files.iter()) 44 display_files(ui, &repo, files.iter())
43 } else { 45 } else {
50 fn display_files<'a>( 52 fn display_files<'a>(
51 ui: &Ui, 53 ui: &Ui,
52 repo: &Repo, 54 repo: &Repo,
53 files: impl IntoIterator<Item = &'a HgPath>, 55 files: impl IntoIterator<Item = &'a HgPath>,
54 ) -> Result<(), CommandError> { 56 ) -> Result<(), CommandError> {
55 let cwd = hg::utils::current_dir()?; 57 let cwd = HgPathBuf::from(get_bytes_from_path(hg::utils::current_dir()?));
56 let rooted_cwd = cwd 58 let working_directory =
57 .strip_prefix(repo.working_directory_path()) 59 HgPathBuf::from(get_bytes_from_path(repo.working_directory_path()));
58 .expect("cwd was already checked within the repository");
59 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
60 60
61 let mut stdout = ui.stdout_buffer(); 61 let mut stdout = ui.stdout_buffer();
62 62
63 for file in files { 63 for file in files {
64 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?; 64 let file = working_directory.join(file);
65 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
65 stdout.write_all(b"\n")?; 66 stdout.write_all(b"\n")?;
66 } 67 }
67 stdout.flush()?; 68 stdout.flush()?;
68 Ok(()) 69 Ok(())
69 } 70 }